forked from vllm-project/vllm
-
Notifications
You must be signed in to change notification settings - Fork 0
[1] function calls on minimax m2 #2
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
qandrew
wants to merge
8
commits into
func_call_2
Choose a base branch
from
func_call_2_minimax
base: func_call_2
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
8 commits
Select commit
Hold shift + click to select a range
fa60401
minimax_m2 script
0dfcf3a
minimax chat completions multi turn
9dbe397
reasoning parser fix
8081bdb
multi turn function call responsesAPI minimax
4aa06fd
function tool not custom tool example
af1965f
fix any type
526f8f2
convert function tool hack for responsesAPI
33f7e6e
clean
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
| import json | ||
|
|
||
| from openai import OpenAI | ||
|
|
||
| """ | ||
| https://huggingface.co/MiniMaxAI/MiniMax-M2/blob/main/docs/tool_calling_guide.md | ||
|
|
||
| vllm serve MiniMaxAI/MiniMax-M2 \ | ||
| --tensor-parallel-size 4 \ | ||
| --tool-call-parser minimax_m2 \ | ||
| --reasoning-parser minimax_m2_append_think \ | ||
| --enable-auto-tool-choice \ | ||
| --port 8000 | ||
| """ | ||
|
|
||
| client = OpenAI(base_url="http://localhost:8000/v1", api_key="dummy") | ||
|
|
||
|
|
||
| def get_weather(location: str, unit: str): | ||
| return f"The weather for {location} in {unit} is 20" | ||
|
|
||
|
|
||
| tool_functions = {"get_weather": get_weather} | ||
|
|
||
| tools = [ | ||
| { | ||
| "type": "function", | ||
| "function": { | ||
| "name": "get_weather", | ||
| "description": "Get the current weather in a given location", | ||
| "parameters": { | ||
| "type": "object", | ||
| "properties": { | ||
| "location": { | ||
| "type": "string", | ||
| "description": "City and state, e.g., 'San Francisco, CA'", | ||
| }, | ||
| "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, | ||
| }, | ||
| "required": ["location", "unit"], | ||
| }, | ||
| }, | ||
| } | ||
| ] | ||
|
|
||
| messages = [ | ||
| { | ||
| "role": "user", | ||
| "content": "What's the weather like in San Francisco? use celsius.", | ||
| } | ||
| ] | ||
|
|
||
| response = client.chat.completions.create( | ||
| model=client.models.list().data[0].id, | ||
| messages=messages, | ||
| tools=tools, | ||
| tool_choice="auto", | ||
| ) | ||
|
|
||
| # print(response) | ||
|
|
||
| # tool_call = response.choices[0].message.tool_calls[0].function | ||
| # print(f"Function called: {tool_call.name}") | ||
| # print(f"Arguments: {tool_call.arguments}") | ||
| # print(f"Result: {get_weather(**json.loads(tool_call.arguments))}") | ||
|
|
||
| # feed back into | ||
| # import fbvscode; fbvscode.set_trace() | ||
|
|
||
| print("=== First response ===") | ||
| print(response) | ||
|
|
||
| # Step 3: Extract and call the function | ||
| tool_call = response.choices[0].message.tool_calls[0].function | ||
| name = tool_call.name | ||
| args = json.loads(tool_call.arguments) | ||
| result = tool_functions[name](**args) | ||
|
|
||
| print(f"\nFunction called: {name}") | ||
| print(f"Arguments: {args}") | ||
| print(f"Result: {result}") | ||
|
|
||
| # Step 4: Send the result back to the model | ||
| messages.append( | ||
| {"role": "assistant", "tool_calls": response.choices[0].message.tool_calls} | ||
| ) | ||
| messages.append( | ||
| { | ||
| "role": "tool", | ||
| "tool_call_id": response.choices[0].message.tool_calls[0].id, | ||
| "content": result, | ||
| } | ||
| ) | ||
|
|
||
| # Step 5: Second call — model sees tool output | ||
| second_response = client.chat.completions.create( | ||
| model=client.models.list().data[0].id, | ||
| messages=messages, | ||
| ) | ||
|
|
||
| print("\n=== Second response ===") | ||
| print(second_response.choices[0].message) |
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 |
|---|---|---|
|
|
@@ -15,6 +15,8 @@ | |
|
|
||
| logger = init_logger(__name__) | ||
|
|
||
| import re | ||
| from typing import Tuple | ||
|
|
||
| @ReasoningParserManager.register_module("minimax_m2") | ||
| class MiniMaxM2ReasoningParser(BaseThinkingReasoningParser): | ||
|
|
@@ -66,4 +68,13 @@ | |
| def extract_reasoning_content( | ||
| self, model_output: str, request: ChatCompletionRequest | ResponsesRequest | ||
| ) -> tuple[str | None, str | None]: | ||
| return None, "<think>" + model_output | ||
|
|
||
| match = re.search(r"</think>\s*", model_output, re.DOTALL) | ||
| if not match: | ||
| return model_output, "" | ||
|
|
||
| end_idx = match.end() | ||
| before = model_output[:end_idx] | ||
| after = model_output[end_idx:] | ||
| return before.strip(), after.strip() | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this makes the result |
||
| # return None, "<think>" + model_output | ||
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.
with this
vllm serve MiniMaxAI/MiniMax-M2 --tensor-parallel-size 4 --tool-call-parser minimax_m2 --reasoning-parser minimax_m2_append_think --enable-auto-tool-choice --port 8000