Spaces:
Runtime error
Runtime error
Update regex
Browse files
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(
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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")
|