Spaces:
Build error
Build error
Download utils/openai_utils.py from codora/ai-app-creator: direct link, hf CLI and curl.
- Browser
- Download file 933 Bytes
-
https://huggingface.co/spaces/codora/ai-app-creator/resolve/main/utils/openai_utils.py
- Command line
-
hf download hf://spaces/codora/ai-app-creator/utils/openai_utils.py
-
curl -L -o openai_utils.py https://huggingface.co/spaces/codora/ai-app-creator/resolve/main/utils/openai_utils.py
933 Bytes
| 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)}" | |