2026-02-07 09:17:16 +00:00
|
|
|
|
"""
|
|
|
|
|
|
DeepSeek V3 tool call parser.
|
|
|
|
|
|
|
|
|
|
|
|
Format uses special unicode tokens:
|
|
|
|
|
|
<|tool▁calls▁begin|>
|
|
|
|
|
|
<|tool▁call▁begin|>type<|tool▁sep|>function_name
|
|
|
|
|
|
```json
|
|
|
|
|
|
{"arg": "value"}
|
|
|
|
|
|
```
|
|
|
|
|
|
<|tool▁call▁end|>
|
|
|
|
|
|
<|tool▁calls▁end|>
|
|
|
|
|
|
|
2026-03-15 03:55:24 +01:00
|
|
|
|
Fixes Issue #989: Support for multiple simultaneous tool calls.
|
2026-02-07 09:17:16 +00:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
import uuid
|
2026-03-15 03:55:24 +01:00
|
|
|
|
import logging
|
|
|
|
|
|
from typing import List, Optional, Tuple
|
2026-02-07 09:17:16 +00:00
|
|
|
|
|
|
|
|
|
|
from openai.types.chat.chat_completion_message_tool_call import (
|
|
|
|
|
|
ChatCompletionMessageToolCall,
|
|
|
|
|
|
Function,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
from environments.tool_call_parsers import ParseResult, ToolCallParser, register_parser
|
|
|
|
|
|
|
2026-03-15 03:55:24 +01:00
|
|
|
|
logger = logging.getLogger(__name__)
|
2026-02-07 09:17:16 +00:00
|
|
|
|
|
|
|
|
|
|
@register_parser("deepseek_v3")
|
|
|
|
|
|
class DeepSeekV3ToolCallParser(ToolCallParser):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Parser for DeepSeek V3 tool calls.
|
|
|
|
|
|
|
|
|
|
|
|
Uses special unicode tokens with fullwidth angle brackets and block elements.
|
|
|
|
|
|
Extracts type, function name, and JSON arguments from the structured format.
|
2026-03-15 03:55:24 +01:00
|
|
|
|
Ensures all tool calls are captured when the model executes multiple actions.
|
2026-02-07 09:17:16 +00:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
START_TOKEN = "<|tool▁calls▁begin|>"
|
|
|
|
|
|
|
2026-03-15 03:55:24 +01:00
|
|
|
|
# Updated PATTERN: Using \s* instead of literal \n for increased robustness
|
|
|
|
|
|
# against variations in model formatting (Issue #989).
|
2026-02-07 09:17:16 +00:00
|
|
|
|
PATTERN = re.compile(
|
2026-03-15 03:55:24 +01:00
|
|
|
|
r"<|tool▁call▁begin|>(?P<type>.*?)<|tool▁sep|>(?P<function_name>.*?)\s*```json\s*(?P<function_arguments>.*?)\s*```\s*<|tool▁call▁end|>",
|
2026-03-05 20:32:38 +03:00
|
|
|
|
re.DOTALL,
|
2026-02-07 09:17:16 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def parse(self, text: str) -> ParseResult:
|
2026-03-15 03:55:24 +01:00
|
|
|
|
"""
|
|
|
|
|
|
Parses the input text and extracts all available tool calls.
|
|
|
|
|
|
"""
|
2026-02-07 09:17:16 +00:00
|
|
|
|
if self.START_TOKEN not in text:
|
|
|
|
|
|
return text, None
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
2026-03-15 03:55:24 +01:00
|
|
|
|
# Using finditer to capture ALL tool calls in the sequence
|
|
|
|
|
|
matches = list(self.PATTERN.finditer(text))
|
2026-02-07 09:17:16 +00:00
|
|
|
|
if not matches:
|
|
|
|
|
|
return text, None
|
|
|
|
|
|
|
|
|
|
|
|
tool_calls: List[ChatCompletionMessageToolCall] = []
|
2026-03-15 03:55:24 +01:00
|
|
|
|
|
2026-02-07 09:17:16 +00:00
|
|
|
|
for match in matches:
|
2026-03-15 03:55:24 +01:00
|
|
|
|
func_name = match.group("function_name").strip()
|
|
|
|
|
|
func_args = match.group("function_arguments").strip()
|
|
|
|
|
|
|
2026-02-07 09:17:16 +00:00
|
|
|
|
tool_calls.append(
|
|
|
|
|
|
ChatCompletionMessageToolCall(
|
|
|
|
|
|
id=f"call_{uuid.uuid4().hex[:8]}",
|
|
|
|
|
|
type="function",
|
|
|
|
|
|
function=Function(
|
2026-03-15 03:55:24 +01:00
|
|
|
|
name=func_name,
|
|
|
|
|
|
arguments=func_args,
|
2026-02-07 09:17:16 +00:00
|
|
|
|
),
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-03-15 03:55:24 +01:00
|
|
|
|
if tool_calls:
|
|
|
|
|
|
# Content is text before the first tool call block
|
|
|
|
|
|
content_index = text.find(self.START_TOKEN)
|
|
|
|
|
|
content = text[:content_index].strip()
|
|
|
|
|
|
return content if content else None, tool_calls
|
2026-02-07 09:17:16 +00:00
|
|
|
|
|
2026-03-15 03:55:24 +01:00
|
|
|
|
return text, None
|
2026-02-07 09:17:16 +00:00
|
|
|
|
|
2026-03-15 03:55:24 +01:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"Error parsing DeepSeek V3 tool calls: {e}")
|
2026-02-07 09:17:16 +00:00
|
|
|
|
return text, None
|