from openai import OpenAI import streamlit as st import time st.title("Diabetes Chatbot") client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"]) assistantId = "asst_CcAjXh98c21Wif0S1K250in7" thread = client.beta.threads.create() def chat_with_gpt(question): message = client.beta.threads.messages.create( thread_id=thread.id, role="user", content=question ) run = client.beta.threads.runs.create( thread_id=thread.id, assistant_id=assistantId, instructions="This is a chatbot to answer questions about diabetes." ) run = client.beta.threads.runs.retrieve( thread_id=thread.id, run_id=run.id ) while run.completed_at is None: time.sleep(1) run = client.beta.threads.runs.retrieve( thread_id=thread.id, run_id=run.id ) messages = client.beta.threads.messages.list( thread_id=thread.id ) answer = messages.data[0].content[0].text.value return answer if "openai_model" not in st.session_state: st.session_state["openai_model"] = "gpt4omini" if "messages" not in st.session_state: st.session_state.messages = [] for message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(message["content"]) if prompt := st.chat_input("Please ask me questions about diabetes!"): st.session_state.messages.append({"role": "user", "content": prompt}) with st.chat_message("user"): st.markdown(prompt) response = chat_with_gpt(prompt) with st.chat_message("assistant"): st.markdown(response) st.session_state.messages.append({"role": "assistant", "content": response})