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")
|
||||
DB_PATH = "cluster.db"
|
||||
chat_history = []
|
||||
|
||||
# --- KI KONFIGURATION ---
|
||||
AI_PROVIDER = "google" # "openai", "google" oder "ollama"
|
||||
@@ -41,39 +42,46 @@ def get_system_prompt():
|
||||
|
||||
# --- KI FUNKTIONEN ---
|
||||
|
||||
async def get_ai_response(user_input, system_prompt): # <--- system_prompt hinzugefügt
|
||||
if AI_PROVIDER == "openai":
|
||||
client = openai.OpenAI(api_key=OPENAI_API_KEY)
|
||||
response = client.chat.completions.create(
|
||||
model="gpt-4o",
|
||||
messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": user_input}]
|
||||
)
|
||||
return response.choices[0].message.content
|
||||
async def get_ai_response(user_input, system_prompt):
|
||||
global chat_history
|
||||
|
||||
# 1. Die neue User-Nachricht dem Gedächtnis hinzufügen
|
||||
chat_history.append({"role": "user", "content": user_input})
|
||||
|
||||
# 2. Das Gedächtnis auf die letzten 10 Nachrichten begrenzen (damit es nicht zu teuer/lang wird)
|
||||
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":
|
||||
# Ollama nutzt das OpenAI-Format, braucht aber keinen Key
|
||||
client = openai.OpenAI(base_url=OLLAMA_BASE_URL, api_key="ollama", timeout=20.0)
|
||||
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)
|
||||
if AI_PROVIDER == "openai" or AI_PROVIDER == "ollama":
|
||||
url = OLLAMA_BASE_URL if AI_PROVIDER == "ollama" else None
|
||||
key = "ollama" if AI_PROVIDER == "ollama" else OPENAI_API_KEY
|
||||
|
||||
# Generierung mit dem neuen SDK
|
||||
response = client.models.generate_content(
|
||||
model='gemini-2.5-flash',
|
||||
contents=user_input,
|
||||
config=types.GenerateContentConfig(
|
||||
system_instruction=system_prompt
|
||||
)
|
||||
client = openai.OpenAI(base_url=url, api_key=key)
|
||||
response = client.chat.completions.create(
|
||||
model="gpt-4o" if AI_PROVIDER == "openai" else "llama3",
|
||||
messages=messages
|
||||
)
|
||||
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 ---
|
||||
|
||||
Reference in New Issue
Block a user