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);
@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.
Create an assistance (Verify is a unique instance by retrieving the assistance id)
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)
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)
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)
Retrieve the messages from the thread (You could identify the messages by the role: “assistant”)
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).