#Assistant API Help

1 messages · Page 1 of 1 (latest)

slender thistle
#

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})”
remote fjordBOT
#

Hi @slender thistle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the provided code and the information from the knowledge sources, it seems like the issue might be related to the way you're handling the thread and the messages within it.

In your send_message function, you're creating a new run and then listing all the messages in the thread. However, you're not filtering these messages based on the run ID. This means that every time you call send_message, you're getting all the messages from the thread, not just the ones related to the current run. This could be why you're seeing the first response for every other question.

Here's a potential solution: you could modify your send_message function to only consider the messages from the current run. You can do this by filtering the messages based on the run_id:

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" and message.run_id == run.id:
            response = message.content[0].text.value

    return response

This modification ensures that you're only considering the assistant messages from the current run.

Please note that this is a suggestion based on the provided information and the actual issue might be different based on the complete context of your application.