Using OpenAi’s Assistants API
Assistants API makes it easier to build assistive AI apps that have goals and can call models and tools
Thread: A conversation session between an Assistant and a user. Threads store Messages.
Message: A message created by an Assistant or a user. Messages can include text, images, and other files. Messages stored as a list on the Thread.
Run: An invocation of an Assistant on a Thread. The Assistant uses it’s configuration and the Thread’s Messages to perform tasks by calling models and tools. As part of a Run, the Assistant appends Messages to the Thread.
Run Step: A detailed list of steps the Assistant took as part of a Run. An Assistant can call tools or create Messages during it’s run. Examining Run Steps allows you to introspect how the Assistant is getting to it’s final results.
First, we create a session of a client
You can use OpenAI’s playground to create your Assistant in a user interface.
Get the ID of your Assistant (in the Playground below the name of the Assistant)
A Thread represents a conversation.
Pass any user-specific context and files in this thread by creating Messages.
ThreadMessage(id='msg_9x1SPRtQLbWqXMVNIXkBZ0tF', assistant_id=None, content=[MessageContentText(text=Text(annotations=[], value='I need to solve the equation `3x + 11 = 14`. Can you help me?'), type='text')], created_at=1699425718, file_ids=[], metadata={}, object='thread.message', role='user', run_id=None, thread_id='thread_5uZgyTMf0t7Im9TPxvAVDmUD')For the Assistant to respond to the user message, you need to create a Run.
This makes the Assistant read the Thread and decide whether to call tools or simply use the model to best answer the user query.
As the run progresses, the assistant appends Messages to the thread with the role="assistant".
You can optionally pass additional instructions to the Assistant while creating the Run
This creates a Run in a queued status.
You can periodically retrieve the Run to check on its status to see if it has moved to completed.
Run(id='run_EF9AwlwFnOQJ3wPRCIdNGVaS', assistant_id='asst_YhNjYRQT38e4I5N8WsZ7ihz1', cancelled_at=None, completed_at=None, created_at=1699425952, expires_at=1699426552, failed_at=None, file_ids=[], instructions='Please address the user as Jane Doe. The user has a premium account.', last_error=None, metadata={}, model='gpt-4-1106-preview', object='thread.run', required_action=None, started_at=None, status='queued', thread_id='thread_5uZgyTMf0t7Im9TPxvAVDmUD', tools=[ToolAssistantToolsCode(type='code_interpreter')])Once the Run completes, you can retrieve the Messages added by the Assistant to the Thread.
SyncCursorPage[ThreadMessage](data=[ThreadMessage(id='msg_aFjIRT2SoI0i3zcAVlFsx3Yn', assistant_id='asst_YhNjYRQT38e4I5N8WsZ7ihz1', content=[MessageContentText(text=Text(annotations=[], value='The solution to the equation \\(3x + 11 = 14\\) is \\(x = 1\\).'), type='text')], created_at=1699425958, file_ids=[], metadata={}, object='thread.message', role='assistant', run_id='run_EF9AwlwFnOQJ3wPRCIdNGVaS', thread_id='thread_5uZgyTMf0t7Im9TPxvAVDmUD'), ThreadMessage(id='msg_9x1SPRtQLbWqXMVNIXkBZ0tF', assistant_id=None, content=[MessageContentText(text=Text(annotations=[], value='I need to solve the equation `3x + 11 = 14`. Can you help me?'), type='text')], created_at=1699425718, file_ids=[], metadata={}, object='thread.message', role='user', run_id=None, thread_id='thread_5uZgyTMf0t7Im9TPxvAVDmUD')], object='list', first_id='msg_aFjIRT2SoI0i3zcAVlFsx3Yn', last_id='msg_9x1SPRtQLbWqXMVNIXkBZ0tF', has_more=False)This tutorial is based on OpenAI’s Assistant API documentation
Congratulations! You have completed this tutorial 👍
Next, you may want to go back to the lab’s website
Jan Kirenz