Webseite überarbeitet und Telegram Bot funktion hinzugefügt #1
40
main.py
40
main.py
@@ -92,54 +92,38 @@ def get_system_prompt():
|
|||||||
|
|
||||||
async def get_ai_response(user_input, system_prompt):
|
async def get_ai_response(user_input, system_prompt):
|
||||||
global chat_history
|
global chat_history
|
||||||
|
|
||||||
# 1. Die neue User-Nachricht dem Gedächtnis hinzufügen
|
|
||||||
chat_history.append({"role": "user", "content": user_input})
|
chat_history.append({"role": "user", "content": user_input})
|
||||||
|
|
||||||
# 2. Gedächtnis auf die letzten 30 Nachrichten begrenzen
|
|
||||||
chat_history = chat_history[-30:]
|
chat_history = chat_history[-30:]
|
||||||
|
|
||||||
ai_msg = ""
|
ai_msg = ""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if AI_PROVIDER == "openai" or AI_PROVIDER == "ollama":
|
# Kombinierte Logik für OpenAI, Ollama und NVIDIA (alle nutzen das OpenAI SDK)
|
||||||
|
if AI_PROVIDER in ["openai", "ollama", "nvidia"]:
|
||||||
messages = [{"role": "system", "content": system_prompt}] + chat_history
|
messages = [{"role": "system", "content": system_prompt}] + chat_history
|
||||||
|
|
||||||
# Sicherstellen, dass die URL für Ollama korrekt endet
|
|
||||||
if AI_PROVIDER == "ollama":
|
if AI_PROVIDER == "ollama":
|
||||||
url = OLLAMA_BASE_URL
|
url = OLLAMA_BASE_URL
|
||||||
if not url.endswith('/v1') and not url.endswith('/v1/'):
|
if not url.endswith('/v1') and not url.endswith('/v1/'):
|
||||||
url = url.rstrip('/') + '/v1'
|
url = url.rstrip('/') + '/v1'
|
||||||
key = "ollama"
|
key = "ollama"
|
||||||
model_to_use = OLLAMA_MODEL
|
model_to_use = OLLAMA_MODEL
|
||||||
else:
|
elif AI_PROVIDER == "nvidia":
|
||||||
url = None # Benutzt Standard OpenAI URL
|
url = "https://integrate.api.nvidia.com/v1"
|
||||||
|
key = NVIDIA_API_KEY
|
||||||
|
model_to_use = NVIDIA_MODEL
|
||||||
|
else: # openai
|
||||||
|
url = None
|
||||||
key = OPENAI_API_KEY
|
key = OPENAI_API_KEY
|
||||||
model_to_use = OPENAI_MODEL
|
model_to_use = OPENAI_MODEL
|
||||||
|
|
||||||
client = openai.OpenAI(base_url=url, api_key=key)
|
# WICHTIG: Hier .AsyncOpenAI nutzen, da die Funktion async ist
|
||||||
response = client.chat.completions.create(
|
client = openai.AsyncOpenAI(base_url=url, api_key=key)
|
||||||
|
response = await client.chat.completions.create(
|
||||||
model=model_to_use,
|
model=model_to_use,
|
||||||
messages=messages
|
messages=messages
|
||||||
)
|
)
|
||||||
ai_msg = response.choices[0].message.content
|
ai_msg = response.choices[0].message.content
|
||||||
|
|
||||||
elif AI_PROVIDER == "nvidia":
|
|
||||||
if not NVIDIA_API_KEY:
|
|
||||||
return "Fehler: NVIDIA_API_KEY fehlt!"
|
|
||||||
|
|
||||||
# NVIDIA nutzt ebenfalls das OpenAI Interface
|
|
||||||
client = openai.OpenAI(
|
|
||||||
base_url="https://integrate.api.nvidia.com/v1",
|
|
||||||
api_key=NVIDIA_API_KEY
|
|
||||||
)
|
|
||||||
messages = [{"role": "system", "content": system_prompt}] + chat_history
|
|
||||||
response = client.chat.completions.create(
|
|
||||||
model=NVIDIA_MODEL,
|
|
||||||
messages=messages
|
|
||||||
)
|
|
||||||
ai_msg = response.choices[0].message.content
|
|
||||||
|
|
||||||
elif AI_PROVIDER == "google":
|
elif AI_PROVIDER == "google":
|
||||||
# Für Google Gemini
|
# Für Google Gemini
|
||||||
if not GOOGLE_API_KEY:
|
if not GOOGLE_API_KEY:
|
||||||
@@ -382,7 +366,7 @@ async def get_settings():
|
|||||||
"provider": AI_PROVIDER,
|
"provider": AI_PROVIDER,
|
||||||
"google_model": GOOGLE_MODEL,
|
"google_model": GOOGLE_MODEL,
|
||||||
"openai_model": OPENAI_MODEL,
|
"openai_model": OPENAI_MODEL,
|
||||||
"mvidia_model": NVIDIA_MODEL,
|
"nvidia_model": NVIDIA_MODEL,
|
||||||
"ollama_model": OLLAMA_MODEL,
|
"ollama_model": OLLAMA_MODEL,
|
||||||
"ollama_base_url": OLLAMA_BASE_URL # URL ans Frontend schicken
|
"ollama_base_url": OLLAMA_BASE_URL # URL ans Frontend schicken
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user