#Assistant API thread run response

1 messages · Page 1 of 1 (latest)

gentle hinge
#

Why im unable to get the response of thread even though im running the thread
Here is my complete code :

exports.petMateAssistant = functions.https.onRequest(async (req, res) => {
   
    try {
        const configSnapshot = await refs.config.get();
         const configData = configSnapshot.docs[0].data();
         const { assistantId,vectorStoreId } = configData;
        const snapshot = await refs.pets.where('userId', '==', userID).get();
        if (snapshot.empty) {
            res.status(404).send('No pet found for the provided userID');
            return;
        }
        const petData = snapshot.docs[0].data();

        let petInstructions = petAssistant()
        const thread = await openai.beta.threads.create({
            messages: [ { role: "user", content: query} ],
            tool_resources: {
              "file_search": {
                "vector_store_ids": [vectorStoreId]
              }
            }
          });
          const run = await openai.beta.threads.runs.create(
            thread.id,
            {
              assistant_id: assistantId,
              model: "gpt-4o",
              instructions: petInstructions,
              tools: [{"type": "file_search"}]
            }
          );


        const completedRun = await waitForCompletion(thread.id, run.id);
       
        res.status(200).json({
            response: "Response"
        });
    } catch (error) {
        console.error(error);
        res.status(500).send(`Error: ${error.message}`);
    }
})
round surgeBOT
#

Hi @gentle hinge. 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, it seems like you are creating a thread and a run, but not retrieving the messages from the completed run. The waitForCompletion function is not defined in the provided code, so I'm assuming it's a function that waits for the run to complete.

To retrieve the messages from the completed run, you can use the client.beta.threads.messages.list method as shown in the code snippet from the knowledge source (https://community.openai.com/t/583579):

# Retrieve messages from thread after run complete
messages = client.beta.threads.messages.list(
    thread_id=thread_ID
)

for message in messages.data:
    for content in message.content:
        if content.type == 'text' and content.text and content.text.value:
            value = content.text.value
            print("### Extracted value:", value)

In your case, you would need to translate this Python code into JavaScript, and replace thread_ID with thread.id from your code. This will retrieve the list of messages from the completed run, and you can then extract the assistant's response from these messages.

Please note that the actual implementation might vary depending on the structure of the response and the specific requirements of your application.

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