Skip to content

Instantly share code, notes, and snippets.

@schroneko
Last active November 7, 2023 04:26
Show Gist options
  • Save schroneko/6a8afb95b97b5720fd6a2d8c4c38dddb to your computer and use it in GitHub Desktop.
Save schroneko/6a8afb95b97b5720fd6a2d8c4c38dddb to your computer and use it in GitHub Desktop.
assistants_api.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"name": "assistants_api.ipynb",
"authorship_tag": "ABX9TyMtcTT4ELgfredlSMidnYw9",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/schroneko/6a8afb95b97b5720fd6a2d8c4c38dddb/assistants_api.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"## 公式の Assistants API のページの動作確認(翻訳版)\n",
"\n",
"https://platform.openai.com/docs/assistants/overview"
],
"metadata": {
"id": "KeW2FfdndbKA"
}
},
{
"cell_type": "code",
"source": [
"!pip install -Uqq openai"
],
"metadata": {
"id": "AbfDtz14ds7O"
},
"execution_count": 2,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## API Key の取得\n",
"\n",
"https://platform.openai.com/account/api-keys"
],
"metadata": {
"id": "WpMGsXXXBoIK"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "AZ2ASKtKdZ2F"
},
"outputs": [],
"source": [
"# 左側鍵マークから OPENAI_API_KEY を登録\n",
"# 名前はなんでも良いですが、↓getの中身と登録名を揃えてください\n",
"from google.colab import userdata\n",
"api_key = userdata.get('OPENAI_API_KEY')"
]
},
{
"cell_type": "code",
"source": [
"from openai import OpenAI\n",
"\n",
"client = OpenAI(\n",
" api_key=api_key\n",
")"
],
"metadata": {
"id": "n8lqvZxmdvXX"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"assistant = client.beta.assistants.create(\n",
" name=\"数学の家庭教師\",\n",
" instructions=\"あなたは数学の家庭教師です。コードを書いて実行し、数学の質問に答えてください。\",\n",
" tools=[{\"type\": \"code_interpreter\"}],\n",
" model=\"gpt-4-1106-preview\"\n",
")"
],
"metadata": {
"id": "YOvGKwGBd9Fh"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"assistant"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "NSIFuCk9kf0s",
"outputId": "fe4c99b5-a0e0-4288-dba0-56b8d0f9746d"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Assistant(id='asst_SYw4LlYJJQ0njzTAq7J0Qu6x', created_at=1699307029, description=None, file_ids=[], instructions='あなたは数学の家庭教師です。コードを書いて実行し、数学の質問に答えてください。', metadata={}, model='gpt-4-1106-preview', name='数学の家庭教師', object='assistant', tools=[ToolCodeInterpreter(type='code_interpreter')])"
]
},
"metadata": {},
"execution_count": 61
}
]
},
{
"cell_type": "code",
"source": [
"thread = client.beta.threads.create()"
],
"metadata": {
"id": "-OmkLmB9eRbA"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"thread"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "o7q7Qaz3kegi",
"outputId": "d5512f9f-e6b8-4a52-9059-7398d1f605f6"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Thread(id='thread_chsZjWv573qwSY39sJjHCCqI', created_at=1699307029, metadata={}, object='thread')"
]
},
"metadata": {},
"execution_count": 63
}
]
},
{
"cell_type": "code",
"source": [
"message = client.beta.threads.messages.create(\n",
" thread_id=thread.id,\n",
" role=\"user\",\n",
" content=\"方程式 `3x + 11 = 14` を解きたい。手伝ってもらえますか?\"\n",
")"
],
"metadata": {
"id": "T9-dJoUAi2zR"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"message"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "yh71OouBi3m_",
"outputId": "80378a8e-c14c-4391-c995-27fc28c2a939"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"ThreadMessage(id='msg_fpFnALo8wNtHVjZFoE9xHL84', assistant_id=None, content=[MessageContentText(text=Text(annotations=[], value='方程式 `3x + 11 = 14` を解きたい。手伝ってもらえますか?'), type='text')], created_at=1699307029, file_ids=[], metadata={}, object='thread.message', role='user', run_id=None, thread_id='thread_chsZjWv573qwSY39sJjHCCqI')"
]
},
"metadata": {},
"execution_count": 65
}
]
},
{
"cell_type": "code",
"source": [
"run = client.beta.threads.runs.create(\n",
" thread_id=thread.id,\n",
" assistant_id=assistant.id,\n",
" instructions=\"ユーザー名を Jane Doe としてください。このユーザーはプレミアムアカウントを持っています。\"\n",
")"
],
"metadata": {
"id": "6j14EYroi5Ra"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"run"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "UlFQMtdmkh3W",
"outputId": "76aa7cf1-e0a2-483c-a90f-60ed3a92148b"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Run(id='run_U9GV0RVfBBYNNE0sPMm6Nxev', assistant_id='asst_SYw4LlYJJQ0njzTAq7J0Qu6x', cancelled_at=None, completed_at=None, created_at=1699307029, expires_at=1699307629, failed_at=None, file_ids=[], instructions='ユーザー名を Jane Doe としてください。このユーザーはプレミアムアカウントを持っています。', last_error=None, metadata={}, model='gpt-4-1106-preview', object='thread.run', required_action=None, started_at=None, status='queued', thread_id='thread_chsZjWv573qwSY39sJjHCCqI', tools=[ToolAssistantToolsCode(type='code_interpreter')])"
]
},
"metadata": {},
"execution_count": 67
}
]
},
{
"cell_type": "code",
"source": [
"run = client.beta.threads.runs.retrieve(\n",
" thread_id=thread.id,\n",
" run_id=run.id\n",
")"
],
"metadata": {
"id": "qTxmYI8Mi8E2"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"run"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "9APpe4L3lN9C",
"outputId": "172681ed-20ac-4985-dcca-e5d71471dd28"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Run(id='run_U9GV0RVfBBYNNE0sPMm6Nxev', assistant_id='asst_SYw4LlYJJQ0njzTAq7J0Qu6x', cancelled_at=None, completed_at=None, created_at=1699307029, expires_at=1699307629, failed_at=None, file_ids=[], instructions='ユーザー名を Jane Doe としてください。このユーザーはプレミアムアカウントを持っています。', last_error=None, metadata={}, model='gpt-4-1106-preview', object='thread.run', required_action=None, started_at=None, status='queued', thread_id='thread_chsZjWv573qwSY39sJjHCCqI', tools=[ToolAssistantToolsCode(type='code_interpreter')])"
]
},
"metadata": {},
"execution_count": 69
}
]
},
{
"cell_type": "code",
"source": [
"# Run All をするとこの部分のレスポンスが間に合わないので`assistant_id=None`になっていたら以下を再度実行\n",
"messages = client.beta.threads.messages.list(\n",
" thread_id=thread.id\n",
")"
],
"metadata": {
"id": "4WSi_odmi9t-"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"messages"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "i02NxSeyi_Vw",
"outputId": "88378ba1-cd37-427f-f48e-6955299f3d4a"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"SyncCursorPage[ThreadMessage](data=[ThreadMessage(id='msg_ns4NWmDtGwg67rTZYzWycCrL', assistant_id='asst_SYw4LlYJJQ0njzTAq7J0Qu6x', content=[MessageContentText(text=Text(annotations=[], value='方程式 `3x + 11 = 14` の解は `x = 1` です。'), type='text')], created_at=1699307033, file_ids=[], metadata={}, object='thread.message', role='assistant', run_id='run_U9GV0RVfBBYNNE0sPMm6Nxev', thread_id='thread_chsZjWv573qwSY39sJjHCCqI'), ThreadMessage(id='msg_fpFnALo8wNtHVjZFoE9xHL84', assistant_id=None, content=[MessageContentText(text=Text(annotations=[], value='方程式 `3x + 11 = 14` を解きたい。手伝ってもらえますか?'), type='text')], created_at=1699307029, file_ids=[], metadata={}, object='thread.message', role='user', run_id=None, thread_id='thread_chsZjWv573qwSY39sJjHCCqI')], object='list', first_id='msg_ns4NWmDtGwg67rTZYzWycCrL', last_id='msg_fpFnALo8wNtHVjZFoE9xHL84', has_more=False)"
]
},
"metadata": {},
"execution_count": 73
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "TQckelksjAks"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment