main.py aktualisiert
This commit is contained in:
42
main.py
42
main.py
@@ -48,13 +48,15 @@ async def get_ai_response(user_input, system_prompt):
|
||||
# 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)
|
||||
# 2. Gedächtnis auf die letzten 10 Nachrichten begrenzen
|
||||
chat_history = chat_history[-10:]
|
||||
|
||||
# 3. Den System-Prompt immer als Basis voranstellen
|
||||
messages = [{"role": "system", "content": system_prompt}] + chat_history
|
||||
ai_msg = ""
|
||||
|
||||
try:
|
||||
if AI_PROVIDER == "openai" or AI_PROVIDER == "ollama":
|
||||
# Für OpenAI / Ollama
|
||||
messages = [{"role": "system", "content": system_prompt}] + chat_history
|
||||
url = OLLAMA_BASE_URL if AI_PROVIDER == "ollama" else None
|
||||
key = "ollama" if AI_PROVIDER == "ollama" else OPENAI_API_KEY
|
||||
|
||||
@@ -66,19 +68,37 @@ async def get_ai_response(user_input, system_prompt):
|
||||
ai_msg = response.choices[0].message.content
|
||||
|
||||
elif AI_PROVIDER == "google":
|
||||
# Für Google Gemini
|
||||
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:
|
||||
|
||||
# Wir müssen unser Array in das spezielle Google-Format umwandeln
|
||||
google_history = []
|
||||
|
||||
# Alle Nachrichten AUSSER der allerletzten (die aktuelle User-Frage) in die History packen
|
||||
for msg in chat_history[:-1]:
|
||||
role = "user" if msg["role"] == "user" else "model"
|
||||
google_history.append(
|
||||
types.Content(role=role, parts=[types.Part.from_text(text=msg["content"])])
|
||||
)
|
||||
|
||||
# Chat MIT dem übersetzten Gedächtnis starten
|
||||
chat = client.chats.create(
|
||||
model='gemini-2.5-flash',
|
||||
config=types.GenerateContentConfig(system_instruction=system_prompt),
|
||||
history=google_history
|
||||
)
|
||||
|
||||
# Jetzt erst die neue Nachricht an den Chat mit Gedächtnis schicken
|
||||
response = chat.send_message(user_input)
|
||||
ai_msg = response.text
|
||||
|
||||
# 4. Die Antwort der KI ebenfalls ins Gedächtnis aufnehmen!
|
||||
except Exception as e:
|
||||
ai_msg = f"Fehler bei der KI-Anfrage: {e}"
|
||||
print(f"KI Fehler: {e}")
|
||||
|
||||
# 3. 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."
|
||||
|
||||
Reference in New Issue
Block a user