import gradio as gr import openai # Set your OpenAI API key openai.api_key = "YOUR_API_KEY" # Replace with your actual API key # Maximum number of messages to keep in history MAX_HISTORY = 10 def chat_fn(message, history): # Convert history to the correct format for OpenAI messages = [] for user_msg, bot_msg in history[-MAX_HISTORY:]: # Keep only the last MAX_HISTORY messages messages.append({"role": "user", "content": user_msg}) messages.append({"role": "assistant", "content": bot_msg}) # Add the new user message messages.append({"role": "user", "content": message}) # Call OpenAI API response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages, max_tokens=300 ) # Get the model's reply bot_reply = response.choices[0].message["content"] # Update history history.append((message, bot_reply)) # Return empty textbox and updated history return "", history # Gradio interface with gr.Blocks() as demo: gr.Markdown("# 💬 JuriX Chatbot") chatbot = gr.Chatbot() msg = gr.Textbox(label="Type your message here", placeholder="Enter your message...") msg.submit(chat_fn, [msg, chat]()_