Sync from GitHub Actions: 73ae4457dcc243cc5a3129137c4f8d7c50f310b1
Browse files- api/core/chatbot.py +16 -4
api/core/chatbot.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
# api/core/chatbot.py
|
| 2 |
import os
|
| 3 |
import google.generativeai as genai
|
|
|
|
| 4 |
|
| 5 |
class MBTIChatbot:
|
| 6 |
def __init__(self):
|
| 7 |
print("[INIT] Initializing MBTI Chatbot (Lite Version)...")
|
|
|
|
| 8 |
|
| 9 |
# 1. Setup Google Gemini
|
| 10 |
api_key = os.getenv("GEMINI_API_KEY")
|
|
@@ -14,30 +16,40 @@ class MBTIChatbot:
|
|
| 14 |
genai.configure(api_key=api_key)
|
| 15 |
|
| 16 |
try:
|
| 17 |
-
# Pake Gemini 2.0 Flash (Standard)
|
| 18 |
self.model = genai.GenerativeModel('gemini-2.0-flash')
|
| 19 |
except Exception:
|
| 20 |
print("[WARN] 2.0 Flash failed, fallback to Lite")
|
| 21 |
self.model = genai.GenerativeModel('gemini-2.0-flash-lite')
|
| 22 |
|
| 23 |
def generate_response(self, user_query, lang="en"):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
system_prompt = f"""
|
| 29 |
You are Sentimind AI, an expert in MBTI personality types and mental health.
|
| 30 |
{lang_instruction}
|
| 31 |
|
|
|
|
|
|
|
|
|
|
| 32 |
USER QUERY:
|
| 33 |
{user_query}
|
| 34 |
|
| 35 |
INSTRUCTIONS:
|
| 36 |
- Answer directly based on your extensive knowledge about MBTI and Psychology.
|
|
|
|
|
|
|
|
|
|
| 37 |
- Be empathetic, insightful, and use formatting (bullet points) if helpful.
|
| 38 |
- Keep answers concise (under 200 words) unless asked for details.
|
| 39 |
- DO NOT use emojis in your response. Keep it clean and text-only.
|
| 40 |
"""
|
|
|
|
| 41 |
try:
|
| 42 |
response = self.model.generate_content(system_prompt)
|
| 43 |
return response.text
|
|
|
|
| 1 |
# api/core/chatbot.py
|
| 2 |
import os
|
| 3 |
import google.generativeai as genai
|
| 4 |
+
from .nlp_handler import MBTI_EXPLANATIONS
|
| 5 |
|
| 6 |
class MBTIChatbot:
|
| 7 |
def __init__(self):
|
| 8 |
print("[INIT] Initializing MBTI Chatbot (Lite Version)...")
|
| 9 |
+
# ... (rest of init)
|
| 10 |
|
| 11 |
# 1. Setup Google Gemini
|
| 12 |
api_key = os.getenv("GEMINI_API_KEY")
|
|
|
|
| 16 |
genai.configure(api_key=api_key)
|
| 17 |
|
| 18 |
try:
|
|
|
|
| 19 |
self.model = genai.GenerativeModel('gemini-2.0-flash')
|
| 20 |
except Exception:
|
| 21 |
print("[WARN] 2.0 Flash failed, fallback to Lite")
|
| 22 |
self.model = genai.GenerativeModel('gemini-2.0-flash-lite')
|
| 23 |
|
| 24 |
def generate_response(self, user_query, lang="en"):
|
| 25 |
+
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."
|
| 26 |
+
|
| 27 |
+
# Prepare Knowledge Base String
|
| 28 |
+
knowledge_base = "Here are the official MBTI definitions used in Sentimind:\n"
|
| 29 |
+
for mbti, desc in MBTI_EXPLANATIONS.items():
|
| 30 |
+
d_text = desc.get(lang, desc['en'])
|
| 31 |
+
knowledge_base += f"- {mbti}: {d_text}\n"
|
| 32 |
|
| 33 |
system_prompt = f"""
|
| 34 |
You are Sentimind AI, an expert in MBTI personality types and mental health.
|
| 35 |
{lang_instruction}
|
| 36 |
|
| 37 |
+
INTERNAL KNOWLEDGE BASE:
|
| 38 |
+
{knowledge_base}
|
| 39 |
+
|
| 40 |
USER QUERY:
|
| 41 |
{user_query}
|
| 42 |
|
| 43 |
INSTRUCTIONS:
|
| 44 |
- Answer directly based on your extensive knowledge about MBTI and Psychology.
|
| 45 |
+
- USE the Internal Knowledge Base for definitions if asked about specific types.
|
| 46 |
+
- STRICTLY REFUSE to answer questions unrelated to MBTI, Personality, Psychology, or Mental Health.
|
| 47 |
+
- If asked about coding, politics, math, or general trivia, say: "Maaf bro, gua cuma ahli soal MBTI dan Psikologi aja." (or English equivalent).
|
| 48 |
- Be empathetic, insightful, and use formatting (bullet points) if helpful.
|
| 49 |
- Keep answers concise (under 200 words) unless asked for details.
|
| 50 |
- DO NOT use emojis in your response. Keep it clean and text-only.
|
| 51 |
"""
|
| 52 |
+
|
| 53 |
try:
|
| 54 |
response = self.model.generate_content(system_prompt)
|
| 55 |
return response.text
|