Nexari-Server / tools_engine.py
Nexari-Research's picture
Update tools_engine.py
550bae8 verified
# tools_engine.py - intent detection + web search
from transformers import pipeline
import re
print(">>> Tools: Loading Intent Classification Model...")
try:
intent_classifier = pipeline("zero-shot-classification", model="typeform/distilbert-base-uncased-mnli")
except Exception as e:
print(f"Intent model load failed: {e}")
intent_classifier = None
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:
if intent_classifier:
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):
try:
from duckduckgo_search import DDGS
except Exception as e:
print(f"Search library import failed: {e}")
return {"query": user_text, "results": []}
try:
query = user_text or ""
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") or "").strip()
body = (r.get("body") or "").strip()
body = re.sub(r'\s+',' ', body)
url = r.get("href") or r.get("url") or r.get("link") or ""
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": []}