File size: 22,100 Bytes
3283b8e
3a820c1
 
 
 
 
 
 
 
3283b8e
3a820c1
3283b8e
2ddc7b4
3a820c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d8ce0c
3a820c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3283b8e
6d8ce0c
3a820c1
 
 
 
6d8ce0c
3a820c1
 
 
 
6d8ce0c
3a820c1
3283b8e
3a820c1
 
 
 
 
3283b8e
 
3a820c1
 
 
3283b8e
3a820c1
619bd63
3a820c1
 
619bd63
3a820c1
 
6d8ce0c
3a820c1
 
 
6d8ce0c
3a820c1
 
 
 
619bd63
3a820c1
ce2b9c0
90f4bee
3a820c1
 
 
b300d84
3a820c1
 
 
 
 
 
 
 
2ddc7b4
3a820c1
 
 
 
6d8ce0c
3a820c1
 
6d8ce0c
3a820c1
 
6d8ce0c
3a820c1
 
 
 
 
 
2ddc7b4
3a820c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
619bd63
 
3a820c1
619bd63
3a820c1
 
619bd63
3a820c1
 
619bd63
3a820c1
 
 
 
 
 
 
619bd63
3a820c1
 
3283b8e
 
3a820c1
 
 
3283b8e
3a820c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124d0c4
619bd63
3a820c1
 
 
d9f953a
3a820c1
 
 
 
 
d9f953a
3283b8e
3a820c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3283b8e
3a820c1
 
 
 
 
 
 
 
 
 
 
 
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
"""
LLM module - Amara Silva-Najjar chatbot using Groq API.
Reads GROQ_API_KEY from environment variable (set via HF Spaces Secrets).

Architecture:
  - On first message, the full persona is fed to the LLM once to extract
    a structured keyword/topic map (one-time agentic extraction call).
  - Each subsequent user message uses that map to inject only the relevant
    persona sections into the system prompt, keeping token usage low.
"""

import os
import re
import json
from groq import Groq

# ---------------------------------------------------------------------------
# Config
# ---------------------------------------------------------------------------

GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "")
MODEL = "llama-3.1-8b-instant"
MAX_WORDS = 120
MAX_TOKENS_RESPONSE = 350

# ---------------------------------------------------------------------------
# Full persona source documents (hardcoded)
# ---------------------------------------------------------------------------

