Skip to content

Commit 2a7dbc3

Browse files
committed
[Frontend] OpenAI Responses API supports Tool/Function calling
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
1 parent 2c85258 commit 2a7dbc3

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

examples/online_serving/openai_responses_client_with_tools.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,21 @@ def get_weather(latitude: float, longitude: float) -> str:
2727

2828

2929
tools = [
30-
{
31-
"type": "function",
32-
"name": "get_weather",
33-
"description":
34-
"Get current temperature for provided coordinates in celsius.",
35-
"parameters": {
36-
"type": "object",
37-
"properties": {
38-
"latitude": {
39-
"type": "number"
30+
{
31+
"type": "function",
32+
"name": "get_weather",
33+
"description": "Get current temperature for provided coordinates in celsius.",
34+
"parameters": {
35+
"type": "object",
36+
"properties": {
37+
"latitude": {"type": "number"},
38+
"longitude": {"type": "number"},
4039
},
41-
"longitude": {
42-
"type": "number"
43-
}
40+
"required": ["latitude", "longitude"],
41+
"additionalProperties": False,
4442
},
45-
"required": ["latitude", "longitude"],
46-
"additionalProperties": False
47-
},
48-
"strict": True
49-
}
43+
"strict": True,
44+
}
5045
]
5146

5247
input_messages = [
@@ -59,14 +54,13 @@ def main():
5954
model = "Qwen/Qwen3-1.7B"
6055
client = OpenAI(base_url=base_url, api_key="empty")
6156
response = client.responses.create(
62-
model=model, input=input_messages, tools=tools, tool_choice="required"
57+
model=model, input=input_messages, tools=tools, tool_choice="required"
6358
)
6459
tool_call = response.output[0]
6560
args = json.loads(tool_call.arguments)
66-
61+
6762
result = get_weather(args["latitude"], args["longitude"])
68-
69-
63+
7064
input_messages.append(tool_call) # append model's function call message
7165
input_messages.append(
7266
{ # append result message
@@ -76,9 +70,9 @@ def main():
7670
}
7771
)
7872
response_2 = client.responses.create(
79-
model=model,
80-
input=input_messages,
81-
tools=tools,
73+
model=model,
74+
input=input_messages,
75+
tools=tools,
8276
)
8377
print(response_2.output_text)
8478

0 commit comments

Comments
 (0)