Spaces:
Runtime error
Runtime error
tech-envision
commited on
Commit
·
008a8e2
1
Parent(s):
aa795b6
Handle multiple tool calls
Browse files- 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 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
| 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 |
|