mirror of
https://github.com/ollama/ollama-python.git
synced 2026-10-12 11:57:27 +00:00
fix: handle multiple tool calls in tools examples (#634)
This commit is contained in:
@@ -62,6 +62,9 @@ async def main():
|
|||||||
|
|
||||||
if response.message.tool_calls:
|
if response.message.tool_calls:
|
||||||
# There may be multiple tool calls in the response
|
# There may be multiple tool calls in the response
|
||||||
|
# Add the assistant message with tool calls to the conversation
|
||||||
|
messages.append(response.message)
|
||||||
|
|
||||||
for tool in response.message.tool_calls:
|
for tool in response.message.tool_calls:
|
||||||
# Ensure the function is available, and then call it
|
# Ensure the function is available, and then call it
|
||||||
if function_to_call := available_functions.get(tool.function.name):
|
if function_to_call := available_functions.get(tool.function.name):
|
||||||
@@ -71,14 +74,12 @@ async def main():
|
|||||||
print('Function output:', output)
|
print('Function output:', output)
|
||||||
else:
|
else:
|
||||||
print('Function', tool.function.name, 'not found')
|
print('Function', tool.function.name, 'not found')
|
||||||
|
output = 'Function not found'
|
||||||
|
|
||||||
# Only needed to chat with the model using the tool call results
|
# Add each tool result as a separate message
|
||||||
if response.message.tool_calls:
|
messages.append({'role': 'tool', 'content': str(output), 'tool_name': tool.function.name})
|
||||||
# Add the function response to messages for the model to use
|
|
||||||
messages.append(response.message)
|
|
||||||
messages.append({'role': 'tool', 'content': str(output), 'tool_name': tool.function.name})
|
|
||||||
|
|
||||||
# Get final response from model with function outputs
|
# Get final response from model with all tool call results
|
||||||
final_response = await client.chat('llama3.1', messages=messages)
|
final_response = await client.chat('llama3.1', messages=messages)
|
||||||
print('Final response:', final_response.message.content)
|
print('Final response:', final_response.message.content)
|
||||||
|
|
||||||
|
|||||||
+7
-6
@@ -60,6 +60,9 @@ response: ChatResponse = chat(
|
|||||||
|
|
||||||
if response.message.tool_calls:
|
if response.message.tool_calls:
|
||||||
# There may be multiple tool calls in the response
|
# There may be multiple tool calls in the response
|
||||||
|
# Add the assistant message with tool calls to the conversation
|
||||||
|
messages.append(response.message)
|
||||||
|
|
||||||
for tool in response.message.tool_calls:
|
for tool in response.message.tool_calls:
|
||||||
# Ensure the function is available, and then call it
|
# Ensure the function is available, and then call it
|
||||||
if function_to_call := available_functions.get(tool.function.name):
|
if function_to_call := available_functions.get(tool.function.name):
|
||||||
@@ -69,14 +72,12 @@ if response.message.tool_calls:
|
|||||||
print('Function output:', output)
|
print('Function output:', output)
|
||||||
else:
|
else:
|
||||||
print('Function', tool.function.name, 'not found')
|
print('Function', tool.function.name, 'not found')
|
||||||
|
output = 'Function not found'
|
||||||
|
|
||||||
# Only needed to chat with the model using the tool call results
|
# Add each tool result as a separate message
|
||||||
if response.message.tool_calls:
|
messages.append({'role': 'tool', 'content': str(output), 'tool_name': tool.function.name})
|
||||||
# Add the function response to messages for the model to use
|
|
||||||
messages.append(response.message)
|
|
||||||
messages.append({'role': 'tool', 'content': str(output), 'tool_name': tool.function.name})
|
|
||||||
|
|
||||||
# Get final response from model with function outputs
|
# Get final response from model with all tool call results
|
||||||
final_response = chat('llama3.1', messages=messages)
|
final_response = chat('llama3.1', messages=messages)
|
||||||
print('Final response:', final_response.message.content)
|
print('Final response:', final_response.message.content)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user