Skip to content

Commit

Permalink
initial openai assistant (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyliu513 authored Aug 28, 2024
1 parent a5655b0 commit 6c38f2c
Showing 1 changed file with 142 additions and 0 deletions.
142 changes: 142 additions & 0 deletions openai/assistant1.ipynb
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
}

0 comments on commit 6c38f2c

Please sign in to comment.