-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from dotenv import load_dotenv\n", | ||
"load_dotenv()\n", | ||
"\n", | ||
"from openai import OpenAI\n", | ||
"client = OpenAI()\n", | ||
"\n", | ||
"assistant = client.beta.assistants.create(\n", | ||
" name=\"Math Tutor\",\n", | ||
" instructions=\"You are a personal math tutor. Write and run code to answer math questions.\",\n", | ||
" tools=[{\"type\": \"code_interpreter\"}],\n", | ||
" model=\"gpt-4o\",\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"thread = client.beta.threads.create()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 11, | ||
"metadata": {}, | ||
"outputs": [], | ||
"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", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from typing_extensions import override\n", | ||
"from openai import AssistantEventHandler\n", | ||
" \n", | ||
"# First, we create a EventHandler class to define\n", | ||
"# how we want to handle the events in the response stream.\n", | ||
" \n", | ||
"class EventHandler(AssistantEventHandler): \n", | ||
" @override\n", | ||
" def on_text_created(self, text) -> None:\n", | ||
" print(f\"\\nassistant > \", end=\"\", flush=True)\n", | ||
" \n", | ||
" @override\n", | ||
" def on_text_delta(self, delta, snapshot):\n", | ||
" print(delta.value, end=\"\", flush=True)\n", | ||
" \n", | ||
" def on_tool_call_created(self, tool_call):\n", | ||
" print(f\"\\nassistant > {tool_call.type}\\n\", flush=True)\n", | ||
" \n", | ||
" def on_tool_call_delta(self, delta, snapshot):\n", | ||
" if delta.type == 'code_interpreter':\n", | ||
" if delta.code_interpreter.input:\n", | ||
" print(delta.code_interpreter.input, end=\"\", flush=True)\n", | ||
" if delta.code_interpreter.outputs:\n", | ||
" print(f\"\\n\\noutput >\", flush=True)\n", | ||
" for output in delta.code_interpreter.outputs:\n", | ||
" if output.type == \"logs\":\n", | ||
" print(f\"\\n{output.logs}\", flush=True)\n", | ||
" \n", | ||
"# Then, we use the `stream` SDK helper \n", | ||
"# with the `EventHandler` class to create the Run \n", | ||
"# and stream the response.\n", | ||
" \n", | ||
"with client.beta.threads.runs.stream(\n", | ||
" thread_id=thread.id,\n", | ||
" assistant_id=assistant.id,\n", | ||
" instructions=\"Please address the user as Jane Doe. The user has a premium account.\",\n", | ||
" event_handler=EventHandler(),\n", | ||
") as stream:\n", | ||
" stream.until_done()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 12, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"failed\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"run = client.beta.threads.runs.create_and_poll(\n", | ||
" thread_id=thread.id,\n", | ||
" assistant_id=assistant.id,\n", | ||
" instructions=\"Please address the user as Jane Doe. The user has a premium account.\"\n", | ||
")\n", | ||
"if run.status == 'completed': \n", | ||
" messages = client.beta.threads.messages.list(\n", | ||
" thread_id=thread.id\n", | ||
" )\n", | ||
" print(messages)\n", | ||
"else:\n", | ||
" print(run)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "titan", | ||
"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.11.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |