Skip to content

Commit 4daa951

Browse files
committed
Make ChatAgentSupport reply in preferred language
1 parent 86da8b5 commit 4daa951

3 files changed

Lines changed: 23 additions & 14 deletions

File tree

Tool/Sources/LangChain/Agent.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ public extension Agent {
107107
)
108108
case .generate:
109109
var thoughts = constructBaseScratchpad(intermediateSteps: intermediateSteps)
110-
thoughts += "\n\n\(llmPrefix)I now need to return a final answer based on the previous steps:"
110+
thoughts += """
111+
112+
\(llmPrefix)I now need to return a final answer based on the previous steps:
113+
(Please continue with `Final Answer:`)
114+
"""
111115
let input = AgentInput(input: input, thoughts: .text(thoughts))
112116
let output = try await chatModelChain.call(input, callbackManagers: callbackManagers)
113117
let nextAction = parseOutput(output)

Tool/Sources/LangChain/Agents/ChatAgent.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22
import Logger
33
import Parsing
44

5-
private func formatInstruction(toolsNames: String) -> String {
5+
private func formatInstruction(toolsNames: String, preferredLanguage: String) -> String {
66
"""
77
The way you use the tools is by specifying a json blob.
88
Specifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here).
@@ -29,7 +29,7 @@ private func formatInstruction(toolsNames: String) -> String {
2929
Observation: the result of the action
3030
... (this Thought/Action/Observation can repeat N times)
3131
Thought: I now know the final answer
32-
Final Answer: the final answer to the original input question
32+
Final Answer: the final answer to the original input question \(preferredLanguage)
3333
"""
3434
}
3535

@@ -40,7 +40,7 @@ public class ChatAgent: Agent {
4040
public let chatModelChain: ChatModelChain<AgentInput<String>>
4141
let tools: [AgentTool]
4242

43-
public init(chatModel: ChatModel, tools: [AgentTool]) {
43+
public init(chatModel: ChatModel, tools: [AgentTool], preferredLanguage: String) {
4444
self.tools = tools
4545
chatModelChain = .init(
4646
chatModel: chatModel,
@@ -57,7 +57,12 @@ public class ChatAgent: Agent {
5757
5858
\(tools.map { "\($0.name): \($0.description)" }.joined(separator: "\n"))
5959
60-
\(formatInstruction(toolsNames: tools.map(\.name).joined(separator: ",")))
60+
\(formatInstruction(
61+
toolsNames: tools.map(\.name).joined(separator: ","),
62+
preferredLanguage: preferredLanguage.isEmpty
63+
? ""
64+
: "(in \(preferredLanguage)"
65+
))
6166
6267
Begin! Reminder to always use the exact characters `Final Answer` when responding.
6368
"""
@@ -92,9 +97,9 @@ public class ChatAgent: Agent {
9297
let baseScratchpad = constructBaseScratchpad(intermediateSteps: intermediateSteps)
9398
if baseScratchpad.isEmpty { return .text("") }
9499
return .text("""
95-
This was your previous work (but I haven't seen any of it! I only see what you return as final answer):
100+
This was your previous work (but I haven't seen any of it! I only see what you return as `Final Answer`):
96101
\(baseScratchpad)
97-
(Please continue with `Thought:`)
102+
(Please continue with `Thought:` or `Final Answer:`)
98103
""")
99104
}
100105

@@ -158,12 +163,12 @@ public class ChatAgent: Agent {
158163
var parsableContent = text[...]
159164
let finalAnswer = try? forceParser.parse(&parsableContent)
160165
.trimmingCharacters(in: .whitespacesAndNewlines)
161-
166+
162167
var answer = finalAnswer ?? text
163168
if answer.isEmpty {
164169
answer = "Sorry, I don't know."
165170
}
166-
171+
167172
return .finish(AgentFinish(returnValue: String(answer), log: text))
168173
}
169174
}

Tool/Tests/LangChainTests/ChatAgentTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class ChatAgentParseOutputTests: XCTestCase {
1818
Because 42 is the answer to everything.
1919
"""
2020

21-
let agent = ChatAgent(chatModel: FakeChatModel(), tools: [])
21+
let agent = ChatAgent(chatModel: FakeChatModel(), tools: [], preferredLanguage: "")
2222
let result = agent.parseOutput(finalAnswer)
2323
XCTAssertEqual(result, .finish(.init(
2424
returnValue: """
@@ -36,7 +36,7 @@ final class ChatAgentParseOutputTests: XCTestCase {
3636
Because 42 is the answer to everything.
3737
"""
3838

39-
let agent = ChatAgent(chatModel: FakeChatModel(), tools: [])
39+
let agent = ChatAgent(chatModel: FakeChatModel(), tools: [], preferredLanguage: "")
4040
let result = agent.parseOutput(finalAnswer)
4141
XCTAssertEqual(result, .finish(.init(
4242
returnValue: """
@@ -60,7 +60,7 @@ final class ChatAgentParseOutputTests: XCTestCase {
6060
```
6161
"""
6262

63-
let agent = ChatAgent(chatModel: FakeChatModel(), tools: [])
63+
let agent = ChatAgent(chatModel: FakeChatModel(), tools: [], preferredLanguage: "")
6464
let result = agent.parseOutput(reply)
6565
XCTAssertEqual(result, .actions([
6666
.init(
@@ -81,7 +81,7 @@ final class ChatAgentParseOutputTests: XCTestCase {
8181
```
8282
"""
8383

84-
let agent = ChatAgent(chatModel: FakeChatModel(), tools: [])
84+
let agent = ChatAgent(chatModel: FakeChatModel(), tools: [], preferredLanguage: "")
8585
let result = agent.parseOutput(reply)
8686
XCTAssertEqual(result, .finish(.init(
8787
returnValue: """
@@ -98,7 +98,7 @@ final class ChatAgentParseOutputTests: XCTestCase {
9898
Because 42 is the answer to everything.
9999
"""
100100

101-
let agent = ChatAgent(chatModel: FakeChatModel(), tools: [])
101+
let agent = ChatAgent(chatModel: FakeChatModel(), tools: [], preferredLanguage: "")
102102
let result = agent.parseOutput(reply)
103103
XCTAssertEqual(result, .finish(.init(
104104
returnValue: reply,

0 commit comments

Comments
 (0)