File size: 2,556 Bytes
550bae8
 
aebecbc
c869cda
550bae8
 
 
 
 
 
c869cda
550bae8
aebecbc
 
550bae8
 
 
 
 
 
aebecbc
163ed47
550bae8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6aa1326
323191f
 
 
 
 
 
 
98e4c13
 
81473e3
98e4c13
 
aebecbc
98e4c13
 
 
323191f
 
 
98e4c13
 
81473e3
35c38d8
c949f11
98e4c13
35c38d8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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": []}