Nexari-Research commited on
Commit
35c38d8
·
verified ·
1 Parent(s): 9e84f89

Update tools_engine.py

Browse files
Files changed (1) hide show
  1. tools_engine.py +42 -70
tools_engine.py CHANGED
@@ -1,11 +1,6 @@
1
  """
2
- Nexari Tools Engine (UPDATED)
3
- Author: Piyush
4
- Improvements:
5
- - Canonical intent labels returned
6
- - More robust greeting detection
7
- - Safer web search formatting
8
- - Defensive error handling
9
  """
10
 
11
  from duckduckgo_search import DDGS
@@ -13,89 +8,66 @@ from transformers import pipeline
13
  import re
14
 
15
  print(">>> Tools: Loading Intent Classification Model...")
16
- # zero-shot pipeline; if heavy, Docker predownload already handles it
17
  intent_classifier = pipeline("zero-shot-classification", model="typeform/distilbert-base-uncased-mnli")
18
 
19
  def analyze_intent(user_text):
20
- """
21
- Returns canonical intent labels:
22
- - "internet_search"
23
- - "coding_request"
24
- - "checking_time"
25
- - "general"
26
- """
27
  if not user_text:
28
  return "general"
29
-
30
  text_lower = user_text.lower().strip()
31
-
32
- # === HARDCODED SAFETY (Greeting/Identity) ===
33
  direct_chat_triggers = [
34
- "hi", "hello", "hey", "hlo", "hola", "namaste",
35
- "what is your name", "who are you", "who r you", "your name"
36
  ]
37
- # exact match or common short greeting at start
38
  if text_lower in direct_chat_triggers or any(text_lower.startswith(t + " ") for t in direct_chat_triggers):
39
- print(f">>> Brain Override: Detected Greeting/Identity -> Force 'general'")
40
  return "general"
41
 
42
- # === ZERO-SHOT PASSES ===
43
- candidate_labels = [
44
- "internet search",
45
- "general conversation",
46
- "coding request",
47
- "checking time"
48
- ]
49
-
50
  try:
51
  result = intent_classifier(user_text, candidate_labels)
52
  top_label = result['labels'][0]
53
  confidence = result['scores'][0]
54
- print(f">>> Brain: Detected '{top_label}' ({confidence:.2f})")
55
-
56
- if confidence > 0.5:
57
- # map to canonical labels
58
- mapping = {
59
- "internet search": "internet_search",
60
- "general conversation": "general",
61
- "coding request": "coding_request",
62
- "checking time": "checking_time"
63
- }
64
  return mapping.get(top_label, "general")
65
- except Exception as e:
66
- print(f"Intent Error: {e}")
67
-
68
  return "general"
69
 
70
- def perform_web_search(user_text, max_results=3):
71
  """
72
- Execute a brief web search and return a compact summary.
 
 
 
 
 
 
 
73
  """
74
  try:
75
- clean_query = user_text.lower()
76
- remove_phrases = ["search for", "google", "find", "tell me about", "latest info on", "news about", "lookup"]
77
- for phrase in remove_phrases:
78
- clean_query = clean_query.replace(phrase, "")
79
-
80
- clean_query = clean_query.strip()
81
- if len(clean_query) < 2:
82
- clean_query = user_text
83
-
84
- print(f">>> Action: Searching Web for '{clean_query}'...")
85
- results = list(DDGS().text(clean_query, max_results=max_results))
86
 
87
- if results:
88
- parts = []
89
- for r in results:
90
- title = r.get('title', '').strip()
91
- body = re.sub(r'\s+', ' ', r.get('body', '').strip())
92
- # keep short
93
- if len(body) > 250:
94
- body = body[:250].rsplit(' ', 1)[0] + "..."
95
- parts.append(f"- {title}: {body}")
96
- summary = "\n".join(parts)
97
- return f"### WEB DATA (short) ###\n{summary}\nINSTRUCTION: Use this to inform the answer; prefer concise synthesis."
98
- return ""
99
  except Exception as e:
100
- print(f"Search Error: {e}")
101
- return ""
 
1
  """
2
+ tools_engine.py - Improved perform_web_search to return structured results with URLs and snippets,
3
+ and canonical intent detection unchanged.
 
 
 
 
 
4
  """
5
 
6
  from duckduckgo_search import DDGS
 
8
  import re
9
 
10
  print(">>> Tools: Loading Intent Classification Model...")
 
11
  intent_classifier = pipeline("zero-shot-classification", model="typeform/distilbert-base-uncased-mnli")
12
 
13
  def analyze_intent(user_text):
 
 
 
 
 
 
 
14
  if not user_text:
15
  return "general"
 
16
  text_lower = user_text.lower().strip()
 
 
17
  direct_chat_triggers = [
18
+ "hi","hello","hey","hlo","namaste",
19
+ "what is your name", "who are you", "your name"
20
  ]
 
21
  if text_lower in direct_chat_triggers or any(text_lower.startswith(t + " ") for t in direct_chat_triggers):
 
22
  return "general"
23
 
24
+ candidate_labels = ["internet search","general conversation","coding request","checking time"]
 
 
 
 
 
 
 
25
  try:
26
  result = intent_classifier(user_text, candidate_labels)
27
  top_label = result['labels'][0]
28
  confidence = result['scores'][0]
29
+ mapping = {
30
+ "internet search": "internet_search",
31
+ "general conversation": "general",
32
+ "coding request": "coding_request",
33
+ "checking time": "checking_time"
34
+ }
35
+ if confidence > 0.45:
 
 
 
36
  return mapping.get(top_label, "general")
37
+ except Exception:
38
+ pass
 
39
  return "general"
40
 
41
+ def perform_web_search(user_text, max_results=4):
42
  """
43
+ Return structured results:
44
+ {
45
+ "query": "...",
46
+ "results": [
47
+ {"title": "...", "snippet": "...", "url": "..."},
48
+ ...
49
+ ]
50
+ }
51
  """
52
  try:
53
+ query = user_text
54
+ # sanitize small verbs
55
+ remove_phrases = ["search for","find","google","look up","lookup","what is","tell me"]
56
+ q = query.lower()
57
+ for p in remove_phrases:
58
+ q = q.replace(p, "")
59
+ q = q.strip() or query
 
 
 
 
60
 
61
+ results = list(DDGS().text(q, max_results=max_results))
62
+ structured = {"query": q, "results": []}
63
+ for r in results:
64
+ title = r.get("title","").strip()
65
+ body = re.sub(r'\s+',' ', r.get("body","").strip())
66
+ url = r.get("href") or r.get("url") or r.get("link") or ""
67
+ # short snippet
68
+ snippet = body[:320]
69
+ structured["results"].append({"title": title, "snippet": snippet, "url": url})
70
+ return structured
 
 
71
  except Exception as e:
72
+ print(f"Search error: {e}")
73
+ return {"query": user_text, "results": []}