Access message from threads and assistant

Does anyone know how to access the response message from the assistant?
I am using nodejs.

    const openai = new OpenAI({
      apiKey: '****', // defaults to process.env["OPENAI_API_KEY"]
    });


    // Add the file to the assistant
    const assistant = await openai.beta.assistants.create({
      instructions: "You are a teacher. Use your knowledge base to best respond to student queries.",
      model: "gpt-4-1106-preview",
      tools: [{ type: "retrieval" }],
      // file_ids: ["file-8GPAe7Xx8lDGePc6ETMu9kN4"]
    });

    const thread = await openai.beta.threads.create({
      messages: [
        {
          "role": "user",
          "content": "Let me know the content of the file.",
          // "file_ids": ["file-8GPAe7Xx8lDGePc6ETMu9kN4"]
        }
      ]
    });

    const run = await openai.beta.threads.runs.create(
      thread.id,
      {
        assistant_id: assistant.id,
        instructions: "Please address the user as Jane Doe. The user has a premium account."
      }
    );


    const run_response = await openai.beta.threads.runs.retrieve(
      thread.id,
      run.id
    );
    const messages: any = await openai.beta.threads.messages.list(
      thread.id
    );

    // console.log(messages.data[0].content);

    const threadMessages = await openai.beta.threads.messages.list(
      thread.id
    );

    console.log(threadMessages.data);
4 Likes

I struggled to find it out and finally was able to do this in python as below. I will post the code shortly in github

run = openai.beta.threads.runs.create(
  thread_id=thread.id,
  assistant_id=assistant.id
)
print(run)


while run.status !="completed":
  run = openai.beta.threads.runs.retrieve(
    thread_id=thread.id,
    run_id=run.id
  )
  print(run.status)

messages = openai.beta.threads.messages.list(
  thread_id=thread.id
)

print(messages.data[0].content[0].text.value)
7 Likes

After constructing the assistant and the messages and others, you use the “runs” to actually set it into motion, and then get the response.

All the steps:

https://platform.openai.com/docs/assistants/overview

Also be sure to have the “list run steps” and “cancel runs” methods handy in case they go crazy.

https://platform.openai.com/docs/api-reference/runs/listRuns

1 Like

Indeed, yesterday I have also found the solution you need to use a while as you did to keep checking the state, and wait for completion.

They admit it is cumbersome and said they will improve.

In JavaScript, I use !==, to make sure will make the strong ==

Thought to share this recording. The code is also there at the bottom of the video

5 Likes

@ joyasree78 Awesome tutorial. Thanks for sharing

@joyasree78 wondering how to keep the interaction on the same thread. When I add print("FYI, your thread is: ", thread.id) before printing the message content, I’m getting different thread IDs. As a result, the assistant doesn’t remember previous questions. Thanks

@ibrahim.aka.ajax Remember that each thread is an instance and if you are in a loop creating a thread and messages on each iteration the thread.id is going to be different. I think that could be the case, because when calling the run with the assitantId and threadId you provide those values so it doesn’t have to change. It could be helpful to have a snapshot or your code.

So, if I understand this correctly (assuming the assistant is already created and file(s) uploaded), we create the thread with the first question, then loop until it responds. To follow up with another question in the same thread…? That’s the question. I’m actually programming in php, using curl, but a python example of how to do that would be just as helpful.

The steps are as follow:

  1. Create an assistance (Verify is a unique instance by retrieving the assistance id)
  2. Create the thread (You could have as many as you want but to keep the context of a conversation you have to pass the same thread id you want to follow)
  3. Create messages on your thread (This could be user inputs to start the conversation Important to use the thread id you want to create messages for)
  4. Run the thread with the assistance you want (Imagine you told someone to execute a task the agent is going to queue the task and proceed to execute it (status: in-progress) here is where you have to pool the run with the runId until the status change to completed which means the responses of your assistance where added to the thread Important to provide the assistanceId you want to use with the threadId you want to provide the context of the conversation. Also when you execute a run the thread locks which means you can’t add messages to the thread or execute another run over it until the state passes to a terminal state)
  5. Retrieve the messages from the thread (You could identify the messages by the role: “assistant”)
1 Like

where did you find this portion? - ```
print(messages.data[0].content[0].text.value)

The official documentation stops at that step, just says “And finally, display them to the user!” haha

2 Likes

I had to print the JSON and keep testing until I was able to find this.
That is, manually.
The documentation is pretty bad, sadly. They just release piece of codes, and you need to figure it out the rest.

@jorgeguerrabrazil Your solution works for me. In simple cases I got 48 runs of the while loop. An asyncronous version of openai.beta.threads.runs.create would be preferable in the final (non-beta) version of the lib (if possible).

@jorgeguerrabrazil

As this topic has a selected solution can it be closed?

1 Like

Yes, you can!
Thanks :ok_hand::grin::two_hearts:
The solution is okay

1 Like