Skip to content

Commit 73c4141

Browse files
committed
applying user context to requests
1 parent 96de0b1 commit 73c4141

File tree

2 files changed

+72
-12
lines changed

2 files changed

+72
-12
lines changed

Sources/GenKit/Sessions/ChatSession.swift

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,24 @@ public struct ChatSessionRequest {
156156
public let model: String
157157
public let toolCallback: ToolCallback?
158158

159-
public private(set) var messages: [Message] = []
159+
public private(set) var system: String? = nil
160+
public private(set) var history: [Message] = []
160161
public private(set) var tools: [Tool] = []
161162
public private(set) var tool: Tool? = nil
162-
public private(set) var memories: [String] = []
163+
public private(set) var context: [String] = []
163164

164165
public init(service: ChatService, model: String, toolCallback: ToolCallback? = nil) {
165166
self.service = service
166167
self.model = model
167168
self.toolCallback = toolCallback
168169
}
169170

170-
public mutating func with(messages: [Message]) {
171-
self.messages = messages
171+
public mutating func with(system: String?) {
172+
self.system = system
173+
}
174+
175+
public mutating func with(history: [Message]) {
176+
self.history = history
172177
}
173178

174179
public mutating func with(tools: [Tool]) {
@@ -184,8 +189,33 @@ public struct ChatSessionRequest {
184189
}
185190
}
186191

187-
public mutating func with(memories: [String]) {
188-
self.memories = memories
192+
public mutating func with(context: [String]) {
193+
self.context = context
194+
}
195+
196+
var messages: [Message] {
197+
var messages: [Message] = []
198+
199+
// Apply user context
200+
var systemContext = ""
201+
if !context.isEmpty {
202+
systemContext = """
203+
The following is context about the current user:
204+
<user_context>
205+
\(context.joined(separator: "\n"))
206+
</user_context>
207+
"""
208+
}
209+
210+
// Apply system prompt
211+
if let system {
212+
messages.append(.init(kind: .instruction, role: .system, content: [system, systemContext].joined(separator: "\n\n")))
213+
}
214+
215+
// Apply history
216+
messages += history
217+
218+
return messages
189219
}
190220
}
191221

Sources/GenKit/Sessions/VisionSession.swift

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,51 @@ public struct VisionSessionRequest {
6666
public let model: String
6767
public let toolCallback: ToolCallback?
6868

69-
public private(set) var messages: [Message] = []
70-
public private(set) var memories: [String] = []
69+
public private(set) var system: String? = nil
70+
public private(set) var history: [Message] = []
71+
public private(set) var context: [String] = []
7172

7273
public init(service: VisionService, model: String, toolCallback: ToolCallback? = nil) {
7374
self.service = service
7475
self.model = model
7576
self.toolCallback = toolCallback
7677
}
7778

78-
public mutating func with(messages: [Message]) {
79-
self.messages = messages
79+
public mutating func with(system: String?) {
80+
self.system = system
8081
}
8182

82-
public mutating func with(memories: [String]) {
83-
self.memories = memories
83+
public mutating func with(history: [Message]) {
84+
self.history = history
85+
}
86+
87+
public mutating func with(context: [String]) {
88+
self.context = context
89+
}
90+
91+
var messages: [Message] {
92+
var messages: [Message] = []
93+
94+
// Apply user context
95+
var systemContext = ""
96+
if !context.isEmpty {
97+
systemContext = """
98+
The following is context about the current user:
99+
<user_context>
100+
\(context.joined(separator: "\n"))
101+
</user_context>
102+
"""
103+
}
104+
105+
// Apply system prompt
106+
if let system {
107+
messages.append(.init(kind: .instruction, role: .system, content: [system, systemContext].joined(separator: "\n\n")))
108+
}
109+
110+
// Apply history
111+
messages += history
112+
113+
return messages
84114
}
85115
}
86116

0 commit comments

Comments
 (0)