Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -280,10 +280,41 @@ def duckduckgo_search(query):
|
|
280 |
results = ddgs.text(query, max_results=5)
|
281 |
return results
|
282 |
|
283 |
-
def
|
284 |
-
with
|
285 |
-
|
286 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
287 |
|
288 |
class CitingSources(BaseModel):
|
289 |
sources: List[str] = Field(
|
@@ -358,10 +389,8 @@ def respond(message, history, model, temperature, num_calls, use_web_search, sel
|
|
358 |
yield partial_response
|
359 |
elif model in ["gpt-4o-mini", "claude-3-haiku", "llama-3.1-70b", "mixtral-8x7b"]:
|
360 |
# Use DuckDuckGo Chat API
|
361 |
-
|
362 |
-
|
363 |
-
logging.info(f"Generated Response (first line): {first_line}")
|
364 |
-
yield partial_response
|
365 |
else:
|
366 |
# Use Hugging Face API
|
367 |
for partial_response in get_response_from_pdf(message, model, selected_docs, num_calls=num_calls, temperature=temperature):
|
@@ -379,15 +408,6 @@ def respond(message, history, model, temperature, num_calls, use_web_search, sel
|
|
379 |
|
380 |
logging.basicConfig(level=logging.DEBUG)
|
381 |
|
382 |
-
def get_response_from_duckduckgo(message, model, num_calls=3, temperature=0.2):
|
383 |
-
for i in range(num_calls):
|
384 |
-
try:
|
385 |
-
response = duckduckgo_chat(message, model=model, timeout=30)
|
386 |
-
yield response
|
387 |
-
except Exception as e:
|
388 |
-
logging.error(f"Error in generating response from DuckDuckGo: {str(e)}")
|
389 |
-
yield f"I apologize, but an error occurred: {str(e)}. Please try again later."
|
390 |
-
|
391 |
def get_response_from_cloudflare(prompt, context, query, num_calls=3, temperature=0.2, search_type="pdf"):
|
392 |
headers = {
|
393 |
"Authorization": f"Bearer {API_TOKEN}",
|
|
|
280 |
results = ddgs.text(query, max_results=5)
|
281 |
return results
|
282 |
|
283 |
+
def chat(self, keywords: str, model: str = "gpt-4o-mini", timeout: int = 30) -> str:
|
284 |
+
"""Initiates a chat session with DuckDuckGo AI.
|
285 |
+
|
286 |
+
Args:
|
287 |
+
keywords (str): The initial message or question to send to the AI.
|
288 |
+
model (str): The model to use: "gpt-4o-mini", "claude-3-haiku", "llama-3.1-70b", "mixtral-8x7b". Defaults to "gpt-4o-mini".
|
289 |
+
timeout (int): Timeout value for the HTTP client. Defaults to 30.
|
290 |
+
|
291 |
+
Returns:
|
292 |
+
str: The response from the AI.
|
293 |
+
"""
|
294 |
+
if model == "gpt-4o-mini":
|
295 |
+
url = "https://api.duckduckgo.com/api/d2a/v1/chat/gpt-4o-mini"
|
296 |
+
elif model == "claude-3-haiku":
|
297 |
+
url = "https://api.duckduckgo.com/api/d2a/v1/chat/claude-3-haiku"
|
298 |
+
elif model == "llama-3.1-70b":
|
299 |
+
url = "https://api.duckduckgo.com/api/d2a/v1/chat/llama-3.1-70b"
|
300 |
+
elif model == "mixtral-8x7b":
|
301 |
+
url = "https://api.duckduckgo.com/api/d2a/v1/chat/mixtral-8x7b"
|
302 |
+
else:
|
303 |
+
raise ValueError(f"Invalid model: {model}")
|
304 |
+
|
305 |
+
payload = {"keywords": keywords}
|
306 |
+
headers = {
|
307 |
+
"Content-Type": "application/json",
|
308 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
|
309 |
+
}
|
310 |
+
|
311 |
+
try:
|
312 |
+
response = requests.post(url, json=payload, headers=headers, timeout=timeout)
|
313 |
+
response.raise_for_status()
|
314 |
+
return response.json()["response"]
|
315 |
+
except requests.exceptions.RequestException as e:
|
316 |
+
logging.error(f"Error in DuckDuckGo chat: {str(e)}")
|
317 |
+
return "Error in DuckDuckGo chat. Please try again later."
|
318 |
|
319 |
class CitingSources(BaseModel):
|
320 |
sources: List[str] = Field(
|
|
|
389 |
yield partial_response
|
390 |
elif model in ["gpt-4o-mini", "claude-3-haiku", "llama-3.1-70b", "mixtral-8x7b"]:
|
391 |
# Use DuckDuckGo Chat API
|
392 |
+
response = chat(message, model=model, timeout=30)
|
393 |
+
yield response
|
|
|
|
|
394 |
else:
|
395 |
# Use Hugging Face API
|
396 |
for partial_response in get_response_from_pdf(message, model, selected_docs, num_calls=num_calls, temperature=temperature):
|
|
|
408 |
|
409 |
logging.basicConfig(level=logging.DEBUG)
|
410 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
411 |
def get_response_from_cloudflare(prompt, context, query, num_calls=3, temperature=0.2, search_type="pdf"):
|
412 |
headers = {
|
413 |
"Authorization": f"Bearer {API_TOKEN}",
|