2025-02-02 14:58:18 -05:00
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2025-06-03 11:20:17 -07:00
|
|
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
2024-11-15 01:42:49 +01:00
|
|
|
|
import copy
|
2024-12-18 03:34:08 -03:00
|
|
|
|
import json
|
2024-11-15 01:42:49 +01:00
|
|
|
|
|
2023-12-13 12:28:13 -08:00
|
|
|
|
import pytest
|
|
|
|
|
|
|
2025-12-15 20:54:52 +08:00
|
|
|
|
from vllm.sampling_params import SamplingParams
|
|
|
|
|
|
from vllm.tokenizers.mistral import MistralTokenizer
|
|
|
|
|
|
from vllm.tool_parsers.mistral_tool_parser import (
|
2025-04-25 21:03:34 +08:00
|
|
|
|
MistralToolCall,
|
|
|
|
|
|
MistralToolParser,
|
|
|
|
|
|
)
|
2024-09-17 19:50:37 +02:00
|
|
|
|
|
2024-09-14 01:20:06 +08:00
|
|
|
|
from ...utils import check_logprobs_close
|
2024-05-09 00:44:35 +09:00
|
|
|
|
|
2023-12-13 12:28:13 -08:00
|
|
|
|
MODELS = [
|
2024-12-18 03:34:08 -03:00
|
|
|
|
"mistralai/Mistral-7B-Instruct-v0.3",
|
2023-12-13 12:28:13 -08:00
|
|
|
|
]
|
|
|
|
|
|
|
2024-11-05 16:02:23 -05:00
|
|
|
|
MISTRAL_FORMAT_MODELS = [
|
|
|
|
|
|
"mistralai/Mistral-7B-Instruct-v0.3",
|
2024-11-05 21:28:29 -07:00
|
|
|
|
# uses the v3-Tekken tokenizer
|
|
|
|
|
|
"mistralai/Ministral-8B-Instruct-2410",
|
2025-09-02 11:38:55 +02:00
|
|
|
|
# Mistral-Nemo is too big for CI, but passes locally
|
2024-11-05 21:28:29 -07:00
|
|
|
|
# "mistralai/Mistral-Nemo-Instruct-2407"
|
2024-11-05 16:02:23 -05:00
|
|
|
|
]
|
|
|
|
|
|
|
2024-09-17 19:50:37 +02:00
|
|
|
|
SAMPLING_PARAMS = SamplingParams(max_tokens=512, temperature=0.0, logprobs=5)
|
2024-09-20 23:33:03 +02:00
|
|
|
|
SYMBOLIC_LANG_PROMPTS = [
|
|
|
|
|
|
"勇敢な船乗りについての詩を書く", # japanese
|
|
|
|
|
|
"寫一首關於勇敢的水手的詩", # chinese
|
2024-11-05 21:28:29 -07:00
|
|
|
|
"ပုံပြင်လေးပြောပြပါ်:\n", # burmese
|
|
|
|
|
|
"Repeat the phrase 'URGENCY🌶️':\nURGENCY🌶️\nURGENCY🌶️\n", # see https://github.com/vllm-project/vllm/pull/9625
|
2024-09-20 23:33:03 +02:00
|
|
|
|
]
|
2024-09-17 19:50:37 +02:00
|
|
|
|
|
|
|
|
|
|
# for function calling
|
|
|
|
|
|
TOOLS = [
|
|
|
|
|
|
{
|
|
|
|
|
|
"type": "function",
|
|
|
|
|
|
"function": {
|
|
|
|
|
|
"name": "get_current_weather",
|
|
|
|
|
|
"description": "Get the current weather in a given location",
|
|
|
|
|
|
"parameters": {
|
|
|
|
|
|
"type": "object",
|
|
|
|
|
|
"properties": {
|
|
|
|
|
|
"city": {
|
|
|
|
|
|
"type": "string",
|
2025-10-06 06:12:40 +01:00
|
|
|
|
"description": "The city to find the weather for, e.g. "
|
|
|
|
|
|
"'San Francisco'",
|
2024-09-17 19:50:37 +02:00
|
|
|
|
},
|
|
|
|
|
|
"state": {
|
|
|
|
|
|
"type": "string",
|
2025-10-06 06:12:40 +01:00
|
|
|
|
"description": "the two-letter abbreviation for the state that "
|
|
|
|
|
|
"the city is in, e.g. 'CA' which would mean 'California'",
|
2025-10-05 15:06:22 +01:00
|
|
|
|
},
|
2024-09-17 19:50:37 +02:00
|
|
|
|
"unit": {
|
|
|
|
|
|
"type": "string",
|
|
|
|
|
|
"description": "The unit to fetch the temperature in",
|
|
|
|
|
|
"enum": ["celsius", "fahrenheit"],
|
2025-10-05 15:06:22 +01:00
|
|
|
|
},
|
2024-09-17 19:50:37 +02:00
|
|
|
|
},
|
|
|
|
|
|
"required": ["city", "state", "unit"],
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2024-11-15 01:42:49 +01:00
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
"type": "function",
|
|
|
|
|
|
"function": {
|
|
|
|
|
|
"name": "rewrite",
|
|
|
|
|
|
"description": "Rewrites text",
|
|
|
|
|
|
"parameters": {
|
|
|
|
|
|
"type": "object",
|
|
|
|
|
|
"required": [],
|
|
|
|
|
|
"properties": {
|
|
|
|
|
|
"text": {
|
|
|
|
|
|
"type": "string",
|
|
|
|
|
|
"description": "The input text to rewrite.",
|
2025-10-05 15:06:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2024-11-15 01:42:49 +01:00
|
|
|
|
},
|
2025-10-05 15:06:22 +01:00
|
|
|
|
]
|
|
|
|
|
|
MSGS = [
|
2024-11-15 01:42:49 +01:00
|
|
|
|
{"role": "system", "content": "You are an assistant."},
|
|
|
|
|
|
{
|
|
|
|
|
|
"role": "user",
|
2025-10-06 06:12:40 +01:00
|
|
|
|
"content": "Could you please rewrite the below article? \n\n My English needs "
|
|
|
|
|
|
"improvving, maybe I make errors.",
|
2024-11-15 01:42:49 +01:00
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
|
"content": "",
|
|
|
|
|
|
"tool_calls": [
|
|
|
|
|
|
{
|
|
|
|
|
|
"id": "bbc5b7ede",
|
|
|
|
|
|
"type": "function",
|
|
|
|
|
|
"function": {
|
|
|
|
|
|
"name": "rewrite",
|
2025-10-06 06:12:40 +01:00
|
|
|
|
"arguments": '{"text":"My English needs improvving, maybe '
|
|
|
|
|
|
'I make errors."}',
|
2025-10-05 15:06:22 +01:00
|
|
|
|
},
|
2024-11-15 01:42:49 +01:00
|
|
|
|
}
|
2025-10-05 15:06:22 +01:00
|
|
|
|
],
|
2024-11-15 01:42:49 +01:00
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
"role": "tool",
|
2025-10-06 06:12:40 +01:00
|
|
|
|
"content": '{"action":"rewrite","outcome":"My English needs improving, maybe '
|
|
|
|
|
|
'I make errors."}',
|
2024-11-15 01:42:49 +01:00
|
|
|
|
"tool_call_id": "bbc5b7ede",
|
|
|
|
|
|
"name": "rewrite",
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
"role": "assistant",
|
|
|
|
|
|
"content": "---\n\nMy English needs improving, maybe I make errors",
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
"role": "user",
|
|
|
|
|
|
"content": (
|
|
|
|
|
|
"Can you tell me what the temperate will be in Dallas, in fahrenheit?"
|
|
|
|
|
|
),
|
|
|
|
|
|
},
|
|
|
|
|
|
]
|
2024-09-17 19:50:37 +02:00
|
|
|
|
|
2024-12-18 03:34:08 -03:00
|
|
|
|
SAMPLE_JSON_SCHEMA = {
|
|
|
|
|
|
"type": "object",
|
|
|
|
|
|
"properties": {
|
|
|
|
|
|
"name": {"type": "string"},
|
|
|
|
|
|
"age": {"type": "integer"},
|
|
|
|
|
|
"skills": {
|
|
|
|
|
|
"type": "array",
|
|
|
|
|
|
"items": {"type": "string", "maxLength": 10},
|
|
|
|
|
|
"minItems": 3,
|
|
|
|
|
|
},
|
|
|
|
|
|
"work_history": {
|
|
|
|
|
|
"type": "array",
|
|
|
|
|
|
"items": {
|
|
|
|
|
|
"type": "object",
|
|
|
|
|
|
"properties": {
|
|
|
|
|
|
"company": {"type": "string"},
|
|
|
|
|
|
"duration": {"type": "number"},
|
|
|
|
|
|
"position": {"type": "string"},
|
|
|
|
|
|
},
|
|
|
|
|
|
"required": ["company", "position"],
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
"required": ["name", "age", "skills", "work_history"],
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-13 12:28:13 -08:00
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("model", MODELS)
|
|
|
|
|
|
@pytest.mark.parametrize("dtype", ["bfloat16"])
|
2024-05-09 00:44:35 +09:00
|
|
|
|
@pytest.mark.parametrize("max_tokens", [64])
|
|
|
|
|
|
@pytest.mark.parametrize("num_logprobs", [5])
|
2025-03-26 16:30:30 +08:00
|
|
|
|
def test_models(
|
|
|
|
|
|
hf_runner,
|
|
|
|
|
|
vllm_runner,
|
|
|
|
|
|
example_prompts,
|
|
|
|
|
|
model: str,
|
|
|
|
|
|
dtype: str,
|
|
|
|
|
|
max_tokens: int,
|
|
|
|
|
|
num_logprobs: int,
|
|
|
|
|
|
) -> None:
|
2024-05-09 00:44:35 +09:00
|
|
|
|
# TODO(sang): Sliding window should be tested separately.
|
2024-06-07 22:31:32 -07:00
|
|
|
|
with hf_runner(model, dtype=dtype) as hf_model:
|
|
|
|
|
|
hf_outputs = hf_model.generate_greedy_logprobs_limit(
|
|
|
|
|
|
example_prompts, max_tokens, num_logprobs
|
|
|
|
|
|
)
|
2023-12-13 12:28:13 -08:00
|
|
|
|
|
2024-08-27 14:40:02 +02:00
|
|
|
|
with vllm_runner(model, dtype=dtype, tokenizer_mode="mistral") as vllm_model:
|
2024-06-08 01:59:20 -07:00
|
|
|
|
vllm_outputs = vllm_model.generate_greedy_logprobs(
|
|
|
|
|
|
example_prompts, max_tokens, num_logprobs
|
|
|
|
|
|
)
|
2024-08-27 14:40:02 +02:00
|
|
|
|
|
2024-05-09 00:44:35 +09:00
|
|
|
|
check_logprobs_close(
|
|
|
|
|
|
outputs_0_lst=hf_outputs,
|
|
|
|
|
|
outputs_1_lst=vllm_outputs,
|
|
|
|
|
|
name_0="hf",
|
|
|
|
|
|
name_1="vllm",
|
|
|
|
|
|
)
|
2024-09-07 01:02:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
2024-11-05 16:02:23 -05:00
|
|
|
|
@pytest.mark.parametrize("model", MISTRAL_FORMAT_MODELS)
|
2024-09-07 01:02:05 +02:00
|
|
|
|
@pytest.mark.parametrize("dtype", ["bfloat16"])
|
|
|
|
|
|
@pytest.mark.parametrize("max_tokens", [64])
|
|
|
|
|
|
@pytest.mark.parametrize("num_logprobs", [5])
|
2025-03-26 16:30:30 +08:00
|
|
|
|
def test_mistral_format(
|
|
|
|
|
|
vllm_runner,
|
|
|
|
|
|
example_prompts,
|
|
|
|
|
|
model: str,
|
|
|
|
|
|
dtype: str,
|
|
|
|
|
|
max_tokens: int,
|
|
|
|
|
|
num_logprobs: int,
|
|
|
|
|
|
) -> None:
|
2024-09-07 01:02:05 +02:00
|
|
|
|
with vllm_runner(
|
|
|
|
|
|
model,
|
|
|
|
|
|
dtype=dtype,
|
|
|
|
|
|
tokenizer_mode="mistral",
|
|
|
|
|
|
load_format="mistral",
|
|
|
|
|
|
config_format="mistral",
|
|
|
|
|
|
) as mistral_format_model:
|
|
|
|
|
|
mistral_format_outputs = mistral_format_model.generate_greedy_logprobs(
|
|
|
|
|
|
example_prompts, max_tokens, num_logprobs
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-03-15 01:02:20 -04:00
|
|
|
|
with vllm_runner(
|
|
|
|
|
|
model,
|
|
|
|
|
|
dtype=dtype,
|
2025-11-21 22:58:59 +01:00
|
|
|
|
tokenizer_mode="hf",
|
2025-03-15 01:02:20 -04:00
|
|
|
|
load_format="safetensors",
|
|
|
|
|
|
config_format="hf",
|
|
|
|
|
|
) as hf_format_model:
|
|
|
|
|
|
hf_format_outputs = hf_format_model.generate_greedy_logprobs(
|
|
|
|
|
|
example_prompts, max_tokens, num_logprobs
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2024-09-07 01:02:05 +02:00
|
|
|
|
check_logprobs_close(
|
|
|
|
|
|
outputs_0_lst=hf_format_outputs,
|
|
|
|
|
|
outputs_1_lst=mistral_format_outputs,
|
|
|
|
|
|
name_0="hf",
|
|
|
|
|
|
name_1="mistral",
|
|
|
|
|
|
)
|
2024-09-17 19:50:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
2024-11-05 16:02:23 -05:00
|
|
|
|
@pytest.mark.parametrize("model", MISTRAL_FORMAT_MODELS)
|
2024-09-20 23:33:03 +02:00
|
|
|
|
@pytest.mark.parametrize("dtype", ["bfloat16"])
|
2025-03-26 16:30:30 +08:00
|
|
|
|
def test_mistral_symbolic_languages(vllm_runner, model: str, dtype: str) -> None:
|
2024-11-05 16:02:23 -05:00
|
|
|
|
with vllm_runner(
|
|
|
|
|
|
model,
|
|
|
|
|
|
dtype=dtype,
|
|
|
|
|
|
max_model_len=8192,
|
|
|
|
|
|
tokenizer_mode="mistral",
|
|
|
|
|
|
config_format="mistral",
|
|
|
|
|
|
load_format="mistral",
|
|
|
|
|
|
) as vllm_model:
|
|
|
|
|
|
for prompt in SYMBOLIC_LANG_PROMPTS:
|
|
|
|
|
|
msg = {"role": "user", "content": prompt}
|
2025-07-21 19:18:33 +08:00
|
|
|
|
outputs = vllm_model.llm.chat([msg], sampling_params=SAMPLING_PARAMS)
|
2024-11-05 16:02:23 -05:00
|
|
|
|
assert "<EFBFBD>" not in outputs[0].outputs[0].text.strip()
|
2024-09-20 23:33:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
2025-04-25 21:03:34 +08:00
|
|
|
|
@pytest.mark.parametrize("model", MISTRAL_FORMAT_MODELS)
|
2024-09-17 19:50:37 +02:00
|
|
|
|
@pytest.mark.parametrize("dtype", ["bfloat16"])
|
2025-03-26 16:30:30 +08:00
|
|
|
|
def test_mistral_function_calling(vllm_runner, model: str, dtype: str) -> None:
|
2024-09-17 19:50:37 +02:00
|
|
|
|
with vllm_runner(
|
|
|
|
|
|
model,
|
|
|
|
|
|
dtype=dtype,
|
|
|
|
|
|
tokenizer_mode="mistral",
|
|
|
|
|
|
config_format="mistral",
|
|
|
|
|
|
load_format="mistral",
|
|
|
|
|
|
) as vllm_model:
|
2024-11-15 01:42:49 +01:00
|
|
|
|
msgs = copy.deepcopy(MSGS)
|
2025-07-21 19:18:33 +08:00
|
|
|
|
outputs = vllm_model.llm.chat(
|
|
|
|
|
|
msgs, tools=TOOLS, sampling_params=SAMPLING_PARAMS
|
|
|
|
|
|
)
|
2024-09-17 19:50:37 +02:00
|
|
|
|
|
2025-07-21 19:18:33 +08:00
|
|
|
|
tokenizer = vllm_model.llm.get_tokenizer()
|
2024-11-15 01:42:49 +01:00
|
|
|
|
tool_parser = MistralToolParser(tokenizer)
|
|
|
|
|
|
|
|
|
|
|
|
model_output = outputs[0].outputs[0].text.strip()
|
|
|
|
|
|
assert model_output.startswith(tool_parser.bot_token), model_output
|
|
|
|
|
|
parsed_message = tool_parser.extract_tool_calls(model_output, None)
|
|
|
|
|
|
|
|
|
|
|
|
assert parsed_message.tools_called
|
2025-04-25 21:03:34 +08:00
|
|
|
|
|
|
|
|
|
|
assert MistralToolCall.is_valid_id(parsed_message.tool_calls[0].id)
|
2024-11-15 01:42:49 +01:00
|
|
|
|
assert parsed_message.tool_calls[0].function.name == "get_current_weather"
|
|
|
|
|
|
assert (
|
|
|
|
|
|
parsed_message.tool_calls[0].function.arguments
|
|
|
|
|
|
== '{"city": "Dallas", "state": "TX", "unit": "fahrenheit"}'
|
|
|
|
|
|
) # noqa
|
|
|
|
|
|
assert parsed_message.content is None
|
2024-12-18 03:34:08 -03:00
|
|
|
|
|
|
|
|
|
|
|
2025-06-26 10:01:17 +09:00
|
|
|
|
def test_mistral_function_call_nested_json():
|
2025-09-02 11:38:55 +02:00
|
|
|
|
"""Ensure that the function-name regex captures the entire outermost
|
2025-06-26 10:01:17 +09:00
|
|
|
|
JSON block, including nested braces."""
|
|
|
|
|
|
|
|
|
|
|
|
# Create a minimal stub tokenizer that provides the few attributes the
|
|
|
|
|
|
# parser accesses (`version` and `get_vocab`).
|
|
|
|
|
|
class _StubMistralTokenizer(MistralTokenizer):
|
|
|
|
|
|
version = 11 # Satisfy the version check
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
|
def get_vocab():
|
|
|
|
|
|
# Provide the special TOOL_CALLS token expected by the parser.
|
|
|
|
|
|
return {"[TOOL_CALLS]": 0}
|
|
|
|
|
|
|
|
|
|
|
|
tokenizer = _StubMistralTokenizer()
|
|
|
|
|
|
parser = MistralToolParser(tokenizer)
|
|
|
|
|
|
|
|
|
|
|
|
# Craft a model output featuring nested JSON inside the arguments.
|
|
|
|
|
|
args_dict = {
|
|
|
|
|
|
"city": "Dallas",
|
|
|
|
|
|
"state": "TX",
|
|
|
|
|
|
"unit": "fahrenheit",
|
|
|
|
|
|
"sub_dict": {"foo": "bar", "inner": {"x": 1, "y": 2}},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
model_output = f"{parser.bot_token}get_current_weather{json.dumps(args_dict)}"
|
|
|
|
|
|
|
|
|
|
|
|
parsed = parser.extract_tool_calls(model_output, None)
|
|
|
|
|
|
|
|
|
|
|
|
# Assertions: the tool call is detected and the full nested JSON is parsed
|
|
|
|
|
|
# without truncation.
|
|
|
|
|
|
assert parsed.tools_called
|
|
|
|
|
|
|
|
|
|
|
|
assert MistralToolCall.is_valid_id(parsed.tool_calls[0].id)
|
|
|
|
|
|
assert parsed.tool_calls[0].function.name == "get_current_weather"
|
|
|
|
|
|
assert json.loads(parsed.tool_calls[0].function.arguments) == args_dict
|
|
|
|
|
|
# No additional content outside the tool call should be returned.
|
|
|
|
|
|
assert parsed.content is None
|
2025-12-02 23:51:58 +01:00
|
|
|
|
|
|
|
|
|
|
# multiple calls
|
|
|
|
|
|
multiple_args_dict = [
|
|
|
|
|
|
{
|
|
|
|
|
|
"city": "Dallas",
|
|
|
|
|
|
"state": "TX",
|
|
|
|
|
|
"unit": "fahrenheit",
|
|
|
|
|
|
"sub_dict": {"foo": "bar", "inner": {"x": 1, "y": 2}},
|
|
|
|
|
|
},
|
|
|
|
|
|
{},
|
|
|
|
|
|
{"a": 0},
|
|
|
|
|
|
{"a": 1, "b": "c"},
|
|
|
|
|
|
]
|
|
|
|
|
|
names = ["get_current_weather", "get_current_weather_2", "random", "random_2"]
|
|
|
|
|
|
|
|
|
|
|
|
model_output = "".join(
|
|
|
|
|
|
[
|
|
|
|
|
|
f"{parser.bot_token}{name}{json.dumps(args)}"
|
|
|
|
|
|
for name, args in zip(names, multiple_args_dict)
|
|
|
|
|
|
]
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
parsed = parser.extract_tool_calls(model_output, None)
|
|
|
|
|
|
|
|
|
|
|
|
# Assertions: the tool call is detected and the full nested JSON is parsed
|
|
|
|
|
|
# without truncation.
|
|
|
|
|
|
assert parsed.tools_called
|
|
|
|
|
|
assert len(parsed.tool_calls) == len(multiple_args_dict)
|
|
|
|
|
|
|
|
|
|
|
|
for i, tool_call in enumerate(parsed.tool_calls):
|
|
|
|
|
|
assert MistralToolCall.is_valid_id(tool_call.id)
|
|
|
|
|
|
assert tool_call.function.name == names[i]
|
|
|
|
|
|
assert json.loads(tool_call.function.arguments) == multiple_args_dict[i]
|
|
|
|
|
|
# No additional content outside the tool call should be returned.
|
|
|
|
|
|
assert parsed.content is None
|