main.py aktualisiert
This commit is contained in:
66
main.py
66
main.py
@@ -21,6 +21,7 @@ templates = Jinja2Templates(directory="templates")
|
|||||||
|
|
||||||
SSH_KEY = os.path.expanduser("~/.ssh/id_rsa")
|
SSH_KEY = os.path.expanduser("~/.ssh/id_rsa")
|
||||||
DB_PATH = "cluster.db"
|
DB_PATH = "cluster.db"
|
||||||
|
chat_history = []
|
||||||
|
|
||||||
# --- KI KONFIGURATION ---
|
# --- KI KONFIGURATION ---
|
||||||
AI_PROVIDER = "google" # "openai", "google" oder "ollama"
|
AI_PROVIDER = "google" # "openai", "google" oder "ollama"
|
||||||
@@ -41,39 +42,46 @@ def get_system_prompt():
|
|||||||
|
|
||||||
# --- KI FUNKTIONEN ---
|
# --- KI FUNKTIONEN ---
|
||||||
|
|
||||||
async def get_ai_response(user_input, system_prompt): # <--- system_prompt hinzugefügt
|
async def get_ai_response(user_input, system_prompt):
|
||||||
if AI_PROVIDER == "openai":
|
global chat_history
|
||||||
client = openai.OpenAI(api_key=OPENAI_API_KEY)
|
|
||||||
response = client.chat.completions.create(
|
# 1. Die neue User-Nachricht dem Gedächtnis hinzufügen
|
||||||
model="gpt-4o",
|
chat_history.append({"role": "user", "content": user_input})
|
||||||
messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": user_input}]
|
|
||||||
)
|
# 2. Das Gedächtnis auf die letzten 10 Nachrichten begrenzen (damit es nicht zu teuer/lang wird)
|
||||||
return response.choices[0].message.content
|
chat_history = chat_history[-10:]
|
||||||
|
|
||||||
|
# 3. Den System-Prompt immer als Basis voranstellen
|
||||||
|
messages = [{"role": "system", "content": system_prompt}] + chat_history
|
||||||
|
|
||||||
elif AI_PROVIDER == "ollama":
|
if AI_PROVIDER == "openai" or AI_PROVIDER == "ollama":
|
||||||
# Ollama nutzt das OpenAI-Format, braucht aber keinen Key
|
url = OLLAMA_BASE_URL if AI_PROVIDER == "ollama" else None
|
||||||
client = openai.OpenAI(base_url=OLLAMA_BASE_URL, api_key="ollama", timeout=20.0)
|
key = "ollama" if AI_PROVIDER == "ollama" else OPENAI_API_KEY
|
||||||
response = client.chat.completions.create(
|
|
||||||
model="llama3", # Oder dein bevorzugtes Modell
|
|
||||||
messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": user_input}]
|
|
||||||
)
|
|
||||||
return response.choices[0].message.content
|
|
||||||
|
|
||||||
elif AI_PROVIDER == "google":
|
|
||||||
# Initialisierung des neuen Clients
|
|
||||||
client = genai.Client(api_key=GOOGLE_API_KEY)
|
|
||||||
|
|
||||||
# Generierung mit dem neuen SDK
|
client = openai.OpenAI(base_url=url, api_key=key)
|
||||||
response = client.models.generate_content(
|
response = client.chat.completions.create(
|
||||||
model='gemini-2.5-flash',
|
model="gpt-4o" if AI_PROVIDER == "openai" else "llama3",
|
||||||
contents=user_input,
|
messages=messages
|
||||||
config=types.GenerateContentConfig(
|
|
||||||
system_instruction=system_prompt
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
return response.text
|
ai_msg = response.choices[0].message.content
|
||||||
|
|
||||||
|
elif AI_PROVIDER == "google":
|
||||||
|
client = genai.Client(api_key=GOOGLE_API_KEY)
|
||||||
|
# Für Google Gemini bauen wir die History etwas anders zusammen
|
||||||
|
# oder nutzen die interne start_chat Funktion:
|
||||||
|
chat = client.chats.create(model='gemini-2.5-flash', config=types.GenerateContentConfig(
|
||||||
|
system_instruction=system_prompt
|
||||||
|
))
|
||||||
|
# Hier müssten wir eigentlich die history übergeben, aber zum Testen
|
||||||
|
# reicht oft auch der kombinierte String:
|
||||||
|
response = chat.send_message(user_input)
|
||||||
|
ai_msg = response.text
|
||||||
|
|
||||||
return "Fehler: Kein KI-Provider konfiguriert."
|
# 4. Die Antwort der KI ebenfalls ins Gedächtnis aufnehmen!
|
||||||
|
chat_history.append({"role": "assistant", "content": ai_msg})
|
||||||
|
return ai_msg
|
||||||
|
|
||||||
|
# return "Fehler: Kein KI-Provider konfiguriert."
|
||||||
|
|
||||||
|
|
||||||
# --- DATENBANK INITIALISIERUNG ---
|
# --- DATENBANK INITIALISIERUNG ---
|
||||||
|
|||||||
Reference in New Issue
Block a user