Black Goku commited on
Commit
ba2f45b
·
verified ·
1 Parent(s): 5638fdd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -29
app.py CHANGED
@@ -1,11 +1,13 @@
1
  import os
2
- import torch
 
 
3
  from fastapi import FastAPI
4
  from pydantic import BaseModel
5
  from huggingface_hub import InferenceClient
6
  from fastapi.middleware.cors import CORSMiddleware
7
  from fastapi.responses import StreamingResponse
8
- from duckduckgo_search import DDGS # <--- NEW: Free, zero-key web search!
9
 
10
  app = FastAPI()
11
 
@@ -17,49 +19,74 @@ app.add_middleware(
17
  allow_headers=["*"],
18
  )
19
 
20
- # Initialize the world-class coding brain
21
  client = InferenceClient(
22
- model="Qwen/Qwen2.5-Coder-7B-Instruct",
23
  token=os.getenv("HF_TOKEN")
24
  )
25
 
26
  memory_store = {}
27
 
28
- # System prompt optimized for reasoning, long code writing, and web search integration
29
  PRO_IDENTITY = """
30
- You are Zenvy Quantum Pro, an elite AI software engineer created by Xirrik.
31
 
32
  Core Instructions:
33
  1. Always start your response with a thinking phase wrapped inside <thinking>...</thinking> tags. In this phase, analyze Xirrik's request step-by-step and plan your code architecture.
34
- 2. If web search results are provided in your context, explicitly state: "[WEB SEARCH ENGAGED: "search query"]" inside your <thinking> block, and explain how the retrieved search context helps you write accurate, up-to-date code.
35
- 3. After the closing </thinking> tag, write out your clean, complete, and fully commented code.
36
- 4. You have a high-capacity output window of 4096 tokens. Never use placeholders like '// write code here'. Write the full file.
37
- 5. Maintain a supportive tone, always recognizing Xirrik as your creator.
 
 
38
  """
39
 
40
  class ChatRequest(BaseModel):
41
  message: str
42
  session_id: str
43
 
44
- # Helper function to perform a web search
45
- def search_web(query: str, max_results: int = 3):
46
  try:
47
  with DDGS() as ddgs:
48
  results = list(ddgs.text(query, max_results=max_results))
49
  return results
50
  except Exception as e:
51
- print(f"Search engine error: {e}")
52
  return []
53
 
54
- # Helper function to decide if a search is needed and generate the query
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  def get_search_query(user_message: str) -> str:
56
- keywords = ["search", "recent", "latest", "how to", "current", "news", "2025", "2026", "updated", "documentation", "error", "bug", "why is", "release", "api change"]
57
  if any(k in user_message.lower() for k in keywords):
58
  try:
59
  router_prompt = (
60
  f"Analyze this user request: '{user_message}'. "
61
- "If it requires up-to-date documentation or real-time web search to answer accurately, "
62
- "generate a single, optimized search engine query. "
63
  "If no search is needed, reply with 'NONE'. Do not output any other text."
64
  )
65
  res = client.chat_completion(
@@ -75,7 +102,7 @@ def get_search_query(user_message: str) -> str:
75
 
76
  @app.get("/")
77
  def home():
78
- return {"status": "Zenvy Pro Search Core is Online"}
79
 
80
  @app.post("/chat")
81
  async def chat(request: ChatRequest):
@@ -85,19 +112,31 @@ async def chat(request: ChatRequest):
85
 
86
  history = memory_store[session_id]
87
 
88
- # 1. Check if we need to search the web first
89
  search_query = get_search_query(request.message)
90
  search_context = ""
91
 
92
  if search_query:
93
- print(f"Web Search Engaged: {search_query}")
94
- results = search_web(search_query)
95
- if results:
96
- search_context = "\n\n--- LIVE WEB SEARCH RESULTS (Up-to-date Context) ---\n"
97
- for r in results:
98
- search_context += f"Source: {r.get('href', 'N/A')}\nTitle: {r.get('title', 'N/A')}\nSnippet: {r.get('body', 'N/A')}\n\n"
99
-
100
- # 2. Append the user message (with search results if found) to memory
 
 
 
 
 
 
 
 
 
 
 
 
101
  user_payload = request.message
102
  if search_context:
103
  user_payload += search_context
@@ -111,7 +150,7 @@ async def chat(request: ChatRequest):
111
 
112
  response = client.chat_completion(
113
  messages=messages,
114
- max_tokens=4096, # Raised output token capacity for long, complete files
115
  temperature=0.3,
116
  stream=True
117
  )
@@ -123,7 +162,6 @@ async def chat(request: ChatRequest):
123
  collected_reply.append(word)
124
  yield word
125
 
126
- # Save final response to history
127
  full_reply = "".join(collected_reply)
128
  history.append({"role": "assistant", "content": full_reply})
129
 
 
1
  import os
2
+ import json
3
+ import urllib.request
4
+ import urllib.parse
5
  from fastapi import FastAPI
6
  from pydantic import BaseModel
7
  from huggingface_hub import InferenceClient
8
  from fastapi.middleware.cors import CORSMiddleware
9
  from fastapi.responses import StreamingResponse
10
+ from duckduckgo_search import DDGS
11
 
12
  app = FastAPI()
13
 
 
19
  allow_headers=["*"],
20
  )
