Adi362 commited on
Commit
2a10f9f
·
verified ·
1 Parent(s): 9f256ee

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +38 -20
main.py CHANGED
@@ -47,14 +47,20 @@ try:
47
  except Exception as e:
48
  print(f"Failed to initialize local fallback LLM: {e}")
49
 
 
 
50
  async def evaluate_needs_search(query: str) -> bool:
51
  """Uses a fast, small model to determine if the query requires real-time data."""
52
  if not GROQ_API_KEY:
53
  return False
54
 
55
- system_prompt = """You are a highly efficient classification router.
 
 
 
56
  Determine if the user's query requires up-to-date, real-time information or current events data from the internet to answer accurately.
57
- Respond ONLY with "YES" if it requires search, or "NO" if it can be answered with general knowledge up to 2023.
 
58
  DO NOT provide any other text."""
59
 
60
  try:
@@ -81,22 +87,32 @@ DO NOT provide any other text."""
81
  print(f"Routing evaluation error: {e}")
82
  return False # Default to no search on error to save latency
83
 
84
- def perform_search(query: str, max_results: int = 3) -> str:
85
- """Performs a web search using DuckDuckGo."""
86
- try:
87
- with DDGS() as ddgs:
88
- results = list(ddgs.text(query, max_results=max_results))
89
-
90
- if not results:
91
- return "No recent information found."
92
-
93
- context = "Here is some current information I found on the web regarding the user's query:\n\n"
94
- for i, r in enumerate(results):
95
- context += f"Source {i+1} [{r.get('title', 'No Title')}]: {r.get('body', '')}\n"
96
- return context
97
- except Exception as e:
98
- print(f"Search error: {e}")
99
- return "Search failed or was blocked."
 
 
 
 
 
 
 
 
 
 
100
 
101
  @app.post("/chat/completions")
102
  async def situation_aware_chat(request: ChatRequest):
@@ -114,9 +130,11 @@ async def situation_aware_chat(request: ChatRequest):
114
  print("Search complete.")
115
 
116
  # 2. Prepare the final prompt
117
- system_base = "You are 'Situation Aware AI', an advanced assistant integrated into the Edyx platform."
 
 
118
  if context_injection:
119
- system_base += "\n\nThe user has asked a question that requires current knowledge. You have been provided with real-time web search results below. Incorporate this information seamlessly into your answer to provide the most up-to-date and accurate response. Do not mention that you 'searched the web' unless asked, just present the facts.\n\n" + context_injection
120
 
121
  # Construct message array preserving history
122
  final_messages = [{"role": "system", "content": system_base}]
 
47
  except Exception as e:
48
  print(f"Failed to initialize local fallback LLM: {e}")
49
 
50
+ import datetime
51
+
52
  async def evaluate_needs_search(query: str) -> bool:
53
  """Uses a fast, small model to determine if the query requires real-time data."""
54
  if not GROQ_API_KEY:
55
  return False
56
 
57
+ current_date = datetime.datetime.now().strftime("%B %d, %Y")
58
+
59
+ system_prompt = f"""You are a highly efficient classification router.
60
+ Today's date is {current_date}.
61
  Determine if the user's query requires up-to-date, real-time information or current events data from the internet to answer accurately.
62
+ If the user asks about an event, person, software, or fact that changes frequently or occurred near or after {current_date}, it requires search.
63
+ Respond ONLY with "YES" if it requires search, or "NO" if it can be answered with general, static knowledge.
64
  DO NOT provide any other text."""
65
 
66
  try:
 
87
  print(f"Routing evaluation error: {e}")
88
  return False # Default to no search on error to save latency
89
 
90
+ import time
91
+
92
+ def perform_search(query: str, max_results: int = 4) -> str:
93
+ """Performs a web search using DuckDuckGo with basic retry logic."""
94
+ for attempt in range(2):
95
+ try:
96
+ with DDGS() as ddgs:
97
+ results = list(ddgs.text(query, max_results=max_results))
98
+
99
+ if not results:
100
+ if attempt == 0:
101
+ time.sleep(1) # short backoff
102
+ continue
103
+ return "Search returned no direct results. Please rely on your internal knowledge."
104
+
105
+ context = "Here is real-time web search data regarding the user's query:\n\n"
106
+ for i, r in enumerate(results):
107
+ context += f"Source {i+1} [{r.get('title', 'Unknown Title')}]: {r.get('body', '')}\nURL: {r.get('href', 'N/A')}\n\n"
108
+ return context
109
+ except Exception as e:
110
+ print(f"Search attempt {attempt + 1} failed: {e}")
111
+ if attempt == 0:
112
+ time.sleep(1)
113
+ else:
114
+ return f"Search engine was temporarily unavailable ({e}). Please rely on your internal knowledge instead."
115
+ return ""
116
 
117
  @app.post("/chat/completions")
118
  async def situation_aware_chat(request: ChatRequest):
 
130
  print("Search complete.")
131
 
132
  # 2. Prepare the final prompt
133
+ current_time_str = datetime.datetime.now().strftime("%A, %B %d, %Y %I:%M %p")
134
+ system_base = f"You are 'Situation Aware AI', an advanced assistant integrated into the Edyx platform. Today's current date and time is {current_time_str}. Use this date as your absolute frame of reference for 'now'."
135
+
136
  if context_injection:
137
+ system_base += f"\n\nThe user has asked a question that requires current knowledge. You have been provided with real-time web search results below. Synthesize a comprehensive, highly accurate, state-of-the-art response using ONLY the provided facts. Cite your sources naturally in your response.\n\n--- WEB SEARCH RESULTS ---\n{context_injection}\n--- END RESULTS ---"
138
 
139
  # Construct message array preserving history
140
  final_messages = [{"role": "system", "content": system_base}]