Skip to content

Commit 94d2a01

Browse files
committed
Update RAGChatAgent
1 parent 8af71af commit 94d2a01

1 file changed

Lines changed: 31 additions & 33 deletions

File tree

Tool/Sources/RAGChatAgent/RAGChatAgent.swift

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,46 @@ import ChatBasic
33
import Foundation
44
import OpenAIService
55

6-
public struct ChatAgentConfiguration: Codable {
7-
public var capabilityIds: Set<String>
8-
public var temperature: Double?
9-
public var modelId: String?
10-
public var model: ChatModel?
11-
public var stop: [String]?
12-
public var maxTokens: Int?
13-
public var minimumReplyTokens: Int?
14-
public var runFunctionsAutomatically: Bool?
15-
public var apiKey: String?
16-
}
17-
18-
public actor RAGChatAgent: ChatAgent {
19-
let configuration: ChatAgentConfiguration
6+
public class RAGChatAgent: ChatAgent {
7+
public let configuration: RAGChatAgentConfiguration
208

21-
init(configuration: ChatAgentConfiguration) {
9+
public init(configuration: RAGChatAgentConfiguration) {
2210
self.configuration = configuration
2311
}
2412

2513
public func send(_ request: Request) async -> AsyncThrowingStream<Response, any Error> {
26-
fatalError()
27-
// var continuation: AsyncThrowingStream<Response, any Error>.Continuation!
28-
// let stream = AsyncThrowingStream<Response, any Error> { cont in
29-
// continuation = cont
30-
// }
31-
//
32-
// await withTaskCancellationHandler {
33-
// <#code#>
34-
// } onCancel: {
35-
// continuation.finish(throwing: CancellationError())
36-
// }
37-
//
38-
// return .init { continuation in
39-
// Task {
40-
// let response = try await chatGPTService.send(content: request.text, summary: nil)
41-
// continuation.finish()
42-
// }
43-
// }
14+
let service = getService()
15+
let stream = AsyncThrowingStream<Response, any Error> { continuation in
16+
let task = Task(priority: .userInitiated) {
17+
do {
18+
let response = try await service.send(content: request.text, summary: nil)
19+
for try await item in response {
20+
if Task.isCancelled {
21+
continuation.finish()
22+
return
23+
}
24+
continuation.yield(.contentToken(item))
25+
}
26+
continuation.finish()
27+
} catch {
28+
continuation.finish(throwing: error)
29+
}
30+
}
31+
32+
continuation.onTermination = { _ in
33+
task.cancel()
34+
}
35+
}
36+
37+
return stream
4438
}
4539
}
4640

4741
extension RAGChatAgent {
42+
func getService() -> ChatGPTServiceType {
43+
fatalError()
44+
}
45+
4846
var allCapabilities: [String: any RAGChatAgentCapability] {
4947
RAGChatAgentCapabilityContainer.capabilities
5048
}

0 commit comments

Comments
 (0)