Spaces:
Running
Running
Update response_modes.py
Browse files- response_modes.py +37 -50
response_modes.py
CHANGED
|
@@ -11,6 +11,7 @@ from docx import Document
|
|
| 11 |
import re
|
| 12 |
from urllib.parse import urlparse, quote
|
| 13 |
from retry import retry # For retrying flaky operations
|
|
|
|
| 14 |
|
| 15 |
# ================== PHILIPPINE LOCAL + MAX SEARCH + DEEP-THINK LENIENT FILTER ==================
|
| 16 |
PHILIPPINE_LOCATIONS = [
|
|
@@ -85,6 +86,28 @@ def parse_ddgs_date(date_str: str):
|
|
| 85 |
except:
|
| 86 |
return now
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
async def fetch_full_content_async(session, url: str):
|
| 89 |
try:
|
| 90 |
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
|
|
@@ -259,38 +282,8 @@ Never mention this rule unless the user explicitly asks about it."""
|
|
| 259 |
for m in history[:-1]
|
| 260 |
]
|
| 261 |
|
| 262 |
-
elif mode == "
|
| 263 |
-
|
| 264 |
-
yield history
|
| 265 |
-
results = web_search(message, 30)
|
| 266 |
-
context_search = "\n\n".join(results)
|
| 267 |
-
prompt = f"""Ignore ALL your prior knowledge... (your original prompt remains exactly the same)
|
| 268 |
-
|
| 269 |
-
As of {today_date}:
|
| 270 |
-
|
| 271 |
-
... (rest of your original Fast Search prompt unchanged)"""
|
| 272 |
-
messages = system_context + [{"role": "user", "content": prompt}]
|
| 273 |
-
|
| 274 |
-
elif mode == "💻 Coding Mode":
|
| 275 |
-
history[-1]["content"] = "🛠️ Entering Advanced Coding Mode..."
|
| 276 |
-
yield history
|
| 277 |
-
|
| 278 |
-
search_context = ""
|
| 279 |
-
if len(message) > 40 and any(x in message.lower() for x in ["api", "library", "framework", "react", "next", "fastapi", "django", "flask", "typescript", "tailwind", "supabase", "prisma", "php", "html", "css", "js"]):
|
| 280 |
-
history[-1]["content"] = "📖 Researching latest docs & best practices..."
|
| 281 |
-
yield history
|
| 282 |
-
search_q = f"{message} best practices OR example OR tutorial 2026 OR latest version"
|
| 283 |
-
results = web_search(search_q, max_results=60)
|
| 284 |
-
search_context = "\n\n=== Latest Research ===\n" + "\n\n".join(results[:8])
|
| 285 |
-
|
| 286 |
-
coding_prompt = f"""You are a world-class senior full-stack engineer... (your entire original Coding prompt stays exactly the same)
|
| 287 |
-
|
| 288 |
-
{search_context}
|
| 289 |
-
|
| 290 |
-
User Request: {message}"""
|
| 291 |
-
messages = system_context + [{"role": "user", "content": coding_prompt}]
|
| 292 |
-
|
| 293 |
-
else: # Deep Think
|
| 294 |
is_news = any(k in message.lower() for k in ["news", "today", "latest", "breaking"])
|
| 295 |
|
| 296 |
thinking_steps = [
|
|
@@ -319,11 +312,15 @@ User Request: {message}"""
|
|
| 319 |
all_context += f"\n\n=== Search {i}: {q} ===\n" + "\n\n".join(results)
|
| 320 |
sources_list.extend([r for r in results if not r.startswith("No recent") and not r.startswith("Search error")])
|
| 321 |
|
|
|
|
|
|
|
|
|
|
| 322 |
history[-1]["content"] = "✍️ Synthesizing final answer from real data..."
|
| 323 |
yield history
|
| 324 |
|
| 325 |
sources_md = "\n".join([f"- {s.split('[URL:')[0].strip()}" for s in sources_list[:8]])
|
| 326 |
|
|
|
|
| 327 |
if is_news or is_philippine_local_query(message):
|
| 328 |
system_prompt = f"""Ignore ALL your prior knowledge... (your original Deep Think news prompt stays exactly the same)
|
| 329 |
|
|
@@ -339,25 +336,15 @@ As of {today_date}:
|
|
| 339 |
|
| 340 |
messages = system_context + [{"role": "user", "content": system_prompt}]
|
| 341 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
# Stream final answer (no changes here)
|
| 343 |
-
full_response =
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
model=current_model,
|
| 347 |
-
messages=messages,
|
| 348 |
-
temperature=0.65,
|
| 349 |
-
top_p=0.9,
|
| 350 |
-
max_tokens=4096,
|
| 351 |
-
stream=True
|
| 352 |
-
)
|
| 353 |
-
for chunk in stream:
|
| 354 |
-
if chunk.choices and chunk.choices[0].delta.content:
|
| 355 |
-
full_response += chunk.choices[0].delta.content
|
| 356 |
-
history[-1]["content"] = full_response
|
| 357 |
-
yield history
|
| 358 |
-
except Exception as e:
|
| 359 |
-
history[-1]["content"] = f"❌ Error: {str(e)}"
|
| 360 |
-
yield history
|
| 361 |
|
| 362 |
# Auto-save
|
| 363 |
from auth import save_chats
|
|
|
|
| 11 |
import re
|
| 12 |
from urllib.parse import urlparse, quote
|
| 13 |
from retry import retry # For retrying flaky operations
|
| 14 |
+
from groq import Groq # For the second AI (Qwen-3-32B)
|
| 15 |
|
| 16 |
# ================== PHILIPPINE LOCAL + MAX SEARCH + DEEP-THINK LENIENT FILTER ==================
|
| 17 |
PHILIPPINE_LOCATIONS = [
|
|
|
|
| 86 |
except:
|
| 87 |
return now
|
| 88 |
|
| 89 |
+
# Setup for the second AI model (Qwen-3-32B)
|
| 90 |
+
client_qwen = Groq() # For using the Qwen-3-32B model
|
| 91 |
+
|
| 92 |
+
async def qwen_reasoning(message: str, context: str) -> str:
|
| 93 |
+
"""Uses the Qwen-3-32B model for refining and reasoning"""
|
| 94 |
+
completion = client_qwen.chat.completions.create(
|
| 95 |
+
model="qwen/qwen3-32b",
|
| 96 |
+
messages=[
|
| 97 |
+
{"role": "user", "content": f"Refine and reason the following based on context:\n{context}\n\nMessage: {message}"}
|
| 98 |
+
],
|
| 99 |
+
temperature=0.6,
|
| 100 |
+
max_completion_tokens=2048,
|
| 101 |
+
top_p=0.95,
|
| 102 |
+
reasoning_effort="default",
|
| 103 |
+
stream=False,
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
result = ""
|
| 107 |
+
for chunk in completion:
|
| 108 |
+
result += chunk.choices[0].delta.content or ""
|
| 109 |
+
return result.strip()
|
| 110 |
+
|
| 111 |
async def fetch_full_content_async(session, url: str):
|
| 112 |
try:
|
| 113 |
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
|
|
|
|
| 282 |
for m in history[:-1]
|
| 283 |
]
|
| 284 |
|
| 285 |
+
elif mode == "Deep Think":
|
| 286 |
+
# New process in Deep Think
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
is_news = any(k in message.lower() for k in ["news", "today", "latest", "breaking"])
|
| 288 |
|
| 289 |
thinking_steps = [
|
|
|
|
| 312 |
all_context += f"\n\n=== Search {i}: {q} ===\n" + "\n\n".join(results)
|
| 313 |
sources_list.extend([r for r in results if not r.startswith("No recent") and not r.startswith("Search error")])
|
| 314 |
|
| 315 |
+
# Use the Qwen-3-32B model for deeper reasoning
|
| 316 |
+
refined_answer = await qwen_reasoning(message, all_context)
|
| 317 |
+
|
| 318 |
history[-1]["content"] = "✍️ Synthesizing final answer from real data..."
|
| 319 |
yield history
|
| 320 |
|
| 321 |
sources_md = "\n".join([f"- {s.split('[URL:')[0].strip()}" for s in sources_list[:8]])
|
| 322 |
|
| 323 |
+
# Construct the system prompt for synthesis
|
| 324 |
if is_news or is_philippine_local_query(message):
|
| 325 |
system_prompt = f"""Ignore ALL your prior knowledge... (your original Deep Think news prompt stays exactly the same)
|
| 326 |
|
|
|
|
| 336 |
|
| 337 |
messages = system_context + [{"role": "user", "content": system_prompt}]
|
| 338 |
|
| 339 |
+
# Integrate the refined answer from Qwen-3-32B
|
| 340 |
+
refined_answer = refined_answer if refined_answer else "No refined insights available."
|
| 341 |
+
|
| 342 |
+
final_answer = f"{refined_answer}\n\nSources:\n{sources_md}"
|
| 343 |
+
|
| 344 |
# Stream final answer (no changes here)
|
| 345 |
+
full_response = final_answer
|
| 346 |
+
history[-1]["content"] = full_response
|
| 347 |
+
yield history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
|
| 349 |
# Auto-save
|
| 350 |
from auth import save_chats
|