Added ability to use multiple tools
This commit is contained in:
+20
-15
@@ -94,11 +94,12 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
|||||||
response = get_chat_response(messages)
|
response = get_chat_response(messages)
|
||||||
tool_calls = []
|
tool_calls = []
|
||||||
if use_anthropic:
|
if use_anthropic:
|
||||||
for message in response.content:
|
fullMessage = []
|
||||||
if message.type == "tool_use":
|
for message_part in response.content:
|
||||||
tool_calls.append(message)
|
fullMessage.append(message_part)
|
||||||
else:
|
if message_part.type == "tool_use":
|
||||||
messages.append({"role": "assistant", "content": response.content})
|
tool_calls.append(message_part)
|
||||||
|
messages.append({"role": "assistant", "content": fullMessage})
|
||||||
else:
|
else:
|
||||||
assistant_message = response.choices[0].message
|
assistant_message = response.choices[0].message
|
||||||
if hasattr(assistant_message, 'function_call') and assistant_message.function_call is not None:
|
if hasattr(assistant_message, 'function_call') and assistant_message.function_call is not None:
|
||||||
@@ -107,29 +108,33 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
|||||||
toolUseCount = 0
|
toolUseCount = 0
|
||||||
|
|
||||||
while len(tool_calls) > 0 and toolUseCount < 50:
|
while len(tool_calls) > 0 and toolUseCount < 50:
|
||||||
|
tool_use_results = []
|
||||||
|
while len(tool_calls) > 0:
|
||||||
|
tool_call = tool_calls.pop(0)
|
||||||
|
function_name = tool_call.name
|
||||||
|
|
||||||
tool_call = tool_calls.pop(0)
|
tool_response = call_tool(tool_call)
|
||||||
function_name = tool_call.name
|
tool_use_results.append({"type": "tool_result", "tool_use_id": tool_call.id, "content": json.dumps(tool_response)})
|
||||||
|
|
||||||
tool_response = call_tool(tool_call)
|
|
||||||
|
|
||||||
formatted_result = {}
|
formatted_result = {}
|
||||||
|
|
||||||
if use_anthropic:
|
if use_anthropic:
|
||||||
formatted_result = {"role": "user", "content":[{"type": "tool_result", "tool_use_id": tool_call.id, "content": json.dumps(tool_response)}]}
|
formatted_result = {"role": "user", "content":tool_use_results}
|
||||||
else:
|
else:
|
||||||
formatted_result = {"role": "function", "name": function_name, "content": json.dumps(tool_response)}
|
formatted_result = {"role": "function", "name": function_name, "content": json.dumps(tool_use_results[0])}
|
||||||
|
|
||||||
messages.append(formatted_result)
|
messages.append(formatted_result)
|
||||||
|
|
||||||
response = get_chat_response(messages)
|
response = get_chat_response(messages)
|
||||||
assistant_message = ""
|
assistant_message = ""
|
||||||
if use_anthropic:
|
if use_anthropic:
|
||||||
for message in response.content:
|
fullMessage = []
|
||||||
if message.type == "tool_use":
|
for message_part in response.content:
|
||||||
tool_calls.append(message)
|
fullMessage.append(message_part)
|
||||||
else:
|
if message_part.type == "tool_use":
|
||||||
messages.append({"role": "assistant", "content": response.content})
|
tool_calls.append(message_part)
|
||||||
|
messages.append({"role": "assistant", "content": fullMessage})
|
||||||
else:
|
else:
|
||||||
assistant_message = response.choices[0].message
|
assistant_message = response.choices[0].message
|
||||||
conversation_history[user_id].append({"role": "assistant", "content": assistant_message})
|
conversation_history[user_id].append({"role": "assistant", "content": assistant_message})
|
||||||
|
|||||||
Reference in New Issue
Block a user