| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | """Set up this example by starting a vLLM OpenAI-compatible server with tool call |
| 4 | options enabled. |
| 5 | Reasoning models can be used through the Responses API as seen here |
| 6 | https://platform.openai.com/docs/api-reference/responses |
| 7 | For example: |
| 8 | vllm serve Qwen/Qwen3-1.7B --reasoning-parser qwen3 \ |
| 9 | --structured-outputs-config.backend xgrammar \ |
| 10 | --enable-auto-tool-choice --tool-call-parser hermes |
| 11 | """ |
| 12 | |
| 13 | import json |
| 14 | |
| 15 | from openai import OpenAI |
| 16 | |
| 17 | |
| 18 | def get_weather(latitude: float, longitude: float) -> str: |
| 19 | """Mock function to simulate getting weather data. |
| 20 | In a real application, this would call an external weather API. |
| 21 | """ |
| 22 | return f"Current temperature at ({latitude}, {longitude}) is 20°C." |
| 23 | |
| 24 | |
| 25 | tools = [ |
| 26 | { |
| 27 | "type": "function", |
| 28 | "name": "get_weather", |
| 29 | "description": "Get current temperature for provided coordinates in celsius.", |
| 30 | "parameters": { |
| 31 | "type": "object", |
| 32 | "properties": { |
| 33 | "latitude": {"type": "number"}, |
| 34 | "longitude": {"type": "number"}, |
| 35 | }, |
| 36 | "required": ["latitude", "longitude"], |
| 37 | "additionalProperties": False, |
| 38 | }, |
| 39 | "strict": True, |
| 40 | } |
| 41 | ] |
| 42 | |
| 43 | input_messages = [ |
| 44 | {"role": "user", "content": "What's the weather like in Paris today?"} |
| 45 | ] |
| 46 | |
| 47 | |
| 48 | def main(): |
| 49 | base_url = "http://0.0.0.0:8000/v1" |
| 50 | client = OpenAI(base_url=base_url, api_key="empty") |
| 51 | model = client.models.list().data[0].id |
| 52 | response = client.responses.create( |
| 53 | model=model, input=input_messages, tools=tools, tool_choice="required" |
| 54 | ) |
| 55 | |
| 56 | for out in response.output: |
| 57 | if out.type == "function_call": |
| 58 | print("Function call:", out.name, out.arguments) |
| 59 | tool_call = out |
| 60 | args = json.loads(tool_call.arguments) |
| 61 | result = get_weather(args["latitude"], args["longitude"]) |
| 62 | |
| 63 | input_messages.append(tool_call) # append model's function call message |
| 64 | input_messages.append( |
| 65 | { # append result message |
| 66 | "type": "function_call_output", |
| 67 | "call_id": tool_call.call_id, |
| 68 | "output": str(result), |
| 69 | } |
| 70 | ) |
| 71 | response_2 = client.responses.create( |
| 72 | model=model, |
| 73 | input=input_messages, |
| 74 | tools=tools, |
| 75 | ) |
| 76 | print(response_2.output_text) |
| 77 | |
| 78 | |
| 79 | if __name__ == "__main__": |
| 80 | main() |