File size: 933 Bytes
71736e8
 
 
 
 
 
 
 
 
 
9e991a9
71736e8
 
 
 
 
 
 
 
 
 
727bb86
71736e8
 
 
9e991a9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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)}"