Spaces:
Running
Running
Update response_modes.py
Browse files- response_modes.py +38 -7
response_modes.py
CHANGED
|
@@ -2,7 +2,8 @@ import os
|
|
| 2 |
import requests
|
| 3 |
import asyncio
|
| 4 |
import aiohttp
|
| 5 |
-
import
|
|
|
|
| 6 |
from datetime import datetime, timedelta
|
| 7 |
from auth import save_chats
|
| 8 |
from bs4 import BeautifulSoup
|
|
@@ -233,20 +234,50 @@ def clean_response(text: str) -> str:
|
|
| 233 |
return ""
|
| 234 |
text = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL | re.IGNORECASE)
|
| 235 |
text = re.sub(r'```(?:think|reasoning).*?```', '', text, flags=re.DOTALL | re.IGNORECASE)
|
| 236 |
-
text = re.sub(r'\n{3,}', '\n\n', text)
|
| 237 |
return text.strip()
|
| 238 |
|
| 239 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
def qwen_reasoning(message: str, search_context: str, chat_context: str = "") -> str:
|
| 241 |
-
"""Respects persistent chat context + cleaner prompt"""
|
| 242 |
system_instruction = ""
|
| 243 |
if chat_context and str(chat_context).strip():
|
| 244 |
-
system_instruction = f"""[PERSISTENT CHAT RULE
|
| 245 |
{chat_context}
|
| 246 |
|
| 247 |
"""
|
| 248 |
|
| 249 |
-
prompt = f"""
|
|
|
|
| 250 |
|
| 251 |
Real-time search data from the web:
|
| 252 |
{search_context}
|
|
@@ -254,14 +285,14 @@ Real-time search data from the web:
|
|
| 254 |
User asked: "{message}"
|
| 255 |
|
| 256 |
Rules:
|
| 257 |
-
- Answer directly and naturally
|
| 258 |
- NEVER mention data cutoffs, "no news available", or system limits.
|
| 259 |
- Use clear markdown (headings, bullets, bold).
|
| 260 |
- Be helpful and engaging.
|
| 261 |
- Never output <think>, reasoning tags, or internal thoughts.
|
| 262 |
- If the user asks for sources, they will be added automatically.
|
| 263 |
|
| 264 |
-
Final answer:"""
|
| 265 |
|
| 266 |
try:
|
| 267 |
completion = client_qwen.chat.completions.create(
|
|
|
|
| 2 |
import requests
|
| 3 |
import asyncio
|
| 4 |
import aiohttp
|
| 5 |
+
import textwrap
|
| 6 |
+
import re
|
| 7 |
from datetime import datetime, timedelta
|
| 8 |
from auth import save_chats
|
| 9 |
from bs4 import BeautifulSoup
|
|
|
|
| 234 |
return ""
|
| 235 |
text = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL | re.IGNORECASE)
|
| 236 |
text = re.sub(r'```(?:think|reasoning).*?```', '', text, flags=re.DOTALL | re.IGNORECASE)
|
| 237 |
+
text = re.sub(r'\n{3,}', '\n\n', text)
|
| 238 |
return text.strip()
|
| 239 |
|
| 240 |
|
| 241 |
+
def format_sources(sources_list, max_sources=7):
|
| 242 |
+
"""Super clean, beautiful sources for both Philippine & global news"""
|
| 243 |
+
if not sources_list:
|
| 244 |
+
return ""
|
| 245 |
+
|
| 246 |
+
formatted = []
|
| 247 |
+
for i, entry in enumerate(sources_list[:max_sources], 1):
|
| 248 |
+
title = "Source"
|
| 249 |
+
snippet = ""
|
| 250 |
+
url = "#"
|
| 251 |
+
|
| 252 |
+
if '**' in entry:
|
| 253 |
+
title_match = re.search(r'\*\*(.+?)\*\*', entry)
|
| 254 |
+
if title_match:
|
| 255 |
+
title = title_match.group(1).strip()
|
| 256 |
+
|
| 257 |
+
if 'Snippet:' in entry:
|
| 258 |
+
snippet_part = entry.split('Snippet:')[1].split('\n')[0]
|
| 259 |
+
snippet = snippet_part.strip()[:260] + "..." if len(snippet_part) > 260 else snippet_part.strip()
|
| 260 |
+
|
| 261 |
+
url_match = re.search(r'\[URL:\s*(https?://[^\]]+)', entry)
|
| 262 |
+
if url_match:
|
| 263 |
+
url = url_match.group(1).strip()
|
| 264 |
+
|
| 265 |
+
formatted.append(f"{i}. **{title}** \n {snippet}\n [Read full →]({url})")
|
| 266 |
+
|
| 267 |
+
return "\n\n".join(formatted)
|
| 268 |
+
|
| 269 |
+
|
| 270 |
def qwen_reasoning(message: str, search_context: str, chat_context: str = "") -> str:
|
| 271 |
+
"""Respects persistent chat context + cleaner prompt (no syntax error)"""
|
| 272 |
system_instruction = ""
|
| 273 |
if chat_context and str(chat_context).strip():
|
| 274 |
+
system_instruction = f"""[PERSISTENT CHAT RULE — ALWAYS FOLLOW FOR THIS CHAT]
|
| 275 |
{chat_context}
|
| 276 |
|
| 277 |
"""
|
| 278 |
|
| 279 |
+
prompt = textwrap.dedent(f"""\
|
| 280 |
+
{system_instruction}You are a professional, friendly news analyst.
|
| 281 |
|
| 282 |
Real-time search data from the web:
|
| 283 |
{search_context}
|
|
|
|
| 285 |
User asked: "{message}"
|
| 286 |
|
| 287 |
Rules:
|
| 288 |
+
- Answer directly and naturally — start with the actual content.
|
| 289 |
- NEVER mention data cutoffs, "no news available", or system limits.
|
| 290 |
- Use clear markdown (headings, bullets, bold).
|
| 291 |
- Be helpful and engaging.
|
| 292 |
- Never output <think>, reasoning tags, or internal thoughts.
|
| 293 |
- If the user asks for sources, they will be added automatically.
|
| 294 |
|
| 295 |
+
Final answer:""")
|
| 296 |
|
| 297 |
try:
|
| 298 |
completion = client_qwen.chat.completions.create(
|