from openai import OpenAI import gradio as gr from dotenv import load_dotenv import os from html.entities import codepoint2name import tempfile import markdown2 # Get the API key from the environment variable api_key = os.getenv("OPENAI_API_KEY") client = OpenAI(api_key=api_key) def predict(message, history): history_openai_format = [ { "role": "system", "content": "You are a helpful assistant who responds with a descriptive step by step process on how to use the no-code platform bubble.io." }] last_2_history = history if len(history) < 2 else history[-2:0] for human, assistant in last_2_history: history_openai_format.append({"role": "user", "content": human }) history_openai_format.append({"role": "assistant", "content":assistant}) history_openai_format.append({"role": "user", "content": message}) response = client.chat.completions.create(model='gpt-4o', messages= history_openai_format, temperature=0.4, stream=True) partial_message = "" for chunk in response: if chunk.choices[0].delta.content is not None: partial_message = partial_message + chunk.choices[0].delta.content yield partial_message def convert_special_chars(text): return ''.join(f'&{codepoint2name[ord(char)]};' if ord(char) in codepoint2name else char for char in text) def download_chat_history(history): chat_data = [] for human, assistance in history: chat_data.append({"sender": "User", "content": human}) chat_data.append({"sender": "AI", "content": assistance}) html_content = """