Spaces:
Sleeping
Sleeping
File size: 1,883 Bytes
1a6d961 |
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 |
import re
def parse_followup_and_tools(input_text):
# Remove extra brackets and excess quotes
cleaned_text = re.sub(r'\[|\]|"+', ' ', input_text)
# Extract response content
response_pattern = re.compile(r'<response>(.*?)</response>', re.DOTALL)
response_parts = response_pattern.findall(cleaned_text)
combined_response = ' '.join(response_parts)
# Normalize spaces in the combined response
combined_response = ' '.join(combined_response.split())
parsed_interacts = []
parsed_tools = []
# Parse interacts and tools
blocks = re.finditer(r'<(interact|tools?)(.*?)>(.*?)</\1>', cleaned_text, re.DOTALL)
for block in blocks:
block_type, _, content = block.groups()
content = content.strip()
if block_type == 'interact':
question_blocks = re.split(r'\s*-\s*text:', content)[1:]
for qblock in question_blocks:
parts = re.split(r'\s*options:\s*', qblock, maxsplit=1)
if len(parts) == 2:
question = ' '.join(parts[0].split()) # Normalize spaces
options = [' '.join(opt.split()) for opt in re.split(r'\s*-\s*', parts[1]) if opt.strip()]
parsed_interacts.append({'question': question, 'options': options})
elif block_type.startswith('tool'): # This will match both 'tool' and 'tools'
tool_match = re.search(r'text:\s*(.*?)\s*options:\s*-\s*(.*)', content, re.DOTALL)
if tool_match:
tool_name = ' '.join(tool_match.group(1).split()) # Normalize spaces
option = ' '.join(tool_match.group(2).split()) # Normalize spaces
parsed_tools.append({'name': tool_name, 'input': option})
return combined_response, parsed_interacts, parsed_tools |