Nexari-Server / tools_engine.py
Nexari-Research's picture
Update tools_engine.py
92cda7a verified
raw
history blame
2.53 kB
"""
tools_engine.py - Improved perform_web_search to return structured results with URLs and snippets,
and canonical intent detection unchanged.
"""
from duckduckgo_search import DDGS
from transformers import pipeline
import re
print(">>> Tools: Loading Intent Classification Model...")
intent_classifier = pipeline("zero-shot-classification", model="typeform/distilbert-base-uncased-mnli")
def analyze_intent(user_text):
if not user_text:
return "general"
text_lower = user_text.lower().strip()
direct_chat_triggers = [
"hi","hello","hey","hlo","namaste",
"what is your name", "who are you", "your name"
]
if text_lower in direct_chat_triggers or any(text_lower.startswith(t + " ") for t in direct_chat_triggers):
return "general"
candidate_labels = ["internet search","general conversation","coding request","checking time"]
try:
result = intent_classifier(user_text, candidate_labels)
top_label = result['labels'][0]
confidence = result['scores'][0]
mapping = {
"internet search": "internet_search",
"general conversation": "general",
"coding request": "coding_request",
"checking time": "checking_time"
}
if confidence > 0.45:
return mapping.get(top_label, "general")
except Exception:
pass
return "general"
def perform_web_search(user_text, max_results=4):
"""
Return structured results:
{
"query": "...",
"results": [
{"title": "...", "snippet": "...", "url": "..."},
...
]
}
"""
try:
query = user_text
# sanitize small verbs
remove_phrases = ["search for","find","google","look up","lookup","what is","tell me"]
q = query.lower()
for p in remove_phrases:
q = q.replace(p, "")
q = q.strip() or query
results = list(DDGS().text(q, max_results=max_results))
structured = {"query": q, "results": []}
for r in results:
title = r.get("title","").strip()
body = re.sub(r'\s+',' ', r.get("body","").strip())
url = r.get("href") or r.get("url") or r.get("link") or ""
# short snippet
snippet = body[:320]
structured["results"].append({"title": title, "snippet": snippet, "url": url})
return structured
except Exception as e:
print(f"Search error: {e}")
return {"query": user_text, "results": []}