#Messages in Assistants API

1 messages · Page 1 of 1 (latest)

reef stone
#

If i send a message to the Assistant via Assistants API in a thread, how can i retrieve the Assistant's response to it?

opal hatchBOT
#

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

#

To retrieve the Assistant's response to a message sent via the Assistants API in a thread, you can use the openai.beta.threads.messages.list method. This method returns all messages in the thread. You can then filter the messages to get the Assistant's responses.

Here is an example of how you can do this in Node.js:

const messages = await openai.beta.threads.messages.list(thread.id);
const assistantMessages = messages.data.filter(msg => msg.role === 'assistant');

In this code, thread.id is the ID of the thread where you sent the message. The filter function is used to get only the messages where the role is 'assistant', which are the responses from the Assistant.

You can also order the messages by the "create_at" timestamp to properly order the thread when grabbing the assistant response. Here is how you can do it in Python:

messages = client.beta.threads.messages.list(
             thread_id=thread.id,
             # Orders it by ascending order, so the latest response is at the top
             order='asc' 
)
#

For more details, you can refer to these forum posts: Forum Post: Access message from threads and assistant (https://community.openai.com/t/479817), Forum Post: Retrieve only Assistant's response from the last run (https://community.openai.com/t/551746), Forum Post: Issues with Assistant API + threads (https://community.openai.com/t/496041), Forum Post: Using Flask to interface a chat window with Assistant API (https://community.openai.com/t/503415), Forum Post: Assistant API response messages + token count (https://community.openai.com/t/556270), Forum Post: Assistant API response missing code (https://community.openai.com/t/583579), Forum Post: Thread.id being treated as object (https://community.openai.com/t/513815).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: