1- -- This shows how to use tool calls with the Responses API
1+ -- This shows how to use tool calls with the Responses API chat session,
2+ -- including per-request tool_choice overrides to control tool usage.
23
34local openai = require (" openai" )
45local cjson = require (" cjson" )
56
67local client = openai .new (os.getenv (" OPENAI_API_KEY" ))
78
8- -- Define a simple weather tool
9+ -- Define a simple addition tool
910local tools = {
1011 {
1112 type = " function" ,
12- name = " get_weather " ,
13- description = " Get the current weather for a location " ,
13+ name = " add_numbers " ,
14+ description = " Add two numbers and return the sum. " ,
1415 parameters = {
1516 type = " object" ,
1617 properties = {
17- location = {
18- type = " string" ,
19- description = " The city and state, e.g. San Francisco, CA"
20- },
21- unit = {
22- type = " string" ,
23- enum = {" celsius" , " fahrenheit" },
24- description = " The temperature unit"
25- }
18+ a = { type = " number" },
19+ b = { type = " number" }
2620 },
27- required = {" location " }
21+ required = { " a " , " b " }
2822 }
2923 }
3024}
3125
32- -- Simulate getting weather data
33- local function get_weather (location , unit )
34- unit = unit or " fahrenheit"
35- -- In a real app, you'd call a weather API here
26+ -- Simulate the tool locally
27+ local function add_numbers (a , b )
3628 return {
37- location = location ,
38- temperature = unit == " celsius" and 22 or 72 ,
39- unit = unit ,
40- conditions = " sunny"
29+ sum = a + b ,
30+ explanation = (" The sum of %d and %d is %d." ):format (a , b , a + b )
4131 }
4232end
4333
4434local session = client :new_responses_chat_session ({
45- tools = tools
35+ tools = tools ,
36+ instructions = " You are a careful math assistant. Always call the provided tool to do arithmetic."
4637})
4738
48- print (" Asking about weather..." )
49- local response , err , raw = session :send (" What's the weather like in San Francisco?" )
39+ -- Send initial message, forcing a tool call
40+ print (" Sending initial message..." )
41+ local response , err , raw = session :send (" What is 123 + 456? Respond with the total." , {tool_choice = " required" })
5042
5143if not response then
5244 print (" Error:" , err )
@@ -62,37 +54,34 @@ for _, output_item in ipairs(response.output or {}) do
6254 end
6355end
6456
65- if # tool_calls > 0 then
66- print (" Model requested tool calls:" )
67-
68- for _ , tool_call in ipairs (tool_calls ) do
69- print (" - Function:" , tool_call .name )
70- print (" Arguments:" , cjson .encode (tool_call .arguments ))
57+ if # tool_calls == 0 then
58+ print (" Error: expected a tool call response" )
59+ os.exit (1 )
60+ end
7161
72- -- Execute the tool
73- if tool_call . name == " get_weather " then
74- local args = tool_call .arguments
75- local result = get_weather ( args . location , args . unit )
62+ print ( " Model requested tool calls: " )
63+ for _ , tool_call in ipairs ( tool_calls ) do
64+ print ( " - Function: " , tool_call .name )
65+ print ( " Arguments: " , tool_call . arguments )
7666
77- print (" Result:" , cjson .encode (result ))
67+ -- Execute the tool (arguments is a JSON string, decode it first)
68+ local args = cjson .decode (tool_call .arguments )
69+ local result = add_numbers (args .a , args .b )
70+ print (" Result:" , cjson .encode (result ))
7871
79- -- Send the tool result back
80- local follow_up , err2 = session :send ({
81- {
82- type = " function_call_output" ,
83- call_id = tool_call .call_id ,
84- output = cjson .encode (result )
85- }
86- })
72+ -- Send the tool result back, forcing a text response (no more tool calls)
73+ local final , err2 = session :send ({
74+ {
75+ type = " function_call_output" ,
76+ call_id = tool_call .call_id ,
77+ output = cjson .encode (result )
78+ }
79+ }, { tool_choice = " none " })
8780
88- if follow_up then
89- print (" \n Final response:" , follow_up :get_output_text ())
90- else
91- print (" Error sending tool result:" , err2 )
92- end
93- end
81+ if not final then
82+ print (" Error sending tool result:" , err2 )
83+ os.exit (1 )
9484 end
95- else
96- -- No tool calls, just print the response
97- print (" Response:" , response :get_output_text ())
85+
86+ print (" \n Final response:" , final :get_output_text ())
9887end
0 commit comments