Spaces:
Sleeping
Sleeping
Update tools_engine.py
Browse files- tools_engine.py +42 -70
tools_engine.py
CHANGED
|
@@ -1,11 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
-
|
| 3 |
-
|
| 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",
|
| 35 |
-
"what is your name", "who are you", "
|
| 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 |
-
|
| 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 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
"coding request": "coding_request",
|
| 62 |
-
"checking time": "checking_time"
|
| 63 |
-
}
|
| 64 |
return mapping.get(top_label, "general")
|
| 65 |
-
except Exception
|
| 66 |
-
|
| 67 |
-
|
| 68 |
return "general"
|
| 69 |
|
| 70 |
-
def perform_web_search(user_text, max_results=
|
| 71 |
"""
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
"""
|
| 74 |
try:
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
for
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 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 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 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
|
| 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": []}
|