tech-envision commited on
Commit
008a8e2
·
1 Parent(s): aa795b6

Handle multiple tool calls

Browse files
Files changed (1) hide show
  1. src/chat.py +22 -21
src/chat.py CHANGED
@@ -139,30 +139,31 @@ class ChatSession:
139
  conversation: Conversation,
140
  depth: int = 0,
141
  ) -> ChatResponse:
142
- if depth >= MAX_TOOL_CALL_DEPTH or not response.message.tool_calls:
143
- return response
144
-
145
- for call in response.message.tool_calls:
146
- if call.function.name == "execute_terminal":
147
- result = execute_terminal(**call.function.arguments)
148
- else:
149
- continue
 
 
 
 
 
 
 
 
 
 
 
 
150
 
151
- messages.append(
152
- {
153
- "role": "tool",
154
- "name": call.function.name,
155
- "content": str(result),
156
- }
157
- )
158
- DBMessage.create(
159
- conversation=conversation,
160
- role="tool",
161
- content=str(result),
162
- )
163
  nxt = await self.ask(messages, think=True)
164
  self._store_assistant_message(conversation, nxt.message)
165
- return await self._handle_tool_calls(messages, nxt, conversation, depth + 1)
 
166
 
167
  return response
168
 
 
139
  conversation: Conversation,
140
  depth: int = 0,
141
  ) -> ChatResponse:
142
+ while depth < MAX_TOOL_CALL_DEPTH and response.message.tool_calls:
143
+ for call in response.message.tool_calls:
144
+ if call.function.name == "execute_terminal":
145
+ result = execute_terminal(**call.function.arguments)
146
+ else:
147
+ _LOG.warning("Unsupported tool call: %s", call.function.name)
148
+ result = f"Unsupported tool: {call.function.name}"
149
+
150
+ messages.append(
151
+ {
152
+ "role": "tool",
153
+ "name": call.function.name,
154
+ "content": str(result),
155
+ }
156
+ )
157
+ DBMessage.create(
158
+ conversation=conversation,
159
+ role="tool",
160
+ content=str(result),
161
+ )
162
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  nxt = await self.ask(messages, think=True)
164
  self._store_assistant_message(conversation, nxt.message)
165
+ response = nxt
166
+ depth += 1
167
 
168
  return response
169