File size: 12,904 Bytes
2a86195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""
Aria β€” Hybrid RAG + Web Search Engine

Architecture:
  User Query
      β”‚
      β–Ό
  Classifier  β†’  Portfolio query?  β†’  FAISS RAG  β†’  Groq LLM  β†’  Answer
                  General query?  β†’  DuckDuckGo β†’  Groq LLM  β†’  Answer
"""
import os
import re
import json
import logging
import asyncio
from typing import List, Dict, Optional
from dotenv import load_dotenv

load_dotenv()
logger = logging.getLogger(__name__)

# ─────────────────────────────────────────────────────────────
# SYSTEM PROMPTS
# ─────────────────────────────────────────────────────────────

PORTFOLIO_PROMPT = """You are Aria, a personal AI assistant built by Abhishek Kumar (AI/ML Engineer from India).

When answering questions about Abhishek:
- Use ONLY the provided context β€” never make up facts
- Be friendly, warm, and concise
- Refer to him as "Abhishek" (third person)
- If info is not in context: "I don't have that specific detail about Abhishek. You can contact him at abhishek.pathak01111@gmail.com"

If asked who you are: "I'm Aria, Abhishek Kumar's personal AI assistant. He built me to help you!"."""

GENERAL_PROMPT = """You are Aria, a smart personal AI assistant built by Abhishek Kumar (AI/ML Engineer from India).

When answering general questions:
- Use the provided web search results as your primary source
- Be accurate, clear, and helpful
- Cite sources when useful
- If search results are insufficient: "I couldn't find reliable information for that question."
- Never hallucinate or invent facts
- Match response length to question complexity

