from openai import OpenAI
client = OpenAI()
client.files.delete("file-abc123")
Assistantオブジェクト
アシスタントの作成
アシスタントを作成します。
パラメータは、modelのみ必須で、あとはオプションとなります。
instructionには、アシスタントに対する指示が記述されます。
from openai import OpenAI
client = OpenAI()
my_assistant = client.beta.assistants.create(
instructions="You are an HR bot, and you have access to files to answer employee questions about company policies.",
name="HR Helper",
tools=[{"type": "retrieval"}],
model="gpt-4",
file_ids=["file-abc123"],
)
print(my_assistant)
アシスタントの取得
アシスタントIDを指定してアシスタントオブジェクトのインスタンスを取得します。
from openai import OpenAI
client = OpenAI()
my_assistant = client.beta.assistants.retrieve("asst_abc123")
アシスタントの変更
アシスタントidを指定して、アシスタントパラメータを変更します。
from openai import OpenAI
client = OpenAI()
my_updated_assistant = client.beta.assistants.update(
"asst_abc123",
instructions="You are an HR bot, and you have access to files to answer employee questions about company policies. Always response with info from either of the files.",
name="HR Helper",
tools=[{"type": "retrieval"}],
model="gpt-4",
file_ids=["file-abc123", "file-abc456"],
)
アシスタントの削除
アシスタントIDを指定してアシスタントを削除します。
from openai import OpenAI
client = OpenAI()
response = client.beta.assistants.delete("asst_abc123")
Threadオブジェクト
スレッドはユーザとの一連の対話を保持するオブジェクトです。
スレッドの作成
スレッドオブジェクトを作成します。
from openai import OpenAI
client = OpenAI()
empty_thread = client.beta.threads.create()
スレッドの取得
from openai import OpenAI
client = OpenAI()
my_thread = client.beta.threads.retrieve("thread_abc123")
from openai import OpenAI
client = OpenAI()
response = client.beta.threads.delete("thread_abc123")
Messageオブジェクト
メッセージオブジェクトはスレッドオブジェクトに含まれます。スレッドidを指定して作成します。
メッセージの作成
スレッドオブジェクト内にメッセージオブジェクトを追加します。
from openai import OpenAI
client = OpenAI()
thread_message = client.beta.threads.messages.create(
"thread_abc123",
role="user",
content="How does AI work? Explain it in simple terms.",
)
print(thread_message)
model
The ID of the Model to be used to execute this run. If a value is provided here, it will override the model associated with the assistant. If not, the model associated with the assistant will be used.
instructions
Override the default system message of the assistant. This is useful for modifying the behavior on a per-run basis.
tools
Override the tools the assistant can use for this run. This is useful for modifying the behavior on a per-run basis.
metadata
Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.
from openai import OpenAI
client = OpenAI()
run = client.beta.threads.runs.retrieve(
thread_id="thread_abc123",
run_id="run_abc123"
)
runの変更
from openai import OpenAI
client = OpenAI()
run = client.beta.threads.runs.update(
thread_id="thread_abc123",
run_id="run_abc123",
metadata={"user_id": "user_abc123"},
)
print(run)
ツールの出力送信 (submit_tool_output)
submit_tool_outputは、Function_callingの実行のために使用します。
When a run has the status: "requires_action" and required_action.type is submit_tool_outputs, this endpoint can be used to submit the outputs from the tool calls once they're all completed. All outputs must be submitted in a single request.
from openai import OpenAI
client = OpenAI()
run = client.beta.threads.create_and_run(
assistant_id="asst_abc123",
thread={
"messages": [
{"role": "user", "content": "Explain deep learning to a 5 year old."}
]
}
)