Skip to content

Commit 0fb7f79

Browse files
committed
feat: use MistralToolCall when using mistral tool parser
1 parent 563649a commit 0fb7f79

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

vllm/entrypoints/openai/tool_parsers/mistral_tool_parser.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import json
22
import re
3+
from random import choices
4+
from string import ascii_letters, digits
35
from typing import Dict, List, Sequence, Union
46

57
import partial_json_parser
68
from partial_json_parser.core.options import Allow
9+
from pydantic import Field
710

811
from vllm.entrypoints.openai.protocol import (DeltaFunctionCall, DeltaMessage,
912
DeltaToolCall,
@@ -19,6 +22,19 @@
1922

2023
logger = init_logger(__name__)
2124

25+
ALPHANUMERIC = ascii_letters + digits
26+
27+
28+
class MistralToolCall(ToolCall):
29+
id: str = Field(
30+
default_factory=lambda: MistralToolCall.generate_random_id())
31+
32+
@staticmethod
33+
def generate_random_id():
34+
# Mistral Tool Call Ids must be alphanumeric with a maximum length of 9.
35+
# https://github.com/mistralai/mistral-common/blob/21ee9f6cee3441e9bb1e6ed2d10173f90bd9b94b/src/mistral_common/protocol/instruct/validator.py#L299
36+
return "".join(choices(ALPHANUMERIC, k=9))
37+
2238

2339
class MistralToolParser(ToolParser):
2440
"""
@@ -71,8 +87,8 @@ def extract_tool_calls(self,
7187
# load the JSON, and then use it to build the Function and
7288
# Tool Call
7389
function_call_arr = json.loads(raw_tool_call)
74-
tool_calls: List[ToolCall] = [
75-
ToolCall(
90+
tool_calls: List[MistralToolCall] = [
91+
MistralToolCall(
7692
type="function",
7793
function=FunctionCall(
7894
name=raw_function_call["name"],

0 commit comments

Comments
 (0)