-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[Feature] OpenAI-Compatible Tools API + Streaming for Hermes & Mistral models #5649
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
Conversation
…= "auto"; set default `tool_choice` to "auto"
… instead of vllm/engine/arg_utils.py
…none" AND NOT "auto"
… to receive the arguments & validate them
…long with validation
…to-tool-choice and --tool-use-prompt-template are specified
Progress! I as of current commits, I can now get the hermes 2 pro model to generate a tool call using the Server: python -m vllm.entrypoints.openai.api_server --model NousResearch/Hermes-2-Pro-Llama-3-8B --tool-use-prompt-template examples/tool_template_hermes_2_pro.jinja --enable-api-tools --enable-auto-tool-choice Client: python examples/openai_chat_completion_client_with_tools.py Result
Now, working on getting it to work for non-streaming responses - then, streaming! |
A question I asked in the discord, with some open questions about how to handle configuration: Setting up function calling models for an open model requires a lot of configurations if you want to be unopinionated about the model. Here is a brief list of all the parameters that would be needed:
The question is, it is better to have all of these as separate CLI flags, or would a JSON configuration file be preferable so that people can create (and track in version control!) configs that work for popular models?) |
Please see ongoing conversation with the Hugging Face team, Nous Research & transformers maintainer here - this will make it MUCH easier to implement OpenAI API-compatible tool calling into vLLM regardless of model prompt/tokenizer configs. HF PR for Hermes 2 Pro: https://huggingface.co/NousResearch/Hermes-2-Pro-Llama-3-8B/discussions/13#66724ea9bd5875ad665f1416 HF PR for Mistral 7B instruct v0.3: https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.3/discussions/35 Once these are merged, there will be a STANDARD way in transformers to handle templating in tool responses just like for templating chat conversations into prompts with a chat template, and hopefully to pull out tool calls from generated text. |
hey great initiative and nice to see Hermes Pro model's tool calls working. there's a slight issue with this tool call -- our format requires new lines after <tool_call> XML tags: <tool_call> Also tool choice should also work since it's basically passing the chosen tool only as part of |
Thanks! Tool choice is already working via guided decoding, but I will update the PR to fix the template |
Ok the most recent commit seems to fix it:
|
@K-Mistele
In addition, I can't find openai_chat_completion_client_with_tools.py in the example fille. Can you give me some advice? |
Please see my reply here. This is a draft pull request, which means it has not been merged into vLLM's codebase, and it is not ready to be merged yet. It is still a work-in-progress. As such, none of its additions or features are available in vLLM yet. If you are interested in testing or contributing to this pull request, please see the origin fork at the |
Given that guided decoding is not enabled for "auto" tool use, what is the error handling planned in case the LLM does not output valid JSON? |
Great question! Unfortunately since each model that supports tool calling uses its' own format for function calls (as opposed to tool choice with guided decoding, where we're forcing the LLM to call a specific tool in a specific format at decode-time) the response format is up to the model and its' trainer. At this point, we are still exploring ways to handle extraction of tool calls from disparate formats to OpenAI-compatible calls in a way that isn't opinionated (we want to support multiple formats including Mistral, Hermes 2 Pro, Firefunction, etc). Until we have a good answer on this, we probably won't have a good answer for how to handle errors. There are a couple possible options once we have attempted to extract the model's tool call format into OpenAI's:
In the interim, we may try to solve this problem by "ignoring" it - detect if the model is generating a tool call, but avoid the destructuring/extraction issue by returning the tool call in the model's format to the client. Not exactly sure what this would look like, but it's a possibility. |
Copy/pasting from discord: I'd really like to make progress on tool use and right now with hugging face adding support into transformers for passing tools into the chat template, that solves one of the main issues I think the blocker now is figuring out how to handle decodiong "auto" choice tool calls from each model's specific format (mistral vs. Hermes pro vs. firefunction) and returning that to the client ESPECIALLY when streaming is requested. Until there's a "canonical" way to do decode model-specific tool-calls into the OpenAI format e.g. through transformers or "reverse templates" or something, it might be best to approach this like chat templates which is to try & support it as good as we can where possible Here's what I mean by this:
So each one needs a very different implementation. I propose creating a ToolCallParser abstract class that can be implemented for different models like mistral and hermes. If a user is using a tool-calling model, they can use a CLI flag to toggle which parser they want, if they want one at all. If not, a tool call would be treated like a regular chat completion, and they can handle it client-side. This way, we can ship support for tool calls incrementally in the absence of a commonly-accepted "best practice" on how to do this. People can also add support for other models that are important to them in a minimally-invasive way. Then, once a better way to implement this more broadly is available, we can deprecate this approach I'd really appreciate feedback on this before moving forward and would especially love to hear if @mgoin and @simon-mo would consider this an appropriate approach that would be likely to be approved. |
@br3no would love to get your feedback and review for this PR! |
I was following along the whole time, thank you so much @K-Mistele ❤️ |
How can I use it with llama 3.1 8b model? |
@meetzuber you have to write a prompt template for l3.1 if I understand the implementation correctly. |
From my understanding you have to write a I'm experimenting with a more general approach to tool parsing that generalizes over different models Since OpenAI API compatible tool calls only require two string arguments (?=.*?\(\?P<name>.*?\))(?=.*?\(\?P<arguments>.*?\)) The tool calling regex must then be provided with a cli arg Then as an example for the Llama 3.1 model with JSON based tool calling your {"name": "(?P<name>.*?)", "parameters": (?P<arguments>.*?)} which would then extract the tool call: https://regex101.com/r/RhZ4zx/1 For a different custom tool calling format just provide another <function=(?P<name>.*?)>(?P<arguments>.*?)<\/function> which also extracts tool call(s): https://regex101.com/r/9pM6IL/2 |
I will be adding support in a separate PR |
Unfortunately this approach does not work with streaming. Supporting tools in streaming mode was a core requirement of this PR since many applications use streaming mode by default if they are user-facing, and there is no way to “turn off streaming” if the model starts generating a tool call. |
@K-Mistele Documentation would be much appreciated, so the community can be quick to add support for future models with tool support. |
Only below options are mentioned in tool/function calling. and there are only 2 options for tool call parser mistral and hermes.
https://docs.vllm.ai/en/latest/serving/openai_compatible_server.html#automatic-function-calling |
That's not implemented on the main branch |
These are implemented in v0.6.0 release. |
@K-Mistele can mentioned which is the pull request for llama3.1 tool support...ithink most people is been waiting for it. |
@K-Mistele can mentioned which is the pull request for Qwen2 tool support...ithink most people is been waiting for it. |
There is not a pull request for it yet because it's still a work-in-progress and not ready for review. I can create a draft though. |
There is not a PR for this, and at this point I hadn't planned on it. If this is something you're interested, please feel free to create an issue for a feature request, and tag me in it. If it gets enough interest, I'll add it. |
…l models (vllm-project#5649) Co-authored-by: constellate <[email protected]> Co-authored-by: Kyle Mistele <[email protected]> Signed-off-by: Alvant <[email protected]>
…l models (vllm-project#5649) Co-authored-by: constellate <[email protected]> Co-authored-by: Kyle Mistele <[email protected]> Signed-off-by: LeiWang1999 <[email protected]>
DRAFT: OpenAI Tool Use Checklist
This (Draft) PR will add support for OpenAI-style tool calling in a way that is minimally opinionated about tool use formats & prompt formatting.
The following features are expected to be supported:
tool_choice="auto"
- named tool choice is already supported via guided decodingI'd welcome anyone who wants to contribute on this, and would be happy to add you to the Constellate AI vllm fork that this PR is based off of - please just leave a comment!
Checklist/roadmap:
tools
andtool_choice
tool_choice="auto"
response.tool_calls
tool_use
chat templateFIX #3237 #4656 (link existing issues this PR will resolve)
BEFORE SUBMITTING, PLEASE READ THE CHECKLIST BELOW AND FILL IN THE DESCRIPTION ABOVE
PR Checklist (Click to Expand)
Thank you for your contribution to vLLM! Before submitting the pull request, please ensure the PR meets the following criteria. This helps vLLM maintain the code quality and improve the efficiency of the review process.
PR Title and Classification
Only specific types of PRs will be reviewed. The PR title is prefixed appropriately to indicate the type of change. Please use one of the following:
[Bugfix]
for bug fixes.[CI/Build]
for build or continuous integration improvements.[Doc]
for documentation fixes and improvements.[Model]
for adding a new model or improving an existing model. Model name should appear in the title.[Frontend]
For changes on the vLLM frontend (e.g., OpenAI API server,LLM
class, etc.)[Kernel]
for changes affecting CUDA kernels or other compute kernels.[Core]
for changes in the core vLLM logic (e.g.,LLMEngine
,AsyncLLMEngine
,Scheduler
, etc.)[Hardware][Vendor]
for hardware-specific changes. Vendor name should appear in the prefix (e.g.,[Hardware][AMD]
).[Misc]
for PRs that do not fit the above categories. Please use this sparingly.Note: If the PR spans more than one category, please include all relevant prefixes.
Code Quality
The PR need to meet the following code quality standards:
format.sh
to format your code.docs/source/
if the PR modifies the user-facing behaviors of vLLM. It helps vLLM user understand and utilize the new features or changes.Notes for Large Changes
Please keep the changes as concise as possible. For major architectural changes (>500 LOC excluding kernel/data/config/test), we would expect a GitHub issue (RFC) discussing the technical design and justification. Otherwise, we will tag it with
rfc-required
and might not go through the PR.What to Expect for the Reviews
The goal of the vLLM team is to be a transparent reviewing machine. We would like to make the review process transparent and efficient and make sure no contributor feel confused or frustrated. However, the vLLM team is small, so we need to prioritize some PRs over others. Here is what you can expect from the review process:
action-required
label on the PR if there are changes required. The contributor should address the comments and ping the reviewer to re-review the PR.Thank You
Finally, thank you for taking the time to read these guidelines and for your interest in contributing to vLLM. Your contributions make vLLM a great tool for everyone!