xml_tool_message
langroid/agent/xml_tool_message.py
XMLToolMessage
¶
Bases: ToolMessage
Abstract class for tools formatted using XML instead of JSON.
When a subclass defines a field with the attribute verbatim=True
,
instructions are sent to the LLM to ensure the field's content is:
- preserved as is, including whitespace, indents, quotes, newlines, etc
with no escaping, and
- enclosed in a CDATA section in the XML output.
This is useful for LLMs sending code as part of a tool;
results can be far superior compared to sending code in JSON-formatted tools,
where code needs to confirm to JSON's strict rules and escaping requirements.
(see test_xml_tool_message.py for an example).
extract_field_values(formatted_string)
classmethod
¶
Extracts field values from an XML-formatted string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
formatted_string |
str
|
The XML-formatted string to parse. |
required |
Returns:
Type | Description |
---|---|
Optional[Dict[str, Any]]
|
Optional[Dict[str, Any]]: A dictionary containing the extracted field values, where keys are the XML element names and values are their corresponding contents. |
Optional[Dict[str, Any]]
|
Returns None if parsing fails or the root element is not a dictionary. |
Raises:
Type | Description |
---|---|
XMLSyntaxError
|
If the input string is not valid XML. |
Source code in langroid/agent/xml_tool_message.py
parse(formatted_string)
classmethod
¶
Parses the XML-formatted string and returns an instance of the class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
formatted_string |
str
|
The XML-formatted string to parse. |
required |
Returns:
Type | Description |
---|---|
Optional[XMLToolMessage]
|
Optional["XMLToolMessage"]: An instance of the class if parsing succeeds, None otherwise. |
Source code in langroid/agent/xml_tool_message.py
format_example()
¶
Format the current instance as an XML example.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A string representation of the current instance in XML format. |
Raises:
Type | Description |
---|---|
ValueError
|
If the result from etree.tostring is not a string. |
Source code in langroid/agent/xml_tool_message.py
find_candidates(text)
classmethod
¶
Find and extract all potential XML tool messages from the given text.
This method searches for XML-like structures in the input text that match
the expected format of the tool message. It looks for opening and closing
tags that correspond to the root element defined in the XMLToolMessage class,
which is by default
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
The input text to search for XML tool messages. |
required |
Returns:
Type | Description |
---|---|
List[str]
|
List[str]: A list of strings, each representing a potential XML tool message. These candidates include both the opening and closing tags, so that they are individually parseable. |
Note
This method ensures that all candidates are valid and parseable by inserting a closing tag if it's missing for the last candidate.