このトピックでは、実行の作成、一覧表示、取得、変更、ツール出力の送信、実行の終了待機など、アシスタント API の実行コンポーネントの機能について説明します。
機能 | 説明 |
実行の作成 | スレッドを実行するための実行クラスを作成します。 |
スレッドと実行の作成 | スレッドを作成して実行します。 |
実行の一覧表示 | スレッド内の実行を一覧表示します。 |
実行ステップの一覧表示 | 実行の実行ステップを一覧表示します。 |
実行の取得 | 実行インスタンスを取得します。 |
実行ステップの取得 | 実行ステップを取得します。 |
実行の変更 | 実行インスタンスを変更します。 |
ツール出力を実行に送信 | ツール結果を実行インスタンスに送信し、実行を続行します。 |
実行タスクの作成
HTTP
サンプルコード
非ストリーミング
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/threads/thread_cc2a3e9d-436b-482b-91c5-377e0f376506/runs' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--data '{
"assistant_id": "asst_46e95014-ccac-435d-b884-c97ac44a94d9",
"metadata": {}
}'
ストリーミング
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/threads/thread_cc2a3e9d-436b-482b-91c5-377e0f376506/runs' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--data '{
"assistant_id": "asst_46e95014-ccac-435d-b884-c97ac44a94d9",
"metadata": {},
"stream": true
}'
リクエストパラメータ
パラメータ | 説明 | タイプ | 必須 |
thread id | 実行するスレッドの ID。 | str | はい |
assistant id | 実行するアシスタントの ID。 | str | はい |
model | 使用するモデルの名前。ここで指定した場合、アシスタントで定義されたモデルはオーバーライドされます。 | str | いいえ |
instructions | システムプロンプト。ここで指定した場合、アシスタントで定義された命令はオーバーライドされます。 | str | いいえ |
additional instruction | 元の命令の末尾に追加の命令を追加します。 | str | いいえ |
tools | ツールリスト。ここで指定した場合、アシスタントで定義されたツールリストはオーバーライドされます。詳細については、以下の注記を参照してください。 | str | いいえ |
metadata | その他の関連パラメータの説明。 | str | いいえ |
stream | ストリーミング出力モードを有効にするかどうかを指定します。 | boolean | いいえ |
temperature | モデルによって生成される応答の多様性を制御します。 | float | いいえ |
top_p | サンプリングメソッドの確率しきい値。 | float | いいえ |
top_k | サンプリングの候補セットのサイズ。 | integer | いいえ |
カスタムツールの認証情報は、次の方法で構成します。
{
"type": "plugin_type",
"auth": {
"type": "user_http",
"user_token": "bearer-token", }
}
アシスタントがプラグインの呼び出しをリクエストすると、アシスタントは次のフィールドに bearer トークンを構成します。
{"plugin_type":{"user_token": "bearer-token"}}'
次に、リクエストされたプラグインのヘッダーに bearer トークンを追加します。
サンプルレスポンス
非ストリーミング
{
"id": "run_3de634fa-75d4-4370-adcf-92ba2a60c396",
"object": "thread.run",
"created_at": 1711517598169,
"thread_id": "thread_cc2a3e9d-436b-482b-91c5-377e0f376506",
"assistant_id": "asst_46e95014-ccac-435d-b884-c97ac44a94d9",
"status": "in_progress",
"model": "qwen-max",
"instructions": "You are an intelligent assistant. You can use different tools based on user needs and then provide answers. Use tools when needed.", // あなたはインテリジェントなアシスタントです。ユーザーのニーズに基づいてさまざまなツールを使用し、回答を提供できます。必要に応じてツールを使用してください。
"tools": [
{
"type": "search"
},
{
"type": "text_to_image"
}
],
"metadata": {},
"request_id": "225292ff-9843-95d4-8591-b1377c33e2be"
}
ストリーミング
event:thread.created
data:{"id":"thread_5b7824dc-dbf9-4538-b214-624ee199caa4","object":"thread","created_at":1714448101977,"metadata":{},"request_id":"f38314af-21c4-945e-acdc-cb9765d1d48f"}
event:thread.run.created
data:{"id":"run_04fba6a3-3ac9-42e8-ba2b-9b5c889a8d58","object":"thread.run","created_at":1714448101996,"thread_id":"thread_5b7824dc-dbf9-4538-b214-624ee199caa4","assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","status":"queued","required_action":null,"last_error":null,"expires_at":null,"started_at":null,"cancelled_at":null,"failed_at":null,"completed_at":null,"model":"qwen-max","instructions":"You are an intelligent assistant. You can use different tools based on user needs and then provide answers. Use tools when needed.","tools":[{"type":"text_to_image"},{"type":"function","function":{"name":"get_current_weather","description":"Get the current weather in a given location","parameters":{"type":"object","properties":{"location":{"type":"string","description":"The city and state, e.g. San Francisco, CA"},"unit":{"type":"string","enum":["celsius","fahrenheit"]}},"required":["location"]}}}],"file_ids":[],"metadata":{},"usage":null,"temperature":null,"top_p":null,"top_k":null,"max_tokens":null,"truncation_strategy":null,"request_id":"f38314af-21c4-945e-acdc-cb9765d1d48f"}
// {"type":"function","function":{"name":"get_current_weather","description":"指定された場所の現在の天気を取得します","parameters":{"type":"object","properties":{"location":{"type":"string","description":"都市と州、例:サンフランシスコ、カリフォルニア"},"unit":{"type":"string","enum":["celsius","fahrenheit"]}},"required":["location"]}}}],"file_ids":[],"metadata":{},"usage":null,"temperature":null,"top_p":null,"top_k":null,"max_tokens":null,"truncation_strategy":null,"request_id":"f38314af-21c4-945e-acdc-cb9765d1d48f"}
event:thread.run.queued
data:{"id":"run_04fba6a3-3ac9-42e8-ba2b-9b5c889a8d58","object":"thread.run","created_at":1714448101996,"thread_id":"thread_5b7824dc-dbf9-4538-b214-624ee199caa4","assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","status":"queued","required_action":null,"last_error":null,"expires_at":null,"started_at":null,"cancelled_at":null,"failed_at":null,"completed_at":null,"model":"qwen-max","instructions":"You are an intelligent assistant. You can use different tools based on user needs and then provide answers. Use tools when needed.","tools":[{"type":"text_to_image"},{"type":"function","function":{"name":"get_current_weather","description":"Get the current weather in a given location","parameters":{"type":"object","properties":{"location":{"type":"string","description":"The city and state, e.g. San Francisco, CA"},"unit":{"type":"string","enum":["celsius","fahrenheit"]}},"required":["location"]}}}],"file_ids":[],"metadata":{},"usage":null,"temperature":null,"top_p":null,"top_k":null,"max_tokens":null,"truncation_strategy":null,"request_id":"f38314af-21c4-945e-acdc-cb9765d1d48f"}
event:thread.run.in_progress
data:{"id":"run_04fba6a3-3ac9-42e8-ba2b-9b5c889a8d58","object":"thread.run","created_at":1714448101996,"thread_id":"thread_5b7824dc-dbf9-4538-b214-624ee199caa4","assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","status":"in_progress","required_action":null,"last_error":null,"expires_at":null,"started_at":null,"cancelled_at":null,"failed_at":null,"completed_at":null,"model":"qwen-max","instructions":"You are an intelligent assistant. You can use different tools based on user needs and then provide answers. Use tools when needed.","tools":[{"type":"text_to_image"},{"type":"function","function":{"name":"get_current_weather","description":"Get the current weather in a given location","parameters":{"type":"object","properties":{"location":{"type":"string","description":"The city and state, e.g. San Francisco, CA"},"unit":{"type":"string","enum":["celsius","fahrenheit"]}},"required":["location"]}}}],"file_ids":[],"metadata":{},"usage":null,"temperature":null,"top_p":null,"top_k":null,"max_tokens":null,"truncation_strategy":null,"request_id":"f38314af-21c4-945e-acdc-cb9765d1d48f"}
event:thread.run.step.created
data:{"id":"step_52c42910-4993-496d-a7f3-ecd4cf007839","object":"thread.run.step","created_at":1714448102574,"assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","thread_id":"thread_5b7824dc-dbf9-4538-b214-624ee199caa4","run_id":"run_04fba6a3-3ac9-42e8-ba2b-9b5c889a8d58","type":"message_creation","status":"in_progress","step_details":{"type":"message_creation","message_creation":{"message_id":"message_f4f68f6b-6003-4115-8f20-3ea590137502"}},"last_error":null,"expired_at":null,"cancelled_at":null,"failed_at":null,"completed_at":null,"metadata":{},"usage":null,"request_id":"f38314af-21c4-945e-acdc-cb9765d1d48f"}
event:thread.run.step.in_progress
data:{"id":"step_52c42910-4993-496d-a7f3-ecd4cf007839","object":"thread.run.step","created_at":1714448102574,"assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","thread_id":"thread_5b7824dc-dbf9-4538-b214-624ee199caa4","run_id":"run_04fba6a3-3ac9-42e8-ba2b-9b5c889a8d58","type":"message_creation","status":"in_progress","step_details":{"type":"message_creation","message_creation":{"message_id":"message_f4f68f6b-6003-4115-8f20-3ea590137502"}},"last_error":null,"expired_at":null,"cancelled_at":null,"failed_at":null,"completed_at":null,"metadata":{},"usage":null,"request_id":"f38314af-21c4-945e-acdc-cb9765d1d48f"}
event:thread.message.created
data:{"id":"message_f4f68f6b-6003-4115-8f20-3ea590137502","object":"thread.message","created_at":1714448102574,"thread_id":"thread_5b7824dc-dbf9-4538-b214-624ee199caa4","incomplete_details":null,"completed_at":null,"incomplete_at":null,"role":"assistant","content":[],"assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","run_id":"run_04fba6a3-3ac9-42e8-ba2b-9b5c889a8d58","file_ids":[],"metadata":{},"request_id":"f38314af-21c4-945e-acdc-cb9765d1d48f"}
event:thread.message.in_progress
data:{"id":"message_f4f68f6b-6003-4115-8f20-3ea590137502","object":"thread.message","created_at":1714448102574,"thread_id":"thread_5b7824dc-dbf9-4538-b214-624ee199caa4","incomplete_details":null,"completed_at":null,"incomplete_at":null,"role":"assistant","content":[],"assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","run_id":"run_04fba6a3-3ac9-42e8-ba2b-9b5c889a8d58","file_ids":[],"metadata":{},"request_id":"f38314af-21c4-945e-acdc-cb9765d1d48f"}
event:thread.message.delta
data:{"id":"message_f4f68f6b-6003-4115-8f20-3ea590137502","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"9","annotations":[]}}],"file_ids":[]},"request_id":"f38314af-21c4-945e-acdc-cb9765d1d48f"}
event:thread.message.delta
data:{"id":"message_f4f68f6b-6003-4115-8f20-3ea590137502","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"9","annotations":[]}}],"file_ids":[]},"request_id":"f38314af-21c4-945e-acdc-cb9765d1d48f"}
event:thread.message.delta
data:{"id":"message_f4f68f6b-6003-4115-8f20-3ea590137502","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"9","annotations":[]}}],"file_ids":[]},"request_id":"f38314af-21c4-945e-acdc-cb9765d1d48f"}
event:thread.message.delta
data:{"id":"message_f4f68f6b-6003-4115-8f20-3ea590137502","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"times 998","annotations":[]}}],"file_ids":[]},"request_id":"f38314af-21c4-945e-acdc-cb9765d1d48f"}
event:thread.message.delta
data:{"id":"message_f4f68f6b-6003-4115-8f20-3ea590137502","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"equals 997002.","annotations":[]}}],"file_ids":[]},"request_id":"f38314af-21c4-945e-acdc-cb9765d1d48f"}
event:thread.message.delta
data:{"id":"message_f4f68f6b-6003-4115-8f20-3ea590137502","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"","annotations":[]}}],"file_ids":[]},"request_id":"f38314af-21c4-945e-acdc-cb9765d1d48f"}
event:thread.message.completed
data:{"id":"message_f4f68f6b-6003-4115-8f20-3ea590137502","object":"thread.message","created_at":1714448102574,"thread_id":"thread_5b7824dc-dbf9-4538-b214-624ee199caa4","incomplete_details":null,"completed_at":1714448104154,"incomplete_at":null,"role":"assistant","content":[{"type":"text","text":{"value":"999 times 998 equals 997002.","annotations":[]}}],"assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","run_id":"run_04fba6a3-3ac9-42e8-ba2b-9b5c889a8d58","file_ids":[],"metadata":{},"request_id":"f38314af-21c4-945e-acdc-cb9765d1d48f"}
event:thread.run.step.completed
data:{"id":"step_52c42910-4993-496d-a7f3-ecd4cf007839","object":"thread.run.step","created_at":1714448102574,"assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","thread_id":"thread_5b7824dc-dbf9-4538-b214-624ee199caa4","run_id":"run_04fba6a3-3ac9-42e8-ba2b-9b5c889a8d58","type":"message_creation","status":"completed","step_details":{"type":"message_creation","message_creation":{"message_id":"message_f4f68f6b-6003-4115-8f20-3ea590137502"}},"last_error":null,"expired_at":null,"cancelled_at":null,"failed_at":null,"completed_at":1714448104154,"metadata":{},"usage":null,"request_id":"f38314af-21c4-945e-acdc-cb9765d1d48f"}
event:thread.run.completed
data:{"id":"run_04fba6a3-3ac9-42e8-ba2b-9b5c889a8d58","object":"thread.run","created_at":1714448101996,"thread_id":"thread_5b7824dc-dbf9-4538-b214-624ee199caa4","assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","status":"completed","required_action":null,"last_error":null,"expires_at":null,"started_at":null,"cancelled_at":null,"failed_at":null,"completed_at":null,"model":"qwen-max","instructions":"You are an intelligent assistant. You can use different tools based on user needs and then provide answers. Use tools when needed.","tools":[{"type":"text_to_image"},{"type":"function","function":{"name":"get_current_weather","description":"Get the current weather in a given location","parameters":{"type":"object","properties":{"location":{"type":"string","description":"The city and state, e.g. San Francisco, CA"},"unit":{"type":"string","enum":["celsius","fahrenheit"]}},"required":["location"]}}}],"file_ids":[],"metadata":{},"usage":{"input_tokens":419,"output_tokens":16,"total_tokens":435},"temperature":null,"top_p":null,"top_k":null,"max_tokens":null,"truncation_strategy":null,"request_id":"f38314af-21c4-945e-acdc-cb9765d1d48f"}
event:done
data:[DONE]
レスポンスパラメータ
作成された実行クラス。次のパラメータを含みます。
id: 実行 ID。
thread_id: スレッド ID。
assistant_id: アシスタント ID。
request_id: リクエスト ID。
SDK
サンプルコード(ストリーミングモード)
stream_iterator = dashscope.Runs.create(thread.id,
assistant_id=assistant.id,
stream=True)
for event, msg in stream_iterator:
print(event)
print(msg)
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.InvalidateParameter;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.threads.runs.AssistantStreamMessage;
import com.alibaba.dashscope.threads.runs.RunParam;
import com.alibaba.dashscope.threads.runs.Runs;
import io.reactivex.Flowable;
import com.alibaba.dashscope.utils.Constants;
public class Main {
static {
Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
}
public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
Runs runs = new Runs();
RunParam runParam = RunParam.builder().assistantId("assistantId").stream(true).build();
Flowable<AssistantStreamMessage> run = runs.createStream("threadId", runParam);
}
}
リクエストパラメータ
パラメータ | タイプ | デフォルト値 | 説明 |
thread_id | str | - | 実行を作成するスレッドの ID。 |
assistant_id | str | - | アシスタント ID。 |
model | str | - | 使用されるモデル。 |
instructions | str | なし | この実行のプロンプト。 |
additional_instructions | str | なし | この実行の追加命令。 |
tools | List[Dict] | [] | アシスタントが使用するツール。 |
stream | boolean | False | ストリーミング出力モードを有効にするかどうかを指定します。 |
metadata | Dict | なし | アシスタントのメタデータ。 |
temperature | これらのパラメータについては、Qwen を参照してください。 | ||
top_p | |||
top_k | |||
max_tokens | |||
truncation_strategy | Dict | なし | スレッドのトランケーション戦略。有効な値とデフォルト値:
|
workspace | str | なし | ワークスペース ID。 |
api_key | str | なし | API キー。環境変数として設定できます。 |
カスタムツールの認証情報は、次の方法で構成します。
{
"type": "plugin_type",
"auth": {
"type": "user_http",
"user_token": "bearer-token", }
}
アシスタントがプラグインの呼び出しをリクエストすると、アシスタントは次のフィールドに bearer トークンを構成します。
{"plugin_type":{"user_token": "bearer-token"}}'
リクエストヘッダーに bearer トークンを追加します。
サンプルレスポンス
ストリーミング出力モードでは、イベントとイベントデータが返されます。サンプルレスポンス:
thread.run.created
{'tools': [{'type': 'quark_search'}], 'required_action': None, 'id': 'run_0f314675-06a4-4379-929c-d29cd351a446', 'object': 'thread.run', 'created_at': 1714460227904, 'thread_id': 'thread_28f0b00b-d825-48f5-bb27-f3b171a64d33', 'assistant_id': 'asst_9453fe59-d6eb-498e-aeca-9abecd492e51', 'status': 'queued', 'last_error': None, 'expires_at': None, 'started_at': None, 'cancelled_at': None, 'failed_at': None, 'completed_at': None, 'model': 'qwen-max', 'instructions': 'You are a helpful assistant. When asked a question, use tools wherever possible.', 'file_ids': [], 'metadata': {}, 'usage': None, 'temperature': None, 'top_p': None, 'top_k': None, 'max_tokens': None, 'truncation_strategy': None, 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
// {'tools': [{'type': 'quark_search'}], 'required_action': None, 'id': 'run_0f314675-06a4-4379-929c-d29cd351a446', 'object': 'thread.run', 'created_at': 1714460227904, 'thread_id': 'thread_28f0b00b-d825-48f5-bb27-f3b171a64d33', 'assistant_id': 'asst_9453fe59-d6eb-498e-aeca-9abecd492e51', 'status': 'queued', 'last_error': None, 'expires_at': None, 'started_at': None, 'cancelled_at': None, 'failed_at': None, 'completed_at': None, 'model': 'qwen-max', 'instructions': 'あなたは親切なアシスタントです。質問されたら、可能な限りツールを使用してください。', 'file_ids': [], 'metadata': {}, 'usage': None, 'temperature': None, 'top_p': None, 'top_k': None, 'max_tokens': None, 'truncation_strategy': None, 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.run.queued
...
レスポンスパラメータ
パラメータ | タイプ | 説明 |
status_code | int | HTTP ステータスコード。200 は成功を示し、その他のコードは失敗を示します。 |
id | str | 実行 ID。UUID 文字列としてフォーマットされます。 |
model | str | 実行で使用されるモデルの名前。 |
instructions | str | アシスタントの命令情報。 |
metadata | object | アシスタントのメタデータのキーと値のペア。 |
status | str | 実行のステータス。 |
thread_id | str | 実行に関連付けられたスレッドの ID。 |
tools | List[Tool] | 実行中に使用されるツール。 |
created_at | timestamp | 実行が作成されたタイムスタンプ。 |
gmt_created | datetime | 実行が作成された日時。 |
gmt_modified | datetime | 実行が変更された日時。 |
code | str | リクエストの失敗時に返されるエラーコード。このパラメータは、Python 用 SDK を使用した場合にのみ返されます。 |
message | str | リクエストの失敗時に返されるエラーメッセージ。このパラメータは、Python 用 SDK を使用した場合にのみ返されます。 |
スレッドと実行タスクの作成
HTTP
サンプルコード
非ストリーミング
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/threads/runs' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--data '{
"assistant_id": "asst_ce66cfa8-b2f8-4e4c-8e17-2c335145be42",
"thread": {
"messages": [
{
"role": "user",
"content": "What is the weather in Singapore?" // シンガポールの天気はどうですか?
}
]
}
}'
ストリーミング
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/threads/runs' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--data '{
"assistant_id": "asst_ce66cfa8-b2f8-4e4c-8e17-2c335145be42",
"thread": {
"messages": [
{
"role": "user",
"content": "Who are you?" // あなたは誰ですか?
}
]
},
"stream": true
}'
リクエストパラメータ
パラメータ | 説明 | タイプ | 必須 |
thread id | 実行するスレッドの ID。 | str | はい |
assistant id | 実行するアシスタントの ID。 | str | はい |
thread | スレッドに関する情報。 | dict | はい |
stream | ストリーミング出力モードを有効にするかどうかを指定します。 | boolean | いいえ |
temperature | モデルによって生成される応答の多様性を制御します。 | float | いいえ |
top_p | サンプリングメソッドの確率しきい値。 | float | いいえ |
top_k | サンプリングの候補セットのサイズ。 | integer | いいえ |
サンプルレスポンス
非ストリーミング
{
"id": "run_99935dbd-e731-4bad-b450-60d9d6bf9a44",
"object": "thread.run",
"created_at": 1711519089921,
"thread_id": "thread_9157dfad-71fd-4add-b7a9-1afa452d2d67",
"assistant_id": "asst_ce66cfa8-b2f8-4e4c-8e17-2c335145be42",
"status": "in_progress",
"model": "qwen-max",
"instructions": "You are an intelligent assistant. You can use different tools based on user needs and then provide answers. Use tools when needed.", // あなたはインテリジェントなアシスタントです。ユーザーのニーズに基づいてさまざまなツールを使用し、回答を提供できます。必要に応じてツールを使用してください。
"tools": [
{
"type": "text_to_image"
},
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location", // 指定された場所の現在の天気を取得します
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA" // 都市と州、例:サンフランシスコ、カリフォルニア
},
"unit": {
"type": "string",
"enum": [
"celsius",
"fahrenheit"
]
}
},
"required": [
"location"
]
}
}
}
],
"metadata": {},
"request_id": "191ecdaa-7ef6-9e29-8572-4f89be05bc6c"
}
ストリーミング
event:thread.created
data:{"id":"thread_4a1e6bee-4e0a-4749-a0c8-f6db8bb7c4bb","object":"thread","created_at":1714448762315,"metadata":{},"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}
event:thread.run.created
data:{"id":"run_65adea5e-9086-4806-b733-bb0a7b53a459","object":"thread.run","created_at":1714448762335,"thread_id":"thread_4a1e6bee-4e0a-4749-a0c8-f6db8bb7c4bb","assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","status":"queued","required_action":null,"last_error":null,"expires_at":null,"started_at":null,"cancelled_at":null,"failed_at":null,"completed_at":null,"model":"qwen-max","instructions":"You are an intelligent assistant. You can use different tools based on user needs and then provide answers. Use tools when needed.","tools":[{"type":"text_to_image"},{"type":"function","function":{"name":"get_current_weather","description":"Get the current weather in a given location","parameters":{"type":"object","properties":{"location":{"type":"string","description":"The city and state, e.g. San Francisco, CA"},"unit":{"type":"string","enum":["celsius","fahrenheit"]}},"required":["location"]}}}],"file_ids":[],"metadata":{},"usage":null,"temperature":null,"top_p":null,"top_k":null,"max_tokens":null,"truncation_strategy":null,"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}
// {"type":"function","function":{"name":"get_current_weather","description":"指定された場所の現在の天気を取得します","parameters":{"type":"object","properties":{"location":{"type":"string","description":"都市と州、例:サンフランシスコ、カリフォルニア"},"unit":{"type":"string","enum":["celsius","fahrenheit"]}},"required":["location"]}}}],"file_ids":[],"metadata":{},"usage":null,"temperature":null,"top_p":null,"top_k":null,"max_tokens":null,"truncation_strategy":null,"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}
event:thread.run.queued
...
レスポンスパラメータ
作成された実行クラス。次のパラメータを含みます。
id: 実行 ID。
thread_id: スレッド ID。
assistant_id: アシスタント ID。
request_id: リクエスト ID。
SDK
サンプルコード
from dashscope import Runs
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
run = Runs.create('thread_id',
assistant_id='assistant_id',
model='the_model_to_use',
instructions='Instructions', // 命令
additional_instructions='additional_instructions', // 追加の命令
tools=['list_of_tool_to_use'], // 使用するツールのリスト
metadata={'key': 'value'})
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.InvalidateParameter;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.threads.runs.Run;
import com.alibaba.dashscope.threads.runs.RunParam;
import com.alibaba.dashscope.threads.runs.Runs;
import com.alibaba.dashscope.utils.Constants;
public class Main {
static {
Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
}
public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
Runs runs = new Runs();
RunParam runParam = RunParam.builder().assistantId("assistantId").build();
Run run = runs.create("threadId", runParam);
}
}
リクエストパラメータ
パラメータ | タイプ | デフォルト値 | 説明 |
thread_id | str | - | 実行を作成するスレッドの ID。 |
assistant_id | str | - | アシスタント ID。 |
model | str | - | 使用されるモデル。 |
instructions | str | なし | この実行のプロンプト。 |
additional_instructions | str | なし | この実行の追加命令。 |
tools | List[Dict] | [] | アシスタントが使用するツール。 |
metadata | Dict | なし | アシスタントのメタデータ。 |
サンプルレスポンス
結果は実行オブジェクトで、次の JSON コンテンツを含みます。
{
"account_id": "YOUR_AK", // あなたの AK
"assistant_id": "asst_8eeaedde-3f8b-41d2-9433-0e88a6042ecb",
"created_at": 1711347311426,
"file_ids": [],
"id": "run_f2c663e6-69f9-4494-9017-acf788db5a17",
"instructions": "You are a helpful assistant. When asked a question, use tools wherever possible.", // あなたは親切なアシスタントです。質問されたら、可能な限りツールを使用してください。
"metadata": {},
"model": "qwen-max-allinone",
"object": "thread.run",
"request_id": "53561cd0-350c-918d-b3f7-716a0e41bcfd",
"status": "in_progress",
"status_code": 200,
"thread_id": "thread_45ca9a78-2b1f-4172-b4cf-cf8c748c1c32",
"tools": [
{
"type": "search"
},
{
"function": {
"description": "Add to number", // 数値を加算する
"name": "big_add",
"parameters": {
"properties": {
"left": {
"description": "The left operator", // 左側の演算子
"type": "integer"
},
"right": {
"description": "The right operator.", // 右側の演算子
"type": "integer"
}
},
"required": [
"left",
"right"
],
"type": "object"
}
},
"type": "function"
}
]
}
実行タスクの一覧表示
HTTP
サンプルコード
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/threads/thread_cc2a3e9d-436b-482b-91c5-377e0f376506/runs?limit=20&order=asc' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"
リクエストパラメータ
パラメータ | 説明 | タイプ | 必須 |
thread id | 実行が属するスレッドの ID。 | str | はい |
limit | 返される実行の最大数。 | int | いいえ |
order | 作成時刻に基づいてメッセージをソートする昇順または降順。デフォルト値:desc。 | str | いいえ |
サンプルレスポンス
{
"object": "list",
"data": [
{
"id": "run_3de634fa-75d4-4370-adcf-92ba2a60c396",
"object": "thread.run",
"created_at": 1711517598169,
"thread_id": "thread_cc2a3e9d-436b-482b-91c5-377e0f376506",
"assistant_id": "asst_46e95014-ccac-435d-b884-c97ac44a94d9",
"status": "completed",
"required_action": {},
"last_error": {},
"expires_at": 0,
"started_at": 0,
"cancelled_at": 0,
"failed_at": 0,
"completed_at": 0,
"model": "qwen-max",
"instructions": "You are an intelligent assistant. You can use different tools based on user needs and then provide answers. Use tools when needed.", // あなたはインテリジェントなアシスタントです。ユーザーのニーズに基づいてさまざまなツールを使用し、回答を提供できます。必要に応じてツールを使用してください。
"tools": [
{
"type": "search"
},
{
"type": "text_to_image"
}
],
"metadata": {},
"usage": {
"prompt_tokens": 66,
"completion_tokens": 9,
"total_tokens": 75
}
}
],
"first_id": "run_3de634fa-75d4-4370-adcf-92ba2a60c396",
"last_id": "run_3de634fa-75d4-4370-adcf-92ba2a60c396",
"has_more": false,
"request_id": "ff3d2aea-555c-9825-80ca-24029a636641"
}
レスポンスパラメータ
実行クラスのリストを返します。次のパラメータを含みます。
実行のリスト。
SDK
サンプルコード
from dashscope import Runs
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
runs = Runs.list('thread_id',
limit=1,
order='desc',
after='',
before='')
import com.alibaba.dashscope.common.GeneralListParam;
import com.alibaba.dashscope.common.ListResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.InvalidateParameter;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.threads.runs.Run;
import com.alibaba.dashscope.threads.runs.Runs;
import com.alibaba.dashscope.utils.Constants;
public class Main {
static {
Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
}
public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
Runs runs = new Runs();
GeneralListParam listRuns = GeneralListParam.builder().build();
ListResult<Run> run = runs.list("threadId", listRuns);
}
}
リクエストパラメータ
パラメータ | タイプ | 説明 |
thread_id | str | 実行が属するスレッドの ID。 |
limit | str | |
order | str | |
after | str | |
before | str | |
workspace | str | ワークスペース ID。 |
api_key | str | API キー。環境変数として設定できます。 |
レスポンスパラメータ
パラメータ | タイプ | 説明 |
has_more | bool | |
last_id | str | |
first_id | str | |
data | list[Run] | 実行オブジェクトのリスト。 |
実行ステップの一覧表示
HTTP
サンプルコード
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/threads/thread_cc2a3e9d-436b-482b-91c5-377e0f376506/runs/run_3de634fa-75d4-4370-adcf-92ba2a60c396/steps?limit=20&order=asc' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"
リクエストパラメータ
パラメータ | 説明 | タイプ | 必須 |
thread id | 実行が属するスレッドの ID。 | str | はい |
run id | ステップが属する実行の ID。 | str | はい |
limit | 返されるステップの最大数。 | int | いいえ |
order | 作成時刻に基づいてメッセージをソートする昇順または降順。デフォルト値:desc。 | str | いいえ |
サンプルレスポンス
{
"object": "list",
"data": [
{
"id": "step_4db180b5-d44a-4b12-9390-4307c6cb87a5",
"object": "thread.run.step",
"created_at": 1711517599333,
"assistant_id": "asst_46e95014-ccac-435d-b884-c97ac44a94d9",
"thread_id": "thread_cc2a3e9d-436b-482b-91c5-377e0f376506",
"run_id": "run_3de634fa-75d4-4370-adcf-92ba2a60c396",
"type": "message_creation",
"status": "completed",
"step_details": {
"type": "message_creation",
"message_creation": {
"message_id": "message_75c67937-a829-4741-ae83-6839cae81269"
}
},
"last_error": {
"code": "",
"message": ""
},
"expires_at": 0,
"started_at": 0,
"cancelled_at": -1,
"failed_at": -1,
"completed_at": -1,
"metadata": {},
"usage": {}
}
],
"first_id": "step_4db180b5-d44a-4b12-9390-4307c6cb87a5",
"last_id": "step_4db180b5-d44a-4b12-9390-4307c6cb87a5",
"has_more": false,
"request_id": "df979778-7c6a-9f45-b7da-758ae00d1e19"
}
レスポンスパラメータ
実行ステップクラスのリストを返します。次のパラメータを含みます。
実行ステップクラスのリスト。
request_id: リクエスト ID。
SDK
サンプルコード
from dashscope import Steps
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
steps = Steps.list('run_id',
thread_id='thread_id',
limit='')
import com.alibaba.dashscope.common.GeneralListParam;
import com.alibaba.dashscope.common.ListResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.InvalidateParameter;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.threads.runs.RunStep;
import com.alibaba.dashscope.threads.runs.Runs;
import com.alibaba.dashscope.utils.Constants;
public class Main {
static {
Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
}
public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
Runs runs = new Runs();
GeneralListParam listSteps = GeneralListParam.builder().build();
ListResult<RunStep> run = runs.listSteps("threadId", "runId", listSteps);
}
}
リクエストパラメータ
パラメータ | タイプ | 説明 |
run_id | str | 実行ステップが属する実行の ID。 |
thread_id | str | 実行が属するスレッドの ID。 |
limit | str | |
order | ||
after | ||
before | ||
workspace | str | ワークスペース ID。 |
api_key | str | API キー。環境変数として設定できます。 |
レスポンスパラメータ
パラメータ | タイプ | 説明 |
has_more | bool | |
last_id | str | |
first_id | str | |
data | list[Step] | 実行ステップのリスト。 |
実行タスクの取得
HTTP
サンプルコード
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/threads/thread_cc2a3e9d-436b-482b-91c5-377e0f376506/runs/run_3de634fa-75d4-4370-adcf-92ba2a60c396' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"
リクエストパラメータ
パラメータ | 説明 | タイプ | 必須 |
thread id | 実行が属するスレッドの ID。 | str | はい |
run_id | 取得する実行の ID。 | str | はい |
サンプルレスポンス
{
"id": "run_3de634fa-75d4-4370-adcf-92ba2a60c396",
"object": "thread.run",
"created_at": 1711517598169,
"thread_id": "thread_cc2a3e9d-436b-482b-91c5-377e0f376506",
"assistant_id": "asst_46e95014-ccac-435d-b884-c97ac44a94d9",
"status": "completed",
"required_action": {},
"last_error": {},
"expires_at": 0,
"started_at": 0,
"cancelled_at": 0,
"failed_at": 0,
"completed_at": 0,
"model": "qwen-max",
"instructions": "You are an intelligent assistant. You can use different tools based on user needs and then provide answers. Use tools when needed.", // あなたはインテリジェントなアシスタントです。ユーザーのニーズに基づいてさまざまなツールを使用し、回答を提供できます。必要に応じてツールを使用してください。
"tools": [
{
"type": "search"
},
{
"type": "text_to_image"
}
],
"metadata": {},
"usage": {
"prompt_tokens": 66,
"completion_tokens": 9,
"total_tokens": 75
},
"request_id": "05c5f4ea-5e6b-9901-9849-7a0943a0db43"
}
レスポンスパラメータ
取得された実行クラスを返します。次のパラメータを含みます。
id: 実行 ID。
thread_id: スレッド ID。
assistant_id: アシスタント ID。
request_id: リクエスト ID。
SDK
サンプルコード
from dashscope import Runs
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
run = Runs.retrieve('run_id',
thread_id='thread_id')
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.InvalidateParameter;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.threads.runs.Run;
import com.alibaba.dashscope.threads.runs.Runs;
import com.alibaba.dashscope.utils.Constants;
public class Main {
static {
Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
}
public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
Runs runs = new Runs();
Run run = runs.retrieve("threadId", "runId");
}
}
リクエストパラメータ
パラメータ | タイプ | 説明 |
run_id | str | 取得する実行の ID。 |
thread_id | str | 実行が属するスレッドの ID。 |
workspace | str | ワークスペース ID。 |
api_key | str | API キー。環境変数として設定できます。 |
レスポンスパラメータ
実行タスクの作成 セクションのレスポンスパラメータを参照してください。
実行ステップの取得
HTTP
サンプルコード
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/threads/thread_cc2a3e9d-436b-482b-91c5-377e0f376506/runs/run_3de634fa-75d4-4370-adcf-92ba2a60c396/steps/step_4db180b5-d44a-4b12-9390-4307c6cb87a5' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"
リクエストパラメータ
パラメータ | 説明 | タイプ | 必須 |
thread id | 実行が属するスレッドの ID。 | str | はい |
run_id | ステップが属する実行の ID。 | str | はい |
step_id | 取得するステップの ID。 | str | はい |
* | その他のオプションのリクエストパラメータ。 | str | いいえ |
サンプルレスポンス
{
"id": "step_4db180b5-d44a-4b12-9390-4307c6cb87a5",
"object": "thread.run.step",
"created_at": 1711517599333,
"assistant_id": "asst_46e95014-ccac-435d-b884-c97ac44a94d9",
"thread_id": "thread_cc2a3e9d-436b-482b-91c5-377e0f376506",
"run_id": "run_3de634fa-75d4-4370-adcf-92ba2a60c396",
"type": "message_creation",
"status": "completed",
"step_details": {
"type": "message_creation",
"message_creation": {
"message_id": "message_75c67937-a829-4741-ae83-6839cae81269"
}
},
"last_error": {
"code": "",
"message": ""
},
"expires_at": 0,
"started_at": 0,
"cancelled_at": -1,
"failed_at": -1,
"completed_at": -1,
"metadata": {},
"usage": {},
"request_id": "938643a3-d56d-966f-8c17-2cb09772475e"
}
レスポンスパラメータ
実行ステップクラスを返します。次のパラメータを含みます。
id: ステップ ID。
request_id: リクエスト ID。
SDK
サンプルコード
from dashscope import Steps
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
step = Steps.retrieve('step_id',
thread_id='thread_id',
run_id='run_id')
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.InvalidateParameter;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.threads.runs.RunStep;
import com.alibaba.dashscope.threads.runs.Runs;
import com.alibaba.dashscope.utils.Constants;
public class Main {
static {
Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
}
public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
Runs runs = new Runs();
RunStep step = runs.retrieveStep("threadId", "runId", "stepId");
}
}
リクエストパラメータ
パラメータ | タイプ | 説明 |
step_id | str | 取得する実行ステップの ID。 |
run_id | str | 実行ステップが属する実行の ID。 |
thread_id | str | 実行が属するスレッドの ID。 |
workspace | str | ワークスペース ID。 |
api_key | str | API キー。環境変数として設定できます。 |
サンプルレスポンス
{
"assistant_id": "asst_1",
"cancelled_at": null,
"completed_at": 1709866473,
"created_at": 1709866472,
"expires_at": null,
"failed_at": null,
"id": "step_1",
"last_error": null,
"object": "thread.run.step",
"run_id": "run_1",
"status": "completed",
"step_details": {
"message_creation": {
"message_id": "msg_1"
},
"type": "message_creation"
},
"thread_id": "thread_1",
"type": "message_creation",
"usage": {
"completion_tokens": 25,
"prompt_tokens": 809,
"total_tokens": 834
}
}
レスポンスパラメータ
パラメータ | タイプ | 説明 |
id | str | ステップ ID。 |
assistant_id | str | 実行ステップに関連付けられたアシスタントの ID。 |
run_id | str | 実行ステップが属する実行の ID。 |
thread_id | str | 実行が属するスレッドの ID。 |
step_details | Dict | 実行ステップの詳細。 |
created_at | timestamp | アシスタントが作成されたタイムスタンプ。 |
gmt_created | datetime | 実行ステップが作成された日時。 |
gmt_modified | datetime | 実行ステップが変更された日時。 |
code | str | リクエストの失敗時に返されるエラーコード。 このパラメータは、Python を使用した場合にのみ返されます。 |
message | str | リクエストの失敗時に返されるエラーメッセージ。 このパラメータは、Python を使用した場合にのみ返されます。 |
実行タスクの変更
HTTP
サンプルコード
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/threads/thread_cc2a3e9d-436b-482b-91c5-377e0f376506/runs/run_3de634fa-75d4-4370-adcf-92ba2a60c396' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--data '{
"metadata": {
"user": "abc123"
}
}'
リクエストパラメータ
パラメータ | 説明 | タイプ | 必須 |
thread_id | 実行が属するスレッドの ID。 | str | はい |
run_id | 変更する実行の ID。 | str | はい |
* | その他のオプションのリクエストパラメータ。 | str | いいえ |
サンプルレスポンス
{
"id": "run_3de634fa-75d4-4370-adcf-92ba2a60c396",
"object": "thread.run",
"created_at": 1711517598169,
"thread_id": "thread_cc2a3e9d-436b-482b-91c5-377e0f376506",
"assistant_id": "asst_46e95014-ccac-435d-b884-c97ac44a94d9",
"status": "completed",
"required_action": {},
"last_error": {},
"expires_at": 0,
"started_at": 0,
"cancelled_at": 0,
"failed_at": 0,
"completed_at": 0,
"model": "qwen-max",
"instructions": "You are an intelligent assistant. You can use different tools based on user needs and then provide answers. Use tools when needed.", // あなたはインテリジェントなアシスタントです。ユーザーのニーズに基づいてさまざまなツールを使用し、回答を提供できます。必要に応じてツールを使用してください。
"tools": [
{
"type": "search"
},
{
"type": "text_to_image"
}
],
"metadata": {
"user": "abc123"
},
"usage": {
"prompt_tokens": 66,
"completion_tokens": 9,
"total_tokens": 75
},
"request_id": "bb6a5c5c-146f-95b8-bafa-e6c467e4f2d3"
}
レスポンスパラメータ
変更された実行クラスを返します。次のパラメータを含みます。
id: 実行 ID。
request_id: リクエスト ID。
SDK
サンプルコード
from dashscope import Runs
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
run = Runs.update('run_id',
thread_id='thread_id',
metadata={'newkey': 'newvalue'}) // 新しいキー:新しい値
import java.util.HashMap;
import java.util.Map;
import com.alibaba.dashscope.common.UpdateMetadataParam;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.InvalidateParameter;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.threads.runs.Run;
import com.alibaba.dashscope.threads.runs.Runs;
import com.alibaba.dashscope.utils.Constants;
public class Main {
static {
Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
}
public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
Runs runs = new Runs();
Map<String, String> metadata = new HashMap<>();
metadata.put("key", "value");
UpdateMetadataParam updateParam = UpdateMetadataParam.builder().metadata(metadata).build();
Run run = runs.update("threadId", "runId", updateParam);
}
}
リクエストパラメータ
パラメータ | タイプ | 説明 |
run_id | str | 変更する実行の ID。 |
thread_id | str | 実行が属するスレッドの ID。 |
metadata | Dict | アシスタントのメタデータ。 |
workspace | str | ワークスペース ID。 |
api_key | str | API キー。環境変数として設定できます。 |
レスポンスパラメータ
実行タスクの作成 セクションのレスポンスパラメータを参照してください。
ツール出力を送信して実行を続行する
HTTP
サンプルコード
関数呼び出しを使用する場合、中間関数結果を実行に送信し、実行ステータスを require_action
から normal
に変更する必要があります。required_action.type
の値は submit_tool_outputs
である必要があります。
非ストリーミング
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/threads/thread_9157dfad-71fd-4add-b7a9-1afa452d2d67/runs/run_99935dbd-e731-4bad-b450-60d9d6bf9a44/submit_tool_outputs' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--data '{
"tool_outputs": [
{
"tool_call_id": "",
"output": "rain" // 雨
}
]
}'
ストリーミング
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/threads/thread_9157dfad-71fd-4add-b7a9-1afa452d2d67/runs/run_99935dbd-e731-4bad-b450-60d9d6bf9a44/submit_tool_outputs' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--data '{
"tool_outputs": [
{
"tool_call_id": "",
"output": "rain" // 雨
}
],
"stream": true
}'
リクエストパラメータ
パラメータ | 説明 | タイプ | 必須 |
thread_id | 実行が属するスレッドの ID。 | str | はい |
run_id | 実行の ID。 | str | はい |
tool_outputs | 送信するツールの結果。 | array | はい |
stream | ストリーミング出力モードを有効にするかどうかを指定します。 | boolean | いいえ |
サンプルレスポンス
非ストリーミング
{
"id": "run_99935dbd-e731-4bad-b450-60d9d6bf9a44",
"object": "thread.run",
"created_at": 1711519089921,
"thread_id": "thread_9157dfad-71fd-4add-b7a9-1afa452d2d67",
"assistant_id": "asst_ce66cfa8-b2f8-4e4c-8e17-2c335145be42",
"status": "in_progress",
"required_action": {
"type": "submit_tool_outputs",
"submit_tool_outputs": {
"tool_calls": [
{
"function": {
"name": "get_current_weather",
"arguments": "{\"location\": \"Singapore\"}" // {"location": "シンガポール"}
},
"id": "",
"type": "function"
}
]
}
},
"last_error": {},
"expires_at": 0,
"started_at": 0,
"cancelled_at": 0,
"failed_at": 0,
"completed_at": 0,
"model": "qwen-max",
"instructions": "You are an intelligent assistant. You can use different tools based on user needs and then provide answers. Use tools when needed.", // あなたはインテリジェントなアシスタントです。ユーザーのニーズに基づいてさまざまなツールを使用し、回答を提供できます。必要に応じてツールを使用してください。```
"tools": [
{
"type": "text_to_image"
},
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location", // 指定された場所の現在の天気を取得します
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA" // 都市と州、例:サンフランシスコ、カリフォルニア
},
"unit": {
"type": "string",
"enum": [
"celsius",
"fahrenheit"
]
}
},
"required": [
"location"
]
}
}
}
],
"metadata": {},
"usage": null,
"request_id": "792c29a2-e771-9840-9d99-df4f892badd0"
}
ストリーミング
event:thread.run.step.completed
data:{"id":"step_74a8b5e2-3d23-41d9-b5c8-9bf3ba3839d0","object":"thread.run.step","created_at":1714448906129,"assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","thread_id":"thread_c6ac2eb0-ed95-4834-befb-ea66eba85b9f","run_id":"run_36d6fba9-0bf1-47f5-8e7b-f8c8140beda9","type":"tool_calls","status":"completed","step_details":{"type":"tool_calls","tool_calls":[{"id":"","type":"function","function":{"name":"get_current_weather","arguments":"{\"location\": \"Xi'an\"}","output":"rain"}}]},"last_error":null,"expired_at":null,"cancelled_at":null,"failed_at":null,"completed_at":null,"metadata":{},"usage":null,"request_id":"6d1573a4-3cbb-9066-99cc-7247ec17cb30"}
// {"type":"tool_calls","tool_calls":[{"id":"","type":"function","function":{"name":"get_current_weather","arguments":"{\"location\": \"西安\"}","output":"雨"}}]},"last_error":null,"expired_at":null,"cancelled_at":null,"failed_at":null,"completed_at":null,"metadata":{},"usage":null,"request_id":"6d1573a4-3cbb-9066-99cc-7247ec17cb30"}
event:thread.run.queued
...
レスポンスパラメータ
実行クラスを返します。次のパラメータを含みます。
id: 実行 ID。
request_id: リクエスト ID。
SDK
import json
import sys
from http import HTTPStatus
from dashscope import Assistants, Messages, Runs, Steps, Threads
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
def create_assistant():
# create assistant with information
# 情報を使用してアシスタントを作成する
assistant = Assistants.create(
model='qwen-max',
name='smart helper', // スマートヘルパー
description='A tool helper.', // ツールヘルパー
instructions='You are a helpful assistant. When asked a question, use tools wherever possible.', # noqa E501
# あなたは親切なアシスタントです。質問されたら、可能な限りツールを使用してください。
tools=[{
'type': 'function',
'function': {
'name': 'big_add',
'description': 'Add to number', // 数値を加算する
'parameters': {
'type': 'object',
'properties': {
'left': {
'type': 'integer',
'description': 'The left operator' // 左側の演算子
},
'right': {
'type': 'integer',
'description': 'The right operator.' // 右側の演算子
}
},
'required': ['left', 'right']
}
}
}],
)
return assistant
def verify_status_code(res):
if res.status_code != HTTPStatus.OK:
sys.exit(res.status_code)
if __name__ == '__main__':
# create assistant
# アシスタントを作成する
assistant = create_assistant()
...
# get the thread messages.
# スレッドメッセージを取得する
msgs = Messages.list(thread.id)
import com.alibaba.dashscope.assistants.Assistant;
import com.alibaba.dashscope.assistants.AssistantParam;
import com.alibaba.dashscope.tools.ToolFunction;
import com.alibaba.dashscope.assistants.Assistants;
...
import com.alibaba.dashscope.utils.Constants;
public class AssistantFunctionCall {
static {
Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
}
public class AddFunctionTool {
private int left;
private int right;
public AddFunctionTool(int left, int right) {
this.left = left;
this.right = right;
}
public int call() {
return left + right;
}
}
static ToolFunction buildFunction() {
// generate jsonSchema of function.
// 関数の jsonSchema を生成する
ObjectNode jsonSchema = generator.generateSchema(AddFunctionTool.class);
// call with tools of function call, jsonSchema.toString() is jsonschema String.
// 関数呼び出しのツールを使用して呼び出します。jsonSchema.toString() は jsonschema 文字列です。
FunctionDefinition fd = FunctionDefinition.builder().name("add").description("add two number") // 2つの数値を加算する
.parameters(JsonUtils.parseString(jsonSchema.toString()).getAsJsonObject()).build();
return ToolFunction.builder().function(fd).build();
}
static public Assistant createAssistant() throws ApiException, NoApiKeyException{
AssistantParam assistantParam = AssistantParam.builder()
.model("qwen-max") // model must be set.
.description("a helper assistant") // ヘルパーアシスタント
.name("system") // name must be set.
.instructions("You are a helpful assistant. When asked a question, use tools wherever possible.") // あなたは親切なアシスタントです。質問されたら、可能な限りツールを使用してください。
.tool(buildFunction())
.build();
Assistants assistants = new Assistants();
return assistants.create(assistantParam);
}
static public void run(String assistantId) throws ApiException, NoApiKeyException, InvalidateParameter, InputRequiredException, InterruptedException{
// create a thread
// スレッドを作成する
Threads threads = new Threads();
...
// submit action output.
// アクション出力を送信する
}
}
...
}
public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
Assistant assistant = createAssistant();
run(assistant.getId());
}
}
実行の完了を待つ
実行が次のいずれかのステータスになるまで監視します。
cancelled
failed
completed
expired
requires_action
SDK
サンプルコード
from dashscope import Runs
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
run = Runs.wait('run_id',
thread_id='thread_id')
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.InvalidateParameter;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.threads.runs.Run;
import com.alibaba.dashscope.threads.runs.Runs;
import com.alibaba.dashscope.utils.Constants;
public class AssistantFunctionCall {
static {
Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
}
public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
Runs runs = new Runs();
Run run = runs.retrieve("thread_id", "run_id");
while(true){
if(run.getStatus().equals(Run.Status.CANCELLED) ||
run.getStatus().equals(Run.Status.COMPLETED) ||
run.getStatus().equals(Run.Status.FAILED) ||
run.getStatus().equals(Run.Status.REQUIRES_ACTION)||
run.getStatus().equals(Run.Status.EXPIRED)){
break;
}else{
Thread.sleep(1000);
}
run = runs.retrieve("thread_id", run.getId());
}
}
}
リクエストパラメータ
パラメータ | タイプ | 説明 |
run_id | str | 実行の ID。 |
thread_id | str | 実行が属するスレッドの ID。 |
workspace | str | ワークスペース ID。 |
api_key | str | API キー。環境変数として設定できます。 |
レスポンスパラメータ
実行タスクの作成 セクションのレスポンスパラメータを参照してください。
FAQ
Q: アクティブな実行タスクをキャンセルするにはどうすればよいですか?
A: アシスタント API は、アクティブな実行タスクをキャンセルする操作を提供していません。代わりに他のコードロジックを使用することをお勧めします。