OpenAI Assistants API

Notes on OpenAI Assistants API.


There are some benefits on using Assistants over completion. Among them:

  • Being able to save context and messages
  • Retrieval mechanisim for digging throught data (Retrieval tool)
  • Function calling

Model

Building blocks

How Assistant API interacts and works

Model

  • Assistant needs a thread in order to have context, as all sent messages are There
  • Runs needs a reference to assistant and thread so it knows what knowledge base it needs to use, as well as, other tools (if needed) to return a response.

On the UI, we can use the playground to check each one of the steps involved in a run.

Run statuses

  • Informative: cancelled, cancelling, expired, failed, in-progress, queued, completed
  • Needs action: require_action
    • This status will have attached a function call name as well as the arguments to be passed. Once executed, one must return to the run with the output of such function execution so the assistant can continue its work
    • On return, object should be as follows:
      const run = await openai.beta.threads.runs.submitToolOutputs(
        thread.id,
        run.id,
        {
          tool_outputs: [
            {
              tool_call_id: callIds[0],
              output: "22C",
            },
            {
              tool_call_id: callIds[1],
              output: "LA",
            },
          ],
        }
      );
      

Once we run this code, the assistant will go back to the queued state

Knowledge retrieval on Assistant

Once the file is uploaded to OpenAI's servers, it is splitted and stored in a Vector Store. This makes it easier to find relevant splits on an Assistant run afterwards due to embeddings. Small representations of such splits that make it easier to find neares neightbours