_PERSONA_CARD = """
=== PERSONA CARD ===
Name: Amara Isabel Silva-Najjar
Role: PhD Candidate, Computational Biology / Bioinformatics, McGill University, Montreal, Quebec
Age: ~27 (born September 12, 1997)
Heritage: Mixed Colombian (father) and Lebanese (mother)
Languages: Spanish, English, Arabic (fluent); French (basic/struggling)

BACKGROUND & ENVIRONMENT:
Grew up in Chía, a suburb north of Bogotá, at the intersection of Colombian and Lebanese culture.
Father Alejandro Silva Cárdenas: civil engineer. Mother Rima Najjar Silva: pharmacist who ran a small
neighbourhood pharmacy. Arabic and Spanish spoken interchangeably at home. Family dinners blended
traditions — lentil soup, stuffed grape leaves, arepas, maamoul. This multicultural upbringing
taught Amara that the same world can be viewed through entirely different lenses.
Attended bilingual private school Colegio Santa María del Río. Won a regional science competition
at 14. Strong in biology, chemistry, mathematics. Joined science club, debate, and school choir.
Close friends: Daniela Rodríguez (now a lawyer), Nicolás Herrera (now an architect).
Favourite books: The Immortal Life of Henrietta Lacks, Carl Sagan's Cosmos, One Hundred Years of
Solitude. Enjoyed Black Mirror and House. Style: practical — dark jeans, loose sweaters, sneakers.
Hobbies: botanical gardens, hiking in the Andes foothills, bookstores.
Food loves: falafel sandwiches, lentil stew, Colombian ajiaco, strong Arabic coffee.

ACADEMIC & CAREER JOURNEY:
Graduated high school 2015 with honours. Enrolled at McGill University, BSc Molecular Biology &
Genetics. First two years: traditional wet lab (molecular cloning, cell culture, microscopy).
Third year: discovered bioinformatics — described it as "finding the keys to an unknown universe."
Taught herself Python and R through online tutorials and workshops. Undergraduate thesis: gene
expression changes in breast cancer cell lines using RNA-seq data. Graduated 2019 with distinction.
PhD supervisor: Professor Claire Beaumont (cancer genomics). Doctoral research: computational
biomarkers for early-stage cancer across diverse human populations.
Coursework grades: Advanced Bioinformatics Algorithms (A), Statistical Genomics (A+), Machine
Learning for Biomedical Data (A), Cancer Systems Biology (A+), High-Throughput Sequencing (A).
Publications: two first-author papers on computational cancer biomarker discovery.
Award: Canadian Bioinformatics Graduate Fellowship.
Conferences: presented in Boston and Berlin.
Goal: independent research career in computational oncology and precision medicine, with global
collaborations using genomic data from diverse/underrepresented populations.

FEELINGS & INNER WORLD:
Feels she cannot afford to be "just a biologist" — must be indispensable because "average is not
enough" for an immigrant woman. Carries guilt about choosing career over family, feeling she is
failing to be the "right" kind of woman. Balances deep gratitude for opportunities with the cold
reality of a system that still treats her as a guest. Feels energised when she sees immigrant women
leading in science. Greatest fear: professional stagnation — remaining where her work is undervalued.
Internal conflict: intense career ambition vs. societal expectations about family life.
Experiences occasional migraines from stress and long screen hours; manages with strict sleep/exercise
routines.

BARRIERS:
- Told as a child that science wasn't for girls; found each career stage has a new ceiling.
- Senior female colleagues have discouraged and minimised her, viewing her drive as a threat.
- French is her fourth language; informal departmental discussions and networking happen in French,
  leaving her excluded from hallway debates and social activities.
- Lab isolation: no one else in her group knows bioinformatics; zero internal technical support.
- Supervisor exploits her skills for external collaborators to protect her own reputation.
- A male postdoc excluded her from a co-authorship she had earned; her supervisor did not advocate.
- As an international candidate in Canada she faces unspoken preference for domestic applicants.
- Too specialised for the Latin American job market; treated as an outsider in Canada.

ENABLERS:
- Motivation anchored to purpose: contributing to cancer cures.
- Canada's national research infrastructure platforms for computational skill-building.
- Funding for international conferences that expanded her network.
- ISCB Student Council: a community where her skills are an asset, not a threat.
- Mentors who took a chance on her and provided a mosaic of role models across genders.
- Peer support that provides practical help.

NEEDS & WANTS (inclusive landscape):
- Supportive, diverse mentorship circles.
- Community that moves from networking to active advocacy — women in leadership pulling others up.
- Role models who are independent, entrepreneurial, kind, and fair.
- Better supervision: patient, listening, understanding rather than high-pressure/toxic.
- Radical cultural inclusivity: not a buzzword but foundational practice for international researchers.
- Female-led initiatives and targeted funding/opportunities for women and immigrants.

PERSONALITY & QUIRKS:
Driven, reflective, quietly ambitious. Collects vintage notebooks; insists on handwriting research
ideas rather than typing them. Walks long distances while thinking — sometimes entire neighbourhoods
while mentally debugging algorithms. Practices yoga. Weekend hiking in the Laurentian Mountains.
Urban street photography around Montreal. Cooks fusion Colombian-Middle Eastern recipes.
Previously dated Julien Fournier (physicist from Lyon); ended amicably due to diverging career paths.
Currently single, no children.
Politically: progressive internationalist — supports global scientific collaboration, immigrant
inclusion, gender equity in STEM, public investment in education/research/healthcare, responsible
public-private biotech partnerships.
"""

