Skip to content

Commit 284757e

Browse files
committed
Support multiple turns of tool use - for example, a tool that supports continuation tokens
1 parent 5d966e2 commit 284757e

File tree

4 files changed

+88
-106
lines changed

4 files changed

+88
-106
lines changed

e2e_tests/python/chat_session.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,31 +76,25 @@ async def start(self) -> None:
7676

7777
messages.append({"role": "user", "content": [{"text": user_input}]})
7878

79-
llm_response = self.llm_client.get_response(
80-
messages, system_prompt, tools_description
81-
)
82-
logging.debug("\nAssistant: %s", json.dumps(llm_response, indent=2))
83-
print(
84-
f'\nAssistant: {llm_response["output"]["message"]["content"][0]["text"]}'
85-
)
86-
messages.append(llm_response["output"]["message"])
87-
88-
tool_results = await self.execute_requested_tools(
89-
server_manager, llm_response
90-
)
91-
92-
if tool_results:
93-
logging.debug(
94-
"\nTool Results: %s", json.dumps(tool_results, indent=2)
95-
)
96-
messages.append(tool_results)
97-
final_response = self.llm_client.get_response(
79+
llm_response = None
80+
while llm_response is None or llm_response["stopReason"] == "tool_use":
81+
# Get the next response
82+
llm_response = self.llm_client.get_response(
9883
messages, system_prompt, tools_description
9984
)
100-
logging.debug(
101-
"\nFinal response: %s", json.dumps(final_response, indent=2)
102-
)
103-
print(
104-
f'\nAssistant: {final_response["output"]["message"]["content"][0]["text"]}'
85+
messages.append(llm_response["output"]["message"])
86+
logging.debug("\nAssistant: %s", json.dumps(llm_response, indent=2))
87+
if "text" in llm_response["output"]["message"]["content"][0]:
88+
print(
89+
f'\nAssistant: {llm_response["output"]["message"]["content"][0]["text"]}'
90+
)
91+
92+
# Execute tools if requested
93+
tool_results = await self.execute_requested_tools(
94+
server_manager, llm_response
10595
)
106-
messages.append(final_response["output"]["message"])
96+
if tool_results:
97+
logging.debug(
98+
"\nTool Results: %s", json.dumps(tool_results, indent=2)
99+
)
100+
messages.append(tool_results)

e2e_tests/typescript/src/chat_session.ts

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -91,40 +91,35 @@ export class ChatSession {
9191

9292
messages.push({ role: "user", content: [{ text: userInput }] });
9393

94-
const llmResponse = await this.llmClient.getResponse(
95-
messages,
96-
systemPrompt,
97-
toolsDescription
98-
);
99-
100-
logger.debug("\nAssistant: " + JSON.stringify(llmResponse, null, 2));
101-
console.log(
102-
`\nAssistant: ${llmResponse.output!.message!.content![0].text}`
103-
);
104-
messages.push(llmResponse.output!.message!);
105-
106-
const toolResults = await this.executeRequestedTools(
107-
serverManager,
108-
llmResponse
109-
);
110-
111-
if (toolResults) {
112-
logger.debug(
113-
"\nTool Results: " + JSON.stringify(toolResults, null, 2)
114-
);
115-
messages.push(toolResults);
116-
const finalResponse = await this.llmClient.getResponse(
94+
let llmResponse: ConverseCommandOutput | null = null;
95+
while (llmResponse === null || llmResponse.stopReason === "tool_use") {
96+
// Get the next response
97+
llmResponse = await this.llmClient.getResponse(
11798
messages,
11899
systemPrompt,
119100
toolsDescription
120101
);
121-
logger.debug(
122-
"\nFinal response: " + JSON.stringify(finalResponse, null, 2)
123-
);
124-
console.log(
125-
`\nAssistant: ${finalResponse.output!.message!.content![0].text}`
102+
messages.push(llmResponse.output!.message!);
103+
104+
logger.debug("\nAssistant: " + JSON.stringify(llmResponse, null, 2));
105+
if (llmResponse.output?.message?.content?.[0].text) {
106+
console.log(
107+
`\nAssistant: ${llmResponse.output!.message!.content![0].text}`
108+
);
109+
}
110+
111+
// Execute tools if requested
112+
const toolResults = await this.executeRequestedTools(
113+
serverManager,
114+
llmResponse
126115
);
127-
messages.push(finalResponse.output!.message!);
116+
117+
if (toolResults) {
118+
logger.debug(
119+
"\nTool Results: " + JSON.stringify(toolResults, null, 2)
120+
);
121+
messages.push(toolResults);
122+
}
128123
}
129124
}
130125
} finally {

examples/chatbots/python/chat_session.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,34 +72,32 @@ async def start(self) -> None:
7272

7373
messages.append({"role": "user", "content": [{"text": user_input}]})
7474

75-
llm_response = self.llm_client.get_response(
76-
messages, system_prompt, tools_description
77-
)
78-
logging.debug("\nAssistant: %s", json.dumps(llm_response, indent=2))
79-
print(
80-
f'\nAssistant: {llm_response["output"]["message"]["content"][0]["text"]}'
81-
)
82-
messages.append(llm_response["output"]["message"])
83-
84-
tool_results = await self.execute_requested_tools(
85-
server_manager, llm_response
86-
)
87-
88-
if tool_results:
89-
logging.debug(
90-
"\nTool Results: %s", json.dumps(tool_results, indent=2)
91-
)
92-
messages.append(tool_results)
93-
final_response = self.llm_client.get_response(
75+
llm_response = None
76+
while (
77+
llm_response is None or llm_response["stopReason"] == "tool_use"
78+
):
79+
# Get the next response
80+
llm_response = self.llm_client.get_response(
9481
messages, system_prompt, tools_description
9582
)
83+
messages.append(llm_response["output"]["message"])
9684
logging.debug(
97-
"\nFinal response: %s", json.dumps(final_response, indent=2)
85+
"\nAssistant: %s", json.dumps(llm_response, indent=2)
9886
)
99-
print(
100-
f'\nAssistant: {final_response["output"]["message"]["content"][0]["text"]}'
87+
if "text" in llm_response["output"]["message"]["content"][0]:
88+
print(
89+
f'\nAssistant: {llm_response["output"]["message"]["content"][0]["text"]}'
90+
)
91+
92+
# Execute tools if requested
93+
tool_results = await self.execute_requested_tools(
94+
server_manager, llm_response
10195
)
102-
messages.append(final_response["output"]["message"])
96+
if tool_results:
97+
logging.debug(
98+
"\nTool Results: %s", json.dumps(tool_results, indent=2)
99+
)
100+
messages.append(tool_results)
103101

104102
except KeyboardInterrupt:
105103
logging.info("\nExiting...")

examples/chatbots/typescript/src/chat_session.ts

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -85,40 +85,35 @@ export class ChatSession {
8585

8686
messages.push({ role: "user", content: [{ text: userInput }] });
8787

88-
const llmResponse = await this.llmClient.getResponse(
89-
messages,
90-
systemPrompt,
91-
toolsDescription
92-
);
93-
94-
logger.debug("\nAssistant: " + JSON.stringify(llmResponse, null, 2));
95-
console.log(
96-
`\nAssistant: ${llmResponse.output!.message!.content![0].text}`
97-
);
98-
messages.push(llmResponse.output!.message!);
99-
100-
const toolResults = await this.executeRequestedTools(
101-
serverManager,
102-
llmResponse
103-
);
104-
105-
if (toolResults) {
106-
logger.debug(
107-
"\nTool Results: " + JSON.stringify(toolResults, null, 2)
108-
);
109-
messages.push(toolResults);
110-
const finalResponse = await this.llmClient.getResponse(
88+
let llmResponse: ConverseCommandOutput | null = null;
89+
while (llmResponse === null || llmResponse.stopReason === "tool_use") {
90+
// Get the next response
91+
llmResponse = await this.llmClient.getResponse(
11192
messages,
11293
systemPrompt,
11394
toolsDescription
11495
);
115-
logger.debug(
116-
"\nFinal response: " + JSON.stringify(finalResponse, null, 2)
117-
);
118-
console.log(
119-
`\nAssistant: ${finalResponse.output!.message!.content![0].text}`
96+
messages.push(llmResponse.output!.message!);
97+
98+
logger.debug("\nAssistant: " + JSON.stringify(llmResponse, null, 2));
99+
if (llmResponse.output?.message?.content?.[0].text) {
100+
console.log(
101+
`\nAssistant: ${llmResponse.output!.message!.content![0].text}`
102+
);
103+
}
104+
105+
// Execute tools if requested
106+
const toolResults = await this.executeRequestedTools(
107+
serverManager,
108+
llmResponse
120109
);
121-
messages.push(finalResponse.output!.message!);
110+
111+
if (toolResults) {
112+
logger.debug(
113+
"\nTool Results: " + JSON.stringify(toolResults, null, 2)
114+
);
115+
messages.push(toolResults);
116+
}
122117
}
123118
}
124119
} finally {

0 commit comments

Comments
 (0)