import os import openai import gradio as gr openai.api_key = os.getenv("OPENAI_API_KEY") def chat(user_input, history): if history is None: history = [] # Call OpenAI response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[{"role":"system","content":"You are a helpful dental assistant."}] + [{"role":"user","content":m[0]} if i%2==0 else {"role":"assistant","content":m[1]} for i,m in enumerate(history)] + [{"role":"user","content":user_input}] ) reply = response["choices"][0]["message"]["content"] history.append((user_input, reply)) # Keep last 6 exchanges return history[-6:], history[-6:] demo = gr.Interface( fn=chat, inputs=[gr.Textbox(lines=2, placeholder="Ask a dental question"), gr.State()], outputs=[gr.Chatbot(label="Dental Assistant"), gr.State()], title="Dental Clinic Chatbot" ) demo.launch()