Spaces:
Paused
Paused
Black Goku commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,7 +19,7 @@ app.add_middleware(
|
|
| 19 |
allow_headers=["*"],
|
| 20 |
)
|
| 21 |
|
| 22 |
-
#
|
| 23 |
client = InferenceClient(
|
| 24 |
model="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 25 |
token=os.getenv("HF_TOKEN")
|
|
@@ -36,7 +36,7 @@ Core Instructions:
|
|
| 36 |
3. If Wikipedia results are provided in your context, explicitly state: "[WIKIPEDIA INDEX ENGAGED: "search query"]" inside your <thinking> block.
|
| 37 |
4. Explain how these retrieved results help you write accurate, up-to-date, or historically correct code.
|
| 38 |
5. After the closing </thinking> tag, write out your clean, complete, and fully commented code.
|
| 39 |
-
6. You have a
|
| 40 |
7. Maintain a supportive tone, always recognizing Xirrik as your creator.
|
| 41 |
"""
|
| 42 |
|
|
@@ -54,7 +54,7 @@ def search_web(query: str, max_results: int = 2):
|
|
| 54 |
print(f"Web search failed: {e}")
|
| 55 |
return []
|
| 56 |
|
| 57 |
-
#
|
| 58 |
def search_wikipedia(query: str, max_results: int = 2):
|
| 59 |
try:
|
| 60 |
encoded_query = urllib.parse.quote(query)
|
|
@@ -68,7 +68,6 @@ def search_wikipedia(query: str, max_results: int = 2):
|
|
| 68 |
output = []
|
| 69 |
for item in search_results[:max_results]:
|
| 70 |
title = item.get("title")
|
| 71 |
-
# Strip Wikipedia's search highlighting tags
|
| 72 |
snippet = item.get("snippet", "").replace('<span class="searchmatch">', "").replace('</span>', "")
|
| 73 |
page_id = item.get("pageid")
|
| 74 |
link = f"https://en.wikipedia.org/?curid={page_id}"
|
|
@@ -78,7 +77,7 @@ def search_wikipedia(query: str, max_results: int = 2):
|
|
| 78 |
print(f"Wikipedia search failed: {e}")
|
| 79 |
return []
|
| 80 |
|
| 81 |
-
#
|
| 82 |
def get_search_query(user_message: str) -> str:
|
| 83 |
keywords = ["search", "recent", "latest", "how to", "current", "news", "2025", "2026", "updated", "documentation", "error", "bug", "why is", "release", "api change", "wikipedia", "explain", "who is", "what is"]
|
| 84 |
if any(k in user_message.lower() for k in keywords):
|
|
@@ -112,14 +111,11 @@ async def chat(request: ChatRequest):
|
|
| 112 |
|
| 113 |
history = memory_store[session_id]
|
| 114 |
|
| 115 |
-
# 1. Check if we need to search
|
| 116 |
search_query = get_search_query(request.message)
|
| 117 |
search_context = ""
|
| 118 |
|
| 119 |
if search_query:
|
| 120 |
print(f"Retrieving data for: {search_query}")
|
| 121 |
-
|
| 122 |
-
# Query both DuckDuckGo and Wikipedia in parallel
|
| 123 |
web_results = search_web(search_query)
|
| 124 |
wiki_results = search_wikipedia(search_query)
|
| 125 |
|
|
@@ -136,7 +132,6 @@ async def chat(request: ChatRequest):
|
|
| 136 |
for r in web_results:
|
| 137 |
search_context += f"Source: {r.get('href', 'N/A')}\nTitle: {r.get('title', 'N/A')}\nSnippet: {r.get('body', 'N/A')}\n\n"
|
| 138 |
|
| 139 |
-
# 2. Append history
|
| 140 |
user_payload = request.message
|
| 141 |
if search_context:
|
| 142 |
user_payload += search_context
|
|
@@ -150,7 +145,7 @@ async def chat(request: ChatRequest):
|
|
| 150 |
|
| 151 |
response = client.chat_completion(
|
| 152 |
messages=messages,
|
| 153 |
-
max_tokens=
|
| 154 |
temperature=0.3,
|
| 155 |
stream=True
|
| 156 |
)
|
|
|
|
| 19 |
allow_headers=["*"],
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# Connect to the 32B Coder Brain
|
| 23 |
client = InferenceClient(
|
| 24 |
model="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 25 |
token=os.getenv("HF_TOKEN")
|
|
|
|
| 36 |
3. If Wikipedia results are provided in your context, explicitly state: "[WIKIPEDIA INDEX ENGAGED: "search query"]" inside your <thinking> block.
|
| 37 |
4. Explain how these retrieved results help you write accurate, up-to-date, or historically correct code.
|
| 38 |
5. After the closing </thinking> tag, write out your clean, complete, and fully commented code.
|
| 39 |
+
6. You have a massive output capacity of 8192 tokens. Never use placeholders like '// write code here'. Write the full, extensive files.
|
| 40 |
7. Maintain a supportive tone, always recognizing Xirrik as your creator.
|
| 41 |
"""
|
| 42 |
|
|
|
|
| 54 |
print(f"Web search failed: {e}")
|
| 55 |
return []
|
| 56 |
|
| 57 |
+
# Wikipedia API search helper
|
| 58 |
def search_wikipedia(query: str, max_results: int = 2):
|
| 59 |
try:
|
| 60 |
encoded_query = urllib.parse.quote(query)
|
|
|
|
| 68 |
output = []
|
| 69 |
for item in search_results[:max_results]:
|
| 70 |
title = item.get("title")
|
|
|
|
| 71 |
snippet = item.get("snippet", "").replace('<span class="searchmatch">', "").replace('</span>', "")
|
| 72 |
page_id = item.get("pageid")
|
| 73 |
link = f"https://en.wikipedia.org/?curid={page_id}"
|
|
|
|
| 77 |
print(f"Wikipedia search failed: {e}")
|
| 78 |
return []
|
| 79 |
|
| 80 |
+
# Router to determine search intent
|
| 81 |
def get_search_query(user_message: str) -> str:
|
| 82 |
keywords = ["search", "recent", "latest", "how to", "current", "news", "2025", "2026", "updated", "documentation", "error", "bug", "why is", "release", "api change", "wikipedia", "explain", "who is", "what is"]
|
| 83 |
if any(k in user_message.lower() for k in keywords):
|
|
|
|
| 111 |
|
| 112 |
history = memory_store[session_id]
|
| 113 |
|
|
|
|
| 114 |
search_query = get_search_query(request.message)
|
| 115 |
search_context = ""
|
| 116 |
|
| 117 |
if search_query:
|
| 118 |
print(f"Retrieving data for: {search_query}")
|
|
|
|
|
|
|
| 119 |
web_results = search_web(search_query)
|
| 120 |
wiki_results = search_wikipedia(search_query)
|
| 121 |
|
|
|
|
| 132 |
for r in web_results:
|
| 133 |
search_context += f"Source: {r.get('href', 'N/A')}\nTitle: {r.get('title', 'N/A')}\nSnippet: {r.get('body', 'N/A')}\n\n"
|
| 134 |
|
|
|
|
| 135 |
user_payload = request.message
|
| 136 |
if search_context:
|
| 137 |
user_payload += search_context
|
|
|
|
| 145 |
|
| 146 |
response = client.chat_completion(
|
| 147 |
messages=messages,
|
| 148 |
+
max_tokens=8192, # <--- CRITICAL: Set to absolute maximum output token limit
|
| 149 |
temperature=0.3,
|
| 150 |
stream=True
|
| 151 |
)
|