from openai import OpenAI from utils.utils import get_secret OPENAI_API_KEY = get_secret("OPENAI_API_KEY") client = OpenAI(api_key=OPENAI_API_KEY) def chat_with_bot(message, history, chatbot_id): from database import get_chatbot chatbot = get_chatbot(chatbot_id) if not chatbot: return "Ungültige Chatbot-ID oder Chatbot nicht aktiv" conversation = [ {"role": "system", "content": chatbot.custom_instruction}, ] for human, assistant in history: conversation.append({"role": "user", "content": human}) conversation.append({"role": "assistant", "content": assistant}) conversation.append({"role": "user", "content": message}) try: response = client.chat.completions.create(model="gpt-4o-mini", messages=conversation) return response.choices[0].message.content except Exception as e: return f"Ein Fehler ist aufgetreten: {str(e)}"