"vscode:/vscode.git/clone" did not exist on "ff365eea94add487cf91316087bcfa8f2b39c2b8"
test_deepseekv31_tool_parser.py 1.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

import pytest

from vllm.entrypoints.openai.tool_parsers import DeepSeekV31ToolParser
from vllm.transformers_utils.tokenizer import get_tokenizer

MODEL = "deepseek-ai/DeepSeek-V3.1"


@pytest.fixture(scope="module")
def deepseekv31_tokenizer():
    return get_tokenizer(tokenizer_name=MODEL)


@pytest.fixture
def parser(deepseekv31_tokenizer):
    return DeepSeekV31ToolParser(deepseekv31_tokenizer)


def test_extract_tool_calls_with_tool(parser):
    model_output = (
24
25
26
27
28
        "normal text"
        + "<|tool▁calls▁begin|>"
        + '<|tool▁call▁begin|>foo<|tool▁sep|>{"x":1}<|tool▁call▁end|>'
        + "<|tool▁calls▁end|>"
    )
29
30
31
32
    result = parser.extract_tool_calls(model_output, None)
    assert result.tools_called
    assert len(result.tool_calls) == 1
    assert result.tool_calls[0].function.name == "foo"
33
    assert result.tool_calls[0].function.arguments == '{"x":1}'
34
35
36
37
38
    assert result.content == "normal text"


def test_extract_tool_calls_with_multiple_tools(parser):
    model_output = (
39
40
41
42
43
44
45
        "some prefix text"
        + "<|tool▁calls▁begin|>"
        + '<|tool▁call▁begin|>foo<|tool▁sep|>{"x":1}<|tool▁call▁end|>'
        + '<|tool▁call▁begin|>bar<|tool▁sep|>{"y":2}<|tool▁call▁end|>'
        + "<|tool▁calls▁end|>"
        + " some suffix text"
    )
46
47
48
49
50
51
52

    result = parser.extract_tool_calls(model_output, None)

    assert result.tools_called
    assert len(result.tool_calls) == 2

    assert result.tool_calls[0].function.name == "foo"
53
    assert result.tool_calls[0].function.arguments == '{"x":1}'
54
55

    assert result.tool_calls[1].function.name == "bar"
56
    assert result.tool_calls[1].function.arguments == '{"y":2}'
57
58
59

    # prefix is content
    assert result.content == "some prefix text"