-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
[Model] Add mistral function calling format to all models loaded with "mistral" format #8515
Merged
DarkLight1337
merged 13 commits into
vllm-project:main
from
patrickvonplaten:add_mistral_func_call
Sep 17, 2024
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
79c7f7d
WIP
patrickvonplaten 61f0572
WIP
patrickvonplaten cece5cf
WIP
patrickvonplaten aee318d
Merge branch 'main' of https://github.com/vllm-project/vllm into add_…
patrickvonplaten 5a15813
format
patrickvonplaten 51ef13b
format
patrickvonplaten ddec954
format
patrickvonplaten 1ec00bc
Apply suggestion
DarkLight1337 1d4de44
format
DarkLight1337 aaf02a5
Trigger CI build
patrickvonplaten 0575475
Up
patrickvonplaten 1054b12
fix tests
patrickvonplaten f1e72d1
Trigger CI build
patrickvonplaten 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 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,138 @@ | ||
# ruff: noqa | ||
import json | ||
import random | ||
import string | ||
|
||
from vllm import LLM | ||
from vllm.sampling_params import SamplingParams | ||
|
||
# This script is an offline demo for running Pixtral. | ||
DarkLight1337 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# | ||
# If you want to run a server/client setup, please follow this code: | ||
# | ||
# - Server: | ||
# | ||
# ```bash | ||
# vllm serve mistralai/Mistral-7B-Instruct-v0.3 --tokenizer-mode mistral --load-format mistral --config-format mistral | ||
# ``` | ||
# | ||
# - Client: | ||
# | ||
# ```bash | ||
# curl --location 'http://<your-node-url>:8000/v1/chat/completions' \ | ||
# --header 'Content-Type: application/json' \ | ||
# --header 'Authorization: Bearer token' \ | ||
# --data '{ | ||
# "model": "mistralai/Pixtral-12B-2409", | ||
# "messages": [ | ||
# { | ||
# "role": "user", | ||
# "content": [ | ||
# {"type" : "text", "text": "Describe this image in detail please."}, | ||
# {"type": "image_url", "image_url": {"url": "https://s3.amazonaws.com/cms.ipressroom.com/338/files/201808/5b894ee1a138352221103195_A680%7Ejogging-edit/A680%7Ejogging-edit_hero.jpg"}}, | ||
# {"type" : "text", "text": "and this one as well. Answer in French."}, | ||
# {"type": "image_url", "image_url": {"url": "https://www.wolframcloud.com/obj/resourcesystem/images/a0e/a0ee3983-46c6-4c92-b85d-059044639928/6af8cfb971db031b.png"}} | ||
# ] | ||
# } | ||
# ] | ||
# }' | ||
# ``` | ||
# | ||
# Usage: | ||
# python demo.py simple | ||
# python demo.py advanced | ||
|
||
model_name = "mistralai/Mistral-7B-Instruct-v0.3" | ||
# or switch to "mistralai/Mistral-Nemo-Instruct-2407" | ||
# or "mistralai/Mistral-Large-Instruct-2407" | ||
# or any other mistral model with function calling ability | ||
|
||
sampling_params = SamplingParams(max_tokens=8192, temperature=0.0) | ||
llm = LLM(model=model_name, | ||
tokenizer_mode="mistral", | ||
config_format="mistral", | ||
load_format="mistral") | ||
|
||
|
||
def generate_random_id(length=9): | ||
characters = string.ascii_letters + string.digits | ||
random_id = ''.join(random.choice(characters) for _ in range(length)) | ||
return random_id | ||
|
||
|
||
# simulate an API that can be called | ||
def get_current_weather(city: str, state: str, unit: 'str'): | ||
return (f"The weather in {city}, {state} is 85 degrees {unit}. It is " | ||
"partly cloudly, with highs in the 90's.") | ||
|
||
|
||
tool_funtions = {"get_current_weather": get_current_weather} | ||
|
||
tools = [{ | ||
"type": "function", | ||
"function": { | ||
"name": "get_current_weather", | ||
"description": "Get the current weather in a given location", | ||
"parameters": { | ||
"type": "object", | ||
"properties": { | ||
"city": { | ||
"type": | ||
"string", | ||
"description": | ||
"The city to find the weather for, e.g. 'San Francisco'" | ||
}, | ||
"state": { | ||
"type": | ||
"string", | ||
"description": | ||
"the two-letter abbreviation for the state that the city is" | ||
" in, e.g. 'CA' which would mean 'California'" | ||
}, | ||
"unit": { | ||
"type": "string", | ||
"description": "The unit to fetch the temperature in", | ||
"enum": ["celsius", "fahrenheit"] | ||
} | ||
}, | ||
"required": ["city", "state", "unit"] | ||
} | ||
} | ||
}] | ||
|
||
messages = [{ | ||
"role": | ||
"user", | ||
"content": | ||
"Can you tell me what the temperate will be in Dallas, in fahrenheit?" | ||
}] | ||
|
||
outputs = llm.chat(messages, sampling_params=sampling_params, tools=tools) | ||
output = outputs[0].outputs[0].text.strip() | ||
|
||
# append the assistant message | ||
messages.append({ | ||
"role": "assistant", | ||
"content": output, | ||
}) | ||
|
||
# let's now actually parse and execute the model's output simulating an API call by using the | ||
# above defined function | ||
tool_calls = json.loads(output) | ||
tool_answers = [ | ||
tool_funtions[call['name']](**call['arguments']) for call in tool_calls | ||
] | ||
|
||
# append the answer as a tool message and let the LLM give you an answer | ||
messages.append({ | ||
"role": "tool", | ||
"content": "\n\n".join(tool_answers), | ||
"tool_call_id": generate_random_id(), | ||
}) | ||
|
||
outputs = llm.chat(messages, sampling_params, tools=tools) | ||
|
||
print(outputs[0].outputs[0].text.strip()) | ||
# yields | ||
# 'The weather in Dallas, TX is 85 degrees fahrenheit. ' | ||
# 'It is partly cloudly, with highs in the 90's.' |
This file contains 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 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 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 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
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.
Example file of how to use Mistral models with function calling