Spaces:
Sleeping
Sleeping
Update tools_engine.py
Browse files- tools_engine.py +29 -38
tools_engine.py
CHANGED
|
@@ -1,26 +1,24 @@
|
|
| 1 |
"""
|
| 2 |
-
tools_engine.py - Improved perform_web_search to return structured results
|
|
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
from duckduckgo_search import DDGS
|
| 6 |
from transformers import pipeline
|
| 7 |
import re
|
| 8 |
-
import logging
|
| 9 |
-
|
| 10 |
-
logger = logging.getLogger("nexari-tools")
|
| 11 |
|
| 12 |
print(">>> Tools: Loading Intent Classification Model...")
|
| 13 |
-
# Use a lighter model or the one you had.
|
| 14 |
intent_classifier = pipeline("zero-shot-classification", model="typeform/distilbert-base-uncased-mnli")
|
| 15 |
|
| 16 |
def analyze_intent(user_text):
|
| 17 |
if not user_text:
|
| 18 |
return "general"
|
| 19 |
text_lower = user_text.lower().strip()
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
return "general"
|
| 25 |
|
| 26 |
candidate_labels = ["internet search","general conversation","coding request","checking time"]
|
|
@@ -28,55 +26,48 @@ def analyze_intent(user_text):
|
|
| 28 |
result = intent_classifier(user_text, candidate_labels)
|
| 29 |
top_label = result['labels'][0]
|
| 30 |
confidence = result['scores'][0]
|
| 31 |
-
|
| 32 |
mapping = {
|
| 33 |
"internet search": "internet_search",
|
| 34 |
"general conversation": "general",
|
| 35 |
"coding request": "coding_request",
|
| 36 |
"checking time": "checking_time"
|
| 37 |
}
|
| 38 |
-
# Threshold
|
| 39 |
if confidence > 0.45:
|
| 40 |
return mapping.get(top_label, "general")
|
| 41 |
-
except Exception
|
| 42 |
-
|
| 43 |
-
|
| 44 |
return "general"
|
| 45 |
|
| 46 |
def perform_web_search(user_text, max_results=4):
|
| 47 |
"""
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
"""
|
| 50 |
try:
|
| 51 |
-
# cleanup query
|
| 52 |
query = user_text
|
| 53 |
-
|
| 54 |
-
|
|
|
|
| 55 |
for p in remove_phrases:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
logger.info(f"Searching web for: {query}")
|
| 60 |
-
|
| 61 |
-
# DDGS execution
|
| 62 |
-
results_list = []
|
| 63 |
-
with DDGS() as ddgs:
|
| 64 |
-
# .text() returns an iterator
|
| 65 |
-
ddgs_gen = ddgs.text(query, max_results=max_results)
|
| 66 |
-
if ddgs_gen:
|
| 67 |
-
results_list = list(ddgs_gen)
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
for r in
|
| 72 |
title = r.get("title","").strip()
|
| 73 |
body = re.sub(r'\s+',' ', r.get("body","").strip())
|
| 74 |
-
url = r.get("href") or r.get("url") or ""
|
| 75 |
-
|
|
|
|
| 76 |
structured["results"].append({"title": title, "snippet": snippet, "url": url})
|
| 77 |
-
|
| 78 |
return structured
|
| 79 |
-
|
| 80 |
except Exception as e:
|
| 81 |
-
|
| 82 |
return {"query": user_text, "results": []}
|
|
|
|
| 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
|
| 7 |
from transformers import pipeline
|
| 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"]
|
|
|
|
| 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": []}
|