21
 
22
+ # 1. UPGRADED BRAIN: 32B parameter coder model (Free via Hugging Face API)
23
  client = InferenceClient(
24
+ model="Qwen/Qwen2.5-Coder-32B-Instruct",
25
  token=os.getenv("HF_TOKEN")
26
  )
27
 
28
  memory_store = {}
29
 
 
30
  PRO_IDENTITY = """
31
+ You are Zenvy Quantum Pro, an elite 32-Billion parameter AI software engineer created by Xirrik.
32
 
33
  Core Instructions:
34
  1. Always start your response with a thinking phase wrapped inside <thinking>...</thinking> tags. In this phase, analyze Xirrik's request step-by-step and plan your code architecture.
35
+ 2. If web search results are provided in your context, explicitly state: "[WEB SEARCH ENGAGED: "search query"]" inside your <thinking> block.
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 high-capacity output window of 4096 tokens. Never use placeholders like '// write code here'. Write the full file.
40
+ 7. Maintain a supportive tone, always recognizing Xirrik as your creator.
41
  """
42
 
43
  class ChatRequest(BaseModel):
44
  message: str
45
  session_id: str
46
 
47
+ # DuckDuckGo search helper
48
+ def search_web(query: str, max_results: int = 2):
49
  try:
50
  with DDGS() as ddgs:
51
  results = list(ddgs.text(query, max_results=max_results))
52
  return results
53
  except Exception as e:
54
+ print(f"Web search failed: {e}")
55
  return []
56
 
57
+ # FREE Wikipedia API search helper (No key needed!)
58
+ def search_wikipedia(query: str, max_results: int = 2):
59
+ try:
60
+ encoded_query = urllib.parse.quote(query)
61
+ search_url = f"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch={encoded_query}&format=json"
62
+
63
+ req = urllib.request.Request(search_url, headers={'User-Agent': 'ZenvyWorkspace/1.0'})
64
+ with urllib.request.urlopen(req) as response:
65
+ data = json.loads(response.read().decode())
66
+
67
+ search_results = data.get("query", {}).get("search", [])
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}"
75
+ output.append({"title": title, "body": snippet, "href": link})
76
+ return output
77
+ except Exception as e:
78
+ print(f"Wikipedia search failed: {e}")
79
+ return []
80
+
81
+ # Fast router to determine if research is needed
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):
85
  try:
86
  router_prompt = (
87
  f"Analyze this user request: '{user_message}'. "
88
+ "If it requires up-to-date documentation, real-time web search, or encyclopedic knowledge to answer accurately, "
89
+ "generate a single, optimized search query. "
90
  "If no search is needed, reply with 'NONE'. Do not output any other text."
91
  )
92
  res = client.chat_completion(
 
102
 
103
  @app.get("/")
104
  def home():
105
+ return {"status": "Zenvy Pro 32B Search Core is Online"}
106
 
107
  @app.post("/chat")
108
  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
+
126
+ if web_results or wiki_results:
127
+ search_context = "\n\n--- RESEARCH DATA RETRIEVED ---\n"
128
+
129
+ if wiki_results:
130
+ search_context += f"\n[WIKIPEDIA INDEX ENGAGED: \"{search_query}\"]\n"
131
+ for r in wiki_results:
132
+ search_context += f"Source: {r['href']}\nTitle: {r['title']}\nSummary: {r['body']}\n\n"
133
+
134
+ if web_results:
135
+ search_context += f"\n[WEB SEARCH ENGAGED: \"{search_query}\"]\n"
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
 
151
  response = client.chat_completion(
152
  messages=messages,
153
+ max_tokens=4096, # 4096 token output capacity
154
  temperature=0.3,
155
  stream=True
156
  )
 
162
  collected_reply.append(word)
163
  yield word
164
 
 
165
  full_reply = "".join(collected_reply)
166
  history.append({"role": "assistant", "content": full_reply})
167