Skip to content

Commit 21d7ae4

Browse files
committed
Put tool call responses into the tool call struct
1 parent ca9e981 commit 21d7ae4

File tree

13 files changed

+51
-78
lines changed

13 files changed

+51
-78
lines changed

Core/Sources/ChatContextCollectors/WebChatContextCollector/WebChatContextCollector.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ extension WebChatContextCollector {
3232
static func detectLinks(from messages: [ChatMessage]) -> [String] {
3333
return messages.lazy
3434
.compactMap {
35-
$0.content ?? $0.toolCallContext?.toolCalls.map(\.function.arguments)
36-
.joined(separator: " ") ?? ""
35+
$0.content ?? $0.toolCalls?.map(\.function.arguments).joined(separator: " ") ?? ""
3736
}
3837
.map(detectLinks(from:))
3938
.flatMap { $0 }

Core/Sources/ChatGPTChatTab/Chat.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -327,15 +327,13 @@ struct Chat: ReducerProtocol {
327327
}
328328
))
329329

330-
if let responses = message.toolCallContext?.responses {
331-
for response in responses {
332-
all.append(.init(
333-
id: message.id + response.id,
334-
role: .tool,
335-
text: response.summary ?? response.content,
336-
references: []
337-
))
338-
}
330+
for call in message.toolCalls ?? [] {
331+
all.append(.init(
332+
id: message.id + call.response.id,
333+
role: .tool,
334+
text: call.response.summary ?? call.response.content,
335+
references: []
336+
))
339337
}
340338

341339
return all

Core/Sources/ChatService/ChatService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public final class ChatService: ObservableObject {
126126

127127
// if it's stopped before the tool calls finish, remove the message.
128128
await memory.mutateHistory { history in
129-
if history.last?.role == .assistant, history.last?.toolCallContext?.toolCalls != nil {
129+
if history.last?.role == .assistant, history.last?.toolCalls != nil {
130130
history.removeLast()
131131
}
132132
}

Pro

Submodule Pro updated from a2e8aa5 to ede561a

Tool/Sources/LangChain/Chains/LLMChain.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class ChatModelChain<Input>: Chain {
3333
public func parseOutput(_ output: Output) -> String {
3434
if let content = output.content {
3535
return content
36-
} else if let toolCalls = output.toolCallContext?.toolCalls {
36+
} else if let toolCalls = output.toolCalls {
3737
return toolCalls.map { "[\($0.id)] \($0.function.name): \($0.function.arguments)" }
3838
.joined(separator: "\n")
3939
}

Tool/Sources/LangChain/Chains/RefineDocumentChain.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public final class RefineDocumentChain: Chain {
153153
}
154154

155155
func extractAnswer(_ chatMessage: ChatMessage) -> IntermediateAnswer {
156-
for functionCall in chatMessage.toolCallContext?.toolCalls.map(\.function) ?? [] {
156+
for functionCall in chatMessage.toolCalls?.map(\.function) ?? [] {
157157
do {
158158
let intermediateAnswer = try JSONDecoder().decode(
159159
IntermediateAnswer.self,

Tool/Sources/LangChain/Chains/RelevantInformationExtractionChain.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public final class RelevantInformationExtractionChain: Chain {
104104
callbackManagers: callbackManagers
105105
)
106106

107-
if let functionCall = output.toolCallContext?.toolCalls
107+
if let functionCall = output.toolCalls?
108108
.first(where: { $0.function.name == FinalAnswer().name })?.function
109109
{
110110
do {

Tool/Sources/LangChain/Chains/StructuredOutputChatModelChain.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public class StructuredOutputChatModelChain<Output: Decodable>: Chain {
108108
}
109109

110110
public func parseOutput(_ message: ChatMessage) async -> Output? {
111-
if let functionCall = message.toolCallContext?.toolCalls.first?.function {
111+
if let functionCall = message.toolCalls?.first?.function {
112112
do {
113113
let result = try JSONDecoder().decode(
114114
EndFunction.Arguments.self,

Tool/Sources/OpenAIService/APIs/GoogleAIChatCompletionsService.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,13 @@ extension ModelContent {
208208
case .user:
209209
return message.content ?? " "
210210
case .assistant:
211-
if let toolCallContext = message.toolCallContext {
212-
return toolCallContext.toolCalls.map { call in
213-
let response = toolCallContext.responses.first(where: { $0.id == call.id })
211+
if let toolCalls = message.toolCalls {
212+
return toolCalls.map { call in
213+
let response = call.response
214214
return """
215215
Call function: \(call.function.name)
216216
Arguments: \(call.function.arguments)
217-
Result: \(response?.content ?? "N/A")
217+
Result: \(response.content)
218218
"""
219219
}.joined(separator: "\n")
220220
} else {

Tool/Sources/OpenAIService/ChatGPTService.swift

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public class ChatGPTService: ChatGPTServiceType {
169169
role: .user,
170170
content: content,
171171
name: nil,
172-
toolCallContext: nil,
172+
toolCalls: nil,
173173
summary: summary,
174174
references: []
175175
)
@@ -222,7 +222,7 @@ public class ChatGPTService: ChatGPTServiceType {
222222

223223
pendingToolCalls = await memory.history
224224
.last { $0.id == sourceMessageId }?
225-
.toolCallContext?.toolCalls ?? []
225+
.toolCalls ?? []
226226

227227
#if DEBUG
228228
Debugger.didReceiveResponse(content: reply)
@@ -261,7 +261,7 @@ public class ChatGPTService: ChatGPTServiceType {
261261
return try await Debugger.$id.withValue(.init()) {
262262
let message = try await sendMemoryAndWait()
263263
var finalResult = message?.content
264-
var toolCalls = message?.toolCallContext?.toolCalls
264+
var toolCalls = message?.toolCalls
265265
while let sourceMessageId = message?.id, let calls = toolCalls, !calls.isEmpty {
266266
try Task.checkCancellation()
267267
if !configuration.runFunctionsAutomatically {
@@ -273,7 +273,7 @@ public class ChatGPTService: ChatGPTServiceType {
273273
}
274274
guard let nextMessage = try await sendMemoryAndWait() else { break }
275275
finalResult = nextMessage.content
276-
toolCalls = nextMessage.toolCallContext?.toolCalls
276+
toolCalls = nextMessage.toolCalls
277277
}
278278

279279
#if DEBUG
@@ -438,13 +438,11 @@ extension ChatGPTService {
438438
}(),
439439
content: choice.content,
440440
name: choice.name,
441-
toolCallContext: choice.toolCalls.map {
442-
.init(toolCalls: $0.map {
443-
ChatMessage.ToolCall(id: $0.id, type: $0.type, function: .init(
444-
name: $0.function.name,
445-
arguments: $0.function.arguments ?? ""
446-
))
447-
}, responses: [])
441+
toolCalls: choice.toolCalls?.map {
442+
ChatMessage.ToolCall(id: $0.id, type: $0.type, function: .init(
443+
name: $0.function.name,
444+
arguments: $0.function.arguments ?? ""
445+
))
448446
},
449447
references: prompt.references
450448
)
@@ -589,7 +587,7 @@ extension ChatGPTService {
589587
}(),
590588
content: chatMessage.content ?? "",
591589
name: chatMessage.name,
592-
toolCalls: chatMessage.toolCallContext?.toolCalls.map {
590+
toolCalls: chatMessage.toolCalls?.map {
593591
.init(
594592
id: $0.id,
595593
type: $0.type,
@@ -601,14 +599,12 @@ extension ChatGPTService {
601599
}
602600
))
603601

604-
if let responses = chatMessage.toolCallContext?.responses {
605-
for response in responses {
606-
all.append(ChatCompletionsRequestBody.Message(
607-
role: .tool,
608-
content: response.content,
609-
toolCallId: response.id
610-
))
611-
}
602+
for call in chatMessage.toolCalls ?? [] {
603+
all.append(ChatCompletionsRequestBody.Message(
604+
role: .tool,
605+
content: call.response.content,
606+
toolCallId: call.response.id
607+
))
612608
}
613609

614610
return all

0 commit comments

Comments
 (0)