llm.py aktualisiert
This commit is contained in:
@@ -1,30 +1,24 @@
|
||||
from google import genai
|
||||
from config import GEMINI_API_KEY, MODEL_NAME
|
||||
import os
|
||||
import requests
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
class LLM:
|
||||
def __init__(self):
|
||||
if not GEMINI_API_KEY:
|
||||
raise ValueError(
|
||||
"Kein GEMINI_API_KEY gefunden. Bitte überprüfe deine .env-Datei."
|
||||
)
|
||||
API_KEY = os.getenv("OPENROUTER_API_KEY")
|
||||
MODEL = os.getenv("OPENROUTER_MODEL")
|
||||
|
||||
self.client = genai.Client(api_key=GEMINI_API_KEY)
|
||||
def ask_llm(messages):
|
||||
r = requests.post(
|
||||
"https://openrouter.ai/api/v1/chat/completions",
|
||||
headers={
|
||||
"Authorization": f"Bearer {API_KEY}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
json={
|
||||
"model": MODEL,
|
||||
"messages": messages,
|
||||
"temperature": 0.3
|
||||
}
|
||||
)
|
||||
|
||||
with open("prompt.txt", "r", encoding="utf-8") as f:
|
||||
self.system_prompt = f.read()
|
||||
|
||||
def ask(self, user_input: str) -> str:
|
||||
prompt = f"""
|
||||
{self.system_prompt}
|
||||
|
||||
Benutzer:
|
||||
{user_input}
|
||||
"""
|
||||
|
||||
response = self.client.models.generate_content(
|
||||
model=MODEL_NAME,
|
||||
contents=prompt,
|
||||
)
|
||||
|
||||
return response.text.strip()
|
||||
return r.json()["choices"][0]["message"]["content"]
|
||||
Reference in New Issue
Block a user