MCPcopy
hub / github.com/ModelTC/LightLLM / detect_and_parse

Method detect_and_parse

lightllm/server/function_call_parser.py:307–323  ·  view source on GitHub ↗

One-time parsing: Detects and parses tool calls in the provided text. :param text: The complete text to parse. :param tools: List of available tools. :return: ParseResult indicating success or failure, consumed text, leftover text, and parsed calls.

(self, text: str, tools: List[Function])

Source from the content-addressed store, hash-verified

305 self.eot_token = "</tool_call>"
306
307 def detect_and_parse(self, text: str, tools: List[Function]) -> List[ToolCallItem]:
308 """
309 One-time parsing: Detects and parses tool calls in the provided text.
310
311 :param text: The complete text to parse.
312 :param tools: List of available tools.
313 :return: ParseResult indicating success or failure, consumed text, leftover text, and parsed calls.
314 """
315 if "<tool_call>" not in text:
316 return []
317 pattern = r"<tool_call>(.*?)</tool_call>"
318 match_result_list = re.findall(pattern, text, re.DOTALL)
319 calls = []
320 for match_result in match_result_list:
321 match_result = json.loads(match_result)
322 calls.extend(self.parse_base_json(match_result, tools))
323 return calls
324
325
326class MistralDetector(BaseFormatDetector):

Callers 1

parse_onceMethod · 0.45

Calls 2

parse_base_jsonMethod · 0.80
extendMethod · 0.45

Tested by

no test coverage detected