Spaces:
Sleeping
Sleeping
Update tools_engine.py
Browse files- tools_engine.py +36 -42
tools_engine.py
CHANGED
|
@@ -1,76 +1,70 @@
|
|
| 1 |
"""
|
| 2 |
-
Nexari Tools Engine (
|
| 3 |
Author: Piyush
|
| 4 |
-
Description:
|
| 5 |
"""
|
| 6 |
|
| 7 |
from duckduckgo_search import DDGS
|
| 8 |
import wikipedia
|
| 9 |
from transformers import pipeline
|
| 10 |
|
| 11 |
-
print(">>> Tools: Loading Intent Classification Model
|
| 12 |
-
#
|
| 13 |
-
# "typeform/distilbert-base-uncased-mnli" is fast and accurate for intents
|
| 14 |
intent_classifier = pipeline("zero-shot-classification", model="typeform/distilbert-base-uncased-mnli")
|
| 15 |
|
| 16 |
def analyze_intent(user_text):
|
| 17 |
"""
|
| 18 |
-
|
| 19 |
-
Returns: 'internet_search' or 'general_chat'
|
| 20 |
"""
|
| 21 |
-
# Candidates labels defines what the AI looks for
|
| 22 |
candidate_labels = ["internet search", "general conversation", "coding request", "mathematical calculation"]
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
return "general_chat"
|
| 37 |
|
| 38 |
-
def
|
| 39 |
"""
|
| 40 |
-
|
|
|
|
| 41 |
"""
|
| 42 |
-
|
| 43 |
-
# === STEP 1: NEURAL INTENT CHECK ===
|
| 44 |
-
intent = analyze_intent(user_text)
|
| 45 |
-
|
| 46 |
-
# Agar AI ko lagta hai ki ye search nahi hai, to khali laut jao
|
| 47 |
-
if intent != "internet search":
|
| 48 |
-
return ""
|
| 49 |
-
|
| 50 |
-
# === STEP 2: PERFORM SEARCH ===
|
| 51 |
try:
|
| 52 |
-
# Query Cleaning
|
| 53 |
-
clean_query = user_text
|
| 54 |
-
remove_phrases = ["search for", "google", "find", "tell me about", "what is", "do you know", "latest info on"]
|
| 55 |
for phrase in remove_phrases:
|
| 56 |
-
clean_query = clean_query.
|
| 57 |
|
| 58 |
clean_query = clean_query.strip()
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
print(f">>> Tool: Searching for '{clean_query}'...")
|
| 62 |
results = DDGS().text(clean_query, max_results=3)
|
| 63 |
|
| 64 |
if results:
|
| 65 |
search_summary = "\n".join([f"- {r['title']}: {r['body']}" for r in results])
|
| 66 |
return (
|
| 67 |
f"### REAL-TIME INTERNET DATA ###\n"
|
| 68 |
-
f"
|
|
|
|
| 69 |
f"Results:\n{search_summary}\n\n"
|
| 70 |
-
f"INSTRUCTION: Use the above
|
| 71 |
)
|
|
|
|
|
|
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
-
print(f"Tool Error: {e}")
|
| 75 |
-
|
| 76 |
-
return ""
|
|
|
|
| 1 |
"""
|
| 2 |
+
Nexari Tools Engine (Optimized)
|
| 3 |
Author: Piyush
|
| 4 |
+
Description: Efficient Neural Intent Detection & Direct Search Execution.
|
| 5 |
"""
|
| 6 |
|
| 7 |
from duckduckgo_search import DDGS
|
| 8 |
import wikipedia
|
| 9 |
from transformers import pipeline
|
| 10 |
|
| 11 |
+
print(">>> Tools: Loading Intent Classification Model...")
|
| 12 |
+
# Load model once into memory
|
|
|
|
| 13 |
intent_classifier = pipeline("zero-shot-classification", model="typeform/distilbert-base-uncased-mnli")
|
| 14 |
|
| 15 |
def analyze_intent(user_text):
|
| 16 |
"""
|
| 17 |
+
Decides the intent: 'internet search', 'coding request', or 'general conversation'.
|
|
|
|
| 18 |
"""
|
|
|
|
| 19 |
candidate_labels = ["internet search", "general conversation", "coding request", "mathematical calculation"]
|
| 20 |
|
| 21 |
+
try:
|
| 22 |
+
result = intent_classifier(user_text, candidate_labels)
|
| 23 |
+
top_intent = result['labels'][0]
|
| 24 |
+
confidence = result['scores'][0]
|
| 25 |
+
|
| 26 |
+
print(f">>> Intent: {top_intent} ({confidence:.2f})")
|
| 27 |
+
|
| 28 |
+
if confidence > 0.4:
|
| 29 |
+
return top_intent
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"Intent Error: {e}")
|
| 32 |
+
|
| 33 |
return "general_chat"
|
| 34 |
|
| 35 |
+
def perform_web_search(user_text):
|
| 36 |
"""
|
| 37 |
+
Directly executes search without re-analyzing intent.
|
| 38 |
+
Called specifically when app.py decides to search.
|
| 39 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
try:
|
| 41 |
+
# Query Cleaning
|
| 42 |
+
clean_query = user_text.lower()
|
| 43 |
+
remove_phrases = ["search for", "google", "find", "tell me about", "what is", "do you know", "latest info on", "news about"]
|
| 44 |
for phrase in remove_phrases:
|
| 45 |
+
clean_query = clean_query.replace(phrase, "")
|
| 46 |
|
| 47 |
clean_query = clean_query.strip()
|
| 48 |
+
|
| 49 |
+
# Fallback if query is empty
|
| 50 |
+
if len(clean_query) < 2:
|
| 51 |
+
clean_query = user_text
|
| 52 |
|
| 53 |
+
print(f">>> Tool: Searching DuckDuckGo for '{clean_query}'...")
|
| 54 |
results = DDGS().text(clean_query, max_results=3)
|
| 55 |
|
| 56 |
if results:
|
| 57 |
search_summary = "\n".join([f"- {r['title']}: {r['body']}" for r in results])
|
| 58 |
return (
|
| 59 |
f"### REAL-TIME INTERNET DATA ###\n"
|
| 60 |
+
f"Source: DuckDuckGo Web Search\n"
|
| 61 |
+
f"Topic: {clean_query}\n"
|
| 62 |
f"Results:\n{search_summary}\n\n"
|
| 63 |
+
f"INSTRUCTION: Use the above results to answer. If results are irrelevant, rely on your memory."
|
| 64 |
)
|
| 65 |
+
else:
|
| 66 |
+
return "" # No results found
|
| 67 |
|
| 68 |
except Exception as e:
|
| 69 |
+
print(f"Search Tool Error: {e}")
|
| 70 |
+
return ""
|
|
|