diff --git a/authors.yaml b/authors.yaml index e273c542..e8f9188b 100644 --- a/authors.yaml +++ b/authors.yaml @@ -33,6 +33,11 @@ mwu1993: website: "https://www.linkedin.com/in/michael-wu-77440977/" avatar: "https://media.licdn.com/dms/image/C5603AQFhQfx0I-raUg/profile-displayphoto-shrink_800_800/0/1527451059977?e=1704931200&v=beta&t=wkFOj0Rigp6e9wm7aZdziOQ6jHARyXj3EoK1K8jaaas" +ibigio: + name: "Ilan Bigio" + website: "https://twitter.com/ilanbigio" + avatar: "https://pbs.twimg.com/profile_images/1688302223250378752/z-99TOMH_400x400.jpg" + jhills20: name: "James Hills" website: "https://twitter.com/jimmerhills" diff --git a/examples/Assistants_API_overview_python.ipynb b/examples/Assistants_API_overview_python.ipynb new file mode 100644 index 00000000..12d4f7c4 --- /dev/null +++ b/examples/Assistants_API_overview_python.ipynb @@ -0,0 +1,1861 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Assistants API Overview (Python SDK)\n", + "\n", + "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" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Setup\n", + "\n", + "### Python SDK \n", + "\n", + "> **Note**\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.1` at time of writing).\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: openai in /Users/ilan/.virtualenvs/openai/lib/python3.9/site-packages (1.2.3)\n", + "Requirement already satisfied: anyio<4,>=3.5.0 in /Users/ilan/.virtualenvs/openai/lib/python3.9/site-packages (from openai) (3.6.2)\n", + "Requirement already satisfied: distro<2,>=1.7.0 in /Users/ilan/.virtualenvs/openai/lib/python3.9/site-packages (from openai) (1.8.0)\n", + "Requirement already satisfied: httpx<1,>=0.23.0 in /Users/ilan/.virtualenvs/openai/lib/python3.9/site-packages (from openai) (0.23.3)\n", + "Requirement already satisfied: pydantic<3,>=1.9.0 in /Users/ilan/.virtualenvs/openai/lib/python3.9/site-packages (from openai) (1.10.9)\n", + "Requirement already satisfied: tqdm>4 in /Users/ilan/.virtualenvs/openai/lib/python3.9/site-packages (from openai) (4.64.1)\n", + "Requirement already satisfied: typing-extensions<5,>=4.5 in /Users/ilan/.virtualenvs/openai/lib/python3.9/site-packages (from openai) (4.5.0)\n", + "Requirement already satisfied: idna>=2.8 in /Users/ilan/.virtualenvs/openai/lib/python3.9/site-packages (from anyio<4,>=3.5.0->openai) (3.4)\n", + "Requirement already satisfied: sniffio>=1.1 in /Users/ilan/.virtualenvs/openai/lib/python3.9/site-packages (from anyio<4,>=3.5.0->openai) (1.3.0)\n", + "Requirement already satisfied: certifi in /Users/ilan/.virtualenvs/openai/lib/python3.9/site-packages (from httpx<1,>=0.23.0->openai) (2022.12.7)\n", + "Requirement already satisfied: httpcore<0.17.0,>=0.15.0 in /Users/ilan/.virtualenvs/openai/lib/python3.9/site-packages (from httpx<1,>=0.23.0->openai) (0.16.3)\n", + "Requirement already satisfied: rfc3986<2,>=1.3 in /Users/ilan/.virtualenvs/openai/lib/python3.9/site-packages (from rfc3986[idna2008]<2,>=1.3->httpx<1,>=0.23.0->openai) (1.5.0)\n", + "Requirement already satisfied: h11<0.15,>=0.13 in /Users/ilan/.virtualenvs/openai/lib/python3.9/site-packages (from httpcore<0.17.0,>=0.15.0->httpx<1,>=0.23.0->openai) (0.14.0)\n" + ] + } + ], + "source": [ + "!pip install --upgrade openai" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And make sure it's up to date by running:\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Version: 1.2.3\n" + ] + } + ], + "source": [ + "!pip show openai | grep Version" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Pretty Printing Helper" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "import json\n", + "\n", + "# Just a helper for more readable output\n", + "def show_json(obj):\n", + " display(json.loads(obj.model_dump_json()))\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Complete Example with Assistants API\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Assistants\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The easiest way to get started with the Assistants API is through the [Assistants Playground](https://platform.openai.com/playground).\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\"Screenshot\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's begin by creating an assistant! We'll create a Math Tutor just like in our [docs](https://platform.openai.com/docs/assistants/overview).\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\"Screenshot\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can view Assistants you've created in the [Assistants Dashboard](https://platform.openai.com/assistants).\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\"Screenshot\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can also create Assistants directly through the Assistants API, like so:\n" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'id': 'asst_7t98qt8iVDP7xfJnRJpQ2zYe',\n", + " 'created_at': 1699648364,\n", + " 'description': None,\n", + " 'file_ids': [],\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" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Threads\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a new thread:\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "application/json": { + "created_at": 1699647704, + "id": "thread_vtbuepG0aXU9xr82wJbi7fRH", + "metadata": {}, + "object": "thread" + }, + "text/plain": [ + "" + ] + }, + "metadata": { + "application/json": { + "expanded": false, + "root": "root" + } + }, + "output_type": "display_data" + } + ], + "source": [ + "thread = client.beta.threads.create()\n", + "show_json(thread)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Then add the message to the thread:\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "application/json": { + "assistant_id": null, + "content": [ + { + "text": { + "annotations": [], + "value": "I need to solve the equation `3x + 11 = 14`. Can you help me?" + }, + "type": "text" + } + ], + "created_at": 1699647704, + "file_ids": [], + "id": "msg_0xvY310ibhmno6VOiDGFgKjF", + "metadata": {}, + "object": "thread.message", + "role": "user", + "run_id": null, + "thread_id": "thread_vtbuepG0aXU9xr82wJbi7fRH" + }, + "text/plain": [ + "" + ] + }, + "metadata": { + "application/json": { + "expanded": false, + "root": "root" + } + }, + "output_type": "display_data" + } + ], + "source": [ + "message = client.beta.threads.messages.create(\n", + " thread_id=thread.id,\n", + " role=\"user\",\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" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "run = client.beta.threads.runs.create(\n", + " thread_id=thread.id,\n", + " assistant_id=assistant.id,\n", + ")\n", + "show_json(run)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import time\n", + "\n", + "def wait_on_run(run, thread):\n", + " while run.status == \"queued\" or run.status == \"in_progress\":\n", + " run = client.beta.threads.runs.retrieve(\n", + " thread_id=thread.id,\n", + " run_id=run.id,\n", + " )\n", + " time.sleep(0.5)\n", + " return run" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "WARNING:param.JSON00142: Displaying Panel objects in the notebook requires the panel extension to be loaded. Ensure you run pn.extension() before displaying objects in the notebook.\n" + ] + }, + { + "data": { + "text/plain": [ + "JSON(str)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "run = wait_on_run(run, thread)\n", + "show_json(run)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Messages\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that the Run has completed, we can list the Messages in the Thread to see what got added by the Assistant.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "WARNING:param.JSON00148: Displaying Panel objects in the notebook requires the panel extension to be loaded. Ensure you run pn.extension() before displaying objects in the notebook.\n" + ] + }, + { + "data": { + "text/plain": [ + "JSON(str)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "messages = client.beta.threads.messages.list(thread_id=thread.id)\n", + "show_json(messages)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "WARNING:param.JSON00154: Displaying Panel objects in the notebook requires the panel extension to be loaded. Ensure you run pn.extension() before displaying objects in the notebook.\n" + ] + }, + { + "data": { + "text/plain": [ + "JSON(str)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# Create a message to append to our thread\n", + "message = client.beta.threads.messages.create(\n", + " thread_id=thread.id, role=\"user\", content=\"Could you explain this to me?\"\n", + ")\n", + "\n", + "# Execute our run\n", + "run = client.beta.threads.runs.create(\n", + " thread_id=thread.id,\n", + " assistant_id=assistant.id,\n", + ")\n", + "\n", + "# Wait for completion\n", + "wait_on_run(run, thread)\n", + "\n", + "# Retrieve all the messages added after our last user message\n", + "messages = client.beta.threads.messages.list(\n", + " thread_id=thread.id, order=\"asc\", after=message.id\n", + ")\n", + "show_json(messages)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from openai import OpenAI\n", + "\n", + "MATH_ASSISTANT_ID = assistant.id # or a hard-coded ID like \"asst-...\"\n", + "\n", + "client = OpenAI()\n", + "\n", + "\n", + "def submit_message(assistant_id, thread, user_message):\n", + " client.beta.threads.messages.create(\n", + " thread_id=thread.id, role=\"user\", content=user_message\n", + " )\n", + " return client.beta.threads.runs.create(\n", + " thread_id=thread.id,\n", + " assistant_id=assistant_id,\n", + " )\n", + "\n", + "\n", + "def get_response(thread):\n", + " return client.beta.threads.messages.list(thread_id=thread.id, order=\"asc\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def create_thread_and_run(user_input):\n", + " thread = client.beta.threads.create()\n", + " run = submit_message(MATH_ASSISTANT_ID, thread, user_input)\n", + " return thread, run\n", + "\n", + "\n", + "# Emulating concurrent user requests\n", + "thread1, run1 = create_thread_and_run(\"I need to solve the equation `3x + 11 = 14`. Can you help me?\")\n", + "thread2, run2 = create_thread_and_run(\"Could you explain linear algebra to me?\")\n", + "thread3, run3 = create_thread_and_run(\"I don't like math. What can I do?\")\n", + "\n", + "# Now all Runs are executing..." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Once all Runs are going, we can wait on each and get the responses.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "# Messages\n", + "user: I need to solve the equation `3x + 11 = 14`. Can you help me?\n", + "assistant: Yes, subtract 11 from both sides to get `3x = 3`, then divide by 3 to find `x = 1`.\n", + "\n", + "# Messages\n", + "user: Could you explain linear algebra to me?\n", + "assistant: Linear algebra is a branch of mathematics focused on vector spaces and linear mappings between these spaces, which includes the study of vectors, matrices, determinants, eigenvalues, eigenvectors, and systems of linear equations.\n", + "\n", + "# Messages\n", + "user: I don't like math. What can I do?\n", + "assistant: Try relating math topics to your interests, practice regularly to build confidence, or work with a tutor to help make the subject more approachable and enjoyable.\n", + "\n", + "# Messages\n", + "user: I don't like math. What can I do?\n", + "assistant: Try relating math topics to your interests, practice regularly to build confidence, or work with a tutor to help make the subject more approachable and enjoyable.\n", + "user: Thank you!\n", + "assistant: You're welcome! If you have any more questions, feel free to ask.\n", + "\n" + ] + } + ], + "source": [ + "import time\n", + "\n", + "\n", + "# Pretty printing helper\n", + "def pretty_print(messages):\n", + " print(\"# Messages\")\n", + " for m in messages:\n", + " print(f\"{m.role}: {m.content[0].text.value}\")\n", + " print()\n", + "\n", + "\n", + "# Waiting in a loop\n", + "def wait_on_run(run, thread):\n", + " while run.status == \"queued\" or run.status == \"in_progress\":\n", + " run = client.beta.threads.runs.retrieve(\n", + " thread_id=thread.id,\n", + " run_id=run.id,\n", + " )\n", + " time.sleep(0.5)\n", + " return run\n", + "\n", + "\n", + "# Wait for Run 1\n", + "run1 = wait_on_run(run1, thread1)\n", + "pretty_print(get_response(thread1))\n", + "\n", + "# Wait for Run 2\n", + "run2 = wait_on_run(run2, thread2)\n", + "pretty_print(get_response(thread2))\n", + "\n", + "# Wait for Run 3\n", + "run3 = wait_on_run(run3, thread3)\n", + "pretty_print(get_response(thread3))\n", + "\n", + "# Thank our assistant on Thread 3 :)\n", + "run4 = submit_message(MATH_ASSISTANT_ID, thread3, \"Thank you!\")\n", + "run4 = wait_on_run(run4, thread3)\n", + "pretty_print(get_response(thread3))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Et voilà!\n", + "\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" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\"Screenshot\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...or the API, using the Assistant ID.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "assistant = client.beta.assistants.update(\n", + " MATH_ASSISTANT_ID,\n", + " tools=[{\"type\": \"code_interpreter\"}],\n", + ")\n", + "show_json(assistant)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now, let's ask the Assistant to use its new tool.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "# Messages\n", + "user: Generate the first 20 fibbonaci numbers with code.\n", + "assistant: The first 20 Fibonacci numbers are:\n", + "0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181.\n", + "\n" + ] + } + ], + "source": [ + "thread, run = create_thread_and_run(\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" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "run_steps = client.beta.threads.runs.steps.list(\n", + " thread_id=thread.id, run_id=run.id, order=\"asc\"\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's take a look at each Step's `step_details`.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "null\n", + "null\n" + ] + } + ], + "source": [ + "for step in run_steps.data:\n", + " step_details = step.step_details\n", + " print(json.dumps(show_json(step_details), indent=4))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can see the `step_details` for two Steps:\n", + "\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" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\"Screenshot\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Upload the file\n", + "file = client.files.create(\n", + " file=open(\n", + " \"data/language_models_are_unsupervised_multitask_learners.pdf\",\n", + " \"rb\",\n", + " ),\n", + " purpose=\"assistants\",\n", + ")\n", + "# Update Assistant\n", + "assistant = client.beta.assistants.update(\n", + " MATH_ASSISTANT_ID,\n", + " tools=[{\"type\": \"code_interpreter\"}, {\"type\": \"retrieval\"}],\n", + " file_ids=[file.id],\n", + ")\n", + "show_json(assistant)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "thread, run = create_thread_and_run(\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", + "\n", + "- `title`\n", + "- `questions`\n", + " - `question_text`\n", + " - `question_type`: [`MULTIPLE_CHOICE`, `FREE_RESPONSE`]\n", + " - `choices`: [\"choice 1\", \"choice 2\", ...]\n", + "\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" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def get_mock_response_from_user_multiple_choice():\n", + " return \"a\"\n", + "\n", + "\n", + "def get_mock_response_from_user_free_response():\n", + " return \"I don't know.\"\n", + "\n", + "\n", + "def display_quiz(title, questions):\n", + " print(\"Quiz:\", title)\n", + " print()\n", + " responses = []\n", + "\n", + " for q in questions:\n", + " print(q[\"question_text\"])\n", + " response = \"\"\n", + "\n", + " # If multiple choice, print options\n", + " if q[\"question_type\"] == \"MULTIPLE_CHOICE\":\n", + " for i, choice in enumerate(q[\"choices\"]):\n", + " print(f\"{i}. {choice}\")\n", + " response = get_mock_response_from_user_multiple_choice()\n", + "\n", + " # Otherwise, just get response\n", + " elif q[\"question_type\"] == \"FREE_RESPONSE\":\n", + " response = get_mock_response_from_user_free_response()\n", + "\n", + " responses.append(response)\n", + " print()\n", + "\n", + " return responses" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here's what a sample quiz would look like:\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Quiz: Sample Quiz\n", + "\n", + "What is your name?\n", + "\n", + "What is your favorite color?\n", + "0. Red\n", + "1. Blue\n", + "2. Green\n", + "3. Yellow\n", + "\n", + "Responses: [\"I don't know.\", 'a']\n" + ] + } + ], + "source": [ + "responses = display_quiz(\n", + " \"Sample Quiz\",\n", + " [\n", + " {\"question_text\": \"What is your name?\", \"question_type\": \"FREE_RESPONSE\"},\n", + " {\n", + " \"question_text\": \"What is your favorite color?\",\n", + " \"question_type\": \"MULTIPLE_CHOICE\",\n", + " \"choices\": [\"Red\", \"Blue\", \"Green\", \"Yellow\"],\n", + " },\n", + " ],\n", + ")\n", + "print(\"Responses:\", responses)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now, let's define the interface of this function in JSON format, so our Assistant can call it:\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "function_json = {\n", + " \"name\": \"display_quiz\",\n", + " \"description\": \"Displays a quiz to the student, and returns the student's response. A single quiz can have multiple questions.\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"title\": {\"type\": \"string\"},\n", + " \"questions\": {\n", + " \"type\": \"array\",\n", + " \"description\": \"An array of questions, each with a title and potentially options (if multiple choice).\",\n", + " \"items\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"question_text\": {\"type\": \"string\"},\n", + " \"question_type\": {\n", + " \"type\": \"string\",\n", + " \"enum\": [\"MULTIPLE_CHOICE\", \"FREE_RESPONSE\"],\n", + " },\n", + " \"choices\": {\"type\": \"array\", \"items\": {\"type\": \"string\"}},\n", + " },\n", + " \"required\": [\"question_text\"],\n", + " },\n", + " },\n", + " },\n", + " \"required\": [\"title\", \"questions\"],\n", + " },\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Once again, let's update out Assistant either through the Dashboard or the API.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\"Screenshot\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Note**\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" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
{\n",
+       "  \"id\": \"asst_oWtP199nJHb6uR48GseDKtvv\",\n",
+       "  \"created_at\": 1699647366,\n",
+       "  \"description\": null,\n",
+       "  \"file_ids\": [\n",
+       "    \"file-BXMBZx4F9yCVwW27doS6uRiJ\"\n",
+       "  ],\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\": [\n",
+       "    {\n",
+       "      \"type\": \"code_interpreter\"\n",
+       "    },\n",
+       "    {\n",
+       "      \"type\": \"retrieval\"\n",
+       "    },\n",
+       "    {\n",
+       "      \"function\": {\n",
+       "        \"name\": \"display_quiz\",\n",
+       "        \"parameters\": {\n",
+       "          \"type\": \"object\",\n",
+       "          \"properties\": {\n",
+       "            \"title\": {\n",
+       "              \"type\": \"string\"\n",
+       "            },\n",
+       "            \"questions\": {\n",
+       "              \"type\": \"array\",\n",
+       "              \"description\": \"An array of questions, each with a title and potentially options (if multiple choice).\",\n",
+       "              \"items\": {\n",
+       "                \"type\": \"object\",\n",
+       "                \"properties\": {\n",
+       "                  \"question_text\": {\n",
+       "                    \"type\": \"string\"\n",
+       "                  },\n",
+       "                  \"question_type\": {\n",
+       "                    \"type\": \"string\",\n",
+       "                    \"enum\": [\n",
+       "                      \"MULTIPLE_CHOICE\",\n",
+       "                      \"FREE_RESPONSE\"\n",
+       "                    ]\n",
+       "                  },\n",
+       "                  \"choices\": {\n",
+       "                    \"type\": \"array\",\n",
+       "                    \"items\": {\n",
+       "                      \"type\": \"string\"\n",
+       "                    }\n",
+       "                  }\n",
+       "                },\n",
+       "                \"required\": [\n",
+       "                  \"question_text\"\n",
+       "                ]\n",
+       "              }\n",
+       "            }\n",
+       "          },\n",
+       "          \"required\": [\n",
+       "            \"title\",\n",
+       "            \"questions\"\n",
+       "          ]\n",
+       "        },\n",
+       "        \"description\": \"Displays a quiz to the student, and returns the student's response. A single quiz can have multiple questions.\"\n",
+       "      },\n",
+       "      \"type\": \"function\"\n",
+       "    }\n",
+       "  ]\n",
+       "}\n",
+       "
\n" + ], + "text/plain": [ + "\u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"id\"\u001b[0m: \u001b[32m\"asst_oWtP199nJHb6uR48GseDKtvv\"\u001b[0m,\n", + " \u001b[1;34m\"created_at\"\u001b[0m: \u001b[1;36m1699647366\u001b[0m,\n", + " \u001b[1;34m\"description\"\u001b[0m: \u001b[3;35mnull\u001b[0m,\n", + " \u001b[1;34m\"file_ids\"\u001b[0m: \u001b[1m[\u001b[0m\n", + " \u001b[32m\"file-BXMBZx4F9yCVwW27doS6uRiJ\"\u001b[0m\n", + " \u001b[1m]\u001b[0m,\n", + " \u001b[1;34m\"instructions\"\u001b[0m: \u001b[32m\"You are a personal math tutor. Answer questions briefly, in a sentence or less.\"\u001b[0m,\n", + " \u001b[1;34m\"metadata\"\u001b[0m: \u001b[1m{\u001b[0m\u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"model\"\u001b[0m: \u001b[32m\"gpt-4-1106-preview\"\u001b[0m,\n", + " \u001b[1;34m\"name\"\u001b[0m: \u001b[32m\"Math Tutor\"\u001b[0m,\n", + " \u001b[1;34m\"object\"\u001b[0m: \u001b[32m\"assistant\"\u001b[0m,\n", + " \u001b[1;34m\"tools\"\u001b[0m: \u001b[1m[\u001b[0m\n", + " \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"code_interpreter\"\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"retrieval\"\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"function\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"name\"\u001b[0m: \u001b[32m\"display_quiz\"\u001b[0m,\n", + " \u001b[1;34m\"parameters\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"object\"\u001b[0m,\n", + " \u001b[1;34m\"properties\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"title\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"string\"\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"questions\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"array\"\u001b[0m,\n", + " \u001b[1;34m\"description\"\u001b[0m: \u001b[32m\"An array of questions, each with a title and potentially options (if multiple choice).\"\u001b[0m,\n", + " \u001b[1;34m\"items\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"object\"\u001b[0m,\n", + " \u001b[1;34m\"properties\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"question_text\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"string\"\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"question_type\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"string\"\u001b[0m,\n", + " \u001b[1;34m\"enum\"\u001b[0m: \u001b[1m[\u001b[0m\n", + " \u001b[32m\"MULTIPLE_CHOICE\"\u001b[0m,\n", + " \u001b[32m\"FREE_RESPONSE\"\u001b[0m\n", + " \u001b[1m]\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"choices\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"array\"\u001b[0m,\n", + " \u001b[1;34m\"items\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"string\"\u001b[0m\n", + " \u001b[1m}\u001b[0m\n", + " \u001b[1m}\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"required\"\u001b[0m: \u001b[1m[\u001b[0m\n", + " \u001b[32m\"question_text\"\u001b[0m\n", + " \u001b[1m]\u001b[0m\n", + " \u001b[1m}\u001b[0m\n", + " \u001b[1m}\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"required\"\u001b[0m: \u001b[1m[\u001b[0m\n", + " \u001b[32m\"title\"\u001b[0m,\n", + " \u001b[32m\"questions\"\u001b[0m\n", + " \u001b[1m]\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"description\"\u001b[0m: \u001b[32m\"Displays a quiz to the student, and returns the student's response. A single quiz can have multiple questions.\"\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"function\"\u001b[0m\n", + " \u001b[1m}\u001b[0m\n", + " \u001b[1m]\u001b[0m\n", + "\u001b[1m}\u001b[0m\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "assistant = client.beta.assistants.update(\n", + " MATH_ASSISTANT_ID,\n", + " tools=[\n", + " {\"type\": \"code_interpreter\"},\n", + " {\"type\": \"retrieval\"},\n", + " {\"type\": \"function\", \"function\": function_json},\n", + " ],\n", + ")\n", + "show_json(assistant)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And now, we ask for a quiz.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n"
+      ],
+      "text/plain": []
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    },
+    {
+     "data": {
+      "text/plain": [
+       "\u001b[32m'requires_action'\u001b[0m"
+      ]
+     },
+     "execution_count": 26,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "thread, run = create_thread_and_run(\n",
+    "    \"Make a quiz with 2 questions: One open ended, one multiple choice. Then, give me feedback for the responses.\"\n",
+    ")\n",
+    "run = wait_on_run(run, thread)\n",
+    "run.status"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "Now, however, when we check the Run's `status` we see `requires_action`! Let's take a closer.\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/html": [
+       "
{\n",
+       "  \"id\": \"run_cTgSKShYNiCfW5XXuU01tAtP\",\n",
+       "  \"assistant_id\": \"asst_oWtP199nJHb6uR48GseDKtvv\",\n",
+       "  \"cancelled_at\": null,\n",
+       "  \"completed_at\": null,\n",
+       "  \"created_at\": 1699647410,\n",
+       "  \"expires_at\": 1699648010,\n",
+       "  \"failed_at\": null,\n",
+       "  \"file_ids\": [\n",
+       "    \"file-BXMBZx4F9yCVwW27doS6uRiJ\"\n",
+       "  ],\n",
+       "  \"instructions\": \"You are a personal math tutor. Answer questions briefly, in a sentence or less.\",\n",
+       "  \"last_error\": null,\n",
+       "  \"metadata\": {},\n",
+       "  \"model\": \"gpt-4-1106-preview\",\n",
+       "  \"object\": \"thread.run\",\n",
+       "  \"required_action\": {\n",
+       "    \"submit_tool_outputs\": {\n",
+       "      \"tool_calls\": [\n",
+       "        {\n",
+       "          \"id\": \"call_MtNGP8QtyF8raQgCe1iLteto\",\n",
+       "          \"function\": {\n",
+       "            \"arguments\": \"{\\n  \\\"title\\\": \\\"Math Quiz\\\",\\n  \\\"questions\\\": [\\n    {\\n      \\\"question_text\\\": \\\"Calculate the derivative of the function f(x) = 3x^3 - 5x^2 + 6.\\\",\\n      \\\"question_type\\\": \\\"FREE_RESPONSE\\\"\\n    },\\n    {\\n      \\\"question_text\\\": \\\"The graph of a quadratic function ax^2 + bx + c opens downward when:\\\",\\n      \\\"question_type\\\": \\\"MULTIPLE_CHOICE\\\",\\n      \\\"choices\\\": [\\\"a > 0\\\", \\\"a < 0\\\", \\\"b > 0\\\", \\\"c > 0\\\"]\\n    }\\n  ]\\n}\",\n",
+       "            \"name\": \"display_quiz\"\n",
+       "          },\n",
+       "          \"type\": \"function\"\n",
+       "        }\n",
+       "      ]\n",
+       "    },\n",
+       "    \"type\": \"submit_tool_outputs\"\n",
+       "  },\n",
+       "  \"started_at\": 1699647410,\n",
+       "  \"status\": \"requires_action\",\n",
+       "  \"thread_id\": \"thread_sVjQ15Tk8JAHwfwIKy190fMX\",\n",
+       "  \"tools\": [\n",
+       "    {\n",
+       "      \"type\": \"code_interpreter\"\n",
+       "    },\n",
+       "    {\n",
+       "      \"type\": \"retrieval\"\n",
+       "    },\n",
+       "    {\n",
+       "      \"function\": {\n",
+       "        \"name\": \"display_quiz\",\n",
+       "        \"parameters\": {\n",
+       "          \"type\": \"object\",\n",
+       "          \"properties\": {\n",
+       "            \"title\": {\n",
+       "              \"type\": \"string\"\n",
+       "            },\n",
+       "            \"questions\": {\n",
+       "              \"type\": \"array\",\n",
+       "              \"description\": \"An array of questions, each with a title and potentially options (if multiple choice).\",\n",
+       "              \"items\": {\n",
+       "                \"type\": \"object\",\n",
+       "                \"properties\": {\n",
+       "                  \"question_text\": {\n",
+       "                    \"type\": \"string\"\n",
+       "                  },\n",
+       "                  \"question_type\": {\n",
+       "                    \"type\": \"string\",\n",
+       "                    \"enum\": [\n",
+       "                      \"MULTIPLE_CHOICE\",\n",
+       "                      \"FREE_RESPONSE\"\n",
+       "                    ]\n",
+       "                  },\n",
+       "                  \"choices\": {\n",
+       "                    \"type\": \"array\",\n",
+       "                    \"items\": {\n",
+       "                      \"type\": \"string\"\n",
+       "                    }\n",
+       "                  }\n",
+       "                },\n",
+       "                \"required\": [\n",
+       "                  \"question_text\"\n",
+       "                ]\n",
+       "              }\n",
+       "            }\n",
+       "          },\n",
+       "          \"required\": [\n",
+       "            \"title\",\n",
+       "            \"questions\"\n",
+       "          ]\n",
+       "        },\n",
+       "        \"description\": \"Displays a quiz to the student, and returns the student's response. A single quiz can have multiple questions.\"\n",
+       "      },\n",
+       "      \"type\": \"function\"\n",
+       "    }\n",
+       "  ]\n",
+       "}\n",
+       "
\n" + ], + "text/plain": [ + "\u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"id\"\u001b[0m: \u001b[32m\"run_cTgSKShYNiCfW5XXuU01tAtP\"\u001b[0m,\n", + " \u001b[1;34m\"assistant_id\"\u001b[0m: \u001b[32m\"asst_oWtP199nJHb6uR48GseDKtvv\"\u001b[0m,\n", + " \u001b[1;34m\"cancelled_at\"\u001b[0m: \u001b[3;35mnull\u001b[0m,\n", + " \u001b[1;34m\"completed_at\"\u001b[0m: \u001b[3;35mnull\u001b[0m,\n", + " \u001b[1;34m\"created_at\"\u001b[0m: \u001b[1;36m1699647410\u001b[0m,\n", + " \u001b[1;34m\"expires_at\"\u001b[0m: \u001b[1;36m1699648010\u001b[0m,\n", + " \u001b[1;34m\"failed_at\"\u001b[0m: \u001b[3;35mnull\u001b[0m,\n", + " \u001b[1;34m\"file_ids\"\u001b[0m: \u001b[1m[\u001b[0m\n", + " \u001b[32m\"file-BXMBZx4F9yCVwW27doS6uRiJ\"\u001b[0m\n", + " \u001b[1m]\u001b[0m,\n", + " \u001b[1;34m\"instructions\"\u001b[0m: \u001b[32m\"You are a personal math tutor. Answer questions briefly, in a sentence or less.\"\u001b[0m,\n", + " \u001b[1;34m\"last_error\"\u001b[0m: \u001b[3;35mnull\u001b[0m,\n", + " \u001b[1;34m\"metadata\"\u001b[0m: \u001b[1m{\u001b[0m\u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"model\"\u001b[0m: \u001b[32m\"gpt-4-1106-preview\"\u001b[0m,\n", + " \u001b[1;34m\"object\"\u001b[0m: \u001b[32m\"thread.run\"\u001b[0m,\n", + " \u001b[1;34m\"required_action\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"submit_tool_outputs\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"tool_calls\"\u001b[0m: \u001b[1m[\u001b[0m\n", + " \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"id\"\u001b[0m: \u001b[32m\"call_MtNGP8QtyF8raQgCe1iLteto\"\u001b[0m,\n", + " \u001b[1;34m\"function\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"arguments\"\u001b[0m: \u001b[32m\"{\\n \\\"title\\\": \\\"Math Quiz\\\",\\n \\\"questions\\\": [\\n {\\n \\\"question_text\\\": \\\"Calculate the derivative of the function f(x) = 3x^3 - 5x^2 + 6.\\\",\\n \\\"question_type\\\": \\\"FREE_RESPONSE\\\"\\n },\\n {\\n \\\"question_text\\\": \\\"The graph of a quadratic function ax^2 + bx + c opens downward when:\\\",\\n \\\"question_type\\\": \\\"MULTIPLE_CHOICE\\\",\\n \\\"choices\\\": [\\\"a > 0\\\", \\\"a < 0\\\", \\\"b > 0\\\", \\\"c > 0\\\"]\\n }\\n ]\\n}\"\u001b[0m,\n", + " \u001b[1;34m\"name\"\u001b[0m: \u001b[32m\"display_quiz\"\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"function\"\u001b[0m\n", + " \u001b[1m}\u001b[0m\n", + " \u001b[1m]\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"submit_tool_outputs\"\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"started_at\"\u001b[0m: \u001b[1;36m1699647410\u001b[0m,\n", + " \u001b[1;34m\"status\"\u001b[0m: \u001b[32m\"requires_action\"\u001b[0m,\n", + " \u001b[1;34m\"thread_id\"\u001b[0m: \u001b[32m\"thread_sVjQ15Tk8JAHwfwIKy190fMX\"\u001b[0m,\n", + " \u001b[1;34m\"tools\"\u001b[0m: \u001b[1m[\u001b[0m\n", + " \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"code_interpreter\"\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"retrieval\"\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"function\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"name\"\u001b[0m: \u001b[32m\"display_quiz\"\u001b[0m,\n", + " \u001b[1;34m\"parameters\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"object\"\u001b[0m,\n", + " \u001b[1;34m\"properties\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"title\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"string\"\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"questions\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"array\"\u001b[0m,\n", + " \u001b[1;34m\"description\"\u001b[0m: \u001b[32m\"An array of questions, each with a title and potentially options (if multiple choice).\"\u001b[0m,\n", + " \u001b[1;34m\"items\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"object\"\u001b[0m,\n", + " \u001b[1;34m\"properties\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"question_text\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"string\"\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"question_type\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"string\"\u001b[0m,\n", + " \u001b[1;34m\"enum\"\u001b[0m: \u001b[1m[\u001b[0m\n", + " \u001b[32m\"MULTIPLE_CHOICE\"\u001b[0m,\n", + " \u001b[32m\"FREE_RESPONSE\"\u001b[0m\n", + " \u001b[1m]\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"choices\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"array\"\u001b[0m,\n", + " \u001b[1;34m\"items\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"string\"\u001b[0m\n", + " \u001b[1m}\u001b[0m\n", + " \u001b[1m}\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"required\"\u001b[0m: \u001b[1m[\u001b[0m\n", + " \u001b[32m\"question_text\"\u001b[0m\n", + " \u001b[1m]\u001b[0m\n", + " \u001b[1m}\u001b[0m\n", + " \u001b[1m}\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"required\"\u001b[0m: \u001b[1m[\u001b[0m\n", + " \u001b[32m\"title\"\u001b[0m,\n", + " \u001b[32m\"questions\"\u001b[0m\n", + " \u001b[1m]\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"description\"\u001b[0m: \u001b[32m\"Displays a quiz to the student, and returns the student's response. A single quiz can have multiple questions.\"\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"function\"\u001b[0m\n", + " \u001b[1m}\u001b[0m\n", + " \u001b[1m]\u001b[0m\n", + "\u001b[1m}\u001b[0m\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "show_json(run)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Function Name: display_quiz\n", + "Function Arguments:\n" + ] + }, + { + "data": { + "text/html": [ + "
\n"
+      ],
+      "text/plain": []
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    },
+    {
+     "data": {
+      "text/plain": [
+       "\n",
+       "\u001b[1m{\u001b[0m\n",
+       "    \u001b[32m'title'\u001b[0m: \u001b[32m'Math Quiz'\u001b[0m,\n",
+       "    \u001b[32m'questions'\u001b[0m: \u001b[1m[\u001b[0m\n",
+       "        \u001b[1m{\u001b[0m\n",
+       "            \u001b[32m'question_text'\u001b[0m: \u001b[32m'Calculate the derivative of the function f\u001b[0m\u001b[32m(\u001b[0m\u001b[32mx\u001b[0m\u001b[32m)\u001b[0m\u001b[32m = 3x^3 - 5x^2 + 6.'\u001b[0m,\n",
+       "            \u001b[32m'question_type'\u001b[0m: \u001b[32m'FREE_RESPONSE'\u001b[0m\n",
+       "        \u001b[1m}\u001b[0m,\n",
+       "        \u001b[1m{\u001b[0m\n",
+       "            \u001b[32m'question_text'\u001b[0m: \u001b[32m'The graph of a quadratic function ax^2 + bx + c opens downward when:'\u001b[0m,\n",
+       "            \u001b[32m'question_type'\u001b[0m: \u001b[32m'MULTIPLE_CHOICE'\u001b[0m,\n",
+       "            \u001b[32m'choices'\u001b[0m: \u001b[1m[\u001b[0m\u001b[32m'a > 0'\u001b[0m, \u001b[32m'a \u001b[0m\u001b[32m<\u001b[0m\u001b[32m 0'\u001b[0m\u001b[39m, \u001b[0m\u001b[32m'b > 0'\u001b[0m\u001b[39m, \u001b[0m\u001b[32m'c \u001b[0m\u001b[32m>\u001b[0m\u001b[32m 0'\u001b[0m\u001b[1m]\u001b[0m\n",
+       "        \u001b[1m}\u001b[0m\n",
+       "    \u001b[1m]\u001b[0m\n",
+       "\u001b[1m}\u001b[0m"
+      ]
+     },
+     "execution_count": 28,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "# Extract single tool call\n",
+    "tool_call = run.required_action.submit_tool_outputs.tool_calls[0]\n",
+    "name = tool_call.function.name\n",
+    "arguments = json.loads(tool_call.function.arguments)\n",
+    "\n",
+    "print(\"Function Name:\", name)\n",
+    "print(\"Function Arguments:\")\n",
+    "arguments"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "Now let's actually call our `display_quiz` function with the arguments provided by the Assistant:\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Quiz: Math Quiz\n",
+      "\n",
+      "Calculate the derivative of the function f(x) = 3x^3 - 5x^2 + 6.\n",
+      "\n",
+      "The graph of a quadratic function ax^2 + bx + c opens downward when:\n",
+      "0. a > 0\n",
+      "1. a < 0\n",
+      "2. b > 0\n",
+      "3. c > 0\n",
+      "\n",
+      "Responses: [\"I don't know.\", 'a']\n"
+     ]
+    }
+   ],
+   "source": [
+    "responses = display_quiz(arguments[\"title\"], arguments[\"questions\"])\n",
+    "print(\"Responses:\", responses)"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "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"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/html": [
+       "
{\n",
+       "  \"id\": \"run_cTgSKShYNiCfW5XXuU01tAtP\",\n",
+       "  \"assistant_id\": \"asst_oWtP199nJHb6uR48GseDKtvv\",\n",
+       "  \"cancelled_at\": null,\n",
+       "  \"completed_at\": null,\n",
+       "  \"created_at\": 1699647410,\n",
+       "  \"expires_at\": 1699648010,\n",
+       "  \"failed_at\": null,\n",
+       "  \"file_ids\": [\n",
+       "    \"file-BXMBZx4F9yCVwW27doS6uRiJ\"\n",
+       "  ],\n",
+       "  \"instructions\": \"You are a personal math tutor. Answer questions briefly, in a sentence or less.\",\n",
+       "  \"last_error\": null,\n",
+       "  \"metadata\": {},\n",
+       "  \"model\": \"gpt-4-1106-preview\",\n",
+       "  \"object\": \"thread.run\",\n",
+       "  \"required_action\": null,\n",
+       "  \"started_at\": 1699647410,\n",
+       "  \"status\": \"queued\",\n",
+       "  \"thread_id\": \"thread_sVjQ15Tk8JAHwfwIKy190fMX\",\n",
+       "  \"tools\": [\n",
+       "    {\n",
+       "      \"type\": \"code_interpreter\"\n",
+       "    },\n",
+       "    {\n",
+       "      \"type\": \"retrieval\"\n",
+       "    },\n",
+       "    {\n",
+       "      \"function\": {\n",
+       "        \"name\": \"display_quiz\",\n",
+       "        \"parameters\": {\n",
+       "          \"type\": \"object\",\n",
+       "          \"properties\": {\n",
+       "            \"title\": {\n",
+       "              \"type\": \"string\"\n",
+       "            },\n",
+       "            \"questions\": {\n",
+       "              \"type\": \"array\",\n",
+       "              \"description\": \"An array of questions, each with a title and potentially options (if multiple choice).\",\n",
+       "              \"items\": {\n",
+       "                \"type\": \"object\",\n",
+       "                \"properties\": {\n",
+       "                  \"question_text\": {\n",
+       "                    \"type\": \"string\"\n",
+       "                  },\n",
+       "                  \"question_type\": {\n",
+       "                    \"type\": \"string\",\n",
+       "                    \"enum\": [\n",
+       "                      \"MULTIPLE_CHOICE\",\n",
+       "                      \"FREE_RESPONSE\"\n",
+       "                    ]\n",
+       "                  },\n",
+       "                  \"choices\": {\n",
+       "                    \"type\": \"array\",\n",
+       "                    \"items\": {\n",
+       "                      \"type\": \"string\"\n",
+       "                    }\n",
+       "                  }\n",
+       "                },\n",
+       "                \"required\": [\n",
+       "                  \"question_text\"\n",
+       "                ]\n",
+       "              }\n",
+       "            }\n",
+       "          },\n",
+       "          \"required\": [\n",
+       "            \"title\",\n",
+       "            \"questions\"\n",
+       "          ]\n",
+       "        },\n",
+       "        \"description\": \"Displays a quiz to the student, and returns the student's response. A single quiz can have multiple questions.\"\n",
+       "      },\n",
+       "      \"type\": \"function\"\n",
+       "    }\n",
+       "  ]\n",
+       "}\n",
+       "
\n" + ], + "text/plain": [ + "\u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"id\"\u001b[0m: \u001b[32m\"run_cTgSKShYNiCfW5XXuU01tAtP\"\u001b[0m,\n", + " \u001b[1;34m\"assistant_id\"\u001b[0m: \u001b[32m\"asst_oWtP199nJHb6uR48GseDKtvv\"\u001b[0m,\n", + " \u001b[1;34m\"cancelled_at\"\u001b[0m: \u001b[3;35mnull\u001b[0m,\n", + " \u001b[1;34m\"completed_at\"\u001b[0m: \u001b[3;35mnull\u001b[0m,\n", + " \u001b[1;34m\"created_at\"\u001b[0m: \u001b[1;36m1699647410\u001b[0m,\n", + " \u001b[1;34m\"expires_at\"\u001b[0m: \u001b[1;36m1699648010\u001b[0m,\n", + " \u001b[1;34m\"failed_at\"\u001b[0m: \u001b[3;35mnull\u001b[0m,\n", + " \u001b[1;34m\"file_ids\"\u001b[0m: \u001b[1m[\u001b[0m\n", + " \u001b[32m\"file-BXMBZx4F9yCVwW27doS6uRiJ\"\u001b[0m\n", + " \u001b[1m]\u001b[0m,\n", + " \u001b[1;34m\"instructions\"\u001b[0m: \u001b[32m\"You are a personal math tutor. Answer questions briefly, in a sentence or less.\"\u001b[0m,\n", + " \u001b[1;34m\"last_error\"\u001b[0m: \u001b[3;35mnull\u001b[0m,\n", + " \u001b[1;34m\"metadata\"\u001b[0m: \u001b[1m{\u001b[0m\u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"model\"\u001b[0m: \u001b[32m\"gpt-4-1106-preview\"\u001b[0m,\n", + " \u001b[1;34m\"object\"\u001b[0m: \u001b[32m\"thread.run\"\u001b[0m,\n", + " \u001b[1;34m\"required_action\"\u001b[0m: \u001b[3;35mnull\u001b[0m,\n", + " \u001b[1;34m\"started_at\"\u001b[0m: \u001b[1;36m1699647410\u001b[0m,\n", + " \u001b[1;34m\"status\"\u001b[0m: \u001b[32m\"queued\"\u001b[0m,\n", + " \u001b[1;34m\"thread_id\"\u001b[0m: \u001b[32m\"thread_sVjQ15Tk8JAHwfwIKy190fMX\"\u001b[0m,\n", + " \u001b[1;34m\"tools\"\u001b[0m: \u001b[1m[\u001b[0m\n", + " \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"code_interpreter\"\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"retrieval\"\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"function\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"name\"\u001b[0m: \u001b[32m\"display_quiz\"\u001b[0m,\n", + " \u001b[1;34m\"parameters\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"object\"\u001b[0m,\n", + " \u001b[1;34m\"properties\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"title\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"string\"\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"questions\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"array\"\u001b[0m,\n", + " \u001b[1;34m\"description\"\u001b[0m: \u001b[32m\"An array of questions, each with a title and potentially options (if multiple choice).\"\u001b[0m,\n", + " \u001b[1;34m\"items\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"object\"\u001b[0m,\n", + " \u001b[1;34m\"properties\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"question_text\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"string\"\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"question_type\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"string\"\u001b[0m,\n", + " \u001b[1;34m\"enum\"\u001b[0m: \u001b[1m[\u001b[0m\n", + " \u001b[32m\"MULTIPLE_CHOICE\"\u001b[0m,\n", + " \u001b[32m\"FREE_RESPONSE\"\u001b[0m\n", + " \u001b[1m]\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"choices\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"array\"\u001b[0m,\n", + " \u001b[1;34m\"items\"\u001b[0m: \u001b[1m{\u001b[0m\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"string\"\u001b[0m\n", + " \u001b[1m}\u001b[0m\n", + " \u001b[1m}\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"required\"\u001b[0m: \u001b[1m[\u001b[0m\n", + " \u001b[32m\"question_text\"\u001b[0m\n", + " \u001b[1m]\u001b[0m\n", + " \u001b[1m}\u001b[0m\n", + " \u001b[1m}\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"required\"\u001b[0m: \u001b[1m[\u001b[0m\n", + " \u001b[32m\"title\"\u001b[0m,\n", + " \u001b[32m\"questions\"\u001b[0m\n", + " \u001b[1m]\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"description\"\u001b[0m: \u001b[32m\"Displays a quiz to the student, and returns the student's response. A single quiz can have multiple questions.\"\u001b[0m\n", + " \u001b[1m}\u001b[0m,\n", + " \u001b[1;34m\"type\"\u001b[0m: \u001b[32m\"function\"\u001b[0m\n", + " \u001b[1m}\u001b[0m\n", + " \u001b[1m]\u001b[0m\n", + "\u001b[1m}\u001b[0m\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "run = client.beta.threads.runs.submit_tool_outputs(\n", + " thread_id=thread.id,\n", + " run_id=run.id,\n", + " tool_outputs=[\n", + " {\n", + " \"tool_call_id\": tool_call.id,\n", + " \"output\": json.dumps(responses),\n", + " }\n", + " ],\n", + ")\n", + "show_json(run)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can now wait for the Run to complete once again, and check our Thread!\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "# Messages\n", + "user: Make a quiz with 2 questions: One open ended, one multiple choice. Then, give me feedback for the responses.\n", + "assistant: Sure, I can create a math quiz for you. Here is a suggested set of questions, one open-ended and the other multiple choice:\n", + "\n", + "**Question 1 (Open-ended):** \n", + "Calculate the derivative of the function \\( f(x) = 3x^3 - 5x^2 + 6 \\).\n", + "\n", + "**Question 2 (Multiple-choice):**\n", + "The graph of a quadratic function \\( ax^2 + bx + c \\) opens downward when:\n", + "- A) \\( a > 0 \\)\n", + "- B) \\( a < 0 \\)\n", + "- C) \\( b > 0 \\)\n", + "- D) \\( c > 0 \\)\n", + "\n", + "I will now use the `display_quiz` function to present these questions and then provide feedback on your responses. Let's proceed.\n", + "assistant: Thank you for your responses. Here is the feedback:\n", + "\n", + "For **Question 1**, the correct answer requires using the power rule of differentiation. The derivative of \\( f(x) = 3x^3 - 5x^2 + 6 \\) is \\( f'(x) = 9x^2 - 10x \\). This is obtained by differentiating each term separately: the derivative of \\(3x^3\\) is \\(9x^2\\), the derivative of \\(-5x^2\\) is \\(-10x\\), and the derivative of the constant term \\(6\\) is \\(0\\).\n", + "\n", + "For **Question 2**, the correct answer is B) \\( a < 0 \\). The sign of the coefficient \\(a\\) in a quadratic equation \\(ax^2 + bx + c\\) determines the direction in which the parabola opens. If \\(a\\) is positive, the parabola opens upward, and if \\(a\\) is negative, it opens downward. Therefore, the correct choice is \\(a < 0\\). You chose A) \\(a > 0\\), which is incorrect.\n", + "\n", + "Would you like to try more questions or need further clarification on these topics?\n", + "\n" + ] + } + ], + "source": [ + "run = wait_on_run(run, thread)\n", + "pretty_print(get_response(thread))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Woohoo 🎉\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Conclusion\n", + "\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", + "- [Annotations](https://platform.openai.com/docs/assistants/how-it-works/managing-threads-and-messages): parsing file citations\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)! " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "openai", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/examples/data/language_models_are_unsupervised_multitask_learners.pdf b/examples/data/language_models_are_unsupervised_multitask_learners.pdf new file mode 100644 index 00000000..b714945e Binary files /dev/null and b/examples/data/language_models_are_unsupervised_multitask_learners.pdf differ diff --git a/registry.yaml b/registry.yaml index 0d5244bf..9b88e7c1 100644 --- a/registry.yaml +++ b/registry.yaml @@ -1071,7 +1071,7 @@ tags: - completions - functions - + - title: Processing and narrating a video with GPT's visual capabilities and the TTS API path: examples/GPT_with_vision_for_video_understanding.ipynb date: 2023-11-06 @@ -1097,3 +1097,12 @@ - shyamal-anadkat tags: - completions + +- title: Assistants API Overview with the Python SDK + path: examples/Assistants_API_overview_python.ipynb + date: 2023-11-10 + authors: + - ibigio + tags: + - assistants + - functions