Spaces:
Running
Running
File size: 919 Bytes
71736e8 |
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 |
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 "Invalid chatbot ID or chatbot not active"
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-3.5-turbo",
messages=conversation)
return response.choices[0].message.content
except Exception as e:
return f"An error occurred: {str(e)}" |