"The new [Assistants API](https://platform.openai.com/docs/assistants/overview) is a stateful evolution of our [Chat Completions API](https://platform.openai.com/docs/guides/text-generation/chat-completions-api) meant to simplify the creation of assistant-like experiences, and enable developer access to powerful tools like Code Interpreter and Retrieval.\n",
"\n",
"## Chat Completions API vs Assistants API\n",
"\n",
"The primitives of the **Chat Completions API** are `Messages`, on which you perform a `Completion` with a `Model` (`gpt-3.5-turbo`, `gpt-4`, etc). It is lightweight and powerful, but inherently stateless, which means you have to manage conversation state, tool definitions, retrieval documents, and code execution manually.\n",
"\n",
"The primitives of the **Assistants API** are\n",
"\n",
"- `Assistants`, which encapsulate a base model, instructions, tools, and (context) documents,\n",
"- `Threads`, which represent the state of a conversation, and\n",
"- `Runs`, which power the execution of an `Assistant` on a `Thread`, including textual responses and multi-step tool use.\n",
"\n",
"We'll take a look at how these can be used to create powerful, stateful experiences.\n"
"> We've updated our [Python SDK](https://github.com/openai/openai-python) to add support for the Assistants API, so you'll need to update it to the latest version (`1.2.3` at time of writing).\n"
" 'instructions': 'You are a personal math tutor. Answer questions briefly, in a sentence or less.',\n",
" 'metadata': {},\n",
" 'model': 'gpt-4-1106-preview',\n",
" 'name': 'Math Tutor',\n",
" 'object': 'assistant',\n",
" 'tools': []}"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from openai import OpenAI\n",
"\n",
"client = OpenAI()\n",
"\n",
"assistant = client.beta.assistants.create(\n",
" name=\"Math Tutor\",\n",
" instructions=\"You are a personal math tutor. Answer questions briefly, in a sentence or less.\",\n",
" model=\"gpt-4-1106-preview\",\n",
")\n",
"show_json(assistant)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Regardless of whether you create your Assistant through the Dashboard or with the API, you'll want to keep track of the Assistant ID. This is how you'll refer to your Assistant throughout Threads and Runs.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we'll create a new Thread and add a message to it. This will hold the state of our conversation, so we don't have re-send the entire message history each time.\n"
" content=\"I need to solve the equation `3x + 11 = 14`. Can you help me?\",\n",
")\n",
"show_json(message)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> **Note**\n",
"> Even though you're no longer sending the entire history each time, you will still be charged for the tokens of the entire conversation history with each Run.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Runs\n",
"\n",
"Notice how the Thread we created is **not** associated with the Assistant we created earlier! Threads exist independently from Assistants, which may be different from what you'd expect if you've used ChatGPT (where a thread is tied to a model/GPT).\n",
"\n",
"To get a completion from an Assistant for a given Thread, we must create a Run. Creating a Run will indicate to an Assistant it should look at the messages in the Thread and take action: either by adding a single response, or using tools.\n",
"\n",
"> **Note**\n",
"> Runs are a key difference between the Assistants API and Chat Completions API. While in Chat Completions the model will only ever respond with a single message, in the Assistants API a Run may result in an Assistant using one or multiple tools, and potentially adding multiple messages to the Thread.\n",
"\n",
"To get our Assistant to respond to the user, let's create the Run. As mentioned earlier, you must specify _both_ the Assistant and the Thread.\n"
"Unlike creating a completion in the Chat Completions API, **creating a Run is an asynchronous operation**. It will return immediately with the Run's metadata, which includes a `status` that will initially be set to `queued`. The `status` will be updated as the Assistant performs operations (like using tools and adding messages).\n",
"\n",
"To know when the Assistant has completed processing, we can poll the Run in a loop. (Support for streaming is coming soon!) While here we are only checking for a `queued` or `in_progress` status, in practice a Run may undergo a variety of status changes which you can choose to surface to the user. (These are called Steps, and will be covered later.)\n"
"As you can see, Messages are ordered in reverse-chronological order – this was done so the most recent results are always on the first `page` (since results can be paginated). Do keep a look out for this, since this is the opposite order to messages in the Chat Completions API.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's ask our Assistant to explain the result a bit further!\n"
" 'value': 'To solve `3x + 11 = 14`, you want to isolate x. First, subtract 11 from both sides to remove the constant term on the left, leaving you with `3x = 3`. Then divide both sides by 3, the coefficient of x, to get `x = 1`, which is your solution.'},\n",
"This may feel like a lot of steps to get a response back, especially for this simple example. However, you'll soon see how we can add very powerful functionality to our Assistant without changing much code at all!\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Example\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's take a look at how we could potentially put all of this together. Below is all the code you need to use an Assistant you've created.\n",
"\n",
"Since we've already created our Math Assistant, I've saved its ID in `MATH_ASSISTANT_ID`. I then defined two functions:\n",
"\n",
"- `submit_message`: create a Message on a Thread, then start (and return) a new Run\n",
"- `get_response`: returns the list of Messages in a Thread\n"
"I've also defined a `create_thread_and_run` function that I can re-use (which is actually almost identical to the [`client.beta.threads.create_and_run`](https://platform.openai.com/docs/api-reference/runs/createThreadAndRun) compound function in our API ;) ). Finally, we can submit our mock user requests each to a new Thread.\n",
"\n",
"Notice how all of these API calls are asynchronous operations; this means we actually get async behavior in our code without the use of async libraries! (e.g. `asyncio`)\n"
"assistant: Linear algebra is the branch of mathematics concerning vector spaces and linear mappings between such spaces, which includes the study of lines, planes, and subspaces, as well as concepts like vectors, matrices, determinants, eigenvalues, and eigenvectors.\n",
"assistant: Try to find real-life applications of math that interest you, work with a tutor or friend who can make the learning process more enjoyable, or set small, achievable goals to build your confidence and appreciation for the subject over time.\n",
"assistant: Try to find real-life applications of math that interest you, work with a tutor or friend who can make the learning process more enjoyable, or set small, achievable goals to build your confidence and appreciation for the subject over time.\n",
"You may have noticed that this code is not actually specific to our math Assistant at all... this code will work for any new Assistant you create simply by changing the Assistant ID! That is the power of the Assistants API.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tools\n",
"\n",
"A key feature of the Assistants API is the ability to equip our Assistants with Tools, like Code Interpreter, Retrieval, and custom Functions. Let's take a look at each.\n",
"\n",
"### Code Interpreter\n",
"\n",
"Let's equip our Math Tutor with the Code Interpreter tool, which we can do from the Dashboard...\n"
" \"Generate the first 20 fibbonaci numbers with code.\"\n",
")\n",
"run = wait_on_run(run, thread)\n",
"pretty_print(get_response(thread))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And that's it! The Assistant used Code Interpreter in the background, and gave us a final response.\n",
"\n",
"For some use cases this may be enough –however, if we want more details on what precisely an Assistant is doing we can take a look at a Run's Steps.\n",
"\n",
"### Steps\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A Run is composed of one or more Steps. Like a Run, each Step has a `status` that you can query. This is useful for surfacing the progress of a Step to a user (e.g. a spinner while the Assistant is writing code or performing retrieval).\n"
"1. `tool_calls` (plural, since it could be more than one in a single Step)\n",
"2. `message_creation`\n",
"\n",
"The first Step is a `tool_calls`, specifically using the `code_interpreter` which contains:\n",
"\n",
"- `input`, which was the Python code generated before the tool was called, and\n",
"- `output`, which was the result of running the Code Interpreter.\n",
"\n",
"The second Step is a `message_creation`, which contains the `message` that was added to the Thread to communicate the results to the user.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Retrieval\n",
"\n",
"Another powerful tool in the Assistants API is Retrieval: the ability to upload files that the Assistant will use as a knowledge base when answering questions. This can also be enabled from the Dashboard or the API, where we can upload files we want to be used.\n"
"user: What are some cool math concepts behind this ML paper pdf? Explain in two sentences.\n",
"assistant: The document does not contain explicit sections with the phrases \"math concepts\", \"mathematical\", or \"algorithm\". Without visible content or a text-based search that yields results, I am unable to identify the mathematical concepts in the paper.\n",
"\n",
"I would recommend uploading the PDF again, ensuring that it can be accessed with the tools provided, so that I can assist you properly.\n",
" \"What are some cool math concepts behind this ML paper pdf? Explain in two sentences.\"\n",
")\n",
"run = wait_on_run(run, thread)\n",
"pretty_print(get_response(thread))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> **Note**\n",
"> There are more intricacies in Retrieval, like [Annotations](https://platform.openai.com/docs/assistants/how-it-works/managing-threads-and-messages), which may be covered in another cookbook.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Functions\n",
"\n",
"As a final powerful tool for your Assistant, you can specify custom Functions (much like the [Function Calling](https://platform.openai.com/docs/guides/function-calling) in the Chat Completions API). During a Run, the Assistant can then indicate it wants to call one or more functions you specified. You are then responsible for calling the Function, and providing the output back to the Assistant.\n",
"\n",
"Let's take a look at an example by defining a `display_quiz()` Function for our Math Tutor.\n",
"\n",
"This function will take a `title` and an array of `question`s, display the quiz, and get input from the user for each:\n",
"Unfortunately I don't know how to get user input within a Python Notebook, so I'll be mocking out responses with `get_mock_response...`. This is where you'd get the user's actual input.\n"
"> Pasting the function JSON into the Dashboard was a bit finicky due to indentation, etc. I just asked ChatGPT to format my function the same as one of the examples on the Dashboard :).\n"
"The `required_action` field indicates a Tool is waiting for us to run it and submit its output back to the Assistant. Specifically, the `display_quiz` function! Let's start by parsing the `name` and `arguments`.\n",
"\n",
"> **Note**\n",
"> While in this case we know there is only one Tool call, in practice the Assistant may choose to call multiple tools.\n"
"Great! (Remember these responses are the one's we mocked earlier. In reality, we'd be getting input from the back from this function call.)\n",
"\n",
"Now that we have our responses, let's submit them back to the Assistant. We'll need the `tool_call` ID, found in the `tool_call` we parsed out earlier. We'll also need to encode our `list`of responses into a `str`.\n"
"assistant: For the first question regarding the Pythagorean theorem, it is important to know that this theorem relates the lengths of the sides of a right-angled triangle. Specifically, the theorem states that the square of the length of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the lengths of the other two sides. This is intrinsically linked to the properties of a right angle and the way distances are related in Euclidean geometry; as such it only applies to right-angled triangles and not to other types of triangles.\n",
"For the second question concerning the derivative of the function \\( f(x) = 3x^2 + 2x + 1 \\), the correct answer is \\( f'(x) = 6x + 2 \\), which means your choice of \"a\" was correct. The derivative of a function gives the rate of change of the function's output with respect to changes in the input. In the case of a polynomial function like the one given, the power rule can be applied to find the derivative. For the term \\( 3x^2 \\), the derivative is \\( 6x \\), and for \\( 2x \\), it is \\( 2 \\), and the constant \\( 1 \\) drops out as its derivative is zero. So the full derivative function is \\( f'(x) = 6x + 2 \\).\n",
"Overall, well done on the multiple-choice question, and now you have a bit more insight on the open-ended question. Keep studying and asking questions to strengthen your understanding!\n",
"We covered a lot of ground in this notebook, give yourself a high-five! Hopefully you should now have a strong foundation to build powerful, stateful experiences with tools like Code Interpreter, Retrieval, and Functions!\n",
"\n",
"There's a few sections we didn't cover for the sake of brevity, so here's a few resources to explore further:\n",
"- [Files](https://platform.openai.com/docs/api-reference/assistants/file-object): Thread scoped vs Assistant scoped\n",
"- [Parallel Function Calls](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling): calling multiple tools in a single Step\n",
"- Multi-Assistant Thread Runs: single Thread with Messages from multiple Assistants\n",
"- Streaming: coming soon!\n",
"\n",
"Now go off and build something ama[zing](https://www.youtube.com/watch?v=xvFZjo5PgG0&pp=ygUQcmljayByb2xsIG5vIGFkcw%3D%3D)! "