ChatGPT AssistantsAPIを使ってみる
Statusの取得
ChatGPT のDevDayで発表されたAssistantsAPIをpythonから使ってみるメモ
公式ドキュメントを写経すると以下のようになる。
import os
import openai
client = openai.Client()
assistant = client.beta.assistants.create(
name="math tutor",
instructions="You are a personal math tutor. Write and run code to answer math questions.",
tools=[{"type": "code_interpreter"}],
model="gpt-3.5-turbo-1106",
)
thread = client.beta.threads.create()
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="I need to solve the equation `3x + 11 = 14`. Can you help me?"
)
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
instructions="Please address the user as Jane Doe. The user has a premium account."
)
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
print(messages)
これだとChatGPTからのレスポンスを待たずにmessagesを表示してしまうので期待した出力が取れない。
以下の様にするとrun.statusを確認してChatGPTのレスポンスを待つ必要があった
import os
import openai
client = openai.Client()
assistant = client.beta.assistants.create(
name="math tutor",
instructions="You are a personal math tutor. Write and run code to answer math questions.",
tools=[{"type": "code_interpreter"}],
model="gpt-3.5-turbo-1106",
)
thread = client.beta.threads.create()
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="I need to solve the equation `3x + 11 = 14`. Can you help me?"
)
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
instructions="Please address the user as Jane Doe. The user has a premium account."
)
while run.status != "completed":
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
if run.status == "completed":
break
time.sleep(1)
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
print(messages)
ファイルのアップロード
Playgroundだと.csvファイルをアップロードするとfileのindexに失敗みたいなエラーがでてしまう。
多分バグなのですぐ直ると思うが、pythonで.csvファイルの説明をしてもらうメモ
file = client.files.create(
file=open("data.csv", "rb"),
purpose='assistants'
)
assistant = client.beta.assistants.create(
name="Data visualizer",
description="You are great at creating beautiful data visualizations. You analyze data present in .csv files, understand trends, and come up with data visualizations relevant to those trends. You also share a brief text summary of the trends observed.",
model="gpt-4-1106-preview",
tools=[{"type": "code_interpreter"}],
file_ids=[file.id]
)
thread = client.beta.threads.create(
messages=[
{
"role": "user",
"content": "Create 3 data visualizations based on the trends in this file.",
"file_ids": [file.id]
}
]
)
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id
)
while run.status != "completed":
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
print(run)
if run.status == "completed":
break
time.sleep(1)
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
print(messages)
この記事が気に入ったらサポートをしてみませんか?