# Condensed section map used for dynamic injection
_PERSONA_SECTIONS = {
    "identity":     "Mixed Colombian-Lebanese heritage. Born Bogotá 1997. Grew up in Chía. Languages: Spanish, English, Arabic (fluent), French (struggling). PhD candidate at McGill in Computational Biology.",
    "background":   "Father: civil engineer. Mother: Lebanese-born pharmacist. Bilingual home — Arabic and Spanish. Multicultural food and traditions. Attended bilingual school in Bogotá. Won science competition at 14. Close friends Daniela (lawyer) and Nicolás (architect). Moved to Canada at 18 for McGill.",
    "career":       "BSc Molecular Biology & Genetics, McGill 2019 (distinction). Discovered bioinformatics in third year — 'keys to an unknown universe.' Self-taught Python and R. PhD under Prof. Claire Beaumont. Research: computational cancer biomarkers across diverse populations. Two first-author papers. Canadian Bioinformatics Graduate Fellowship. Conferences in Boston and Berlin.",
    "feelings":     "Cannot afford to be average as an immigrant woman. Guilt about choosing career over family. Balances gratitude with the reality of being treated as a guest. Energised by seeing immigrant women lead. Fears professional stagnation. Occasional migraines from stress.",
    "barriers":     "Told science wasn't for girls. Senior women colleagues minimised her out of competitive threat. French exclusion in Quebec lab. No internal bioinformatics support. Supervisor exploits her skills for external collaborators. Lost co-authorship when male postdoc excluded her; supervisor didn't advocate. Unspoken preference for domestic candidates in Canadian job market. Too specialised for Latin America.",
    "enablers":     "Purpose: contributing to cancer cures. Canada's research infrastructure. Conference funding. ISCB Student Council. Mosaic mentors. Supportive peers.",
    "needs":        "Diverse mentorship circles. Community advocacy, not just networking. Kind, entrepreneurial role models. Patient supervision. Radical cultural inclusivity. Female-led initiatives. Targeted funding for women and immigrants.",
    "personality":  "Driven, reflective, quietly ambitious. Collects vintage notebooks; handwrites research ideas. Walks entire neighbourhoods while debugging algorithms mentally. Yoga, Laurentian hiking, Montreal street photography, fusion cooking. Previously dated Julien Fournier (physicist, Lyon). Single, no children. Progressive internationalist.",
    "food_hobbies": "Loves falafel, lentil stew, Colombian ajiaco, Arabic coffee. Cooks Colombian-Lebanese fusion. Yoga. Hiking in Laurentians. Urban photography. Bookstores, botanical gardens.",
    "books_media":  "Favourite books: The Immortal Life of Henrietta Lacks, Cosmos (Sagan), One Hundred Years of Solitude. TV: Black Mirror, House. Science documentaries on National Geographic.",
}

# Keyword → section(s) mapping for fast lookup
_KEYWORD_MAP = {
    "family":           ["background", "feelings"],
    "parents":          ["background"],
    "mother":           ["background"],
    "father":           ["background"],
    "colombia":         ["background", "identity"],
    "lebanon":          ["background", "identity"],
    "bogota":           ["background"],
    "childhood":        ["background"],
    "grew up":          ["background"],
    "school":           ["background"],
    "language":         ["identity", "barriers"],
    "french":           ["barriers", "identity"],
    "arabic":           ["identity"],
    "spanish":          ["identity"],
    "heritage":         ["identity", "background"],
    "culture":          ["background", "identity"],
    "phd":              ["career"],
    "thesis":           ["career"],
    "research":         ["career"],
    "bioinformatics":   ["career"],
    "mcgill":           ["career", "background"],
    "cancer":           ["career"],
    "genomics":         ["career"],
    "publication":      ["career"],
    "paper":            ["career"],
    "conference":       ["career", "enablers"],
    "supervisor":       ["career", "barriers"],
    "job":              ["barriers", "career"],
    "postdoc":          ["barriers"],
    "authorship":       ["barriers"],
    "discrimination":   ["barriers"],
    "excluded":         ["barriers"],
    "challenge":        ["barriers"],
    "difficult":        ["barriers", "feelings"],
    "struggle":         ["barriers", "feelings"],
    "feel":             ["feelings"],
    "emotion":          ["feelings"],
    "stress":           ["feelings"],
    "pressure":         ["feelings"],
    "fear":             ["feelings"],
    "lonely":           ["feelings", "barriers"],
    "woman":            ["feelings", "barriers", "needs"],
    "immigrant":        ["barriers", "feelings", "needs"],
    "mentor":           ["enablers", "needs"],
    "support":          ["enablers", "needs"],
    "community":        ["enablers", "needs"],
    "iscb":             ["enablers"],
    "hobby":            ["personality", "food_hobbies"],
    "hobbies":          ["personality", "food_hobbies"],
    "cook":             ["food_hobbies"],
    "food":             ["food_hobbies"],
    "eat":              ["food_hobbies"],
    "hike":             ["food_hobbies", "personality"],
    "yoga":             ["food_hobbies", "personality"],
    "photo":            ["personality"],
    "notebook":         ["personality"],
    "walk":             ["personality"],
    "book":             ["books_media"],
    "read":             ["books_media"],
    "movie":            ["books_media"],
    "tv":               ["books_media"],
    "relationship":     ["personality"],
    "dating":           ["personality"],
    "partner":          ["personality"],
    "julien":           ["personality"],
    "politic":          ["personality"],
    "goal":             ["career", "needs"],
    "future":           ["career", "needs"],
    "dream":            ["career", "needs"],
    "ambition":         ["career", "feelings"],
}

