pvanand commited on
Commit
f5f5951
·
verified ·
1 Parent(s): bfcfdb0

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +39 -55
main.py CHANGED
@@ -13,7 +13,7 @@ import time
13
  from datetime import datetime, timedelta
14
  import asyncio
15
  import requests
16
- from prompts import CODING_ASSISTANT_PROMPT, NEWS_ASSISTANT_PROMPT, generate_news_prompt, SEARCH_ASSISTANT_PROMPT, generate_search_prompt
17
  from fastapi_cache import FastAPICache
18
  from fastapi_cache.backends.inmemory import InMemoryBackend
19
  from fastapi_cache.decorator import cache
@@ -402,60 +402,6 @@ class FollowupQueryModel(BaseModel):
402
  }
403
  }
404
 
405
- FOLLOWUP_AGENT_PROMPT = """
406
- You are a helpful, intelligent assistant who can create interactive buttons and markdown responses.
407
-
408
- If the user request needs further clarification, provide clarifying questions using <interact> to assist the user.
409
- Else respond with a helpful answer using <response>.
410
- The options in <interact> tags will be rendered as buttons so that user can interact with it. Hence make sure to use it for engaging with the user with Next Steps, followup questions, quizzes etc. whichever appropriate
411
-
412
- Your output format: # Use the following <response>,<interact> tags in any order as many times required.
413
- <response>response to user request formatted using markdown</response>
414
- <interact>
415
- questions:
416
- - text: [First interaction question]
417
- options:
418
- - [Option 1]
419
- - [Option 2]
420
- - [Option 3]
421
- - [Option 4 (if needed)]
422
- - text: [Second interaction question]
423
- options:
424
- - [Option 1]
425
- - [Option 2]
426
- - [Option 3]
427
- # Add more questions as needed
428
- # make sure this section is in valid YAML format
429
- </interact>
430
- """
431
-
432
- FOLLOWUP_DIGIYATRA_PROMPT = """
433
- You are a helpful, assistant tasked to assist digiyatra users, who can create interactive buttons and markdown responses. Provide youtube links to the user if relevant links are given in the context.
434
-
435
- If the user request needs further clarification, provide clarifying questions using <interact> to assist the user.
436
- Else respond with a helpful answer using <response>.
437
- The options in <interact> tags will be rendered as buttons so that user can interact with it. Hence make sure to use it for engaging with the user with Next Steps, followup questions, quizzes etc. whichever appropriate
438
-
439
- Your output format: # Use the following <response>,<interact> tags in any order as many times required.
440
- <response>response to user request formatted using markdown</response>
441
- <interact>
442
- questions:
443
- - text: [First interaction question]
444
- options:
445
- - [Option 1]
446
- - [Option 2]
447
- - [Option 3]
448
- - [Option 4 (if needed)]
449
- - text: [Second interaction question]
450
- options:
451
- - [Option 1]
452
- - [Option 2]
453
- - [Option 3]
454
- # Add more questions as needed
455
- # make sure this section is in valid YAML format
456
- </interact>
457
- """
458
-
459
  import re
460
 
461
  def parse_followup_response(input_text):
@@ -514,6 +460,44 @@ def parse_followup_response(input_text):
514
  return combined_response.strip(), parsed_interacts
515
 
516
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
517
  @app.post("/followup-agent")
518
  async def followup_agent(query: FollowupQueryModel, background_tasks: BackgroundTasks, api_key: str = Depends(verify_api_key)):
519
  """
 
13
  from datetime import datetime, timedelta
14
  import asyncio
15
  import requests
16
+ from prompts import *
17
  from fastapi_cache import FastAPICache
18
  from fastapi_cache.backends.inmemory import InMemoryBackend
19
  from fastapi_cache.decorator import cache
 
402
  }
403
  }
404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
405
  import re
406
 
407
  def parse_followup_response(input_text):
 
460
  return combined_response.strip(), parsed_interacts
461
 
462
 
463
+ import re
464
+
465
+ def parse_followup_and_tools(input_text):
466
+ # Remove extra brackets, excess quotes, and excessive whitespace
467
+ cleaned_text = re.sub(r'\[|\]|"+|\s+', ' ', input_text).strip()
468
+
469
+ # Extract response content
470
+ response_pattern = re.compile(r'<response>(.*?)</response>', re.DOTALL)
471
+ response_parts = response_pattern.findall(cleaned_text)
472
+ combined_response = ' '.join(response_parts).strip()
473
+
474
+ parsed_interacts = []
475
+ parsed_tools = []
476
+
477
+ # Parse interacts and tools
478
+ blocks = re.finditer(r'<(interact|tool)>(.*?)</\1>', cleaned_text, re.DOTALL)
479
+ for block in blocks:
480
+ block_type, content = block.groups()
481
+ content = content.strip()
482
+
483
+ if block_type == 'interact':
484
+ question_blocks = re.split(r'\s*-\s*text:', content)[1:]
485
+ for qblock in question_blocks:
486
+ parts = re.split(r'\s*options:\s*', qblock, maxsplit=1)
487
+ if len(parts) == 2:
488
+ question = parts[0].strip()
489
+ options = [opt.strip() for opt in re.split(r'\s*-\s*', parts[1]) if opt.strip()]
490
+ parsed_interacts.append({'question': question, 'options': options})
491
+
492
+ elif block_type == 'tool':
493
+ tool_match = re.search(r'text:\s*(.*?)\s*options:\s*-\s*(.*)', content, re.DOTALL)
494
+ if tool_match:
495
+ tool_name = tool_match.group(1).strip()
496
+ option = tool_match.group(2).strip()
497
+ parsed_tools.append({'name': tool_name, 'input': option})
498
+
499
+ return combined_response, parsed_interacts, parsed_tools
500
+
501
  @app.post("/followup-agent")
502
  async def followup_agent(query: FollowupQueryModel, background_tasks: BackgroundTasks, api_key: str = Depends(verify_api_key)):
503
  """