@@ -38,24 +38,23 @@ def lookup_weather(city_name: str | None = None, zip_code: str | None = None) ->
3838tools = [
3939 {
4040 "type" : "function" ,
41- "function" : {
42- "name" : "lookup_weather" ,
43- "description" : "Lookup the weather for a given city name or zip code." ,
44- "parameters" : {
45- "type" : "object" ,
46- "properties" : {
47- "city_name" : {
48- "type" : "string" ,
49- "description" : "The city name" ,
50- },
51- "zip_code" : {
52- "type" : "string" ,
53- "description" : "The zip code" ,
54- },
41+ "name" : "lookup_weather" ,
42+ "description" : "Lookup the weather for a given city name or zip code." ,
43+ "strict" : True ,
44+ "parameters" : {
45+ "type" : "object" ,
46+ "properties" : {
47+ "city_name" : {
48+ "type" : ["string" , "null" ],
49+ "description" : "The city name" ,
50+ },
51+ "zip_code" : {
52+ "type" : ["string" , "null" ],
53+ "description" : "The zip code" ,
5554 },
56- "strict" : True ,
57- "additionalProperties" : False ,
5855 },
56+ "required" : ["city_name" , "zip_code" ],
57+ "additionalProperties" : False ,
5958 },
6059 }
6160]
@@ -64,29 +63,36 @@ def lookup_weather(city_name: str | None = None, zip_code: str | None = None) ->
6463 {"role" : "system" , "content" : "You are a weather chatbot." },
6564 {"role" : "user" , "content" : "is it sunny in LA, CA?" },
6665]
67- response = client .chat . completions .create (
66+ response = client .responses .create (
6867 model = MODEL_NAME ,
69- messages = messages ,
68+ input = messages ,
7069 tools = tools ,
7170 tool_choice = "auto" ,
71+ store = False ,
7272)
7373
7474
7575# Now actually call the function as indicated
76- if response .choices [0 ].message .tool_calls :
77- tool_call = response .choices [0 ].message .tool_calls [0 ]
78- function_name = tool_call .function .name
79- function_arguments = json .loads (tool_call .function .arguments )
76+ function_calls = [item for item in response .output if item .type == "function_call" ]
77+ if function_calls :
78+ tool_call = function_calls [0 ]
79+ function_name = tool_call .name
80+ function_arguments = json .loads (tool_call .arguments )
8081 print (function_name )
8182 print (function_arguments )
8283
8384 if function_name == "lookup_weather" :
84- messages .append (response .choices [0 ].message )
8585 result = lookup_weather (** function_arguments )
86- messages .append ({"role" : "tool" , "tool_call_id" : tool_call .id , "content" : json .dumps (result )})
87- response = client .chat .completions .create (model = MODEL_NAME , messages = messages , tools = tools )
86+ messages .extend (response .output )
87+ messages .append ({"type" : "function_call_output" , "call_id" : tool_call .call_id , "output" : json .dumps (result )})
88+ response = client .responses .create (
89+ model = MODEL_NAME ,
90+ input = messages ,
91+ tools = tools ,
92+ store = False ,
93+ )
8894 print (f"Response from { MODEL_NAME } on { API_HOST } :" )
89- print (response .choices [ 0 ]. message . content )
95+ print (response .output_text )
9096
9197else :
92- print (response .choices [ 0 ]. message . content )
98+ print (response .output_text )
0 commit comments