@@ -11,10 +11,10 @@ public class RAGChatAgent: ChatAgent {
1111 }
1212
1313 public func send( _ request: Request ) async -> AsyncThrowingStream < Response , any Error > {
14- let service = getService ( )
1514 let stream = AsyncThrowingStream < Response , any Error > { continuation in
1615 let task = Task ( priority: . userInitiated) {
1716 do {
17+ let service = try await createService ( for: request)
1818 let response = try await service. send ( content: request. text, summary: nil )
1919 for try await item in response {
2020 if Task . isCancelled {
@@ -28,21 +28,40 @@ public class RAGChatAgent: ChatAgent {
2828 continuation. finish ( throwing: error)
2929 }
3030 }
31-
31+
3232 continuation. onTermination = { _ in
3333 task. cancel ( )
3434 }
3535 }
36-
36+
3737 return stream
3838 }
3939}
4040
4141extension RAGChatAgent {
42- func getService( ) -> ChatGPTServiceType {
43- fatalError ( )
42+ func createService( for request: Request ) async throws -> ChatGPTServiceType {
43+ guard let chatGPTConfiguration = configuration. chatGPTConfiguration
44+ else { throw CancellationError ( ) }
45+ let functionProvider = ChatFunctionProvider ( )
46+ let memory = AutoManagedChatGPTMemory (
47+ systemPrompt: configuration. modelConfiguration. systemPrompt,
48+ configuration: chatGPTConfiguration,
49+ functionProvider: functionProvider
50+ )
51+
52+ await memory. mutateHistory { messages in
53+ for history in request. history {
54+ messages. append ( history)
55+ }
56+ }
57+
58+ return ChatGPTService (
59+ memory: memory,
60+ configuration: chatGPTConfiguration,
61+ functionProvider: functionProvider
62+ )
4463 }
45-
64+
4665 var allCapabilities : [ String : any RAGChatAgentCapability ] {
4766 RAGChatAgentCapabilityContainer . capabilities
4867 }
0 commit comments