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);
3 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)
5 Likes
_j
3
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
3 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
WiFo
9
@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.
Benzri
12
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).