llm.py aktualisiert
This commit is contained in:
@@ -1,30 +1,24 @@
|
|||||||
from google import genai
|
import os
|
||||||
from config import GEMINI_API_KEY, MODEL_NAME
|
import requests
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
class LLM:
|
API_KEY = os.getenv("OPENROUTER_API_KEY")
|
||||||
def __init__(self):
|
MODEL = os.getenv("OPENROUTER_MODEL")
|
||||||
if not GEMINI_API_KEY:
|
|
||||||
raise ValueError(
|
def ask_llm(messages):
|
||||||
"Kein GEMINI_API_KEY gefunden. Bitte überprüfe deine .env-Datei."
|
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
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
self.client = genai.Client(api_key=GEMINI_API_KEY)
|
return r.json()["choices"][0]["message"]["content"]
|
||||||
|
|
||||||
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()
|
|
||||||
Reference in New Issue
Block a user