fixed tool use

This commit is contained in:
2024-08-19 12:54:13 -05:00
parent 6536acd647
commit 19ead08efb
3 changed files with 33 additions and 91 deletions
+11 -5
View File
@@ -28,7 +28,8 @@ class AnthropicTelegramInferenceBot(BaseTelegramInferenceBot):
system=self.system_prompt,
messages=messages,
max_tokens=8192,
tools=anthropic_tools
tools=anthropic_tools,
tool_choice={"type": "auto"}
)
except Exception as e:
logging.error(f"An error occurred: {str(e)}")
@@ -55,15 +56,16 @@ class AnthropicTelegramInferenceBot(BaseTelegramInferenceBot):
tool_use_count = 0
while len(tool_calls) > 0 and tool_use_count < 50:
tool_use_results = []
for tool_call in tool_calls:
tool_response = self.call_tool(tool_call)
while len(tool_calls) > 0:
tool_call = tool_calls.pop(0)
tool_response = self.call_tool(tool_call.name, json.dumps(tool_call.input))
tool_use_results.append({"type": "tool_result", "tool_use_id": tool_call.id, "content": json.dumps(tool_response)})
messages.append({"role": "user", "content": tool_use_results})
response = self.get_chat_response(messages)
full_message = []
tool_calls = []
for message_part in response.content:
full_message.append(message_part)
if message_part.type == "tool_use":
@@ -71,7 +73,11 @@ class AnthropicTelegramInferenceBot(BaseTelegramInferenceBot):
messages.append({"role": "assistant", "content": full_message})
tool_use_count += 1
if (tool_use_count == 0):
assistant_reply = response.content
self.conversation_history[user_id].append({"role": "assistant", "content": assistant_reply})
if len(self.conversation_history[user_id]) > 20:
self.conversation_history[user_id] = self.conversation_history[user_id][-20:]