@@ -41,21 +41,21 @@ def subtract_two_numbers(a: int, b: int) -> int:
41
41
},
42
42
}
43
43
44
+ messages = [{'role' : 'user' , 'content' : 'What is three plus one?' }]
45
+ print ('Prompt:' , messages [0 ]['content' ])
44
46
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
+ }
47
51
48
- prompt = 'What is three plus one?'
49
- print ('Prompt:' , prompt )
50
52
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 ()
55
55
56
56
response : ChatResponse = await client .chat (
57
57
'llama3.1' ,
58
- messages = [{ 'role' : 'user' , 'content' : prompt }] ,
58
+ messages = messages ,
59
59
tools = [add_two_numbers , subtract_two_numbers_tool ],
60
60
)
61
61
@@ -66,10 +66,24 @@ async def main():
66
66
if function_to_call := available_functions .get (tool .function .name ):
67
67
print ('Calling function:' , tool .function .name )
68
68
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 )
70
71
else :
71
72
print ('Function' , tool .function .name , 'not found' )
72
73
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
+
73
87
74
88
if __name__ == '__main__' :
75
89
try :
0 commit comments