internlm2_tool_parser.py 8.93 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
import json
5
from collections.abc import Sequence
6
7
8
9

import partial_json_parser
from partial_json_parser.core.options import Allow

10
from vllm.entrypoints.chat_utils import make_tool_call_id
11
from vllm.entrypoints.openai.chat_completion.protocol import (
12
    ChatCompletionRequest,
13
14
)
from vllm.entrypoints.openai.engine.protocol import (
15
16
17
18
19
20
21
    DeltaFunctionCall,
    DeltaMessage,
    DeltaToolCall,
    ExtractedToolCallInformation,
    FunctionCall,
    ToolCall,
)
22
from vllm.logger import init_logger
23
from vllm.tokenizers import TokenizerLike
24
25
26
27
from vllm.tool_parsers.abstract_tool_parser import (
    ToolParser,
)
from vllm.tool_parsers.utils import extract_intermediate_diff
28
29
30
31
32

logger = init_logger(__name__)


class Internlm2ToolParser(ToolParser):
33
    def __init__(self, tokenizer: TokenizerLike):
34
35
36
        super().__init__(tokenizer)
        self.position = 0

37
    def adjust_request(self, request: ChatCompletionRequest) -> ChatCompletionRequest:
38
        request = super().adjust_request(request)
39
        if request.tools and request.tool_choice != "none":
40
            # do not skip special tokens because internlm use the special
41
            # tokens to indicate the start and end of the tool calls
42
43
44
45
            # information.
            request.skip_special_tokens = False
        return request

46
    def get_arguments(self, obj):
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
        if "parameters" in obj:
            return obj.get("parameters")
        elif "arguments" in obj:
            return obj.get("arguments")
        return None

    def extract_tool_calls_streaming(
        self,
        previous_text: str,
        current_text: str,
        delta_text: str,
        previous_token_ids: Sequence[int],
        current_token_ids: Sequence[int],
        delta_token_ids: Sequence[int],
        request: ChatCompletionRequest,
62
    ) -> DeltaMessage | None:
63
        if "<|action_start|>" not in current_text:
64
65
            self.position = len(current_text)
            return DeltaMessage(content=delta_text)
66
        # if the tool call is sent, return an empty delta message
67
        # to make sure the finish_reason will be sent correctly.
68
        if self.current_tool_id > 0:
69
            return DeltaMessage(content="")
70
71

        last_pos = self.position
72
        if "<|action_start|><|plugin|>" not in current_text[last_pos:]:
73
74
75
            return None

        new_delta = current_text[last_pos:]
76
        text, action = new_delta.split("<|action_start|><|plugin|>")
77
78
79
80
81
82

        if len(text) > 0:
            self.position = self.position + len(text)
            return DeltaMessage(content=text)

        action = action.strip()
83
        action = action.split("<|action_end|>".strip())[0]
84
85
86
87
88

        # bit mask flags for partial JSON parsing. If the name hasn't been
        # sent yet, don't allow sending
        # an incomplete string since OpenAI only ever (as far as I have
        # seen) allows sending the entire tool/ function name at once.
89
        flags = Allow.ALL if self.current_tool_name_sent else Allow.ALL & ~Allow.STR
90
91
92
93

        try:
            parsable_arr = action

co63oc's avatar
co63oc committed
94
            # tool calls are generated in an object in internlm2
95
96
            # it's not support parallel tool calls
            try:
97
                tool_call_arr: dict = partial_json_parser.loads(parsable_arr, flags)
98
            except partial_json_parser.core.exceptions.MalformedJSON:
99
                logger.debug("not enough tokens to parse into JSON yet")
100
101
102
103
104
105
106
107
                return None

            # if the current tool name hasn't been sent, send if available
            # - otherwise send nothing
            if not self.current_tool_name_sent:
                function_name = tool_call_arr.get("name")
                if function_name:
                    self.current_tool_id = self.current_tool_id + 1
108
109
110
111
112
113
114
115
116
117
118
119
                    delta = DeltaMessage(
                        tool_calls=[
                            DeltaToolCall(
                                index=self.current_tool_id,
                                type="function",
                                id=make_tool_call_id(),
                                function=DeltaFunctionCall(
                                    name=function_name
                                ).model_dump(exclude_none=True),
                            )
                        ]
                    )
