Can you please check and see why my code keeps printing the first response for every other question I ask after the first one “
def create_thread():
thread = client.beta.threads.create()
return thread
Function to send a message to the assistant
def send_message(thread_id, user_message, i):
# Create a user message in the thread
client.beta.threads.messages.create(
thread_id=thread_id,
role="user",
content=user_message
)
run = client.beta.threads.runs.create_and_poll(thread_id=thread_id, assistant_id=st.session_state.assistant.id)
messages = client.beta.threads.messages.list(
thread_id=thread_id
)
for message in messages.data:
if message.role == "assistant":
response = message.content[0].text.value
return response
Streamlit UI
st.title("Assistant Chatbot")
New Chat Button
if st.button("New Chat"):
st.session_state["thread_id"] = create_thread().id
st.session_state.chat_history = []
Initialize session state variables
if "thread_id" not in st.session_state:
st.session_state.thread_id = create_thread().id
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
Display chat messages from history on app rerun
for message in st.session_state.chat_history:
role = message.get("role", "")
content = message.get("content", "")
with st.chat_message(role):
st.markdown(content)
if prompt := st.chat_input("How can I help?"):
st.chat_message("user").markdown(prompt)
st.session_state.chat_history.append({"role": "user", "content": prompt})
with st.chat_message("assistant"):
response = send_message(st.session_state.thread_id, prompt)
st.markdown(response)
# Add assistant response to chat history
st.session_state.chat_history.append({"role": "assistant", "content": response})”