-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathChatAgent.swift
More file actions
130 lines (120 loc) · 4.51 KB
/
Copy pathChatAgent.swift
File metadata and controls
130 lines (120 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import Foundation
import OpenAPIRuntime
class ChatAgent {
private let agentConfig: Components.Schemas.AgentConfig
private let inferenceApi: Inference
private var sessions: [String: Components.Schemas.Session]
init(
agentConfig: Components.Schemas.AgentConfig,
inferenceApi: Inference
) {
self.agentConfig = agentConfig
self.inferenceApi = inferenceApi
self.sessions = [:]
}
public func createSession(name: String) -> Components.Schemas.Session {
let sessionId = UUID().uuidString
let session = Components.Schemas.Session(
session_id: sessionId,
session_name: name,
started_at: Date(),
turns: []
)
sessions[sessionId] = session
return session
}
public func createAndExecuteTurn(agent_id: String, session_id: String, request: Components.Schemas.CreateAgentTurnRequest) async throws -> AsyncStream<Components.Schemas.AgentTurnResponseStreamChunk> {
return AsyncStream<Components.Schemas.AgentTurnResponseStreamChunk> { continuation in
Task {
let session = sessions[session_id]
let turnId = UUID().uuidString
let startTime = Date()
continuation.yield(
Components.Schemas.AgentTurnResponseStreamChunk(event: Components.Schemas.AgentTurnResponseEvent(payload:
.AgentTurnResponseTurnStartPayload(Components.Schemas.AgentTurnResponseTurnStartPayload(
event_type: .turn_start,
turn_id: turnId
))
))
)
}
}
}
public func run(
session: Components.Schemas.Session,
turnId: String,
inputMessages: [Components.Schemas.Message],
attachments: [Components.Schemas.Turn.output_attachmentsPayload],
samplingParams: Components.Schemas.SamplingParams?,
stream: Bool = false
) -> AsyncStream<Components.Schemas.AgentTurnResponseStreamChunk> {
return AsyncStream<Components.Schemas.AgentTurnResponseStreamChunk> { continuation in
Task {
do {
for try await chunk in try await inferenceApi.chatCompletion(
request: Components.Schemas.ChatCompletionRequest(
messages: inputMessages,
model_id: agentConfig.model,
stream: true,
tools: [] //agentConfig.client_tools
)
) {
switch(chunk.event.delta) {
case .TextDelta(let s):
continuation.yield(
Components.Schemas.AgentTurnResponseStreamChunk(
event: Components.Schemas.AgentTurnResponseEvent(
payload:
.AgentTurnResponseStepProgressPayload(
Components.Schemas.AgentTurnResponseStepProgressPayload(
delta: .TextDelta(s),
event_type: .step_progress,
step_id: UUID().uuidString,
step_type: .inference
)
)
)
)
)
case .ImageDelta(let s):
continuation.yield(
Components.Schemas.AgentTurnResponseStreamChunk(
event: Components.Schemas.AgentTurnResponseEvent(
payload:
.AgentTurnResponseStepProgressPayload(
Components.Schemas.AgentTurnResponseStepProgressPayload(
delta: .ImageDelta(s),
event_type: .step_progress,
step_id: UUID().uuidString,
step_type: .inference
)
)
)
)
)
case .ToolCallDelta(let s):
continuation.yield(
Components.Schemas.AgentTurnResponseStreamChunk(
event: Components.Schemas.AgentTurnResponseEvent(
payload:
.AgentTurnResponseStepProgressPayload(
Components.Schemas.AgentTurnResponseStepProgressPayload(
delta: .ToolCallDelta(s),
event_type: .step_progress,
step_id: UUID().uuidString,
step_type: .inference
)
)
)
)
)
}
}
continuation.finish()
} catch {
print("Error occurred: \(error)")
}
}
}
}
}