Guiyom commited on
Commit
ae878bc
·
verified ·
1 Parent(s): 9e3c505

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -9
app.py CHANGED
@@ -513,33 +513,135 @@ class RaindropSearchBot:
513
  ref_counter += 1
514
 
515
  return output
516
-
517
  def process_request(self, user_request: str) -> str:
518
- """Process user request with improved error handling"""
 
 
 
 
 
 
 
 
519
  try:
520
- # Generate search queries
521
- search_queries = self.generate_search_queries(user_request)
 
522
 
523
- # Get results with fallback
524
- google_results = self.search_with_fallback(search_queries)
525
 
526
  # Add delay before news API call
527
  self.random_delay()
528
 
529
- news_results = self.get_news_results(search_queries)
 
530
 
531
- # Process results
532
  processed_results = self.process_all_results([], google_results, news_results)
533
 
534
  # Generate response
535
  essay = self.generate_essay_response(processed_results, user_request)
536
 
 
537
  return self.format_results(processed_results, essay)
538
 
539
  except Exception as e:
540
  logger.error(f"Error processing request: {e}")
541
- return "An error occurred while processing your request. Please try again later."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
542
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
543
  def generate_search_query(self, user_request: str) -> str:
544
  """Convert user request to optimized search terms."""
545
  logger.info(f"Generating search query for: {user_request}")
 
513
  ref_counter += 1
514
 
515
  return output
516
+
517
  def process_request(self, user_request: str) -> str:
518
+ """
519
+ Process user request with improved error handling and query generation.
520
+
521
+ Args:
522
+ user_request (str): The user's original search request
523
+
524
+ Returns:
525
+ str: Formatted results and analysis
526
+ """
527
  try:
528
+ # Generate optimized search query
529
+ search_query = self.generate_search_queries(user_request)
530
+ logger.info(f"Processing request: {search_query}")
531
 
532
+ # Get search results with fallback
533
+ google_results = self.search_with_fallback(search_query)
534
 
535
  # Add delay before news API call
536
  self.random_delay()
537
 
538
+ # Get news results
539
+ news_results = self.get_news_results(search_query)
540
 
541
+ # Process all results
542
  processed_results = self.process_all_results([], google_results, news_results)
543
 
544
  # Generate response
545
  essay = self.generate_essay_response(processed_results, user_request)
546
 
547
+ # Format and return results
548
  return self.format_results(processed_results, essay)
549
 
550
  except Exception as e:
551
  logger.error(f"Error processing request: {e}")
552
+ return f"""
553
+ An error occurred while processing your request: {str(e)}
554
+
555
+ Please try again with a different search query or contact support if the problem persists.
556
+ """
557
+
558
+ def generate_search_queries(self, user_request: str) -> str:
559
+ """
560
+ Generate optimized search queries from user request.
561
+
562
+ Args:
563
+ user_request (str): The original user query
564
+
565
+ Returns:
566
+ str: Optimized search query
567
+ """
568
+ try:
569
+ # Clean and preprocess the user request
570
+ cleaned_request = self.preprocess_query(user_request)
571
+
572
+ # Generate search query using GPT
573
+ prompt = f"""
574
+ Convert this search request into an optimized search query using proper search operators.
575
+ Request: {cleaned_request}
576
+
577
+ Guidelines:
578
+ - Focus on key concepts and synonyms
579
+ - Use combination of keywords that would appear in titles or descriptions
580
+ - Return only the search terms, no explanation
581
+ - Include alternative phrasings
582
+ - Keep it concise (max 6-8 key terms/phrases)
583
+ - use the formatting authorised in raindrop search:
584
+ o use " for exact search (ex: "artificial intelligence")
585
+ o use - to exclude some terms (ex: -math) // Do not exclude terms that are potentially relevant
586
+ o use match:OR for alternatives (ex: apple match:OR banana )
587
+ o use match:AND for inclusion of both cases systematically (ex: apple match:AND banana )
588
+ o use parenthesis for combinations ( ex: sugar match:AND (banana match:OR apple) )
589
+
590
+ Example elaborate request: ("artificial intelligence" match:OR AI) -"machine learning"
591
+ Use your judgement, think step by steps.
592
+ Return only the search query terms.
593
+ """
594
+
595
+ response = self.client.chat.completions.create(
596
+ model="gpt-4-mini",
597
+ messages=[{"role": "user", "content": prompt}],
598
+ temperature=0.3,
599
+ max_tokens=100
600
+ )
601
+
602
+ optimized_query = response.choices[0].message.content.strip()
603
+ logger.info(f"Generated search query: {optimized_query}")
604
+
605
+ return optimized_query
606
+
607
+ except Exception as e:
608
+ logger.error(f"Error generating search queries: {e}")
609
+ # Fallback to using the original request if query generation fails
610
+ return user_request
611
 
612
+ def preprocess_query(self, query: str) -> str:
613
+ """
614
+ Preprocess the user query to remove unnecessary elements and standardize format.
615
+
616
+ Args:
617
+ query (str): Original query string
618
+
619
+ Returns:
620
+ str: Cleaned query string
621
+ """
622
+ try:
623
+ # Convert to lowercase
624
+ query = query.lower()
625
+
626
+ # Remove extra whitespace
627
+ query = ' '.join(query.split())
628
+
629
+ # Remove special characters except basic punctuation
630
+ query = re.sub(r'[^a-z0-9\s\'".,?!-]', '', query)
631
+
632
+ # Remove multiple punctuation marks
633
+ query = re.sub(r'([.,?!])\1+', r'\1', query)
634
+
635
+ # Ensure proper spacing around quotes
636
+ query = re.sub(r'(?<=[^\s])"', ' "', query)
637
+ query = re.sub(r'"(?=[^\s])', '" ', query)
638
+
639
+ return query
640
+
641
+ except Exception as e:
642
+ logger.error(f"Error preprocessing query: {e}")
643
+ return query
644
+
645
  def generate_search_query(self, user_request: str) -> str:
646
  """Convert user request to optimized search terms."""
647
  logger.info(f"Generating search query for: {user_request}")