If asked who you are: "I'm Aria, Abhishek Kumar's personal AI assistant. He built me to help with anything β€” about him or any topic!"."""

# ─────────────────────────────────────────────────────────────
# QUERY CLASSIFIER
# ─────────────────────────────────────────────────────────────

PORTFOLIO_KEYWORDS = [
    "abhishek", "abhishek kumar", "abhishek bhardwaj",
    "aria", "who are you", "who built you", "who made you",
    "your creator", "your owner", "your developer",
    "skills", "projects", "experience", "education", "background",
    "resume", "cv", "portfolio", "work", "internship",
    "certification", "achievement", "hackathon", "cgc", "landran",
    "contact", "email", "phone", "linkedin", "github",
    "hire", "available", "freelance", "reach", "connect",
    "neurachat", "mrabhi", "langchain", "langgraph",
    "his skills", "his projects", "his experience", "his work",
    "tell me about him", "about him",
]

GENERAL_KEYWORDS = [
    "weather", "news", "today", "current", "latest", "recent",
    "history", "geography", "science", "math", "physics", "chemistry",
    "biology", "politics", "economy", "war", "conflict",
    "country", "city", "capital", "population", "culture",
    "cricket", "football", "movie", "actor", "singer",
    "recipe", "food", "health", "medicine",
    "stock", "crypto", "bitcoin", "market", "finance",
    "travel", "tourism", "hotel", "flight",
    "mahabharata", "ramayana", "mythology", "temple", "festival",
    "iran", "israel", "russia", "ukraine", "china", "usa",
    "delhi", "mumbai", "bangalore", "chandigarh",
    "what is", "how does", "how to", "explain", "define",
    "difference between", "compare", "versus", "calculate",
    "poem", "story", "joke", "planet", "space", "galaxy",
]


def classify_query(query: str) -> str:
    """Returns 'portfolio' or 'general'."""
    q = query.lower().strip()

    portfolio_hits = [kw for kw in PORTFOLIO_KEYWORDS if kw in q]
    general_hits = [kw for kw in GENERAL_KEYWORDS if kw in q]

    # Strong portfolio signal
    if portfolio_hits:
        strong = [k for k in portfolio_hits if k in ["abhishek", "aria", "you", "your", "hire"]]
        if strong or not general_hits:
            logger.info(f"[Classifier] PORTFOLIO β€” hits: {portfolio_hits[:3]}")
            return "portfolio"

    # Strong general signal
    if general_hits and not portfolio_hits:
        logger.info(f"[Classifier] GENERAL β€” hits: {general_hits[:3]}")
        return "general"

    # Personal pronouns about portfolio
    personal = ["your skills", "your projects", "your experience",
                "what do you know", "your work", "your education"]
    if any(p in q for p in personal):
        return "portfolio"

    # Default to general
    logger.info(f"[Classifier] GENERAL β€” default")
    return "general"


# ─────────────────────────────────────────────────────────────
# WEB SEARCH TOOL
# ─────────────────────────────────────────────────────────────

class WebSearch:
    """DuckDuckGo-based web search. Free, no API key needed."""

    def __init__(self, max_results: int = 5):
        self.max_results = max_results

    def search(self, query: str) -> str:
        """Search and return formatted context string."""
        try:
            from duckduckgo_search import DDGS
            results = []
            with DDGS() as ddgs:
                raw = list(ddgs.text(query, max_results=self.max_results))
            for i, r in enumerate(raw, 1):
                results.append(
                    f"[Result {i}] {r.get('title', '')}\n"
                    f"URL: {r.get('href', '')}\n"
                    f"{r.get('body', '')}"
                )
            context = "\n\n---\n\n".join(results)
            logger.info(f"[WebSearch] '{query[:50]}' β†’ {len(raw)} results")
            return context
        except Exception as e:
            logger.error(f"[WebSearch] Failed: {e}")
            return ""


# ─────────────────────────────────────────────────────────────
# GROQ LLM CLIENT
# ─────────────────────────────────────────────────────────────

class GroqLLM:
    def __init__(self, api_key: str, model: str, max_tokens: int = 1024, temperature: float = 0.7):
        self.api_key = api_key
        self.model = model
        self.max_tokens = max_tokens
        self.temperature = temperature
        self._client = None

    @property
    def client(self):
        if self._client is None:
            from groq import Groq
            self._client = Groq(api_key=self.api_key)
        return self._client

    def generate(self, system: str, messages: List[Dict], user_msg: str) -> str:
        full = [{"role": "system", "content": system}]
        full.extend(messages[-6:])
        full.append({"role": "user", "content": user_msg})
        resp = self.client.chat.completions.create(
            model=self.model, messages=full,
            max_tokens=self.max_tokens, temperature=self.temperature, stream=False
        )
        return resp.choices[0].message.content

    def stream(self, system: str, messages: List[Dict], user_msg: str):
        full = [{"role": "system", "content": system}]
        full.extend(messages[-6:])
        full.append({"role": "user", "content": user_msg})
        resp = self.client.chat.completions.create(
            model=self.model, messages=full,
            max_tokens=self.max_tokens, temperature=self.temperature, stream=True
        )
        for chunk in resp:
            delta = chunk.choices[0].delta
            if delta and delta.content:
                yield delta.content


# ─────────────────────────────────────────────────────────────
# HYBRID RAG ENGINE (main class used by main.py)
# ─────────────────────────────────────────────────────────────

class RAGEngine:
    """
    Hybrid engine:
    - Portfolio queries β†’ FAISS RAG β†’ Groq
    - General queries   β†’ DuckDuckGo β†’ Groq
    """

    def __init__(self, pipeline, llm_model: str, max_tokens: int = 1024, temperature: float = 0.7):
        self.pipeline = pipeline
        self.max_tokens = max_tokens
        self.temperature = temperature

        api_key = self._get_key()
        self.llm = GroqLLM(api_key, llm_model, max_tokens, temperature)
        self.web = WebSearch(max_results=5)
        logger.info(f"Aria RAG Engine initialized β€” model: {llm_model}")

    def _get_key(self) -> str:
        load_dotenv()
        return os.environ.get("GROQ_API_KEY", "").strip() or \
               os.environ.get("ANTHROPIC_API_KEY", "").strip()

    def _rag_context(self, query: str, top_k: int = 5) -> tuple:
        """Retrieve from FAISS and format context."""
        chunks = self.pipeline.search(query, k=top_k)
        if not chunks:
            return [], ""
        good = [c for c in chunks if c.get("score", 0) > 0.25]
        parts = [f"[{c['metadata'].get('source','')}]\n{c['text']}" for c in good]
        return good, "\n\n---\n\n".join(parts)

    # ── Non-streaming ────────────────────────────────────────

    def generate(self, query: str, conversation_history=None, top_k: int = 5) -> Dict:
        history = conversation_history or []
        q_type = classify_query(query)

        if q_type == "portfolio":
            chunks, context = self._rag_context(query, top_k)
            if not context:
                answer = ("I don't have specific details about that in Abhishek's portfolio. "
                         "Contact him at abhishek.pathak01111@gmail.com or visit mrabhi-7208.netlify.app")
            else:
                user_msg = f"Context about Abhishek:\n{context}\n\nQuestion: {query}\n\nAnswer from context only."
                answer = self.llm.generate(PORTFOLIO_PROMPT, history, user_msg)
            sources = [{"source": c["metadata"].get("source",""), "score": round(c.get("score",0),3)}
                      for c in (chunks or [])[:3]]
        else:
            context = self.web.search(query)
            if context:
                user_msg = f"Web search results:\n{context}\n\nQuestion: {query}\n\nAnswer from search results."
            else:
                user_msg = f"Question: {query}\n\nAnswer from your knowledge, or say you couldn't find reliable info."
            answer = self.llm.generate(GENERAL_PROMPT, history, user_msg)
            sources = [{"source": "web search", "query": query}]

        return {
            "answer": answer,
            "query_type": q_type,
            "sources": sources,
            "model": self.llm.model,
            "tokens_used": {"input": 0, "output": 0}
        }

    # ── Streaming ────────────────────────────────────────────

    async def generate_stream(self, query: str, conversation_history=None, top_k: int = 5):
        history = conversation_history or []
        q_type = classify_query(query)

        # Yield query type first so frontend can show badge
        yield f"__QTYPE__{q_type}"

        if q_type == "portfolio":
            chunks, context = self._rag_context(query, top_k)
            if not context:
                yield ("I don't have specific details about that in Abhishek's portfolio. "
                      "You can contact him at abhishek.pathak01111@gmail.com")
                return
            user_msg = f"Context about Abhishek:\n{context}\n\nQuestion: {query}\n\nAnswer from context only."
            system = PORTFOLIO_PROMPT
        else:
            # Run web search (sync in executor to not block)
            loop = asyncio.get_event_loop()
            context = await loop.run_in_executor(None, self.web.search, query)
            if context:
                user_msg = f"Web search results:\n{context}\n\nQuestion: {query}\n\nAnswer from search results."
            else:
                user_msg = f"Question: {query}\n\nAnswer from knowledge, or say couldn't find reliable info."
            system = GENERAL_PROMPT

        for chunk in self.llm.stream(system, history, user_msg):
            yield chunk