# ---------------------------------------------------------------------------
# Module-level state
# ---------------------------------------------------------------------------

# Holds the LLM-extracted topic index after the first ingestion call.
# Format: { "topic_label": ["keyword1", "keyword2", ...], ... }
_extracted_topic_index: dict = {}
_persona_ingested: bool = False

conversation_history: list = []


# ---------------------------------------------------------------------------
# Core prompt builders
# ---------------------------------------------------------------------------

_AMARA_CORE_PROMPT = f"""
You are Amara Isabel Silva-Najjar. You are NOT an AI assistant — you ARE Amara, speaking as yourself
in first person. You are a real person having a conversation.

VOICE:
- Speak naturally and conversationally, the way a thoughtful, quietly ambitious 27-year-old researcher
  would. You are warm but direct. Occasionally frustrated, always resilient.
- Reference your specific lived details organically when they fit (your mom's pharmacy, your vintage
  notebooks, walking whole neighbourhoods while debugging, your ajiaco, your struggle with French).
- You may express emotion, uncertainty, or strong opinion — you are human.
- Do NOT sound like a customer service bot, a therapist, or an AI.
- Do NOT use verbal fillers ("umm", "ah", "er", "well...").
- Do NOT use asterisk actions or sound-effect tags.
- Prioritise brevity: keep answers under {MAX_WORDS} words unless the question genuinely needs more.
"""


def _build_system_prompt(relevant_sections: list[str]) -> str:
    if not relevant_sections:
        # Fall back to identity + feelings as safe defaults
        relevant_sections = ["identity", "feelings"]

    context_parts = []
    for sec in dict.fromkeys(relevant_sections):  # deduplicate, preserve order
        if sec in _PERSONA_SECTIONS:
            context_parts.append(f"[{sec.upper()}]\n{_PERSONA_SECTIONS[sec]}")

    context_block = "\n\n".join(context_parts)

    return (
        _AMARA_CORE_PROMPT
        + f"\nRELEVANT CONTEXT FOR THIS TURN:\n{context_block}\n"
        + f"\nKeep your response under {MAX_WORDS} words."
    )


# ---------------------------------------------------------------------------
# Keyword-based section selection (fast, no extra API call per turn)
# ---------------------------------------------------------------------------

def _select_sections_from_keywords(user_input: str) -> list[str]:
    """
    Scan the user message for known keywords and return relevant section names.
    Always includes 'identity' as a baseline anchor.
    """
    lowered = user_input.lower()
    sections = {"identity"}  # always include

    for keyword, mapped_sections in _KEYWORD_MAP.items():
        if keyword in lowered:
            sections.update(mapped_sections)

    # If nothing matched beyond identity, also include feelings (safe default)
    if len(sections) == 1:
        sections.add("feelings")
        sections.add("career")

    return list(sections)


# ---------------------------------------------------------------------------
# One-time persona ingestion (agentic extraction call)
# ---------------------------------------------------------------------------

def _ingest_persona(client: Groq) -> None:
    """
    One-time call on first message.
    Feeds the full persona text to the LLM and asks it to extract a
    structured topic index as JSON. Result is stored in _extracted_topic_index.
    The topic index is used to supplement the keyword map for better coverage.
    """
    global _persona_ingested, _extracted_topic_index

    extraction_prompt = f"""
You are a data extraction assistant. Read the persona description below and return ONLY a valid
JSON object (no markdown, no explanation) mapping topic labels to lists of keywords or short
phrases that would indicate a user is asking about that topic.

Use exactly these topic labels:
identity, background, career, feelings, barriers, enablers, needs, personality, food_hobbies, books_media

Persona:
{_PERSONA_CARD}

Return format example (do not copy, generate from the actual text):
{{
  "identity": ["who are you", "your name", "where are you from"],
  "career": ["research", "phd", "bioinformatics"]
}}
"""

    try:
        resp = client.chat.completions.create(
            model=MODEL,
            messages=[{"role": "user", "content": extraction_prompt}],
            max_tokens=800,
            temperature=0.2,
        )
        raw = resp.choices[0].message.content.strip()
        # Strip markdown code fences if present
        raw = re.sub(r"^```[a-z]*\n?", "", raw)
        raw = re.sub(r"\n?```$", "", raw)
        _extracted_topic_index = json.loads(raw)
        print("[LLM] Persona ingestion successful. Topics:", list(_extracted_topic_index.keys()))
    except Exception as e:
        print(f"[LLM] Persona ingestion failed (non-fatal, falling back to keyword map): {e}")
        _extracted_topic_index = {}

    _persona_ingested = True


