Skip to content

Commit 366180a

Browse files
authored
Improve tool example to showcase chatting (#352)
1 parent d6528cf commit 366180a

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

examples/async-tools.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,21 @@ def subtract_two_numbers(a: int, b: int) -> int:
4141
},
4242
}
4343

44+
messages = [{'role': 'user', 'content': 'What is three plus one?'}]
45+
print('Prompt:', messages[0]['content'])
4446

45-
async def main():
46-
client = ollama.AsyncClient()
47+
available_functions = {
48+
'add_two_numbers': add_two_numbers,
49+
'subtract_two_numbers': subtract_two_numbers,
50+
}
4751

48-
prompt = 'What is three plus one?'
49-
print('Prompt:', prompt)
5052

51-
available_functions = {
52-
'add_two_numbers': add_two_numbers,
53-
'subtract_two_numbers': subtract_two_numbers,
54-
}
53+
async def main():
54+
client = ollama.AsyncClient()
5555

5656
response: ChatResponse = await client.chat(
5757
'llama3.1',
58-
messages=[{'role': 'user', 'content': prompt}],
58+
messages=messages,
5959
tools=[add_two_numbers, subtract_two_numbers_tool],
6060
)
6161

@@ -66,10 +66,24 @@ async def main():
6666
if function_to_call := available_functions.get(tool.function.name):
6767
print('Calling function:', tool.function.name)
6868
print('Arguments:', tool.function.arguments)
69-
print('Function output:', function_to_call(**tool.function.arguments))
69+
output = function_to_call(**tool.function.arguments)
70+
print('Function output:', output)
7071
else:
7172
print('Function', tool.function.name, 'not found')
7273

74+
# Only needed to chat with the model using the tool call results
75+
if response.message.tool_calls:
76+
# Add the function response to messages for the model to use
77+
messages.append(response.message)
78+
messages.append({'role': 'tool', 'content': str(output), 'name': tool.function.name})
79+
80+
# Get final response from model with function outputs
81+
final_response = await client.chat('llama3.1', messages=messages)
82+
print('Final response:', final_response.message.content)
83+
84+
else:
85+
print('No tool calls returned from model')
86+
7387

7488
if __name__ == '__main__':
7589
try:

examples/tools.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def subtract_two_numbers(a: int, b: int) -> int:
4040
},
4141
}
4242

43-
prompt = 'What is three plus one?'
44-
print('Prompt:', prompt)
43+
messages = [{'role': 'user', 'content': 'What is three plus one?'}]
44+
print('Prompt:', messages[0]['content'])
4545

4646
available_functions = {
4747
'add_two_numbers': add_two_numbers,
@@ -50,7 +50,7 @@ def subtract_two_numbers(a: int, b: int) -> int:
5050

5151
response: ChatResponse = chat(
5252
'llama3.1',
53-
messages=[{'role': 'user', 'content': prompt}],
53+
messages=messages,
5454
tools=[add_two_numbers, subtract_two_numbers_tool],
5555
)
5656

@@ -61,6 +61,20 @@ def subtract_two_numbers(a: int, b: int) -> int:
6161
if function_to_call := available_functions.get(tool.function.name):
6262
print('Calling function:', tool.function.name)
6363
print('Arguments:', tool.function.arguments)
64-
print('Function output:', function_to_call(**tool.function.arguments))
64+
output = function_to_call(**tool.function.arguments)
65+
print('Function output:', output)
6566
else:
6667
print('Function', tool.function.name, 'not found')
68+
69+
# Only needed to chat with the model using the tool call results
70+
if response.message.tool_calls:
71+
# Add the function response to messages for the model to use
72+
messages.append(response.message)
73+
messages.append({'role': 'tool', 'content': str(output), 'name': tool.function.name})
74+
75+
# Get final response from model with function outputs
76+
final_response = chat('llama3.1', messages=messages)
77+
print('Final response:', final_response.message.content)
78+
79+
else:
80+
print('No tool calls returned from model')

0 commit comments

Comments
 (0)