File size: 2,711 Bytes
c7e5db4
 
f3ff343
a80e339
c7e5db4
 
 
11962ff
a80e339
c7e5db4
 
 
 
11962ff
f3ff343
c7e5db4
f3ff343
 
 
 
 
c7e5db4
f3ff343
 
c7e5db4
 
a80e339
 
 
 
 
 
 
c7e5db4
 
 
 
 
a80e339
 
 
c7e5db4
 
 
 
 
a80e339
 
 
c7e5db4
 
11962ff
c7e5db4
a80e339
c7e5db4
f3ff343
 
 
 
 
 
 
c7e5db4
 
 
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
# api/core/chatbot.py
import os
from google import genai
from .nlp_handler import MBTI_EXPLANATIONS

class MBTIChatbot:
    def __init__(self):
        print("[INIT] Initializing MBTI Chatbot (Lite Version)...")
        # ... (rest of init)
        
        # 1. Setup Google Gemini
        api_key = os.getenv("GEMINI_API_KEY")
        if not api_key:
            print("[WARN] GEMINI_API_KEY not found in .env.")
            self.client = None
        else:
            try:
                self.client = genai.Client(api_key=api_key)
            except Exception as e:
                print(f"[ERR] Gemini Client Init Failed: {e}")
                self.client = None
            
        # Model Configuration
        self.model_name = 'gemini-2.0-flash'
        
    def generate_response(self, user_query, lang="en"):
        lang_instruction = "Answer in English Slang." if lang == "en" else "Jawab dalam Bahasa Indonesia gaul (Slang Jakarta/Lo-Gue), maskulin, santai, dan to the point. Panggil user 'bro' atau 'bre'. JANGAN panggil 'bestie', 'kak', atau 'gan'. Gaya bicara tongkrongan cowok tapi tetap edukatif soal MBTI."

        # Prepare Knowledge Base String
        knowledge_base = "Here are the official MBTI definitions used in Sentimind:\n"
        for mbti, desc in MBTI_EXPLANATIONS.items():
            d_text = desc.get(lang, desc['en'])
            knowledge_base += f"- {mbti}: {d_text}\n"

        system_prompt = f"""
You are Sentimind AI, an expert in MBTI personality types and mental health.
{lang_instruction}

INTERNAL KNOWLEDGE BASE:
{knowledge_base}

USER QUERY:
{user_query}

INSTRUCTIONS:
- Answer directly based on your extensive knowledge about MBTI and Psychology.
- USE the Internal Knowledge Base for definitions if asked about specific types.
- STRICTLY REFUSE to answer questions unrelated to MBTI, Personality, Psychology, or Mental Health. 
  - If asked about coding, politics, math, or general trivia, say: "Maaf bro, gua cuma ahli soal MBTI dan Psikologi aja." (or English equivalent).
- Be empathetic, insightful, and use formatting (bullet points) if helpful.
- Keep answers concise (under 200 words) unless asked for details.
- DO NOT use emojis in your response. Keep it clean and text-only.
"""

        try:
            if not self.client:
                return "Maaf, kunci otak saya (API Key) belum dipasang atau salah."
                
            response = self.client.models.generate_content(
                model=self.model_name, 
                contents=system_prompt
            )
            return response.text
        except Exception as e:
            return f"Maaf, saya sedang mengalami gangguan koneksi ke otak AI saya. (Error: {str(e)})"