import openai import os import gradio as gr from openai import OpenAI # Ensure the OPENAI_API_KEY environment variable is set openai.api_key = os.getenv("OPENAI_API_KEY") if openai.api_key is None: raise ValueError("Die Umgebungsvariable OPENAI_API_KEY ist nicht gesetzt.") client = OpenAI() def chat_with_gpt(user_input, system_message, temperature, history): # Do not add system message to displayed history if not history: history = [] history.append({"role": "user", "content": user_input}) response = client.chat.completions.create( model="gpt-3.5-turbo", messages=history + [{"role": "system", "content": system_message}], # Add system message here for GPT context but not for display temperature=temperature ) assistant_message = response.choices[0].message.content history.append({"role": "assistant", "content": assistant_message}) # Format for display, excluding the system message and roles formatted_history = [msg["content"] for msg in history if msg["role"] != "system"] return formatted_history, history def reset_history(system_message): return [], [] with gr.Blocks() as demo: gr.HTML("""