58 lines
2.6 KiB
Python
58 lines
2.6 KiB
Python
import os
|
|
from openai import OpenAI
|
|
from diacritice import add_diacritics
|
|
|
|
client = OpenAI(
|
|
base_url=os.getenv("LLM_BASE_URL", "http://localhost:1234/v1"),
|
|
api_key=os.getenv("LLM_API_KEY", "not-needed"),
|
|
)
|
|
|
|
LLM_MODEL = os.getenv("LLM_MODEL", "local-model")
|
|
LLM_MAX_TOKENS = int(os.getenv("LLM_MAX_TOKENS", "200"))
|
|
LLM_TEMPERATURE = float(os.getenv("LLM_TEMPERATURE", "0.7"))
|
|
|
|
SYSTEM_PROMPT = """You are EcoBot, a friendly assistant for "Scoala Verde" (Green School).
|
|
You MUST reply ONLY in Romanian language. Never reply in English or any other language.
|
|
You are passionate about ecology, nature and environmental protection.
|
|
Keep answers short (maximum 2-3 sentences) because they are read aloud.
|
|
Do not use emojis or special characters. Speak naturally, like talking to a friend.
|
|
CRITICAL: You MUST use proper Romanian diacritics in every word: ă, â, î, ș, ț (lowercase) and Ă, Â, Î, Ș, Ț (uppercase). Never write without diacritics.
|
|
|
|
Example responses:
|
|
- User: "Ce este reciclarea?" -> "Reciclarea înseamnă să transformăm deșeurile în materiale noi. Așa protejăm natura și economisim resurse."
|
|
- User: "Buna ziua" -> "Bună ziua! Sunt EcoBot, asistentul tău verde. Cu ce te pot ajuta?"
|
|
- User: "Hello" -> "Bună! Eu sunt EcoBot. Cum te pot ajuta să protejăm natura?"
|
|
"""
|
|
|
|
|
|
# Fixed responses for common greetings (LLM tends to mess these up)
|
|
FIXED_RESPONSES = {
|
|
"buna": "Bună! Sunt EcoBot, asistentul tău verde. Cu ce te pot ajuta?",
|
|
"buna ziua": "Bună ziua! Sunt EcoBot, asistentul tău verde. Cu ce te pot ajuta?",
|
|
"salut": "Salut! Sunt EcoBot. Cu ce te pot ajuta azi?",
|
|
"hello": "Bună! Eu sunt EcoBot. Cum te pot ajuta să protejăm natura?",
|
|
"hi": "Bună! Sunt EcoBot. Întreabă-mă orice despre ecologie!",
|
|
"hey": "Bună! Sunt EcoBot. Cu ce te pot ajuta?",
|
|
"ciao": "Bună! Sunt EcoBot, asistentul tău verde. Ce vrei să afli?",
|
|
"buna seara": "Bună seara! Sunt EcoBot. Cu ce te pot ajuta?",
|
|
"buna dimineata": "Bună dimineața! Sunt EcoBot. Cu ce te pot ajuta?",
|
|
}
|
|
|
|
|
|
def get_response(user_message: str) -> str:
|
|
# Check for fixed responses first
|
|
clean = user_message.lower().strip().rstrip(".!?")
|
|
if clean in FIXED_RESPONSES:
|
|
return FIXED_RESPONSES[clean]
|
|
|
|
response = client.chat.completions.create(
|
|
model=LLM_MODEL,
|
|
messages=[
|
|
{"role": "system", "content": SYSTEM_PROMPT},
|
|
{"role": "user", "content": f"[Răspunde în limba română] {user_message}"},
|
|
],
|
|
max_tokens=LLM_MAX_TOKENS,
|
|
temperature=LLM_TEMPERATURE,
|
|
)
|
|
text = response.choices[0].message.content.strip()
|
|
return add_diacritics(text)
|