walidsobhie-code commited on
Commit
40b1cc9
·
1 Parent(s): 217c8d8

Add web search command (search:query) with DuckDuckGo API

Browse files
Files changed (1) hide show
  1. chat.py +29 -2
chat.py CHANGED
@@ -1,11 +1,13 @@
1
  import torch
 
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
 
4
  SYSTEM_PROMPT = """You are Stack 2.9, an expert AI coding assistant.
5
  - Answer questions naturally and helpfully
6
  - When the user asks for code, write clean complete code
7
  - When the user asks a question, answer in plain language
8
- - Be concise and practical"""
 
9
 
10
  MODEL_NAME = "my-ai-stack/stack-2-9-finetuned"
11
 
@@ -24,7 +26,20 @@ TEMPERATURE = 0.4
24
  TOP_P = 0.9
25
  REP_PENALTY = 1.2
26
 
27
- print(f"Settings: max_tokens={MAX_TOKENS}, temperature={TEMPERATURE}, top_p={TOP_P}\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  # Interactive loop
30
  while True:
@@ -35,6 +50,18 @@ while True:
35
  if not prompt.strip():
36
  continue
37
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  # Prepend system prompt
39
  full_prompt = f"{SYSTEM_PROMPT}\n\nUser: {prompt}\nAssistant:"
40
  inputs = tokenizer(full_prompt, return_tensors='pt').to(model.device)
 
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
7
  - When the user asks for code, write clean complete code
8
  - When the user asks a question, answer in plain language
9
+ - Be concise and practical
10
+ - If asked to search the internet, use the search: command"""
11
 
12
  MODEL_NAME = "my-ai-stack/stack-2-9-finetuned"
13
 
 
26
  TOP_P = 0.9
27
  REP_PENALTY = 1.2
28
 
29
+ print(f"Settings: max_tokens={MAX_TOKENS}, temperature={TEMPERATURE}, top_p={TOP_P}")
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 JSON API"""
34
+ try:
35
+ url = f"https://duckduckgo.com/?q={query}&format=json&no_redirect=1"
36
+ headers = {"User-Agent": "Mozilla/5.0 (compatible; Stack29Bot/1.0)"}
37
+ resp = requests.get(url, headers=headers, timeout=10)
38
+ if resp.status_code == 200:
39
+ return {"success": True, "results": [{"query": query, "source": "duckduckgo"}]}
40
+ return {"success": False, "error": f"HTTP {resp.status_code}"}
41
+ except Exception as e:
42
+ return {"success": False, "error": str(e)}
43
 
44
  # Interactive loop
45
  while True:
 
50
  if not prompt.strip():
51
  continue
52
 
53
+ # Handle search command
54
+ if prompt.lower().startswith("search:"):
55
+ query = prompt[7:].strip()
56
+ print("🔍 Searching...")
57
+ result = web_search(query)
58
+ if result["success"]:
59
+ print(f"✅ Found results for: {query}")
60
+ print(f" (Web search results would appear here)")
61
+ else:
62
+ print(f"❌ Search failed: {result['error']}")
63
+ continue
64
+
65
  # Prepend system prompt
66
  full_prompt = f"{SYSTEM_PROMPT}\n\nUser: {prompt}\nAssistant:"
67
  inputs = tokenizer(full_prompt, return_tensors='pt').to(model.device)