start implementation on new architecture
This commit is contained in:
75
sia/util.py
75
sia/util.py
@@ -1,52 +1,6 @@
|
||||
from typing import Iterator
|
||||
import re
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
from .inference_result import InferenceResult
|
||||
|
||||
def get_valid_root_elements(schema: str) -> set:
|
||||
"""
|
||||
Extract valid root element names from the XML schema.
|
||||
|
||||
Args:
|
||||
schema: XML schema string
|
||||
|
||||
Returns:
|
||||
set: Set of valid root element names
|
||||
"""
|
||||
try:
|
||||
schema = schema.strip()
|
||||
ET.register_namespace('xs', 'http://www.w3.org/2001/XMLSchema')
|
||||
root = ET.fromstring(schema)
|
||||
ns = {'xs': 'http://www.w3.org/2001/XMLSchema'}
|
||||
elements = root.findall(".//xs:element", ns)
|
||||
return {elem.get('name') for elem in elements if elem.get('name')}
|
||||
except ET.ParseError as e:
|
||||
print(f"Error parsing schema: {e}")
|
||||
return set()
|
||||
|
||||
def split_response(response: str, valid_elements: set) -> InferenceResult:
|
||||
"""
|
||||
Split the response into reasoning and actions based on valid XML elements.
|
||||
|
||||
Args:
|
||||
response: Raw response string from the model
|
||||
valid_elements: Set of valid root element names from the schema
|
||||
|
||||
Returns:
|
||||
InferenceResult: Tuple containing reasoning and actions
|
||||
"""
|
||||
elements_pattern = '|'.join(map(re.escape, valid_elements))
|
||||
pattern = f"<({elements_pattern})[^>]*>"
|
||||
matches = list(re.finditer(pattern, response))
|
||||
if not matches:
|
||||
return InferenceResult(response.strip(), "")
|
||||
last_match = matches[-1]
|
||||
split_point = last_match.start()
|
||||
reasoning = response[:split_point].strip()
|
||||
actions = response[split_point:].strip()
|
||||
return InferenceResult(reasoning, actions)
|
||||
|
||||
def stop_before_value(iterator: Iterator[str], stop_value: str) -> Iterator[str]:
|
||||
"""
|
||||
Creates an iterator that yields values from the input iterator
|
||||
@@ -66,4 +20,31 @@ def stop_before_value(iterator: Iterator[str], stop_value: str) -> Iterator[str]
|
||||
if split_point > 0:
|
||||
yield item[:split_point]
|
||||
break
|
||||
yield item
|
||||
yield item
|
||||
|
||||
def escape_text_for_xml(text: str) -> str:
|
||||
"""
|
||||
Convert any text value to a properly escaped XML string.
|
||||
|
||||
Args:
|
||||
text: Text to escape, can be string, number, or None
|
||||
|
||||
Returns:
|
||||
str: XML-escaped string representation
|
||||
"""
|
||||
if text is None:
|
||||
return ''
|
||||
|
||||
# Convert numbers to strings
|
||||
text_str = str(text)
|
||||
|
||||
# Standard XML escaping if string contains CDATA end sequence
|
||||
if ']]>' in text_str:
|
||||
return text_str.replace('&', '&') \
|
||||
.replace('<', '<') \
|
||||
.replace('>', '>') \
|
||||
.replace('"', '"') \
|
||||
.replace("'", ''')
|
||||
|
||||
# Otherwise wrap in CDATA
|
||||
return f'<![CDATA[{text_str}]]>'
|
||||
Reference in New Issue
Block a user