-
-
Notifications
You must be signed in to change notification settings - Fork 17.2k
[Tool Parser]Add tool parser for NVIDIA-Nemotron-Nano-9B-v2 model #42255
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
Open
sniper35
wants to merge
4
commits into
vllm-project:main
Choose a base branch
from
sniper35:add-nemotron-tool-parser
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4b6abc6
tool_parsers: add Nemotron JSON tool parser
sniper35 32367d4
cover streaming extraction for content-plus-tool and parallel-call ch…
sniper35 ce7222b
cover the case the too_name needs to be escaped
sniper35 644451f
refactored parser name to be more specific toward Nano-V2
sniper35 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
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
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,137 @@ | ||
| {%- set ns = namespace(enable_thinking=true) -%} | ||
|
|
||
| {%- for message in messages -%} | ||
| {%- set content = message['content'] -%} | ||
| {%- if message['role'] == 'user' or message['role'] == 'system' -%} | ||
| {%- if '/think' in content -%} | ||
| {%- set ns.enable_thinking = true -%} | ||
| {%- elif '/no_think' in content -%} | ||
| {%- set ns.enable_thinking = false -%} | ||
| {%- endif -%} | ||
| {%- endif -%} | ||
| {%- endfor -%} | ||
|
|
||
| {%- if messages[0]['role'] != 'system' -%} | ||
| {%- set ns.non_tool_system_content = '' -%} | ||
| {{- '<SPECIAL_10>System\n' -}} | ||
| {%- else -%} | ||
| {%- set ns.non_tool_system_content = messages[0]['content'] | ||
| .replace('/think', '') | ||
| .replace('/no_think', '') | ||
| .strip() | ||
| -%} | ||
| {{- '<SPECIAL_10>System\n' + ns.non_tool_system_content }} | ||
| {%- endif -%} | ||
|
|
||
| {%- if tools -%} | ||
| {%- if ns.non_tool_system_content is defined | ||
| and ns.non_tool_system_content != '' -%} | ||
| {{- '\n\n' -}} | ||
| {%- endif -%} | ||
|
|
||
| {{- 'You can use the following tools to assist the user if required:' -}} | ||
| {{- '\n<AVAILABLE_TOOLS>[' -}} | ||
| {%- for tool in tools -%} | ||
| {{- (tool.function if tool.function is defined else tool) | tojson -}} | ||
| {{- ', ' if not loop.last else '' -}} | ||
| {%- endfor -%} | ||
| {{- ']</AVAILABLE_TOOLS>\n\n' -}} | ||
|
|
||
| {{- 'If you decide to call any tool(s), use the following format:\n' -}} | ||
| {{- '<TOOLCALL>[{{"name": "tool_name1", "arguments": "tool_args1"}}, ' -}} | ||
| {{- '{{"name": "tool_name2", "arguments": "tool_args2"}}]</TOOLCALL>\n\n' -}} | ||
|
|
||
| {{- 'The user will execute tool-calls and return responses from tool(s) in this format:\n' -}} | ||
| {{- '<TOOL_RESPONSE>[{{"tool_response1"}}, {{"tool_response2"}}]</TOOL_RESPONSE>\n\n' -}} | ||
|
|
||
| {{- 'Based on the tool responses, you can call additional tools if needed, correct tool calls if any errors are found, or just respond to the user.' -}} | ||
| {%- endif -%} | ||
|
|
||
| {{- '\n' -}} | ||
|
|
||
| {%- set messages = messages[1:] if messages[0]['role'] == 'system' else messages -%} | ||
|
|
||
| {%- if messages[-1]['role'] == 'assistant' -%} | ||
| {%- set ns.last_turn_assistant_content = messages[-1]['content'].strip() -%} | ||
| {%- set messages = messages[:-1] -%} | ||
| {%- endif -%} | ||
|
|
||
| {%- for message in messages -%} | ||
| {%- set content = message['content'] -%} | ||
|
|
||
| {%- if message['role'] == 'user' -%} | ||
| {{- '<SPECIAL_11>User\n' + content.replace('/think', '').replace('/no_think', '').strip() + '\n' }} | ||
|
|
||
| {%- elif message['role'] == 'tool' -%} | ||
| {%- if loop.first or (messages[loop.index0 - 1].role != 'tool') -%} | ||
| {{- '<SPECIAL_11>User\n' + '<TOOL_RESPONSE>[' }} | ||
| {%- endif -%} | ||
| {{- message['content'] -}} | ||
| {{- ', ' if not loop.last and (messages[loop.index0 + 1].role == 'tool') else '' -}} | ||
| {%- if loop.last or (messages[loop.index0 + 1].role != 'tool') -%} | ||
| {{- ']</TOOL_RESPONSE>\n' -}} | ||
| {%- endif -%} | ||
|
|
||
| {%- elif message['role'] == 'assistant' -%} | ||
| {%- if '</think>' in content -%} | ||
| {%- set content = content.split('</think>')[1].strip() -%} | ||
| {%- endif -%} | ||
|
|
||
| {{- '<SPECIAL_11>Assistant\n' + content.strip() }} | ||
|
|
||
| {%- if message.tool_calls -%} | ||
| {%- if content.strip() != '' -%} | ||
| {{- '\n\n' -}} | ||
| {%- endif -%} | ||
| {{- '<TOOLCALL>[' -}} | ||
| {%- for call in message.tool_calls -%} | ||
| {%- set fn = call.function if call.function is defined else call -%} | ||
| {{- '{"name": ' -}} | ||
| {{- fn.name | tojson -}} | ||
| {{- ', "arguments": ' -}} | ||
| {%- if fn.arguments is string -%} | ||
| {{- fn.arguments -}} | ||
| {%- else -%} | ||
| {{- fn.arguments | tojson -}} | ||
| {%- endif -%} | ||
| {{- '}' + (', ' if not loop.last else '') -}} | ||
| {%- endfor -%} | ||
| {{- ']</TOOLCALL>' -}} | ||
| {%- endif -%} | ||
|
|
||
| {{- '\n<SPECIAL_12>\n' -}} | ||
| {%- endif -%} | ||
| {%- endfor -%} | ||
|
|
||
| {%- if add_generation_prompt -%} | ||
| {{- '<SPECIAL_11>Assistant\n' -}} | ||
| {%- if ns.enable_thinking is defined and ns.enable_thinking is false -%} | ||
| {{- '<think></think>' -}} | ||
| {%- else -%} | ||
| {{- '<think>\n' -}} | ||
| {%- endif -%} | ||
| {%- if ns.last_turn_assistant_content is defined | ||
| and ns.last_turn_assistant_content != '' -%} | ||
| {{- ns.last_turn_assistant_content -}} | ||
| {%- endif -%} | ||
|
|
||
| {%- else -%} | ||
| {%- if ns.last_turn_assistant_content is defined | ||
| and ns.last_turn_assistant_content != '' -%} | ||
| {{- '<SPECIAL_11>Assistant\n' -}} | ||
| {%- if ns.enable_thinking is defined and ns.enable_thinking is false -%} | ||
| {{- '<think></think>' -}} | ||
| {%- else -%} | ||
| {{- '<think>\n' -}} | ||
| {%- endif -%} | ||
| {{- ns.last_turn_assistant_content -}} | ||
|
|
||
| {%- if continue_final_message is defined -%} | ||
| {%- if continue_final_message is false -%} | ||
| {{- '\n<SPECIAL_12>\n' -}} | ||
| {%- endif -%} | ||
| {%- else -%} | ||
| {{- '\n<SPECIAL_12>\n' -}} | ||
| {%- endif -%} | ||
| {%- endif -%} | ||
| {%- endif -%} |
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,45 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
|
||
| import json | ||
| from pathlib import Path | ||
|
|
||
| import jinja2.sandbox | ||
|
|
||
| TEMPLATE_PATH = ( | ||
| Path(__file__).resolve().parent.parent.parent | ||
| / "examples" | ||
| / "tool_chat_template_nemotron_nano_v2.jinja" | ||
| ) | ||
|
|
||
|
|
||
| def test_tool_call_name_is_json_escaped(): | ||
| template = jinja2.sandbox.ImmutableSandboxedEnvironment().from_string( | ||
| TEMPLATE_PATH.read_text() | ||
| ) | ||
| tool_name = 'search"quoted\\name' | ||
| rendered = template.render( | ||
| messages=[ | ||
| {"role": "user", "content": "Search docs"}, | ||
| { | ||
| "role": "assistant", | ||
| "content": "", | ||
| "tool_calls": [ | ||
| { | ||
| "function": { | ||
| "name": tool_name, | ||
| "arguments": {"query": "vllm"}, | ||
| }, | ||
| } | ||
| ], | ||
| }, | ||
| {"role": "tool", "content": '{"result": "ok"}'}, | ||
| ], | ||
| add_generation_prompt=False, | ||
| ) | ||
|
|
||
| payload = rendered.split("<TOOLCALL>", 1)[1].split("</TOOLCALL>", 1)[0] | ||
| tool_calls = json.loads(payload) | ||
|
|
||
| assert tool_calls[0]["name"] == tool_name | ||
| assert tool_calls[0]["arguments"] == {"query": "vllm"} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to align with with the latest code base