@@ -2,7 +2,7 @@ import Foundation
22import Logger
33import 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}
0 commit comments