Spaces:
Running
Running
add interaction options
Browse files
main.py
CHANGED
@@ -403,46 +403,46 @@ class FollowupQueryModel(BaseModel):
|
|
403 |
}
|
404 |
|
405 |
FOLLOWUP_AGENT_PROMPT = """
|
406 |
-
You are a helpful,
|
407 |
-
|
408 |
-
If the user request needs further clarification, asnwer to your best of ability in a <response>, further analyze the user request and generate clarifying questions with options <clarification>. Else respond with a helpful answer.
|
409 |
-
The options in <clarification> tags will be rendered as buttons so that user can interact with it. Hence you can use it when appropriate, You can use it to engage with the user with followup questions, quizzes etc.
|
410 |
|
411 |
<response>response to user request in markdown</response>
|
412 |
-
<
|
413 |
questions:
|
414 |
-
- text: [First
|
415 |
options:
|
416 |
- [Option 1]
|
417 |
- [Option 2]
|
418 |
- [Option 3]
|
419 |
- [Option 4 (if needed)]
|
420 |
-
- text: [Second
|
421 |
options:
|
422 |
- [Option 1]
|
423 |
- [Option 2]
|
424 |
- [Option 3]
|
425 |
# Add more questions as needed
|
426 |
# make sure this section is in valid YAML format
|
427 |
-
</
|
|
|
|
|
|
|
428 |
|
429 |
"""
|
430 |
|
431 |
import re
|
432 |
|
433 |
def parse_followup_response(input_text):
|
434 |
-
# Define patterns for response and
|
435 |
response_pattern = re.compile(r'<response>(.*?)<\/response>', re.DOTALL)
|
436 |
-
|
437 |
|
438 |
-
# Find all matches for response and
|
439 |
response_matches = response_pattern.finditer(input_text)
|
440 |
-
|
441 |
|
442 |
# Initialize variables to keep track of the position
|
443 |
last_end = 0
|
444 |
combined_response = ""
|
445 |
-
|
446 |
|
447 |
# Combine responses and capture everything in between
|
448 |
for response_match in response_matches:
|
@@ -453,15 +453,15 @@ def parse_followup_response(input_text):
|
|
453 |
# Update the last end position
|
454 |
last_end = response_match.end()
|
455 |
|
456 |
-
# Check for
|
457 |
-
for
|
458 |
-
# Capture text before the current
|
459 |
-
combined_response += input_text[last_end:
|
460 |
-
# Process the
|
461 |
-
|
462 |
-
if
|
463 |
# Split by "text:" to separate each question block
|
464 |
-
question_blocks =
|
465 |
|
466 |
# Loop through each block and extract the question and its options
|
467 |
for block in question_blocks[1:]:
|
@@ -476,14 +476,14 @@ def parse_followup_response(input_text):
|
|
476 |
options = [option.strip() for option in options_match.group(1).split('-') if option.strip()]
|
477 |
|
478 |
# Add the parsed question and options to the list
|
479 |
-
|
480 |
# Update the last end position
|
481 |
-
last_end =
|
482 |
|
483 |
# Capture any remaining text after the last tag
|
484 |
combined_response += input_text[last_end:].strip()
|
485 |
|
486 |
-
return combined_response.strip(),
|
487 |
|
488 |
|
489 |
@app.post("/followup-agent")
|
@@ -512,11 +512,11 @@ async def followup_agent(query: FollowupQueryModel, background_tasks: Background
|
|
512 |
yield content
|
513 |
|
514 |
logger.info(f"LLM RAW response for query: {query.query}: {full_response}")
|
515 |
-
response_content,
|
516 |
|
517 |
result = {
|
518 |
"response": response_content,
|
519 |
-
"clarification":
|
520 |
}
|
521 |
|
522 |
yield "\n\n" + json.dumps(result)
|
|
|
403 |
}
|
404 |
|
405 |
FOLLOWUP_AGENT_PROMPT = """
|
406 |
+
You are a helpful, interactive assistant with the following skills, use them, as necessory. Use <response> tag to provide responses well formatted using markdown format.
|
|
|
|
|
|
|
407 |
|
408 |
<response>response to user request in markdown</response>
|
409 |
+
<interact>
|
410 |
questions:
|
411 |
+
- text: [First interaction question]
|
412 |
options:
|
413 |
- [Option 1]
|
414 |
- [Option 2]
|
415 |
- [Option 3]
|
416 |
- [Option 4 (if needed)]
|
417 |
+
- text: [Second interaction question]
|
418 |
options:
|
419 |
- [Option 1]
|
420 |
- [Option 2]
|
421 |
- [Option 3]
|
422 |
# Add more questions as needed
|
423 |
# make sure this section is in valid YAML format
|
424 |
+
</interact>
|
425 |
+
|
426 |
+
If the user request needs further clarification, asnwer to your best of ability in a <response>, further analyze the user request and generate clarifying questions using <interact>. Else respond with a helpful answer.
|
427 |
+
The options in <interact> tags will be rendered as buttons so that user can interact with it. Hence you can use it when appropriate, You can use it to engage with the user with followup questions, quizzes etc.
|
428 |
|
429 |
"""
|
430 |
|
431 |
import re
|
432 |
|
433 |
def parse_followup_response(input_text):
|
434 |
+
# Define patterns for response and interact
|
435 |
response_pattern = re.compile(r'<response>(.*?)<\/response>', re.DOTALL)
|
436 |
+
interact_pattern = re.compile(r'<interact>(.*?)<\/interact>', re.DOTALL)
|
437 |
|
438 |
+
# Find all matches for response and interact
|
439 |
response_matches = response_pattern.finditer(input_text)
|
440 |
+
interact_matches = interact_pattern.finditer(input_text)
|
441 |
|
442 |
# Initialize variables to keep track of the position
|
443 |
last_end = 0
|
444 |
combined_response = ""
|
445 |
+
parsed_interacts = []
|
446 |
|
447 |
# Combine responses and capture everything in between
|
448 |
for response_match in response_matches:
|
|
|
453 |
# Update the last end position
|
454 |
last_end = response_match.end()
|
455 |
|
456 |
+
# Check for interacts and parse them
|
457 |
+
for interact_match in interact_matches:
|
458 |
+
# Capture text before the current interact tag
|
459 |
+
combined_response += input_text[last_end:interact_match.start()].strip() + "\n"
|
460 |
+
# Process the interact block
|
461 |
+
interact_text = interact_match.group(1).strip()
|
462 |
+
if interact_text:
|
463 |
# Split by "text:" to separate each question block
|
464 |
+
question_blocks = interact_text.split("- text:")
|
465 |
|
466 |
# Loop through each block and extract the question and its options
|
467 |
for block in question_blocks[1:]:
|
|
|
476 |
options = [option.strip() for option in options_match.group(1).split('-') if option.strip()]
|
477 |
|
478 |
# Add the parsed question and options to the list
|
479 |
+
parsed_interacts.append({'question': question, 'options': options})
|
480 |
# Update the last end position
|
481 |
+
last_end = interact_match.end()
|
482 |
|
483 |
# Capture any remaining text after the last tag
|
484 |
combined_response += input_text[last_end:].strip()
|
485 |
|
486 |
+
return combined_response.strip(), parsed_interacts
|
487 |
|
488 |
|
489 |
@app.post("/followup-agent")
|
|
|
512 |
yield content
|
513 |
|
514 |
logger.info(f"LLM RAW response for query: {query.query}: {full_response}")
|
515 |
+
response_content, interact = parse_followup_response(full_response)
|
516 |
|
517 |
result = {
|
518 |
"response": response_content,
|
519 |
+
"clarification": interact
|
520 |
}
|
521 |
|
522 |
yield "\n\n" + json.dumps(result)
|