|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | + |
| 4 | +import json |
| 5 | + |
| 6 | +import openai # use the official client for correctness check |
| 7 | +import pytest |
| 8 | + |
| 9 | +MODEL_NAME = "Qwen/Qwen3-1.7B" |
| 10 | +tools = [ |
| 11 | + { |
| 12 | + "type": "function", |
| 13 | + "name": "get_current_weather", |
| 14 | + "description": "Get the current weather in a given location", |
| 15 | + "parameters": { |
| 16 | + "type": "object", |
| 17 | + "properties": { |
| 18 | + "city": { |
| 19 | + "type": "string", |
| 20 | + "description": "The city to find the weather for, e.g. 'Vienna'", |
| 21 | + "default": "Vienna", |
| 22 | + }, |
| 23 | + "country": { |
| 24 | + "type": "string", |
| 25 | + "description": "The country that the city is in, e.g. 'Austria'", |
| 26 | + }, |
| 27 | + "unit": { |
| 28 | + "type": "string", |
| 29 | + "description": "The unit to fetch the temperature in", |
| 30 | + "enum": ["celsius", "fahrenheit"], |
| 31 | + }, |
| 32 | + "options": { |
| 33 | + "$ref": "#/$defs/WeatherOptions", |
| 34 | + "description": "Optional parameters for weather query", |
| 35 | + }, |
| 36 | + }, |
| 37 | + "required": ["country", "unit"], |
| 38 | + "$defs": { |
| 39 | + "WeatherOptions": { |
| 40 | + "title": "WeatherOptions", |
| 41 | + "type": "object", |
| 42 | + "additionalProperties": False, |
| 43 | + "properties": { |
| 44 | + "unit": { |
| 45 | + "type": "string", |
| 46 | + "enum": ["celsius", "fahrenheit"], |
| 47 | + "default": "celsius", |
| 48 | + "description": "Temperature unit", |
| 49 | + "title": "Temperature Unit", |
| 50 | + }, |
| 51 | + "include_forecast": { |
| 52 | + "type": "boolean", |
| 53 | + "default": False, |
| 54 | + "description": "Whether to include a 24-hour forecast", |
| 55 | + "title": "Include Forecast", |
| 56 | + }, |
| 57 | + "language": { |
| 58 | + "type": "string", |
| 59 | + "default": "zh-CN", |
| 60 | + "description": "Language of the response", |
| 61 | + "title": "Language", |
| 62 | + "enum": ["zh-CN", "en-US", "ja-JP"], |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + "type": "function", |
| 71 | + "name": "get_forecast", |
| 72 | + "description": "Get the weather forecast for a given location", |
| 73 | + "parameters": { |
| 74 | + "type": "object", |
| 75 | + "properties": { |
| 76 | + "city": { |
| 77 | + "type": "string", |
| 78 | + "description": "The city to get the forecast for, e.g. 'Vienna'", |
| 79 | + "default": "Vienna", |
| 80 | + }, |
| 81 | + "country": { |
| 82 | + "type": "string", |
| 83 | + "description": "The country that the city is in, e.g. 'Austria'", |
| 84 | + }, |
| 85 | + "days": { |
| 86 | + "type": "integer", |
| 87 | + "description": "Number of days to get the forecast for (1-7)", |
| 88 | + }, |
| 89 | + "unit": { |
| 90 | + "type": "string", |
| 91 | + "description": "The unit to fetch the temperature in", |
| 92 | + "enum": ["celsius", "fahrenheit"], |
| 93 | + }, |
| 94 | + }, |
| 95 | + "required": ["country", "days", "unit"], |
| 96 | + }, |
| 97 | + }, |
| 98 | +] |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.asyncio |
| 102 | +@pytest.mark.parametrize("model_name", [MODEL_NAME]) |
| 103 | +@pytest.mark.parametrize("tool_choice", ["auto", "required"]) |
| 104 | +async def test_function_tool_use( |
| 105 | + client: openai.AsyncOpenAI, model_name: str, tool_choice: str |
| 106 | +): |
| 107 | + prompt = [ |
| 108 | + { |
| 109 | + "role": "user", |
| 110 | + "content": "Can you tell me what the current weather is in Berlin and the " |
| 111 | + "forecast for the next 5 days, in fahrenheit?", |
| 112 | + }, |
| 113 | + ] |
| 114 | + response = await client.responses.create( |
| 115 | + model=model_name, |
| 116 | + input=prompt, |
| 117 | + tools=tools, |
| 118 | + tool_choice=tool_choice, |
| 119 | + ) |
| 120 | + |
| 121 | + assert len(response.output) >= 1 |
| 122 | + tool_call = None |
| 123 | + reasoning = None |
| 124 | + for out in response.output: |
| 125 | + if out.type == "function_call": |
| 126 | + tool_call = out |
| 127 | + if out.type == "reasoning": |
| 128 | + reasoning = out |
| 129 | + assert tool_call is not None |
| 130 | + assert tool_call.type == "function_call" |
| 131 | + assert json.loads(tool_call.arguments) is not None |
| 132 | + assert reasoning is not None |
| 133 | + assert reasoning.type == "reasoning" |
| 134 | + |
| 135 | + |
| 136 | +@pytest.mark.asyncio |
| 137 | +async def test_named_tool_use(client: openai.AsyncOpenAI): |
| 138 | + def get_weather(latitude: float, longitude: float) -> str: |
| 139 | + """ |
| 140 | + Mock function to simulate getting weather data. |
| 141 | + In a real application, this would call an external weather API. |
| 142 | + """ |
| 143 | + return f"Current temperature at ({latitude}, {longitude}) is 20°C." |
| 144 | + |
| 145 | + tools = [ |
| 146 | + { |
| 147 | + "type": "function", |
| 148 | + "name": "get_weather", |
| 149 | + "description": ( |
| 150 | + "Get current temperature for provided coordinates in celsius." |
| 151 | + ), |
| 152 | + "parameters": { |
| 153 | + "type": "object", |
| 154 | + "properties": { |
| 155 | + "latitude": {"type": "number"}, |
| 156 | + "longitude": {"type": "number"}, |
| 157 | + }, |
| 158 | + "required": ["latitude", "longitude"], |
| 159 | + "additionalProperties": False, |
| 160 | + }, |
| 161 | + "strict": True, |
| 162 | + } |
| 163 | + ] |
| 164 | + |
| 165 | + input_messages = [ |
| 166 | + {"role": "user", "content": "What's the weather like in Paris today?"} |
| 167 | + ] |
| 168 | + |
| 169 | + response = await client.responses.create( |
| 170 | + model=MODEL_NAME, |
| 171 | + input=input_messages, |
| 172 | + tools=tools, |
| 173 | + tool_choice={"type": "function", "name": "get_weather"}, |
| 174 | + ) |
| 175 | + assert len(response.output) >= 1 |
| 176 | + for out in response.output: |
| 177 | + if out.type == "function_call": |
| 178 | + tool_call = out |
| 179 | + assert tool_call is not None |
| 180 | + assert tool_call.type == "function_call" |
| 181 | + assert tool_call.name == "get_weather" |
| 182 | + args = json.loads(tool_call.arguments) |
| 183 | + assert args["latitude"] is not None |
| 184 | + assert args["longitude"] is not None |
| 185 | + # call the tool |
| 186 | + result = get_weather(args["latitude"], args["longitude"]) |
| 187 | + input_messages.append(tool_call) # append model's function call message |
| 188 | + input_messages.append( |
| 189 | + { # append result message |
| 190 | + "type": "function_call_output", |
| 191 | + "call_id": tool_call.call_id, |
| 192 | + "output": str(result), |
| 193 | + } |
| 194 | + ) |
| 195 | + # create a new response with the tool call result |
| 196 | + response_2 = await client.responses.create(model=MODEL_NAME, input=input_messages) |
| 197 | + # check the output |
| 198 | + assert len(response_2.output_text) > 0 |
0 commit comments