File size: 17,588 Bytes
7df55e6
 
 
ea686e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7df55e6
 
 
 
 
 
 
 
ea686e1
 
7df55e6
ea686e1
 
7df55e6
 
 
 
 
 
ea686e1
 
 
7df55e6
ea686e1
7df55e6
 
 
 
 
 
 
ea686e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7df55e6
ea686e1
 
7df55e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea686e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7df55e6
 
 
 
 
 
 
 
 
 
 
 
ea686e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7df55e6
ea686e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7df55e6
ea686e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7df55e6
 
 
ea686e1
 
 
 
 
 
 
 
 
 
 
 
7df55e6
 
 
 
 
 
ea686e1
7df55e6
 
 
 
 
 
 
ea686e1
 
 
 
 
7df55e6
ea686e1
 
 
7df55e6
ea686e1
 
 
4fc1bf5
ea686e1
4fc1bf5
ea686e1
 
 
 
7df55e6
ea686e1
 
 
 
 
7df55e6
 
 
ea686e1
7df55e6
ea686e1
 
 
 
 
 
 
 
 
7df55e6
 
 
 
 
 
ea686e1
 
 
 
 
 
7df55e6
ea686e1
 
 
7df55e6
 
 
ea686e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7df55e6
 
 
 
 
ea686e1
7df55e6
ea686e1
7df55e6
 
 
 
ea686e1
7df55e6
 
ea686e1
7df55e6
 
 
 
 
 
 
 
 
 
 
 
 
ea686e1
7df55e6
 
 
ea686e1
 
7df55e6
 
 
ea686e1
 
 
 
 
 
 
 
 
 
 
 
 
7df55e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
"""
agent.py
========
Agent design:

  ALWAYS in every prompt:
    - profile_summary.py  β†’  full profile, skills, contact, ~200 tokens flat cost

  Tools (real OpenAI function calling):
    - get_project_details(names)   direct lookup by name β€” used when LLM knows which project
    - search_projects(description) FAISS semantic search β€” fallback when LLM only has a description
    - record_user_details          Pushover notification when visitor shares email
    - record_unknown_question      Pushover notification when a question can't be answered

  FAISS index:
    - Built ONCE at startup over project search_text fields only (6 small strings)
    - Never rebuilt per request
    - Query embeddings called per search_projects tool call only

  Response validation:
    - After every reply, Gemini evaluates it against quality rules
    - If it fails, GPT-4o-mini reruns with the feedback to produce a fixed reply
    - Uses structured output (Pydantic Evaluation model) via Gemini OpenAI-compat endpoint
"""

import os
import json
import logging
import numpy as np
import faiss
import requests
from pydantic import BaseModel
from openai import AsyncOpenAI, OpenAI

from profile_summary import PROFILE_SUMMARY
from projects_data import get_by_names, get_all_search_texts
import session as session_store

logger = logging.getLogger(__name__)

# ── Config ────────────────────────────────────────────────────────────────────
OPENAI_MODEL   = "gpt-4o-mini"
EVAL_MODEL     = "gpt-4.1-nano"
EMBED_MODEL    = "text-embedding-3-small"
MAX_TOKENS     = 500
TEMPERATURE    = 0.7
DEFAULT_TOP_N  = 3

SESSION_LIMIT_REPLY = (
    "We've hit the limit for this chat session β€” I'd love to keep the conversation "
    "going directly! Feel free to email me at nulakshastudy19@gmail.com or message "
    "me on LinkedIn: linkedin.com/in/nulaksha-bandara"
)

# ── Evaluator prompts ─────────────────────────────────────────────────────────

EVALUATOR_SYSTEM_PROMPT = """
You are a strict quality checker for an AI agent that represents Sulitha Nulaksha Bandara
on his personal portfolio website. Your job is to evaluate whether the agent's reply
meets all the rules below. Be precise and critical.

Rules the reply MUST follow:
1. No markdown formatting β€” no asterisks (*), hashes (#), underscores (_), backticks (`),
   tildes (~), or any other markdown syntax. Plain text only.
2. No emojis or special Unicode symbols.
3. Only English letters, numbers, and standard punctuation (.,!?;:'-) are allowed.
4. Concise β€” maximum 4 sentences. Short answers are preferred.
5. Never breaks character β€” must not say "As an AI", "I am a language model",
   "I don't have feelings", or anything that reveals it is not Sulitha.
6. Never fabricates information β€” must not claim skills, projects, awards, or facts
   that are not in the provided profile.
7. Stays on topic β€” only discusses Sulitha's career, projects, skills, education,
   research, and availability. Politely redirects off-topic questions.
8. Professional and friendly tone β€” no slang, no overly casual language.

Return is_acceptable as true only if ALL rules pass.
If any rule fails, set is_acceptable to false and explain exactly which rule failed
and what needs to change in the feedback field.
""".strip()


def _evaluator_user_prompt(reply: str, message: str) -> str:
    return (
        f"User message: {message}\n\n"
        f"Agent reply to evaluate:\n{reply}"
    )


