"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."
"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",
" 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"
"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",
"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](https://platform.openai.com/docs/api-reference/runs/object#runs/object-status) 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': 'Certainly. To solve for x in the equation `3x + 11 = 14`:\\n\\n1. Subtract 11 from both sides: `3x + 11 - 11 = 14 - 11` simplifies to `3x = 3`.\\n2. Divide both sides by 3: `3x / 3 = 3 / 3` simplifies to `x = 1`.\\n\\nSo, the solution is `x = 1`.'},\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 that deals with vector spaces, linear equations, and matrices, focusing on the properties and operations that can be applied to vectors and linear transformations.\n",
"assistant: Try finding aspects of math that relate to your interests or daily life, and consider a tutor or interactive resources to make learning more enjoyable.\n",
"assistant: Try finding aspects of math that relate to your interests or daily life, and consider a tutor or interactive resources to make learning more enjoyable.\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",
"Let's equip our Math Tutor with the [Code Interpreter](https://platform.openai.com/docs/assistants/tools/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"
" 'code_interpreter': {'input': '# Python function to generate the first 20 Fibonacci numbers\\ndef fibonacci(n):\\n fib_sequence = [0, 1]\\n while len(fib_sequence) < n:\\n fib_sequence.append(fib_sequence[-1] + fib_sequence[-2])\\n return fib_sequence\\n\\n# Generate the first 20 Fibonacci numbers\\nfirst_20_fibonacci = fibonacci(20)\\nfirst_20_fibonacci',\n",
"Another powerful tool in the Assistants API is [Retrieval](https://platform.openai.com/docs/assistants/tools/knowledge-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"
"assistant: I am unable to find specific sections referring to \"cool math concepts\" directly in the paper using the available tools. I will now read the beginning of the paper to identify any mathematical concepts that are fundamental to the paper.\n",
"assistant: The paper discusses leveraging large language models as a framework for unsupervised multitask learning, where tasks are implicitly defined by the context within sequences of text. It explores the zero-shot learning capabilities of such models by showing that when a language model is trained on a vast dataset, it begins to generalize and perform tasks without explicit supervision, achieving competitive results across various natural language processing tasks using a probabilistic framework based on sequential modeling and conditional probabilities.\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"
"As a final powerful tool for your Assistant, you can specify custom [Functions](https://platform.openai.com/docs/assistants/tools/function-calling) (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",
"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"
" 'function': {'arguments': '{\\n \"title\": \"Mathematics Quiz\",\\n \"questions\": [\\n {\\n \"question_text\": \"Explain why the square root of a negative number is not a real number.\",\\n \"question_type\": \"FREE_RESPONSE\"\\n },\\n {\\n \"question_text\": \"What is the value of an angle in a regular pentagon?\",\\n \"choices\": [\\n \"72 degrees\",\\n \"90 degrees\",\\n \"108 degrees\",\\n \"120 degrees\"\\n ],\\n \"question_type\": \"MULTIPLE_CHOICE\"\\n }\\n ]\\n}',\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"
"For the first question, it's important to know that the square root of a negative number is not a real number because real numbers consist of all the numbers on the number line, and that includes all positive numbers, zero, and negative numbers. However, the square root of a negative number is not on this number line; instead, it is what we call an imaginary number. When we want to take the square root of a negative number, we typically use the imaginary unit \\(i\\), where \\(i\\) is defined as \\(\\sqrt{-1}\\).\n",
"For the second question, the correct answer is \"108 degrees.\" In a regular pentagon, which is a five-sided polygon with equal sides and angles, each interior angle is \\(108\\) degrees. This is because the sum of the interior angles of a pentagon is \\(540\\) degrees, and when divided by \\(5\\) (the number of angles), it gives \\(108\\) degrees per angle. The choice you selected, \"72 degrees,\" actually refers to the external angle of a regular pentagon, not the internal angle.\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",