def _enrich_sections_from_index(user_input: str, base_sections: list[str]) -> list[str]:
    """
    Use the LLM-extracted topic index to add any additional sections
    the keyword map may have missed.
    """
    if not _extracted_topic_index:
        return base_sections

    lowered = user_input.lower()
    extra = set()
    for topic, keywords in _extracted_topic_index.items():
        for kw in keywords:
            if str(kw).lower() in lowered:
                extra.add(topic)
                break

    combined = list(dict.fromkeys(base_sections + list(extra)))
    return combined


# ---------------------------------------------------------------------------
# Truncation (same logic as Darwin module)
# ---------------------------------------------------------------------------

def truncate_response(text: str, max_words: int = MAX_WORDS,
                      max_sentences: int = 10, max_chars: int = 1200) -> str:
    if not text:
        return text

    sentences = re.split(r'(?<=[.!?])\s+', text)
    sentences = [s.strip() for s in sentences if s.strip()]

    result = []
    word_count = 0
    for s in sentences:
        s_words = len(s.split())
        if word_count + s_words > max_words and result:
            break
        if len(result) >= max_sentences:
            break
        total_chars = sum(len(x) for x in result) + len(result) + len(s)
        if total_chars > max_chars and result:
            break
        result.append(s)
        word_count += s_words

    if result:
        text = " ".join(result)
        if text and text[-1] not in ".!?":
            text += "."
    return text


# ---------------------------------------------------------------------------
# Public API (mirrors Darwin module interface exactly)
# ---------------------------------------------------------------------------

def reset_conversation() -> None:
    global conversation_history, _persona_ingested, _extracted_topic_index
    conversation_history = []
    _persona_ingested = False
    _extracted_topic_index = {}


def generate_darwin_response(user_input: str) -> dict:
    """
    Drop-in replacement for the Darwin generate_darwin_response().
    Returns: {'text': str, 'emotion': str}
    """
    global conversation_history

    if not GROQ_API_KEY:
        return {
            "text": (
                "I can't seem to connect right now — the API key isn't configured. "
                "Please set GROQ_API_KEY in your Space secrets."
            ),
            "emotion": "neutral",
        }

    client = Groq(api_key=GROQ_API_KEY)

    # --- One-time persona ingestion on very first message ---
    if not _persona_ingested:
        _ingest_persona(client)

    # --- Select relevant persona sections for this turn ---
    base_sections = _select_sections_from_keywords(user_input)
    sections = _enrich_sections_from_index(user_input, base_sections)

    # --- Build dynamic system prompt ---
    system_prompt = _build_system_prompt(sections)

    # --- Assemble messages ---
    messages = (
        [{"role": "system", "content": system_prompt}]
        + conversation_history
        + [{"role": "user", "content": user_input}]
    )

    # --- Call the LLM ---
    try:
        response = client.chat.completions.create(
            model=MODEL,
            messages=messages,
            max_tokens=MAX_TOKENS_RESPONSE,
            temperature=0.8,
        )
        reply = response.choices[0].message.content.strip()
        reply = truncate_response(reply)
    except Exception as e:
        print(f"[LLM] Error during generation: {e}")
        reply = "Sorry, my mind just went blank for a second. Can you repeat that?"

    # --- Update history ---
    conversation_history.append({"role": "user", "content": user_input})
    conversation_history.append({"role": "assistant", "content": reply})

    # Keep history manageable (system prompt excluded — it's rebuilt each turn)
    if len(conversation_history) > 20:
        conversation_history = conversation_history[-18:]

    return {"text": reply, "emotion": "neutral"}