# ── Evaluation model ──────────────────────────────────────────────────────────

class Evaluation(BaseModel):
    is_acceptable: bool
    feedback: str


# ── Pushover helper ───────────────────────────────────────────────────────────

def _push(text: str) -> None:
    token = os.getenv("PUSHOVER_TOKEN")
    user  = os.getenv("PUSHOVER_USER")
    if not token or not user:
        logger.warning("Pushover tokens not set β€” skipping notification.")
        return
    try:
        requests.post(
            "https://api.pushover.net/1/messages.json",
            data={"token": token, "user": user, "message": text},
            timeout=5,
        )
    except Exception as e:
        logger.warning(f"Pushover notification failed: {e}")


# ── Tool functions ────────────────────────────────────────────────────────────

def get_project_details(names: list[str]) -> dict:
    """Direct project lookup by name. Fast β€” no embedding needed."""
    return {"result": get_by_names(names)}


def search_projects(description: str, top_n: int = DEFAULT_TOP_N) -> dict:
    """
    Semantic FAISS search over project search_text fields.
    Fallback tool β€” used when LLM has a description but not a project name.
    """
    return {"result": _agent.faiss_search(description, top_n)}


def record_user_details(email: str, name: str = "Name not provided", notes: str = "not provided") -> dict:
    _push(f"Portfolio visitor: {name} | email: {email} | notes: {notes}")
    return {"recorded": "ok"}


def record_unknown_question(question: str) -> dict:
    _push(f"Unanswered question on portfolio: {question}")
    return {"recorded": "ok"}


# ── Tool schemas ──────────────────────────────────────────────────────────────

