Files
Mark-X-TR/Jarvis TR/llm.py
T
2026-02-01 21:29:35 +03:00

170 lines
4.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# llm.py
import os
import json
import requests
from dotenv import load_dotenv
load_dotenv()
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
OPENROUTER_URL = "https://openrouter.ai/api/v1/chat/completions"
MODEL = "arcee-ai/trinity-large-preview:free"
PROMPT_PATH = "core/prompt.txt"
def load_system_prompt() -> str:
try:
with open(PROMPT_PATH, "r", encoding="utf-8") as f:
return f.read()
except Exception as e:
print(f"⚠️ prompt.txt cannot installed: {e}")
return "Senin ismin Jarvis, yardımcı olan bir yapay zeka asistanı"
SYSTEM_PROMPT = load_system_prompt()
def safe_json_parse(text: str) -> dict | None:
if not text:
return None
text = text.strip()
if "```json" in text:
try:
start = text.index("```json") + 7
end = text.index("```", start)
text = text[start:end].strip()
except:
pass
elif "```" in text:
try:
start = text.index("```") + 3
end = text.index("```", start)
text = text[start:end].strip()
except:
pass
try:
start = text.index("{")
end = text.rindex("}") + 1
json_str = text[start:end]
return json.loads(json_str)
except Exception as e:
print(f"⚠️ JSON parse error: {e}")
print(f"The error text: {text[:200]}")
return None
def get_llm_output(user_text: str, memory_block: dict = None) -> dict:
if not user_text or not user_text.strip():
return {
"intent": "chat",
"parameters": {},
"needs_clarification": False,
"text": "Efendim, bunu anlayamadım",
"memory_update": None
}
if not OPENROUTER_API_KEY:
print("❌ OPENROUTER_API_KEY bulunamadı!")
return {
"intent": "chat",
"parameters": {},
"text": "API key eklemeyi unuttunuz, efendim.",
"needs_clarification": False,
"memory_update": None
}
# Memory'yi string'e çevir
memory_str = ""
if memory_block:
memory_str = "\n".join(f"{k}: {v}" for k, v in memory_block.items())
user_prompt = f"""User message: "{user_text}"
Known user memory:
{memory_str if memory_str else "No memory available"}"""
payload = {
"model": MODEL,
"messages": [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_prompt}
],
"temperature": 0.2,
"max_tokens": 500
}
headers = {
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
"Content-Type": "application/json",
"HTTP-Referer": "http://localhost",
"X-Title": "Jarvis-Assistant"
}
try:
response = requests.post(
OPENROUTER_URL,
headers=headers,
json=payload,
timeout=30
)
if response.status_code != 200:
print(f"❌ API Hatası: {response.text}")
return {
"intent": "chat",
"parameters": {},
"text": f"Sir, API error: {response.status_code}",
"needs_clarification": False,
"memory_update": None
}
data = response.json()
content = data["choices"][0]["message"]["content"]
# JSON parse et
parsed = safe_json_parse(content)
if parsed:
return {
"intent": parsed.get("intent", "chat"),
"parameters": parsed.get("parameters", {}),
"needs_clarification": parsed.get("needs_clarification", False),
"text": parsed.get("text"),
"memory_update": parsed.get("memory_update")
}
return {
"intent": "chat",
"parameters": {},
"needs_clarification": False,
"text": content,
"memory_update": None
}
except requests.exceptions.Timeout:
print("❌ API timeout!")
return {
"intent": "chat",
"text": "Efendim, bağlantı süresi aşıma uğradı.",
"parameters": {},
"needs_clarification": False,
"memory_update": None
}
except Exception as e:
print(f"❌ LLM ERROR: {e}")
return {
"intent": "chat",
"text": "Efendim, bir sistem hatasıyla karşılaştım.",
"parameters": {},
"needs_clarification": False,
"memory_update": None
}