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'(.*?)', 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?)(.*?)>(.*?)', 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