pvanand commited on
Commit
fc04628
1 Parent(s): 2cda7d3

Update regex

Browse files
Files changed (1) hide show
  1. main.py +54 -32
main.py CHANGED
@@ -408,38 +408,60 @@ You are a helpful assistant with the following skills, use them, as necessary. I
408
 
409
  import re
410
 
411
- def parse_followup_response(response):
412
- response_parts = response.split("<response>")
413
- if len(response_parts) > 1:
414
- response_content = response_parts[1].split("</response>")[0].strip()
415
- else:
416
- response_content = ""
417
-
418
- clarification_parts = response.split("<clarification>")
419
- if len(clarification_parts) > 1:
420
- clarification_text = clarification_parts[1].split("</clarification>")[0].strip()
421
- clarification = parse_clarification(clarification_text)
422
- else:
423
- clarification = None
424
-
425
- return response_content, clarification
426
-
427
-
428
- def parse_clarification(text):
429
- questions = []
430
- current_question = None
431
-
432
- for line in text.split('\n'):
433
- line = line.strip()
434
- if line.startswith('- text:'):
435
- if current_question:
436
- questions.append(current_question)
437
- current_question = {'text': line[7:].strip(), 'options': []}
438
- elif line.startswith('- ') and current_question:
439
- current_question['options'].append(line[2:].strip())
440
-
441
- if current_question:
442
- questions.append(current_question)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
443
 
444
 
445
  @app.post("/followup-agent")
 
408
 
409
  import re
410
 
411
+ def parse_followup_response(input_text):
412
+ # Define patterns for response and clarification
413
+ response_pattern = re.compile(r'<response>(.*?)<\/response>', re.DOTALL)
414
+ clarification_pattern = re.compile(r'<clarification>(.*?)<\/clarification>', re.DOTALL)
415
+
416
+ # Find all matches for response and clarification
417
+ response_matches = response_pattern.finditer(input_text)
418
+ clarification_matches = clarification_pattern.finditer(input_text)
419
+
420
+ # Initialize variables to keep track of the position
421
+ last_end = 0
422
+ combined_response = ""
423
+ parsed_clarifications = []
424
+
425
+ # Combine responses and capture everything in between
426
+ for response_match in response_matches:
427
+ # Capture text before the current response tag
428
+ combined_response += input_text[last_end:response_match.start()].strip() + "\n"
429
+ # Add the response content
430
+ combined_response += response_match.group(1).strip() + "\n"
431
+ # Update the last end position
432
+ last_end = response_match.end()
433
+
434
+ # Check for clarifications and parse them
435
+ for clarification_match in clarification_matches:
436
+ # Capture text before the current clarification tag
437
+ combined_response += input_text[last_end:clarification_match.start()].strip() + "\n"
438
+ # Process the clarification block
439
+ clarification_text = clarification_match.group(1).strip()
440
+ if clarification_text:
441
+ # Split by "text:" to separate each question block
442
+ question_blocks = clarification_text.split("- text:")
443
+
444
+ # Loop through each block and extract the question and its options
445
+ for block in question_blocks[1:]:
446
+ # Extract the question using regex (up to the "options:" part)
447
+ question_match = re.search(r'^(.*?)\s*options:', block, re.DOTALL)
448
+ if question_match:
449
+ question = question_match.group(1).strip()
450
+
451
+ # Extract the options using regex
452
+ options_match = re.search(r'options:\s*(.*?)$', block, re.DOTALL)
453
+ if options_match:
454
+ options = [option.strip() for option in options_match.group(1).split('-') if option.strip()]
455
+
456
+ # Add the parsed question and options to the list
457
+ parsed_clarifications.append({'question': question, 'options': options})
458
+ # Update the last end position
459
+ last_end = clarification_match.end()
460
+
461
+ # Capture any remaining text after the last tag
462
+ combined_response += input_text[last_end:].strip()
463
+
464
+ return combined_response.strip(), parsed_clarifications
465
 
466
 
467
  @app.post("/followup-agent")