main
py 49 lines 2.15 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 from __future__ import annotations
5
6 from typing import TYPE_CHECKING
7
8 from vllm.parser.engine.registered_adapters import MistralParserToolAdapter
9
10 if TYPE_CHECKING:
11 from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest
12 from vllm.entrypoints.openai.responses.protocol import ResponsesRequest
13
14
15 class MistralToolParser(MistralParserToolAdapter): # type: ignore[valid-type, misc]
16 # Marked so is_mistral_tool_parser() in vllm.utils.mistral recognises
17 # this adapter and lets adjust_request() fire for tool_choice="none"
18 # on grammar-capable Mistral tokenizers.
19 IS_MISTRAL_TOOL_PARSER = True
20
21 # Mistral emits tool calls in the [TOOL_CALLS]name[ARGS]{...} format, not the
22 # plain JSON array / bare-args that the serving layer's generic required and
23 # named handlers expect. Route required/named through this parser's own
24 # extraction (treated like "auto") so the [TOOL_CALLS] format is parsed
25 # correctly instead of crashing or dumping the raw envelope into arguments.
26 supports_required_and_named = False
27
28 @property
29 def bot_token(self) -> str:
30 """Return the Mistral tool-call marker exposed by the legacy parser."""
31 return self._parser_engine.bot_token
32
33 @property
34 def bot_token_id(self) -> int | None:
35 """Return the token id for :attr:`bot_token`."""
36 return self._parser_engine.bot_token_id
37
38 def adjust_request(
39 self,
40 request: ChatCompletionRequest | ResponsesRequest,
41 ) -> ChatCompletionRequest | ResponsesRequest:
42 # Skip the base ToolParser tool_choice -> structured_outputs
43 # conversion. Mistral enforces tool_choice via the grammar ``mode``
44 # (auto/none/required/named); forwarding a tool-derived json_schema
45 # would be unioned into the grammar by mistral-common, allowing raw
46 # JSON in place of a real [TOOL_CALLS] call. Genuine user-provided
47 # structured output on the request is still honored by the engine's
48 # adjust_request.
49 return self._parser_engine.adjust_request(request)