|
| 1 | +# Tool Calling with Dynamo |
| 2 | + |
| 3 | +You can connect Dynamo to external tools and services using function calling (also known as tool calling). By providing a list of available functions, Dynamo can choose |
| 4 | +to output function arguments for the relevant function(s) which you can execute to augment the prompt with relevant external information. |
| 5 | + |
| 6 | +Tool calling (AKA function calling) is controlled using the `tool_choice` and `tools` request parameters. |
| 7 | + |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +To enable this feature, you should set the following flag while launching the backend worker |
| 12 | + |
| 13 | +- `--dyn-tool-call-parser` : select the parser from the available parsers list using the below command |
| 14 | + |
| 15 | +```bash |
| 16 | +# <backend> can be vllm, sglang, trtllm, etc. based on your installation |
| 17 | +python -m dynamo.<backend> --help" |
| 18 | +``` |
| 19 | +
|
| 20 | +> [!NOTE] |
| 21 | +> If no tool call parser is provided by the user, Dynamo will try to use default tool call parsing based on `<TOOLCALL>` and `<|python_tag|>` tool tags. |
| 22 | +
|
| 23 | +> [!TIP] |
| 24 | +> If your model's default chat template doesn't support tool calling, but the model itself does, you can specify a custom chat template per worker |
| 25 | +> with `python -m dynamo.<backend> --custom-jinja-template </path/to/template.jinja>`. |
| 26 | +
|
| 27 | +
|
| 28 | +Parser to Model Mapping |
| 29 | +
|
| 30 | +| Parser Name | Supported Models | |
| 31 | +|-------------|-----------------------------------------------------------------------| |
| 32 | +| hermes | Qwen/Qwen2.5-*, Qwen/QwQ-32B, NousResearch/Hermes-2-Pro-*, NousResearch/Hermes-2-Theta-*, NousResearch/Hermes-3-* | |
| 33 | +| mistral | mistralai/Mistral-7B-Instruct-v0.3, Additional mistral function-calling models are compatible as well.| |
| 34 | +| llama3_json | meta-llama/Llama-3.1-*, meta-llama/Llama-3.2-* | |
| 35 | +| harmony | openai/gpt-oss-* | |
| 36 | +| nemotron_deci | nvidia/nemotron-* | |
| 37 | +| phi4 | Phi-4-* | |
| 38 | +| deepseek_v3_1 | deepseek-ai/DeepSeek-V3.1 | |
| 39 | +| pythonic | meta-llama/Llama-4-* | |
| 40 | +
|
| 41 | +
|
| 42 | +## Examples |
| 43 | +
|
| 44 | +### Launch Dynamo Frontend and Backend |
| 45 | +
|
| 46 | +```bash |
| 47 | +# launch backend worker |
| 48 | +python -m dynamo.vllm --model openai/gpt-oss-20b --dyn-tool-call-parser harmony |
| 49 | +
|
| 50 | +# launch frontend worker |
| 51 | +python -m dynamo.frontend |
| 52 | +``` |
| 53 | +
|
| 54 | +### Tool Calling Request Examples |
| 55 | +
|
| 56 | +- Example 1 |
| 57 | +```python |
| 58 | +from openai import OpenAI |
| 59 | +import json |
| 60 | +
|
| 61 | +client = OpenAI(base_url="http://localhost:8081/v1", api_key="dummy") |
| 62 | +
|
| 63 | +def get_weather(location: str, unit: str): |
| 64 | + return f"Getting the weather for {location} in {unit}..." |
| 65 | +tool_functions = {"get_weather": get_weather} |
| 66 | +
|
| 67 | +tools = [{ |
| 68 | + "type": "function", |
| 69 | + "function": { |
| 70 | + "name": "get_weather", |
| 71 | + "description": "Get the current weather in a given location", |
| 72 | + "parameters": { |
| 73 | + "type": "object", |
| 74 | + "properties": { |
| 75 | + "location": {"type": "string", "description": "City and state, e.g., 'San Francisco, CA'"}, |
| 76 | + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} |
| 77 | + }, |
| 78 | + "required": ["location", "unit"] |
| 79 | + } |
| 80 | + } |
| 81 | +}] |
| 82 | +
|
| 83 | +response = client.chat.completions.create( |
| 84 | + model="openai/gpt-oss-20b", |
| 85 | + messages=[{"role": "user", "content": "What's the weather like in San Francisco in Celsius?"}], |
| 86 | + tools=tools, |
| 87 | + tool_choice="auto", |
| 88 | + max_tokens=10000 |
| 89 | +) |
| 90 | +print(f"{response}") |
| 91 | +tool_call = response.choices[0].message.tool_calls[0].function |
| 92 | +print(f"Function called: {tool_call.name}") |
| 93 | +print(f"Arguments: {tool_call.arguments}") |
| 94 | +print(f"Result: {tool_functions[tool_call.name](**json.loads(tool_call.arguments))}") |
| 95 | +``` |
| 96 | +
|
| 97 | +- Example 2 |
| 98 | +```python |
| 99 | +
|
| 100 | +# Use tools defined in example 1 |
| 101 | +
|
| 102 | +time_tool = { |
| 103 | + "type": "function", |
| 104 | + "function": { |
| 105 | + "name": "get_current_time_nyc", |
| 106 | + "description": "Get the current time in NYC.", |
| 107 | + "parameters": {} |
| 108 | + } |
| 109 | +} |
| 110 | +
|
| 111 | +
|
| 112 | +tools.append(time_tool) |
| 113 | +
|
| 114 | +messages = [ |
| 115 | + {"role": "user", "content": "What's the current time in New York?"} |
| 116 | +] |
| 117 | +
|
| 118 | +
|
| 119 | +response = client.chat.completions.create( |
| 120 | + model="openai/gpt-oss-20b", #client.models.list().data[1].id, |
| 121 | + messages=messages, |
| 122 | + tools=tools, |
| 123 | + tool_choice="auto", |
| 124 | + max_tokens=100, |
| 125 | +) |
| 126 | +print(f"{response}") |
| 127 | +tool_call = response.choices[0].message.tool_calls[0].function |
| 128 | +print(f"Function called: {tool_call.name}") |
| 129 | +print(f"Arguments: {tool_call.arguments}") |
| 130 | +``` |
| 131 | +
|
| 132 | +- Example 3 |
| 133 | +
|
| 134 | +
|
| 135 | +```python |
| 136 | +
|
| 137 | +tools = [ |
| 138 | + { |
| 139 | + "type": "function", |
| 140 | + "function": { |
| 141 | + "name": "get_tourist_attractions", |
| 142 | + "description": "Get a list of top tourist attractions for a given city.", |
| 143 | + "parameters": { |
| 144 | + "type": "object", |
| 145 | + "properties": { |
| 146 | + "city": { |
| 147 | + "type": "string", |
| 148 | + "description": "The name of the city to find attractions for.", |
| 149 | + } |
| 150 | + }, |
| 151 | + "required": ["city"], |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | +] |
| 156 | +
|
| 157 | +def get_messages(): |
| 158 | + return [ |
| 159 | + { |
| 160 | + "role": "user", |
| 161 | + "content": ( |
| 162 | + "I'm planning a trip to Tokyo next week. what are some top tourist attractions in Tokyo? " |
| 163 | + ), |
| 164 | + }, |
| 165 | + ] |
| 166 | +
|
| 167 | +
|
| 168 | +messages = get_messages() |
| 169 | +
|
| 170 | +response = client.chat.completions.create( |
| 171 | + model="openai/gpt-oss-20b", |
| 172 | + messages=messages, |
| 173 | + tools=tools, |
| 174 | + tool_choice="auto", |
| 175 | + max_tokens=100, |
| 176 | +) |
| 177 | +print(f"{response}") |
| 178 | +tool_call = response.choices[0].message.tool_calls[0].function |
| 179 | +print(f"Function called: {tool_call.name}") |
| 180 | +print(f"Arguments: {tool_call.arguments}") |
| 181 | +``` |
0 commit comments