File size: 9,016 Bytes
7cfdc4c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | """
XML Tool Call Parser Module
This module provides a reliable XML tool call parsing system that supports
the XML format with structured function_calls blocks.
"""
import re
import uuid
from typing import List, Dict, Any, Optional
from dataclasses import dataclass
import json
import logging
logger = logging.getLogger(__name__)
@dataclass
class XMLToolCall:
"""Represents a parsed XML tool call."""
function_name: str
parameters: Dict[str, Any]
raw_xml: str
# Regex patterns for extracting XML blocks
_FUNCTION_CALLS_PATTERN = re.compile(
r'<function_calls>(.*?)</function_calls>',
re.DOTALL | re.IGNORECASE
)
_INVOKE_PATTERN = re.compile(
r'<invoke\s+name=["\']([^"\']+)["\']>(.*?)</invoke>',
re.DOTALL | re.IGNORECASE
)
_PARAMETER_PATTERN = re.compile(
r'<parameter\s+name=["\']([^"\']+)["\']>(.*?)</parameter>',
re.DOTALL | re.IGNORECASE
)
def _parse_parameter_value(value: str) -> Any:
"""Parse a parameter value, attempting to convert to appropriate type."""
value = value.strip()
# Try to parse as JSON first
if value.startswith(('{', '[')):
try:
return json.loads(value)
except json.JSONDecodeError:
pass
# Try to parse as boolean
if value.lower() in ('true', 'false'):
return value.lower() == 'true'
# Try to parse as number
try:
if '.' in value:
return float(value)
else:
return int(value)
except ValueError:
pass
# Return as string
return value
def _parse_invoke_block(function_name: str, invoke_content: str, full_block: str) -> Optional[XMLToolCall]:
"""Parse a single invoke block into an XMLToolCall."""
parameters = {}
# Extract all parameters
param_matches = _PARAMETER_PATTERN.findall(invoke_content)
for param_name, param_value in param_matches:
param_value = param_value.strip()
parameters[param_name] = _parse_parameter_value(param_value)
# Extract the raw XML for this specific invoke
invoke_pattern = re.compile(
rf'<invoke\s+name=["\']{re.escape(function_name)}["\']>.*?</invoke>',
re.DOTALL | re.IGNORECASE
)
raw_xml_match = invoke_pattern.search(full_block)
raw_xml = raw_xml_match.group(0) if raw_xml_match else f"<invoke name=\"{function_name}\">...</invoke>"
return XMLToolCall(
function_name=function_name,
parameters=parameters,
raw_xml=raw_xml
)
def parse_xml_tool_calls_to_objects(content: str) -> List[XMLToolCall]:
"""
Parse XML tool calls from content, returning XMLToolCall objects.
Format: <function_calls><invoke name="function_name"><parameter name="param">value</parameter></invoke></function_calls>
Args:
content: The text content potentially containing XML tool calls
Returns:
List of parsed XMLToolCall objects
"""
tool_calls = []
# Find function_calls blocks
function_calls_matches = _FUNCTION_CALLS_PATTERN.findall(content)
for fc_content in function_calls_matches:
# Find all invoke blocks within this function_calls block
invoke_matches = _INVOKE_PATTERN.findall(fc_content)
for function_name, invoke_content in invoke_matches:
try:
tool_call = _parse_invoke_block(function_name, invoke_content, fc_content)
if tool_call:
tool_calls.append(tool_call)
except Exception as e:
logger.error(f"Error parsing invoke block for {function_name}: {e}")
return tool_calls
def strip_xml_tool_calls(content: str) -> str:
"""
Remove XML function call tags from content, leaving only natural text.
Args:
content: Text content that may contain XML tool calls
Returns:
Clean text with XML tool call tags removed
"""
if not content:
return ""
# Remove function_calls, invoke, and parameter tags
cleaned = re.sub(r'<function_calls[^>]*>[\s\S]*?</function_calls>', '', content, flags=re.IGNORECASE)
return cleaned.strip()
def extract_xml_chunks(content: str) -> List[str]:
"""
Extract complete <function_calls> XML chunks from content.
Args:
content: Text content that may contain XML tool calls
Returns:
List of complete XML chunks (including <function_calls> tags)
"""
chunks = []
pos = 0
try:
start_pattern = '<function_calls>'
end_pattern = '</function_calls>'
while pos < len(content):
# Find the next function_calls block
start_pos = content.find(start_pattern, pos)
if start_pos == -1:
break
# Find the matching end tag
end_pos = content.find(end_pattern, start_pos)
if end_pos == -1:
break
# Extract the complete block including tags
chunk_end = end_pos + len(end_pattern)
chunk = content[start_pos:chunk_end]
chunks.append(chunk)
# Move position past this chunk
pos = chunk_end
except Exception as e:
logger.error(f"Error extracting XML chunks: {e}")
logger.error(f"Content was: {content[:200]}...")
return chunks
def parse_xml_tool_calls_with_ids(
xml_chunk: str,
assistant_message_id: Optional[str] = None,
start_index: int = 0
) -> List[Dict[str, Any]]:
"""
Parse XML chunk into tool call format with generated IDs.
Args:
xml_chunk: XML content containing <function_calls><invoke> tags
assistant_message_id: ID of the assistant message (for tool_call_id generation)
start_index: Starting index for XML tool calls (for tool_call_id generation)
Returns:
List of tool_call dictionaries, each with 'function_name', 'arguments', 'id', 'source'
"""
results = []
try:
# Check if this is the new format (contains <function_calls>)
if '<function_calls>' in xml_chunk and '<invoke' in xml_chunk:
# Parse XML tool calls - returns ALL invoke tags within the chunk
parsed_calls = parse_xml_tool_calls_to_objects(xml_chunk)
if not parsed_calls:
logger.error(f"No tool calls found in XML chunk: {xml_chunk[:200]}...")
return results
# Process ALL tool calls found in the chunk
for idx, xml_tool_call in enumerate(parsed_calls):
# Generate tool_call_id in format: xml_tool_index{id}_AssistantMessageId
tool_index = start_index + idx
if assistant_message_id:
tool_call_id = f"xml_tool_index{tool_index}_{assistant_message_id}"
else:
# Fallback if no assistant_message_id yet
tool_call_id = f"xml_tool_index{tool_index}_{str(uuid.uuid4())}"
tool_call = {
"function_name": xml_tool_call.function_name,
"id": tool_call_id,
"arguments": xml_tool_call.parameters,
"source": "xml" # Mark as XML tool call for detection
}
logger.debug(f"Parsed tool call from chunk: {tool_call['function_name']} (id: {tool_call_id})")
results.append(tool_call)
logger.debug(f"Parsed {len(results)} tool call(s) from XML chunk")
return results
# If not the expected <function_calls><invoke> format, return empty list
logger.error(f"XML chunk does not contain expected <function_calls><invoke> format: {xml_chunk[:200]}...")
return results
except Exception as e:
logger.error(f"Error parsing XML chunk: {e}")
logger.error(f"XML chunk was: {xml_chunk[:200]}...")
return results
def parse_xml_tool_calls(content: str) -> List[Dict[str, Any]]:
"""
Parse XML-style function calls from message content.
Convenience function that returns dict format for compatibility with existing code.
Format: <function_calls><invoke name="tool_name"><parameter name="param">value</parameter></invoke></function_calls>
Args:
content: The text content potentially containing XML tool calls
Returns:
List of dicts with 'tool_name', 'parameters', and 'raw_xml' keys
"""
xml_tool_calls = parse_xml_tool_calls_to_objects(content)
# Convert XMLToolCall objects to dict format for compatibility
return [
{
'tool_name': tc.function_name.replace('_', '-'), # Keep hyphenated format
'parameters': tc.parameters,
'raw_xml': tc.raw_xml
}
for tc in xml_tool_calls
] |