-
-
Notifications
You must be signed in to change notification settings - Fork 11.7k
[Feature] Add support for Llama 3.1 and 3.2 tool use #8343
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 16 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b85ad93
Add a tool parser for the Llama 3.1 tool use
maxdebayser 1abbe93
Add streaming support for llama tool use
maxdebayser 2d2a329
Merge branch 'upstream_main' into llama_3.1_tool_use
maxdebayser fa6ebb9
bring tool parser up to date with latest changes
maxdebayser be5eeab
add unit tests and chat template
maxdebayser d8d6de4
Improve streaming logic to support partial results
maxdebayser 15d39c8
Merge branch 'upstream_main' into llama_3.1_tool_use
maxdebayser 37336f3
address review comments
maxdebayser 1babd5d
Merge branch 'vllm-project:main' into llama_3.1_tool_use
maxdebayser e7d34dc
Add documentation about the llama 3 tool use
maxdebayser b22d8c9
Merge branch 'vllm-project:main' into llama_3.1_tool_use
maxdebayser 3bb941a
Move more logic to the chat template
maxdebayser f66c0b1
correct chat template typo again
maxdebayser 0fabb67
Merge branch 'vllm-project:main' into llama_3.1_tool_use
maxdebayser a90055f
Merge branch 'vllm-project:main' into llama_3.1_tool_use
maxdebayser 932a093
fix mypy problem
maxdebayser 669fe67
add template version specialized for llama 3.1
maxdebayser 6f01abf
Merge branch 'vllm-project:main' into llama_3.1_tool_use
maxdebayser 871c568
rename chat template
maxdebayser 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,90 @@ | ||
| {{- bos_token }} | ||
| {%- if custom_tools is defined %} | ||
| {%- set tools = custom_tools %} | ||
| {%- endif %} | ||
| {%- if not tools_in_user_message is defined %} | ||
| {%- set tools_in_user_message = true %} | ||
| {%- endif %} | ||
| {%- if not date_string is defined %} | ||
| {%- set date_string = "26 Jul 2024" %} | ||
| {%- endif %} | ||
| {%- if not tools is defined %} | ||
| {%- set tools = none %} | ||
| {%- endif %} | ||
|
|
||
| {#- This block extracts the system message, so we can slot it into the right place. #} | ||
| {%- if messages[0]['role'] == 'system' %} | ||
| {%- set system_message = messages[0]['content']|trim %} | ||
| {%- set messages = messages[1:] %} | ||
| {%- else %} | ||
| {%- set system_message = "You are a helpful assistant with tool calling capabilities. Only reply with a tool call if the function exists in the library provided by the user. If it doesn't exist, just reply directly in natural language. When you receive a tool call response, use the output to format an answer to the original user question." %} | ||
| {%- endif %} | ||
|
|
||
| {#- System message + builtin tools #} | ||
| {{- "<|start_header_id|>system<|end_header_id|>\n\n" }} | ||
| {%- if tools is not none %} | ||
| {{- "Environment: ipython\n" }} | ||
| {%- endif %} | ||
| {{- "Knowledge Cutoff Date: December 2023\n" }} | ||
| {{- "Today's Date: " + date_string + "\n\n" }} | ||
| {%- if tools is not none and not tools_in_user_message %} | ||
| {{- "You have access to the following functions. To call a function, please respond with JSON for a function call." }} | ||
| {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }} | ||
| {{- "Do not use variables.\n\n" }} | ||
| {%- for t in tools %} | ||
| {{- t | tojson(indent=4) }} | ||
| {{- "\n\n" }} | ||
| {%- endfor %} | ||
| {%- endif %} | ||
| {{- system_message }} | ||
| {{- "<|eot_id|>" }} | ||
|
|
||
| {#- Custom tools are passed in a user message with some extra guidance #} | ||
| {%- if tools_in_user_message and not tools is none %} | ||
| {#- Extract the first user message so we can plug it in here #} | ||
| {%- if messages | length != 0 %} | ||
| {%- set first_user_message = messages[0]['content']|trim %} | ||
| {%- set messages = messages[1:] %} | ||
| {%- else %} | ||
| {{- raise_exception("Cannot put tools in the first user message when there's no first user message!") }} | ||
| {%- endif %} | ||
| {{- '<|start_header_id|>user<|end_header_id|>\n\n' -}} | ||
| {{- "Given the following functions, please respond with a JSON for a function call " }} | ||
| {{- "with its proper arguments that best answers the given prompt.\n\n" }} | ||
| {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }} | ||
| {{- "Do not use variables.\n\n" }} | ||
| {%- for t in tools %} | ||
| {{- t | tojson(indent=4) }} | ||
| {{- "\n\n" }} | ||
| {%- endfor %} | ||
| {{- first_user_message + "<|eot_id|>"}} | ||
| {%- endif %} | ||
|
|
||
| {%- for message in messages %} | ||
| {%- if not (message.role == 'ipython' or message.role == 'tool' or 'tool_calls' in message) %} | ||
| {{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'+ message['content'] | trim + '<|eot_id|>' }} | ||
| {%- elif 'tool_calls' in message %} | ||
| {%- if not message.tool_calls|length == 1 %} | ||
| {{- raise_exception("This model only supports single tool-calls at once!") }} | ||
| {%- endif %} | ||
| {%- set tool_call = message.tool_calls[0].function %} | ||
| {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' -}} | ||
| {{- '{"name": "' + tool_call.name + '", ' }} | ||
| {{- '"parameters": ' }} | ||
| {{- tool_call.arguments | tojson }} | ||
| {{- "}" }} | ||
| {{- "<|eot_id|>" }} | ||
| {%- elif message.role == "tool" or message.role == "ipython" %} | ||
| {{- "<|start_header_id|>ipython<|end_header_id|>\n\n" }} | ||
| {%- if message.content is mapping %} | ||
| {{- message.content | tojson }} | ||
| {%- else %} | ||
| {{- { "output": message.content } | tojson }} | ||
| {%- endif %} | ||
| {{- "<|eot_id|>" }} | ||
| {%- endif %} | ||
| {%- endfor %} | ||
| {%- if add_generation_prompt %} | ||
| {{- '<|start_header_id|>assistant<|end_header_id|>\n\n' }} | ||
| {%- 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
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
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
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 |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| from .abstract_tool_parser import ToolParser | ||
| from .hermes_tool_parser import Hermes2ProToolParser | ||
| from .llama_tool_parser import Llama3JsonToolParser | ||
| from .mistral_tool_parser import MistralToolParser | ||
|
|
||
| __all__ = ["ToolParser", "Hermes2ProToolParser", "MistralToolParser"] | ||
| __all__ = [ | ||
| "ToolParser", "Hermes2ProToolParser", "MistralToolParser", | ||
| "Llama3JsonToolParser" | ||
| ] |
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.
for what it's worth I have had great success with keeping tools in system message, but that's just me