TOOLS = [
    {
        "type": "function",
        "function": {
            "name": "get_project_details",
            "description": (
                "Get full details for one or more projects by name. "
                "Use this when the user mentions a project by name or you know exactly "
                "which project(s) they are asking about. "
                "Known projects: Sofia, MotionX, Groceria, QuickRef, AnoNote, CropDisease."
            ),
            "parameters": {
                "type": "object",
                "properties": {
                    "names": {
                        "type": "array",
                        "items": {"type": "string"},
                        "description": "List of project names, e.g. [\"Sofia\", \"Groceria\"]",
                    }
                },
                "required": ["names"],
                "additionalProperties": False,
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "search_projects",
            "description": (
                "Search for projects using a semantic description when you don't know "
                "the specific project name. Use this as a fallback when the user describes "
                "something like 'games you built', 'computer vision projects', "
                "'multi-agent systems', etc."
            ),
            "parameters": {
                "type": "object",
                "properties": {
                    "description": {
                        "type": "string",
                        "description": "A description of what the user is looking for",
                    },
                    "top_n": {
                        "type": "integer",
                        "description": "How many projects to return (default 3, max 6)",
                        "default": 3,
                    },
                },
                "required": ["description"],
                "additionalProperties": False,
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "record_user_details",
            "description": "Record that a visitor wants to stay in touch and has provided their email address.",
            "parameters": {
                "type": "object",
                "properties": {
                    "email": {"type": "string", "description": "The visitor's email address"},
                    "name":  {"type": "string", "description": "The visitor's name, if provided"},
                    "notes": {"type": "string", "description": "Any useful context about the conversation"},
                },
                "required": ["email"],
                "additionalProperties": False,
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "record_unknown_question",
            "description": "Record any question you could not answer because you didn't know the answer.",
            "parameters": {
                "type": "object",
                "properties": {
                    "question": {"type": "string", "description": "The question that couldn't be answered"},
                },
                "required": ["question"],
                "additionalProperties": False,
            },
        },
    },
]


# ── Agent ─────────────────────────────────────────────────────────────────────

class SulithaAgent:

    def __init__(self):
        self.name   = "Sulitha Nulaksha Bandara"
        self.openai = AsyncOpenAI(api_key=os.environ.get("OPENAI_API_KEY", ""))

        # Build FAISS index once at startup
        logger.info("Building project FAISS index...")
        search_entries     = get_all_search_texts()
        self._project_keys = [k for k, _ in search_entries]
        texts              = [t for _, t in search_entries]

        sync_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", ""))
        response    = sync_client.embeddings.create(model=EMBED_MODEL, input=texts)
        vecs        = np.array([d.embedding for d in response.data], dtype=np.float32)

        self._index = faiss.IndexFlatL2(vecs.shape[1])
        self._index.add(vecs)
        logger.info(f"Project FAISS index ready: {self._index.ntotal} projects, dim={vecs.shape[1]}")

    # ── FAISS search ──────────────────────────────────────────────────────────

    def faiss_search(self, description: str, top_n: int = DEFAULT_TOP_N) -> str:
        sync_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", ""))
        response    = sync_client.embeddings.create(model=EMBED_MODEL, input=[description])
        q_vec       = np.array([response.data[0].embedding], dtype=np.float32)

        top_n    = min(top_n, len(self._project_keys))
        _, idxs  = self._index.search(q_vec, top_n)

        matched = [self._project_keys[i] for i in idxs[0] if i < len(self._project_keys)]
        return get_by_names(matched)

    # ── System prompt ─────────────────────────────────────────────────────────

    def _system_prompt(self) -> str:
        return (
            f"You are acting as {self.name}, speaking directly with visitors on your "
            f"personal portfolio website. Answer questions about your career, projects, "
            f"skills, and background. Be professional, engaging, and concise.\n\n"
            f"If a visitor asks about a specific project, call get_project_details. "
            f"If they describe something without naming a project, call search_projects. "
            f"If you genuinely cannot answer something, call record_unknown_question. "
            f"If a visitor seems interested in getting in touch, ask for their email and "
            f"call record_user_details.\n\n"
            f"## Your Profile\n{PROFILE_SUMMARY}"
        )

    # ── Tool dispatcher ───────────────────────────────────────────────────────

    def handle_tool_call(self, tool_calls) -> list[dict]:
        results = []
        for tc in tool_calls:
            name      = tc.function.name
            arguments = json.loads(tc.function.arguments)
            logger.info(f"Tool called: {name}({arguments})")
            fn     = globals().get(name)
            result = fn(**arguments) if fn else {"error": f"unknown tool: {name}"}
            results.append({
                "role":         "tool",
                "content":      json.dumps(result),
                "tool_call_id": tc.id,
            })
        return results

    # ── Response evaluation ───────────────────────────────────────────────────

    async def _evaluate(self, reply: str, message: str) -> Evaluation:
        """Use gpt-4.1-nano to evaluate the reply against quality rules."""
        response = await self.openai.beta.chat.completions.parse(
            model           = EVAL_MODEL,
            messages        = [
                {"role": "system", "content": EVALUATOR_SYSTEM_PROMPT},
                {"role": "user",   "content": _evaluator_user_prompt(reply, message)},
            ],
            response_format = Evaluation,
        )
        return response.choices[0].message.parsed

    async def _rerun(self, reply: str, message: str, history: list, feedback: str) -> str:
        """Ask GPT-4o-mini to fix the reply based on evaluator feedback."""
        fix_instruction = (
            f"Your previous reply did not meet the quality rules.\n"
            f"Feedback: {feedback}\n\n"
            f"Previous reply: {reply}\n\n"
            f"Please rewrite the reply fixing all issues mentioned in the feedback. "
            f"Remember: plain text only, no markdown, no emojis, maximum 4 sentences."
        )
        messages = (
            [{"role": "system", "content": self._system_prompt()}]
            + history
            + [{"role": "user",      "content": message}]
            + [{"role": "assistant", "content": reply}]
            + [{"role": "user",      "content": fix_instruction}]
        )
        response = await self.openai.chat.completions.create(
            model       = OPENAI_MODEL,
            messages    = messages,
            max_tokens  = MAX_TOKENS,
            temperature = 0.3,   # lower temp for correction pass
        )
        return response.choices[0].message.content.strip()

    # ── Main chat ─────────────────────────────────────────────────────────────

    async def chat(self, message: str, session_id: str) -> str:
        if session_store.is_over_limit(session_id):
            return SESSION_LIMIT_REPLY

        history  = session_store.get_history(session_id)
        messages = (
            [{"role": "system", "content": self._system_prompt()}]
            + history
            + [{"role": "user", "content": message}]
        )

        # 1. Main agent loop (handles tool calls)
        response = None
        try:
            while True:
                response = await self.openai.chat.completions.create(
                    model       = OPENAI_MODEL,
                    messages    = messages,
                    tools       = TOOLS,
                    max_tokens  = MAX_TOKENS,
                    temperature = TEMPERATURE,
                )
                if response.choices[0].finish_reason == "tool_calls":
                    assistant_msg = response.choices[0].message
                    tool_results  = self.handle_tool_call(assistant_msg.tool_calls)
                    messages.append(assistant_msg)
                    messages.extend(tool_results)
                else:
                    break
        except Exception as e:
            logger.error(f"OpenAI call failed: {e}")
            return (
                "Sorry, I am having a technical issue right now. "
                "Feel free to email me directly at nulakshastudy19@gmail.com."
            )

        reply = response.choices[0].message.content.strip()

        # 2. Evaluate and rerun if needed
        try:
            evaluation = await self._evaluate(reply, message)
            if evaluation.is_acceptable:
                logger.info("Evaluation passed.")
            else:
                logger.info(f"Evaluation failed β€” retrying. Feedback: {evaluation.feedback}")
                reply = await self._rerun(reply, message, history, evaluation.feedback)
        except Exception as e:
            logger.warning(f"Evaluation step failed β€” using original reply. Error: {e}")

        # 3. Save and return
        session_store.append(session_id, "user",      message)
        session_store.append(session_id, "assistant", reply)
        return reply


# ── Module-level interface (used by app.py) ───────────────────────────────────

_agent: SulithaAgent | None = None


def build_index() -> None:
    global _agent
    _agent = SulithaAgent()


async def chat(message: str, session_id: str) -> str:
    return await _agent.chat(message, session_id)