Create one my Assistants and my Threads, how do I use it?

create one my Assistants and my Threads, how do I use it? create an assistant that answers math questions, on the playground it works well, how do I send questions to it now? I need an example in CURL

1 Like

You have to use the Assistant API.
Look at the API references:

The key point is that every time you create an entity, you get an ID for that object back, you have to then use that id in subsequent calls, for example:

curl "https://api.openai.com/v1/assistants" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: assistants=v1" \
  -d '{
    "instructions": "You are a personal math tutor. When asked a question, write and run Python code to answer the question.",
    "name": "Math Tutor"
    "tools": [{"type": "code_interpreter"}],
    "model": "gpt-4"
  }'

returns

{
  "id": "asst_abc123",
  "object": "assistant",
  "created_at": 1698984975,
  "name": "Math Tutor",
  "description": null,
  "model": "gpt-4",
  "instructions": "You are a personal math tutor. When asked a question, write and run Python code to answer the question.",
  "tools": [
    {
      "type": "code_interpreter"
    }
  ],
  "file_ids": [],
  "metadata": {}
}

Note that the assistant ID is “asst_abc123”.

The same will happen with the creation of a thread (conversation with a user).

Then you use the Assistant ID and the Theard ID to send that to the Run endpoint. Then the run endpoint will execute the conversation.

curl https://api.openai.com/v1/threads/thread_BDDwIqM4KgHibXX3mqmN3Lgs/runs \
  -H 'Authorization: Bearer $OPENAI_API_KEY' \
  -H 'Content-Type: application/json' \
  -H 'OpenAI-Beta: assistants=v1' \
  -d '{
    "assistant_id": "asst_nGl00s4xa9zmVY6Fvuvz9wwQ"
  }'

if you pay close attention you can see that the thread ID is part of the path, and the assistant ID is part of the data payload.

Right now there is no streaming support for the run endpoint so you have to keep requesting for the run status in a loop using the Run’s ID.

curl https://api.openai.com/v1/threads/thread_BDDwIqM4KgHibXX3mqmN3Lgs/runs/run_5pyUEwhaPk11vCKiDneUWXXY \
  -H 'Authorization: Bearer $OPENAI_API_KEY' \
  -H 'OpenAI-Beta: assistants=v1'

then you get back

{
  "id": "run_UWvV94U0FQYiT2rlbBrdEVmC",
  "object": "thread.run",
  "created_at": 1699063290,
  "assistant_id": "asst_nGl00s4xa9zmVY6Fvuvz9wwQ",
  "thread_id": "thread_BDDwIqM4KgHibXX3mqmN3Lgs",
  "status": "queued",
  "started_at": 1699063290,
  "expires_at": null,
  "cancelled_at": null,
  "failed_at": null,
  "completed_at": 1699063291,
  "last_error": null,
  "model": "gpt-4",
  "instructions": null,
  "tools": [
    {
      "type": "code_interpreter"
    }
  ],
  "file_ids": [
    "file-9F1ex49ipEnKzyLUNnCA0Yzx",
    "file-dEWwUbt2UGHp3v0e0DpCzemP"
  ],
  "metadata": {}
}

Note that the initial status is “queued”. Keep requesting and when you get back the status “completed” then retrieve the messages from the thread.

curl https://api.openai.com/v1/threads/thread_BDDwIqM4KgHibXX3mqmN3Lgs/runs/run_5pyUEwhaPk11vCKiDneUWXXY \
  -H 'Authorization: Bearer $OPENAI_API_KEY' \
  -H 'OpenAI-Beta: assistants=v1'

you will get

{
  "id": "run_5pyUEwhaPk11vCKiDneUWXXY",
  "object": "thread.run",
  "created_at": 1699075072,
  "assistant_id": "asst_nGl00s4xa9zmVY6Fvuvz9wwQ",
  "thread_id": "thread_BDDwIqM4KgHibXX3mqmN3Lgs",
  "status": "completed",
  "started_at": 1699075072,
  "expires_at": null,
  "cancelled_at": null,
  "failed_at": null,
  "completed_at": 1699075073,
  "last_error": null,
  "model": "gpt-3.5-turbo",
  "instructions": null,
  "tools": [
    {
      "type": "code_interpreter"
    }
  ],
  "file_ids": [
    "file-9F1ex49ipEnKzyLUNnCA0Yzx",
    "file-dEWwUbt2UGHp3v0e0DpCzemP"
  ],
  "metadata": {}
}

now that the status is completed, you have to retrieve the thread, which will now have extra messages added by the assistant.

curl https://api.openai.com/v1/threads/thread_abc123/messages \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: assistants=v1"

returns

{
  "object": "list",
  "data": [
    {
      "id": "msg_abc123",
      "object": "thread.message",
      "created_at": 1699016383,
      "thread_id": "thread_abc123",
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": {
            "value": "How does AI work? Explain it in simple terms.",
            "annotations": []
          }
        }
      ],
      "file_ids": [],
      "assistant_id": null,
      "run_id": null,
      "metadata": {}
    },
    {
      "id": "msg_abc456",
      "object": "thread.message",
      "created_at": 1699016383,
      "thread_id": "thread_abc123",
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": {
            "value": "Hello, what is AI?",
            "annotations": []
          }
        }
      ],
      "file_ids": [
        "file-abc123"
      ],
      "assistant_id": null,
      "run_id": null,
      "metadata": {}
    }
  ],
  "first_id": "msg_abc123",
  "last_id": "msg_abc456",
  "has_more": false
}

Note that the messages are in “data” array, then find the last assistant role and get the “text” and then “value”.

Is all in the API reference.

3 Likes

I have a fairly extensive integration of the Assistants API into Langroid (multi-agent framework for LLMs), to leverage thread/assistant persistence, conversation state, tools (fn-calling, file-uploads, code-interpreter, retrieval/RAG). Specifically about your question on threads and assistants, I cache the thread, assistant ids based on user/machine/org (a simple way for now).

Some links you might find useful:

I’ve created 7 assistant api demos on github, davideuler/awesome-assistant-api
You can run them on colab or on your jupyter notebooks. Hope it helps.

1 Like