120
121
122
123
124
125
126
                    self.current_tool_name_sent = True
                    self.streamed_args_for_tool.append("")
                else:
                    delta = None
            # now we know we're on the same tool call and we're streaming
            # arguments
            else:
127
                prev_arguments = self.get_arguments(
128
129
                    self.prev_tool_call_arr[self.current_tool_id]
                )
130
                cur_arguments = self.get_arguments(tool_call_arr)
131
132
133
134
135
136
137

                # not arguments generated
                if not cur_arguments and not prev_arguments:
                    delta = None
                # will never happen
                elif not cur_arguments and prev_arguments:
                    logger.error(
138
139
                        "INVARIANT - impossible to have arguments reset mid-arguments"
                    )
140
141
142
                    delta = None
                # first time to get parameters
                elif cur_arguments and not prev_arguments:
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
                    cur_arguments_json = json.dumps(cur_arguments, ensure_ascii=False)

                    arguments_delta = cur_arguments_json[
                        : cur_arguments_json.index(delta_text) + len(delta_text)
                    ]
                    delta = DeltaMessage(
                        tool_calls=[
                            DeltaToolCall(
                                index=self.current_tool_id,
                                function=DeltaFunctionCall(
                                    arguments=arguments_delta
                                ).model_dump(exclude_none=True),
                            )
                        ]
                    )
                    self.streamed_args_for_tool[self.current_tool_id] += arguments_delta
159
160
                # both prev and cur parameters, send the increase parameters
                elif cur_arguments and prev_arguments:
161
162
                    cur_args_json = json.dumps(cur_arguments, ensure_ascii=False)
                    prev_args_json = json.dumps(prev_arguments, ensure_ascii=False)
163
164

                    argument_diff = extract_intermediate_diff(
165
166
167
168
169
170
171
172
173
174
175
176
177
178
                        cur_args_json, prev_args_json
                    )

                    delta = DeltaMessage(
                        tool_calls=[
                            DeltaToolCall(
                                index=self.current_tool_id,
                                function=DeltaFunctionCall(
                                    arguments=argument_diff
                                ).model_dump(exclude_none=True),
                            )
                        ]
                    )
                    self.streamed_args_for_tool[self.current_tool_id] += argument_diff
179
180
181
182

            # check to see if the name is defined and has been sent. if so,
            # stream the name - otherwise keep waiting
            # finish by setting old and returning None as base case
183
            tool_call_arr["arguments"] = self.get_arguments(tool_call_arr)
184
185
            self.prev_tool_call_arr = [tool_call_arr]
            return delta
186
187
        except Exception:
            logger.exception("Error trying to handle streaming tool call.")
188
            logger.debug(
189
190
                "Skipping chunk as a result of tool streaming extraction error"
            )
191
192
193
194
195
196
197
198
199
            return None

    def extract_tool_calls(
        self,
        model_output: str,
        request: ChatCompletionRequest,
    ) -> ExtractedToolCallInformation:
        text = model_output
        tools = request.tools
200
201
202
203
        if "<|action_start|><|plugin|>" in text:
            text, action = text.split("<|action_start|><|plugin|>")
            action = action.split("<|action_end|>".strip())[0]
            action = action[action.find("{") :]
204
            action_dict = json.loads(action)
205
206
207
208
209
210
211
            name, parameters = (
                action_dict["name"],
                json.dumps(
                    action_dict.get("parameters", action_dict.get("arguments", {})),
                    ensure_ascii=False,
                ),
            )
212
213

            if not tools or name not in [t.function.name for t in tools]:
214
215
216
                ExtractedToolCallInformation(
                    tools_called=False, tool_calls=[], content=text
                )
217
218

            tool_calls = [
219
                ToolCall(function=FunctionCall(name=name, arguments=parameters))
220
221
222
223
            ]
            return ExtractedToolCallInformation(
                tools_called=True,
                tool_calls=tool_calls,
224
225
                content=text if len(text) > 0 else None,
            )
226

227
228
229
        return ExtractedToolCallInformation(
            tools_called=False, tool_calls=[], content=text
        )