walidsobhie-code commited on
Commit ·
0fbc572
1
Parent(s): 35799ef
Add real web search with ddgs (DuckDuckGo HTML scraper, free, no API key)
Browse files
chat.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import torch
|
| 2 |
import requests
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
| 4 |
|
| 5 |
SYSTEM_PROMPT = """You are Stack 2.9, an expert AI coding assistant.
|
| 6 |
- Answer questions naturally and helpfully
|
|
@@ -30,20 +31,14 @@ print(f"Settings: max_tokens={MAX_TOKENS}, temperature={TEMPERATURE}, top_p={TOP
|
|
| 30 |
print("Commands: search:<query> - search the web, quit/exit - stop\n")
|
| 31 |
|
| 32 |
def web_search(query, count=5):
|
| 33 |
-
"""Search the web using DuckDuckGo API"""
|
| 34 |
try:
|
| 35 |
-
import urllib.parse
|
| 36 |
-
encoded_q = urllib.parse.quote(query)
|
| 37 |
-
url = f"https://api.duckduckgo.com/?q={encoded_q}&format=json&no_redirect=1"
|
| 38 |
-
headers = {"User-Agent": "Mozilla/5.0 (compatible; Stack29Bot/1.0)"}
|
| 39 |
-
resp = requests.get(url, headers=headers, timeout=10)
|
| 40 |
-
data = resp.json()
|
| 41 |
-
|
| 42 |
results = []
|
| 43 |
-
|
| 44 |
-
for
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
if results:
|
| 49 |
return {"success": True, "results": results, "query": query}
|
|
|
|
| 1 |
import torch
|
| 2 |
import requests
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
from ddgs import DDGS
|
| 5 |
|
| 6 |
SYSTEM_PROMPT = """You are Stack 2.9, an expert AI coding assistant.
|
| 7 |
- Answer questions naturally and helpfully
|
|
|
|
| 31 |
print("Commands: search:<query> - search the web, quit/exit - stop\n")
|
| 32 |
|
| 33 |
def web_search(query, count=5):
|
| 34 |
+
"""Search the web using DuckDuckGo (no API key needed)"""
|
| 35 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
results = []
|
| 37 |
+
with DDGS() as ddgs:
|
| 38 |
+
for r in ddgs.text(query, max_results=count):
|
| 39 |
+
results.append(f"{r['body'][:200]}")
|
| 40 |
+
if len(results) >= count:
|
| 41 |
+
break
|
| 42 |
|
| 43 |
if results:
|
| 44 |
return {"success": True, "results": results, "query": query}
|