-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
[Frontend] OpenAI Responses API supports Tool/Function calling - non-harmony #26874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
a38e60c
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang 878f104
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang c11eb9d
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang ecd5942
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang b03d0b2
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang 577c191
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang 98af9c9
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang 2f13d99
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang 93e9db5
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang eebab9c
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang 842f7e1
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang 839aaad
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang eb8fa5b
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang 3d91f53
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang 157ab86
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang 2ddd919
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang ce784fe
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang 067cd66
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang f44848a
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang 5b8356c
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang 3ab6b2b
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang f2b7dde
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang 06e802e
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang 13a2749
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang 56f400b
[Frontend] OpenAI Responses API supports Tool/Function calling
chaunceyjiang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
examples/online_serving/openai_responses_client_with_tools.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
| """ | ||
| Set up this example by starting a vLLM OpenAI-compatible server with tool call | ||
| options enabled. | ||
| Reasoning models can be used through the Responses API as seen here | ||
| https://platform.openai.com/docs/api-reference/responses | ||
| For example: | ||
| vllm serve Qwen/Qwen3-1.7B --reasoning-parser qwen3 \ | ||
| --structured-outputs-config.backend xgrammar \ | ||
| --enable-auto-tool-choice --tool-call-parser hermes | ||
| """ | ||
|
|
||
| import json | ||
|
|
||
| from openai import OpenAI | ||
| from utils import get_first_model | ||
|
|
||
|
|
||
| def get_weather(latitude: float, longitude: float) -> str: | ||
| """ | ||
| Mock function to simulate getting weather data. | ||
| In a real application, this would call an external weather API. | ||
| """ | ||
| return f"Current temperature at ({latitude}, {longitude}) is 20°C." | ||
|
|
||
|
|
||
| tools = [ | ||
| { | ||
| "type": "function", | ||
| "name": "get_weather", | ||
| "description": "Get current temperature for provided coordinates in celsius.", | ||
| "parameters": { | ||
| "type": "object", | ||
| "properties": { | ||
| "latitude": {"type": "number"}, | ||
| "longitude": {"type": "number"}, | ||
| }, | ||
| "required": ["latitude", "longitude"], | ||
| "additionalProperties": False, | ||
| }, | ||
| "strict": True, | ||
| } | ||
| ] | ||
|
|
||
| input_messages = [ | ||
| {"role": "user", "content": "What's the weather like in Paris today?"} | ||
| ] | ||
|
|
||
|
|
||
| def main(): | ||
| base_url = "http://0.0.0.0:8000/v1" | ||
| client = OpenAI(base_url=base_url, api_key="empty") | ||
| model = get_first_model(client) | ||
| response = client.responses.create( | ||
| model=model, input=input_messages, tools=tools, tool_choice="required" | ||
| ) | ||
|
|
||
| for out in response.output: | ||
| if out.type == "function_call": | ||
| print("Function call:", out.name, out.arguments) | ||
| tool_call = out | ||
| args = json.loads(tool_call.arguments) | ||
| result = get_weather(args["latitude"], args["longitude"]) | ||
|
|
||
| input_messages.append(tool_call) # append model's function call message | ||
| input_messages.append( | ||
| { # append result message | ||
| "type": "function_call_output", | ||
| "call_id": tool_call.call_id, | ||
| "output": str(result), | ||
| } | ||
| ) | ||
| response_2 = client.responses.create( | ||
| model=model, | ||
| input=input_messages, | ||
| tools=tools, | ||
| ) | ||
| print(response_2.output_text) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
198 changes: 198 additions & 0 deletions
198
tests/v1/entrypoints/openai/serving_responses/test_function_call.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
|
||
| import json | ||
|
|
||
| import openai # use the official client for correctness check | ||
| import pytest | ||
|
|
||
| MODEL_NAME = "Qwen/Qwen3-1.7B" | ||
| tools = [ | ||
| { | ||
| "type": "function", | ||
| "name": "get_current_weather", | ||
| "description": "Get the current weather in a given location", | ||
| "parameters": { | ||
| "type": "object", | ||
| "properties": { | ||
| "city": { | ||
| "type": "string", | ||
| "description": "The city to find the weather for, e.g. 'Vienna'", | ||
| "default": "Vienna", | ||
| }, | ||
| "country": { | ||
| "type": "string", | ||
| "description": "The country that the city is in, e.g. 'Austria'", | ||
| }, | ||
| "unit": { | ||
| "type": "string", | ||
| "description": "The unit to fetch the temperature in", | ||
| "enum": ["celsius", "fahrenheit"], | ||
| }, | ||
| "options": { | ||
| "$ref": "#/$defs/WeatherOptions", | ||
| "description": "Optional parameters for weather query", | ||
| }, | ||
| }, | ||
| "required": ["country", "unit"], | ||
| "$defs": { | ||
| "WeatherOptions": { | ||
| "title": "WeatherOptions", | ||
| "type": "object", | ||
| "additionalProperties": False, | ||
| "properties": { | ||
| "unit": { | ||
| "type": "string", | ||
| "enum": ["celsius", "fahrenheit"], | ||
| "default": "celsius", | ||
| "description": "Temperature unit", | ||
| "title": "Temperature Unit", | ||
| }, | ||
| "include_forecast": { | ||
| "type": "boolean", | ||
| "default": False, | ||
| "description": "Whether to include a 24-hour forecast", | ||
| "title": "Include Forecast", | ||
| }, | ||
| "language": { | ||
| "type": "string", | ||
| "default": "zh-CN", | ||
| "description": "Language of the response", | ||
| "title": "Language", | ||
| "enum": ["zh-CN", "en-US", "ja-JP"], | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| "type": "function", | ||
| "name": "get_forecast", | ||
| "description": "Get the weather forecast for a given location", | ||
| "parameters": { | ||
| "type": "object", | ||
| "properties": { | ||
| "city": { | ||
| "type": "string", | ||
| "description": "The city to get the forecast for, e.g. 'Vienna'", | ||
| "default": "Vienna", | ||
| }, | ||
| "country": { | ||
| "type": "string", | ||
| "description": "The country that the city is in, e.g. 'Austria'", | ||
| }, | ||
| "days": { | ||
| "type": "integer", | ||
| "description": "Number of days to get the forecast for (1-7)", | ||
| }, | ||
| "unit": { | ||
| "type": "string", | ||
| "description": "The unit to fetch the temperature in", | ||
| "enum": ["celsius", "fahrenheit"], | ||
| }, | ||
| }, | ||
| "required": ["country", "days", "unit"], | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.parametrize("model_name", [MODEL_NAME]) | ||
| @pytest.mark.parametrize("tool_choice", ["auto", "required"]) | ||
| async def test_function_tool_use( | ||
| client: openai.AsyncOpenAI, model_name: str, tool_choice: str | ||
| ): | ||
| prompt = [ | ||
| { | ||
| "role": "user", | ||
| "content": "Can you tell me what the current weather is in Berlin and the " | ||
| "forecast for the next 5 days, in fahrenheit?", | ||
| }, | ||
| ] | ||
| response = await client.responses.create( | ||
| model=model_name, | ||
| input=prompt, | ||
| tools=tools, | ||
| tool_choice=tool_choice, | ||
| ) | ||
|
|
||
| assert len(response.output) >= 1 | ||
| tool_call = None | ||
| reasoning = None | ||
| for out in response.output: | ||
| if out.type == "function_call": | ||
| tool_call = out | ||
| if out.type == "reasoning": | ||
| reasoning = out | ||
| assert tool_call is not None | ||
| assert tool_call.type == "function_call" | ||
| assert json.loads(tool_call.arguments) is not None | ||
| assert reasoning is not None | ||
| assert reasoning.type == "reasoning" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_named_tool_use(client: openai.AsyncOpenAI): | ||
| def get_weather(latitude: float, longitude: float) -> str: | ||
| """ | ||
| Mock function to simulate getting weather data. | ||
| In a real application, this would call an external weather API. | ||
| """ | ||
| return f"Current temperature at ({latitude}, {longitude}) is 20°C." | ||
|
|
||
| tools = [ | ||
| { | ||
| "type": "function", | ||
| "name": "get_weather", | ||
| "description": ( | ||
| "Get current temperature for provided coordinates in celsius." | ||
| ), | ||
| "parameters": { | ||
| "type": "object", | ||
| "properties": { | ||
| "latitude": {"type": "number"}, | ||
| "longitude": {"type": "number"}, | ||
| }, | ||
| "required": ["latitude", "longitude"], | ||
| "additionalProperties": False, | ||
| }, | ||
| "strict": True, | ||
| } | ||
| ] | ||
|
|
||
| input_messages = [ | ||
| {"role": "user", "content": "What's the weather like in Paris today?"} | ||
| ] | ||
|
|
||
| response = await client.responses.create( | ||
| model=MODEL_NAME, | ||
| input=input_messages, | ||
| tools=tools, | ||
| tool_choice={"type": "function", "name": "get_weather"}, | ||
| ) | ||
| assert len(response.output) >= 1 | ||
| for out in response.output: | ||
| if out.type == "function_call": | ||
| tool_call = out | ||
| assert tool_call is not None | ||
| assert tool_call.type == "function_call" | ||
| assert tool_call.name == "get_weather" | ||
| args = json.loads(tool_call.arguments) | ||
| assert args["latitude"] is not None | ||
| assert args["longitude"] is not None | ||
| # call the tool | ||
| result = get_weather(args["latitude"], args["longitude"]) | ||
| input_messages.append(tool_call) # append model's function call message | ||
| input_messages.append( | ||
| { # append result message | ||
| "type": "function_call_output", | ||
| "call_id": tool_call.call_id, | ||
| "output": str(result), | ||
| } | ||
| ) | ||
| # create a new response with the tool call result | ||
| response_2 = await client.responses.create(model=MODEL_NAME, input=input_messages) | ||
| # check the output | ||
| assert len(response_2.output_text) > 0 | ||
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.