@@ -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
7488if __name__ == '__main__' :
7589 try :
0 commit comments