文档

Run

更新时间:

本文详细阐述了Assistant API中Run类的各项功能,涵盖了运行任务的创建、列举、检索、修改、提交工具输出以及等待运行任务结束等操作。

本文档主要基于当前Dashscope的run 类所具备的各类可被开发者使用的API 文档。

函数名

类型

Create run

创建一个run 类,用于运行thread

Create thread and run

创建一个thread并直接运行

list run

列出某个thread的多个run

list run step

列出某个run的多个run step

retrieve run

检索run 实例

retrieve run step

检索run step

modify run

修改某个run 实例

Submit tool outputs to run

将tools运行的中间结果传递给run,并让run从暂停状态转换为继续运行状态

创建运行任务

HTTP

代码示例

非流式

curl --location 'https://dashscope.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.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

所需要运行的thread id

str

assistant id

所需要运行的assistant id

str

model

所需要调用的model名字。如果在此传入,会替换掉assistant中定义的model

str

instructions

替换assistant中定义的instruction

str

additional instruction

额外增加一部分instruction至原有instruction 末尾。

str

tools

替换assistant中定义的tool 列表,

说明

自定义tool鉴权信息传递

{

"type": "plugin_type", "auth": {"type": "user_http","user_token": "bearer-token", }

}

assistant在请求插件时会将bearer放在{"plugin_type":{"user_token": "bearer-token"}}'添加到调用plugin的header里.

str

metadata

其他相关参数说明

str

stream

是否流式返回

boolean

temperature

用于控制随机性和多样性的程度。

float

top_p

生成时,核采样方法的概率阈值。

float

top_k

生成时,采样候选集的大小。

integer

返回结果

非流式

{
    "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": "你是一个智能小助手,你可以根据用户的需求去调用不同的工具,进而给出回答。请酌情使用工具。",
    "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":"你是一个智能小助手,你可以根据用户的需求去调用不同的工具,进而给出回答。请酌情使用工具。","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.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":"你是一个智能小助手,你可以根据用户的需求去调用不同的工具,进而给出回答。请酌情使用工具。","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":"你是一个智能小助手,你可以根据用户的需求去调用不同的工具,进而给出回答。请酌情使用工具。","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":"乘以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":"等于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乘以998等于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":"你是一个智能小助手,你可以根据用户的需求去调用不同的工具,进而给出回答。请酌情使用工具。","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]

输出参数

输出run类,并包含除了用户输入以外参数的其他额外字段:

  • id :run id

  • thread id: 运行的run内包含的thread id

  • assistant id:所运行的assistant 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;

public class Main {
    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

-

期望在那个Thread run

assistant_id

str

-

assistant id

model

str

-

指定使用的模型

instructions

str

None

指定本次run所用的指令

additional_instructions

str

None

指定附加的指令

tools

List[Dict]

[]

assistant使用的tools

说明

自定义tool鉴权信息传递

{

"type": "plugin_type", "auth": {"type": "user_http","user_token": "bearer-token", }

}

assistant在请求插件时会将bearer放在{"plugin_type":{"user_token": "bearer-token"}}'添加到调用plugin的header里.

stream

boolean

False

用于控制是否是流式输出。

metadata

Dict

None

assistant关联信息

temperature

通过API使用通义千问

top_p

top_k

max_tokens

truncation_strategy

Dict

None

设置thread中消息截断策略, 目前只支持last_messages,默认为:

"truncation_strategy": {

"type": "last_messages",

"last_messages": 10

}

workspace

str

None

DashScope workspace id

api_key

str

None

DashScope api key,可以通过环境变量等方法设置。

返回结果

流式输出将输出event和event 数据,如下

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}
thread.run.queued
{'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}
thread.run.in_progress
{'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': 'in_progress', '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}
thread.run.step.created
{'step_details': {'tool_calls': [], 'type': 'tool_calls'}, 'usage': None, 'id': 'step_f923bd08-1e22-40d9-bf54-373b21fde378', 'object': 'thread.run.step', 'created_at': 1714460228960, 'assistant_id': 'asst_9453fe59-d6eb-498e-aeca-9abecd492e51', 'thread_id': 'thread_28f0b00b-d825-48f5-bb27-f3b171a64d33', 'run_id': 'run_0f314675-06a4-4379-929c-d29cd351a446', 'type': 'tool_calls', 'status': 'in_progress', 'expired_at': None, 'cancelled_at': None, 'failed_at': None, 'completed_at': None, 'metadata': {}, 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200, 'last_error': None}
thread.run.step.in_progress
{'step_details': {'tool_calls': [], 'type': 'tool_calls'}, 'usage': None, 'id': 'step_f923bd08-1e22-40d9-bf54-373b21fde378', 'object': 'thread.run.step', 'created_at': 1714460228960, 'assistant_id': 'asst_9453fe59-d6eb-498e-aeca-9abecd492e51', 'thread_id': 'thread_28f0b00b-d825-48f5-bb27-f3b171a64d33', 'run_id': 'run_0f314675-06a4-4379-929c-d29cd351a446', 'type': 'tool_calls', 'status': 'in_progress', 'expired_at': None, 'cancelled_at': None, 'failed_at': None, 'completed_at': None, 'metadata': {}, 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200, 'last_error': None}
thread.run.step.delta
{'delta': {'step_details': {'tool_calls': [{'type': 'quark_search', 'quark_search': {'arguments': ''}}], 'type': 'tool_calls'}}, 'id': 'step_f923bd08-1e22-40d9-bf54-373b21fde378', 'object': 'thread.run.step.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.run.step.delta
{'delta': {'step_details': {'tool_calls': [{'type': 'quark_search', 'quark_search': {'arguments': '{"query": "'}}], 'type': 'tool_calls'}}, 'id': 'step_f923bd08-1e22-40d9-bf54-373b21fde378', 'object': 'thread.run.step.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.run.step.delta
{'delta': {'step_details': {'tool_calls': [{'type': 'quark_search', 'quark_search': {'arguments': '今天 北京 天气"}'}}], 'type': 'tool_calls'}}, 'id': 'step_f923bd08-1e22-40d9-bf54-373b21fde378', 'object': 'thread.run.step.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.run.step.delta
{'delta': {'step_details': {'tool_calls': [{'type': 'quark_search', 'quark_search': {'arguments': ''}}], 'type': 'tool_calls'}}, 'id': 'step_f923bd08-1e22-40d9-bf54-373b21fde378', 'object': 'thread.run.step.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.run.step.delta
{'delta': {'step_details': {'tool_calls': [{'type': 'quark_search', 'quark_search': {'arguments': ''}}], 'type': 'tool_calls'}}, 'id': 'step_f923bd08-1e22-40d9-bf54-373b21fde378', 'object': 'thread.run.step.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.run.step.delta
{'delta': {'step_details': {'tool_calls': [{'type': 'quark_search', 'plugin_name': {'output': '{"success": true, "errorCode": null, "errorMsg": null, "data": [{"title": "中国北京市天气情况", "url": null, "desc": "实时天气情况,晴,气温24度,体感温度:23度,西风1级,冷热适宜,感觉很舒适。查看未来15天气预报,请点击这里", "type": "weather_moji", "original": null, "query": null, "source": null}, {"title": "北京市气象局", "url": "http://bj.cma.gov.cn/fzlm/index.html", "desc": "36小时天气预报(2024-01-25 05:24:58北京市气象台发布) 今天白天 最高气温5℃ 晴间多云 3级,阵风5级转2级 偏北风 今天夜间 最低气温-5℃ 晴 2级转3级 偏北风 明天白天 最高气温5℃ 晴 3级转1...", "type": "structure_web_info", "original": null, "query": null, "source": "北京市气象局"}, {"title": "北京", "url": "https://m.weather.com.cn/mweather/101010100.shtml?_t=1480291523052", "desc": "北京天气预报,及时准确发布中央气象台天气信息,便捷查询北京今日天气,北京周末天气,北京一周天气预报,北京蓝天预报,北京天气预报,北京40日天气预报,还提供北京的生活指数、健康指数、...", "type": "normal", "original": null, "query": null, "source": "中国天气"}, {"title": "墨迹天气中文官方网站", "url": "https://m.moji.com/", "desc": "墨迹天气预报app,全球约6.5亿人在使用的天气app,支持199个国家20多万个城市及地区的天气预报查询,下载墨迹天气预报app,实时监测阴晴雨雪,实时空气质量及空气质量等级预报。", "type": "structure_web_info", "original": null, "query": null, "source": "墨迹天气手机版"}, {"title": "北京", "url": "http://sq.weather.com.cn/mweather15d/101010100.shtml?from=singlemessage", "desc": "北京天气预报,及时准确发布中央气象台天气信息,便捷查询北京今日天气,北京周末天气,北京一周天气预报,北京蓝天预报,北京天气预报,北京40日天气预报,还提供北京的生活指数、健康指数、...", "type": "structure_web_info", "original": null, "query": null, "source": "中国天气网"}, {"title": "头条新闻", "url": null, "desc": "中国天气网于1天前报道:预报 预警 雷达 云图 天气地图 专业产品 资讯 视频 节气 我的天空 ...恭王府 北京奥林匹克公园 奥林匹克森林公园 北京石景山游乐园 金海湖 京东大溶洞旅游区 明十三陵 银山塔林风景区 中国航空博物馆 北京欢乐谷 北京中华...", "type": "news_uchq", "original": null, "query": null, "source": null}, {"title": "北京", "url": "http://www.weather.com.cn/weather1d/101010100.shtml", "desc": "北京天气预报,及时准确发布中央气象台天气信息,便捷查询北京今日天气,北京周末天气,北京一周天气预报,北京15日...", "type": "structure_web_info", "original": null, "query": null, "source": "中国天气网"}, {"title": "北京24小时天气查询_北京今日天气预报查询_2345天气王", "url": "http://waptianqi.2345.com/24hour-54511.htm", "desc": "2345天气预报为你及时提供最新的北京天气,今天24小时天气情况等信息,同时还提供当日北京气象指数等信息", "type": "structure_web_info", "original": null, "query": null, "source": "2345天气王"}, {"title": "北京15日天气详情", "url": "http://waptianqi.2345.com/beijing-54511.htm", "desc": "2345天气预报提供北京天气预报,未来北京15日天气,通过2345天气预报详细了解北京天气预报以及北京周边各地区未来15天、30日天气情况,温度,空气质量,降水,风力,气压,紫外线强度等!", "type": "structure_web_info", "original": null, "query": null, "source": "2345天气王"}, {"title": "北京天气预报15天", "url": "https://m.tianqi.com/beijing/15/", "desc": "北京天气预报15天详情 12/11 今天 小雪-2~2 ℃ 12/12 明天 多云转小雪-2~2 ℃ 12/13 后天 小雪-5~0 ℃ 12/14 周四 小雪-5~-1 ℃ 12/15 周五 阴转多云-10~-2 ℃ 12/16 周六 晴-10~-3 ℃ 12/17 ...", "type": "structure_web_info", "original": null, "query": null, "source": "天气网"}, {"title": "北京天气", "url": "https://t.8684.com/beijing_beijing", "desc": "北京天气预报一周15天,北京天气预报,北京天气预报一周,北京天气预报15天,北京本周天气预报,北京下周天气预报,北京天气预报15天查询,北京天气预报10天", "type": "structure_web_info", "original": null, "query": null, "source": "8684天气"}], "requestId": null, "failed": false}'}}], 'type': 'tool_calls'}}, 'id': 'step_f923bd08-1e22-40d9-bf54-373b21fde378', 'object': 'thread.run.step.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.run.step.delta
{'delta': {'step_details': {'tool_calls': [{'quark_search': {'output': ''}}], 'type': 'tool_calls'}}, 'id': 'step_f923bd08-1e22-40d9-bf54-373b21fde378', 'object': 'thread.run.step.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.run.step.delta
{'delta': {'step_details': {'tool_calls': [{'quark_search': {'output': ''}}], 'type': 'tool_calls'}}, 'id': 'step_f923bd08-1e22-40d9-bf54-373b21fde378', 'object': 'thread.run.step.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.run.step.completed
{'step_details': {'tool_calls': [{'type': 'quark_search', 'plugin_name': {'arguments': '{"query": "今天 北京 天气"}', 'output': '{"success": true, "errorCode": null, "errorMsg": null, "data": [{"title": "中国北京市天气情况", "url": null, "desc": "实时天气情况,晴,气温24度,体感温度:23度,西风1级,冷热适宜,感觉很舒适。查看未来15日天气预报,请点击这里", "type": "weather_moji", "original": null, "query": null, "source": null}, {"title": "北京市气象局", "url": "http://bj.cma.gov.cn/fzlm/index.html", "desc": "36小时天气预报(2024-01-25 05:24:58北京市气象台发布) 今天白天 最高气温5℃ 晴间多云 3级,阵风5级转2级 偏北风 今天夜间 最低气温-5℃ 晴 2级转3级 偏北风 明天白天 最高气温5℃ 晴 3级转1...", "type": "structure_web_info", "original": null, "query": null, "source": "北京市气象局"}, {"title": "北京", "url": "https://m.weather.com.cn/mweather/101010100.shtml?_t=1480291523052", "desc": "北京天气预报,及时准确发布中央气象台天气信息,便捷查询北京今日天气,北京周末天气,北京一周天气预报,北京蓝天预报,北京天气预报,北京40日天气预报,还提供北京的生活指数、健康指数、...", "type": "normal", "original": null, "query": null, "source": "中国天气"}, {"title": "墨迹天气中文官方网站", "url": "https://m.moji.com/", "desc": "墨迹天气预报app,全球约6.5亿人在使用的天气app,支持199个国家20多万个城市及地区的天气预报查询,下载墨迹天气预报app,实时监测阴晴雨雪,实时空气质量及空气质量等级预报。", "type": "structure_web_info", "original": null, "query": null, "source": "墨迹天气手机版"}, {"title": "北京", "url": "http://sq.weather.com.cn/mweather15d/101010100.shtml?from=singlemessage", "desc": "北京天气预报,及时准确发布中央气象台天气信息,便捷查询北京今日天气,北京周末天气,北京一周天气预报,北京蓝天预报,北京天气预报,北京40日天气预报,还提供北京的生活指数、健康指数、...", "type": "structure_web_info", "original": null, "query": null, "source": "中国天气网"}, {"title": "头条新闻", "url": null, "desc": "中国天气网于1天前报道:预报 预警 雷达 云图 天气地图 专业产品 资讯 视频 节气 我的天空 ...恭王府 北京奥林匹克公园 奥林匹克森林公园 北京石景山游乐园 金海湖 京东大溶洞旅游区 明十三陵 银山塔林风景区 中国航空博物馆 北京欢乐谷 北京中华...", "type": "news_uchq", "original": null, "query": null, "source": null}, {"title": "北京", "url": "http://www.weather.com.cn/weather1d/101010100.shtml", "desc": "北京天气预报,及时准确发布中央气象台天气信息,便捷查询北京今日天气,北京周末天气,北京一周天气预报,北京15日...", "type": "structure_web_info", "original": null, "query": null, "source": "中国天气网"}, {"title": "北京24小时天气查询_北京今日天气预报查询_2345天气王", "url": "http://waptianqi.2345.com/24hour-54511.htm", "desc": "2345天气预报为你及时提供最新的北京今天天气,今天24小时天气情况等信息,同时还提供当日北京气象指数等信息", "type": "structure_web_info", "original": null, "query": null, "source": "2345天气王"}, {"title": "北京15日天气详情", "url": "http://waptianqi.2345.com/beijing-54511.htm", "desc": "2345天气预报提供北京天气预报,未来北京15日天气,通过2345天气预报详细了解北京天气预报以及北京周边各地区未来15天、30日天气情况,温度,空气质量,降水,风力,气压,紫外线强度等!", "type": "structure_web_info", "original": null, "query": null, "source": "2345天气王"}, {"title": "北京天气预报15天", "url": "https://m.tianqi.com/beijing/15/", "desc": "北京天气预报15天详情 12/11 今天 小雪-2~2 ℃ 12/12 明天 多云转小雪-2~2 ℃ 12/13 后天 小雪-5~0 ℃ 12/14 周四 小雪-5~-1 ℃ 12/15 周五 阴转多云-10~-2 ℃ 12/16 周六 晴-10~-3 ℃ 12/17 ...", "type": "structure_web_info", "original": null, "query": null, "source": "天气网"}, {"title": "北京天气", "url": "https://t.8684.com/beijing_beijing", "desc": "北京天气预报一周15天,北京天气预报,北京天气预报一周,北京天气预报15天,北京本周天气预报,北京下周天气预报,北京天气预报15天查询,北京天气预报10天", "type": "structure_web_info", "original": null, "query": null, "source": "8684天气"}], "requestId": null, "failed": false}'}}], 'type': 'tool_calls'}, 'usage': None, 'id': 'step_f923bd08-1e22-40d9-bf54-373b21fde378', 'object': 'thread.run.step', 'created_at': 1714460228960, 'assistant_id': 'asst_9453fe59-d6eb-498e-aeca-9abecd492e51', 'thread_id': 'thread_28f0b00b-d825-48f5-bb27-f3b171a64d33', 'run_id': 'run_0f314675-06a4-4379-929c-d29cd351a446', 'type': 'tool_calls', 'status': 'in_progress', 'expired_at': None, 'cancelled_at': None, 'failed_at': None, 'completed_at': None, 'metadata': {}, 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200, 'last_error': None}
thread.run.step.created
{'step_details': {'message_creation': {'message_id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d'}, 'type': 'message_creation'}, 'usage': None, 'id': 'step_89e631c7-491a-4bff-b1f5-1298f88db531', 'object': 'thread.run.step', 'created_at': 1714460234236, 'assistant_id': 'asst_9453fe59-d6eb-498e-aeca-9abecd492e51', 'thread_id': 'thread_28f0b00b-d825-48f5-bb27-f3b171a64d33', 'run_id': 'run_0f314675-06a4-4379-929c-d29cd351a446', 'type': 'message_creation', 'status': 'in_progress', 'expired_at': None, 'cancelled_at': None, 'failed_at': None, 'completed_at': None, 'metadata': {}, 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200, 'last_error': None}
thread.run.step.in_progress
{'step_details': {'message_creation': {'message_id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d'}, 'type': 'message_creation'}, 'usage': None, 'id': 'step_89e631c7-491a-4bff-b1f5-1298f88db531', 'object': 'thread.run.step', 'created_at': 1714460234236, 'assistant_id': 'asst_9453fe59-d6eb-498e-aeca-9abecd492e51', 'thread_id': 'thread_28f0b00b-d825-48f5-bb27-f3b171a64d33', 'run_id': 'run_0f314675-06a4-4379-929c-d29cd351a446', 'type': 'message_creation', 'status': 'in_progress', 'expired_at': None, 'cancelled_at': None, 'failed_at': None, 'completed_at': None, 'metadata': {}, 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200, 'last_error': None}
thread.message.created
{'content': [], 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message', 'created_at': 1714460234235, 'thread_id': 'thread_28f0b00b-d825-48f5-bb27-f3b171a64d33', 'incomplete_details': None, 'completed_at': None, 'incomplete_at': None, 'role': 'assistant', 'assistant_id': 'asst_9453fe59-d6eb-498e-aeca-9abecd492e51', 'run_id': 'run_0f314675-06a4-4379-929c-d29cd351a446', 'file_ids': [], 'metadata': {}, 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.message.in_progress
{'content': [], 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message', 'created_at': 1714460234235, 'thread_id': 'thread_28f0b00b-d825-48f5-bb27-f3b171a64d33', 'incomplete_details': None, 'completed_at': None, 'incomplete_at': None, 'role': 'assistant', 'assistant_id': 'asst_9453fe59-d6eb-498e-aeca-9abecd492e51', 'run_id': 'run_0f314675-06a4-4379-929c-d29cd351a446', 'file_ids': [], 'metadata': {}, 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.message.delta
{'delta': {'content': {'text': {'annotations': [], 'value': '今天'}, 'type': 'text'}, 'role': 'assistant'}, 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.message.delta
{'delta': {'content': {'text': {'annotations': [], 'value': '北京的天气情况是'}, 'type': 'text'}, 'role': 'assistant'}, 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.message.delta
{'delta': {'content': {'text': {'annotations': [], 'value': '晴朗的,气温约为24'}, 'type': 'text'}, 'role': 'assistant'}, 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.message.delta
{'delta': {'content': {'text': {'annotations': [], 'value': '度,体感温度为23'}, 'type': 'text'}, 'role': 'assistant'}, 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.message.delta
{'delta': {'content': {'text': {'annotations': [], 'value': '度,西风1级,天气'}, 'type': 'text'}, 'role': 'assistant'}, 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.message.delta
{'delta': {'content': {'text': {'annotations': [], 'value': '较为舒适。不过,我也找到了北京市'}, 'type': 'text'}, 'role': 'assistant'}, 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.message.delta
{'delta': {'content': {'text': {'annotations': [], 'value': '气象局发布的更详细信息,提到'}, 'type': 'text'}, 'role': 'assistant'}, 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.message.delta
{'delta': {'content': {'text': {'annotations': [], 'value': '今天白天最高气温为5℃,'}, 'type': 'text'}, 'role': 'assistant'}, 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.message.delta
{'delta': {'content': {'text': {'annotations': [], 'value': '天气晴间多云,有3'}, 'type': 'text'}, 'role': 'assistant'}, 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.message.delta
{'delta': {'content': {'text': {'annotations': [], 'value': '级偏北风,阵风可达'}, 'type': 'text'}, 'role': 'assistant'}, 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.message.delta
{'delta': {'content': {'text': {'annotations': [], 'value': '5级,夜间最低气温降至-'}, 'type': 'text'}, 'role': 'assistant'}, 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.message.delta
{'delta': {'content': {'text': {'annotations': [], 'value': '5℃,依旧保持晴朗,'}, 'type': 'text'}, 'role': 'assistant'}, 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.message.delta
{'delta': {'content': {'text': {'annotations': [], 'value': '风向转为偏北风,'}, 'type': 'text'}, 'role': 'assistant'}, 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.message.delta
{'delta': {'content': {'text': {'annotations': [], 'value': '风力2级转3级。'}, 'type': 'text'}, 'role': 'assistant'}, 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.message.delta
{'delta': {'content': {'text': {'annotations': [], 'value': '请根据实时天气调整着装,'}, 'type': 'text'}, 'role': 'assistant'}, 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.message.delta
{'delta': {'content': {'text': {'annotations': [], 'value': '注意保暖。'}, 'type': 'text'}, 'role': 'assistant'}, 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.message.delta
{'delta': {'content': {'text': {'annotations': [], 'value': ''}, 'type': 'text'}, 'role': 'assistant'}, 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message.delta', 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.message.completed
{'content': [{'text': {'annotations': [], 'value': '今天北京的天气情况是晴朗的,气温约为24度,体感温度为23度,西风1级,天气较为舒适。不过,我也找到了北京市气象局发布的更详细信息,提到今天白天最高气温为5℃,天气晴间多云,有3级偏北风,阵风可达5级,夜间最低气温降至-5℃,依旧保持晴朗,风向转为偏北风,风力2级转3级。请根据实时天气调整着装,注意保暖。'}, 'type': 'text'}], 'id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d', 'object': 'thread.message', 'created_at': 1714460234235, 'thread_id': 'thread_28f0b00b-d825-48f5-bb27-f3b171a64d33', 'incomplete_details': None, 'completed_at': 1714460245637, 'incomplete_at': None, 'role': 'assistant', 'assistant_id': 'asst_9453fe59-d6eb-498e-aeca-9abecd492e51', 'run_id': 'run_0f314675-06a4-4379-929c-d29cd351a446', 'file_ids': [], 'metadata': {}, 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}
thread.run.step.completed
{'step_details': {'message_creation': {'message_id': 'message_93605cd7-fb74-468b-b6fb-4975cd253f8d'}, 'type': 'message_creation'}, 'usage': None, 'id': 'step_89e631c7-491a-4bff-b1f5-1298f88db531', 'object': 'thread.run.step', 'created_at': 1714460234236, 'assistant_id': 'asst_9453fe59-d6eb-498e-aeca-9abecd492e51', 'thread_id': 'thread_28f0b00b-d825-48f5-bb27-f3b171a64d33', 'run_id': 'run_0f314675-06a4-4379-929c-d29cd351a446', 'type': 'message_creation', 'status': 'completed', 'expired_at': None, 'cancelled_at': None, 'failed_at': None, 'completed_at': 1714460245637, 'metadata': {}, 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200, 'last_error': None}
thread.run.completed
{'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': 'completed', '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': {'input_tokens': 1865, 'output_tokens': 139, 'total_tokens': 2004}, 'temperature': None, 'top_p': None, 'top_k': None, 'max_tokens': None, 'truncation_strategy': None, 'request_id': '38fb88b8-196a-9919-8815-f7ed9aa0891f', 'status_code': 200}

输出参数

字段名

字段类型

字段描述

status_code

int

为调用http status code,200表示调用成功,其他表示调用出错

id

str

Run id,为uuid字符串

model

str

Run 使用的模型名称

instructions

str

assistant instruction信息,指定assisiant功能信息。

metadata

object

key,value对对象信息,描述assistant信息

status

str

Run的状态,包含'in_progress, 等

thread_id

str

Run所属Thread id

tools

List[Tool]

Run使用的tool列表

created_at

timestamp

assistant创建时间

gmt_created

datetime

2024-03-22 17:12:31

gmt_modified

datetime

2024-03-22 17:12:31

code

str

表示请求失败,表示错误码,成功忽略。

python only

message

str

失败,表示失败详细信息,成功忽略。

python only

创建线程和运行任务

HTTP

代码示例

非流式

curl --location 'https://dashscope.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": "西安天气是什么"
            }
        ]
    }
}'

流式

curl --location 'https://dashscope.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": "你是谁"
            }
        ]
    },
    "stream": true
}'

输入参数

输入参数名字

输入参数的描述

参数类型

是否必须

thread id

所需要运行的thread id

str

assistant id

所需要运行的assistant id

str

thread

所需要运行的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": "你是一个智能小助手,你可以根据用户的需求去调用不同的工具,进而给出回答。请酌情使用工具。",
    "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":"你是一个智能小助手,你可以根据用户的需求去调用不同的工具,进而给出回答。请酌情使用工具。","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"}

event:thread.run.queued
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":"你是一个智能小助手,你可以根据用户的需求去调用不同的工具,进而给出回答。请酌情使用工具。","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"}

event:thread.run.in_progress
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":"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":"你是一个智能小助手,你可以根据用户的需求去调用不同的工具,进而给出回答。请酌情使用工具。","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"}

event:thread.run.step.created
data:{"id":"step_15663219-fda6-4030-b35f-617712371f3b","object":"thread.run.step","created_at":1714448762933,"assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","thread_id":"thread_4a1e6bee-4e0a-4749-a0c8-f6db8bb7c4bb","run_id":"run_65adea5e-9086-4806-b733-bb0a7b53a459","type":"message_creation","status":"in_progress","step_details":{"type":"message_creation","message_creation":{"message_id":"message_ae7fce4c-a14d-4420-820e-191e1c4996a2"}},"last_error":null,"expired_at":null,"cancelled_at":null,"failed_at":null,"completed_at":null,"metadata":{},"usage":null,"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}

event:thread.run.step.in_progress
data:{"id":"step_15663219-fda6-4030-b35f-617712371f3b","object":"thread.run.step","created_at":1714448762933,"assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","thread_id":"thread_4a1e6bee-4e0a-4749-a0c8-f6db8bb7c4bb","run_id":"run_65adea5e-9086-4806-b733-bb0a7b53a459","type":"message_creation","status":"in_progress","step_details":{"type":"message_creation","message_creation":{"message_id":"message_ae7fce4c-a14d-4420-820e-191e1c4996a2"}},"last_error":null,"expired_at":null,"cancelled_at":null,"failed_at":null,"completed_at":null,"metadata":{},"usage":null,"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}

event:thread.message.created
data:{"id":"message_ae7fce4c-a14d-4420-820e-191e1c4996a2","object":"thread.message","created_at":1714448762933,"thread_id":"thread_4a1e6bee-4e0a-4749-a0c8-f6db8bb7c4bb","incomplete_details":null,"completed_at":null,"incomplete_at":null,"role":"assistant","content":[],"assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","run_id":"run_65adea5e-9086-4806-b733-bb0a7b53a459","file_ids":[],"metadata":{},"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}

event:thread.message.in_progress
data:{"id":"message_ae7fce4c-a14d-4420-820e-191e1c4996a2","object":"thread.message","created_at":1714448762933,"thread_id":"thread_4a1e6bee-4e0a-4749-a0c8-f6db8bb7c4bb","incomplete_details":null,"completed_at":null,"incomplete_at":null,"role":"assistant","content":[],"assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","run_id":"run_65adea5e-9086-4806-b733-bb0a7b53a459","file_ids":[],"metadata":{},"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}

event:thread.message.delta
data:{"id":"message_ae7fce4c-a14d-4420-820e-191e1c4996a2","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"我是","annotations":[]}}],"file_ids":[]},"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}

event:thread.message.delta
data:{"id":"message_ae7fce4c-a14d-4420-820e-191e1c4996a2","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"你的","annotations":[]}}],"file_ids":[]},"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}

event:thread.message.delta
data:{"id":"message_ae7fce4c-a14d-4420-820e-191e1c4996a2","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"智能","annotations":[]}}],"file_ids":[]},"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}

event:thread.message.delta
data:{"id":"message_ae7fce4c-a14d-4420-820e-191e1c4996a2","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"小助手,可以帮你","annotations":[]}}],"file_ids":[]},"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}

event:thread.message.delta
data:{"id":"message_ae7fce4c-a14d-4420-820e-191e1c4996a2","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"解答问题、提供信息、完成任务","annotations":[]}}],"file_ids":[]},"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}

event:thread.message.delta
data:{"id":"message_ae7fce4c-a14d-4420-820e-191e1c4996a2","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"等。只要告诉我你需要什么帮助,","annotations":[]}}],"file_ids":[]},"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}

event:thread.message.delta
data:{"id":"message_ae7fce4c-a14d-4420-820e-191e1c4996a2","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"我就会尽力为你服务。无论是查询","annotations":[]}}],"file_ids":[]},"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}

event:thread.message.delta
data:{"id":"message_ae7fce4c-a14d-4420-820e-191e1c4996a2","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"资料、制定计划、学习新知识","annotations":[]}}],"file_ids":[]},"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}

event:thread.message.delta
data:{"id":"message_ae7fce4c-a14d-4420-820e-191e1c4996a2","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":",还是娱乐放松,我都在这里","annotations":[]}}],"file_ids":[]},"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}

event:thread.message.delta
data:{"id":"message_ae7fce4c-a14d-4420-820e-191e1c4996a2","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"。有什么我可以帮到你的吗?","annotations":[]}}],"file_ids":[]},"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}

event:thread.message.delta
data:{"id":"message_ae7fce4c-a14d-4420-820e-191e1c4996a2","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"","annotations":[]}}],"file_ids":[]},"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}

event:thread.message.completed
data:{"id":"message_ae7fce4c-a14d-4420-820e-191e1c4996a2","object":"thread.message","created_at":1714448762933,"thread_id":"thread_4a1e6bee-4e0a-4749-a0c8-f6db8bb7c4bb","incomplete_details":null,"completed_at":1714448773999,"incomplete_at":null,"role":"assistant","content":[{"type":"text","text":{"value":"我是你的智能小助手,可以帮你解答问题、提供信息、完成任务等。只要告诉我你需要什么帮助,我就会尽力为你服务。无论是查询资料、制定计划、学习新知识,还是娱乐放松,我都在这里。有什么我可以帮到你的吗?","annotations":[]}}],"assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","run_id":"run_65adea5e-9086-4806-b733-bb0a7b53a459","file_ids":[],"metadata":{},"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}

event:thread.run.step.completed
data:{"id":"step_15663219-fda6-4030-b35f-617712371f3b","object":"thread.run.step","created_at":1714448762933,"assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","thread_id":"thread_4a1e6bee-4e0a-4749-a0c8-f6db8bb7c4bb","run_id":"run_65adea5e-9086-4806-b733-bb0a7b53a459","type":"message_creation","status":"completed","step_details":{"type":"message_creation","message_creation":{"message_id":"message_ae7fce4c-a14d-4420-820e-191e1c4996a2"}},"last_error":null,"expired_at":null,"cancelled_at":null,"failed_at":null,"completed_at":1714448773999,"metadata":{},"usage":null,"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}

event:thread.run.completed
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":"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":"你是一个智能小助手,你可以根据用户的需求去调用不同的工具,进而给出回答。请酌情使用工具。","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":410,"output_tokens":56,"total_tokens":466},"temperature":null,"top_p":null,"top_k":null,"max_tokens":null,"truncation_strategy":null,"request_id":"70673f3f-74b4-9e9a-90bf-de92c23c55b2"}

event:done
data:[DONE]

输出参数

输出run类,并包含除了用户输入以外参数的其他额外字段:

  • id :run id

  • thread id: 运行的run内包含的thread id

  • assistant id:所运行的assistant id

  • request_id :请求id

SDK

代码示例

from dashscope import Runs
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;

public class Main {
    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);
    }
}

返回结果

结果为Run对象,json化内容为

{
    "account_id": "YOUR_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.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

run list 所隶属的thread id

str

limit

创建message的数量

int

order

按照创建时间升序或降序

str

否(默认为“desc")

返回结果

{
    "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": "你是一个智能小助手,你可以根据用户的需求去调用不同的工具,进而给出回答。请酌情使用工具。",
            "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"
}

输出参数

输出List run类,并包含除了用户输入以外参数的其他额外字段:

  • 多个run 组成的列表

SDK

代码示例

from dashscope import Runs
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;

public class Main {
    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

-

指定要list的Thread id

limit

str

-

order

str

-

after

str

-

before

str

-

workspace

str

None

DashScope workspace id

api_key

str

None

DashScope API key,可以通过环境变量等方法设置。

输出参数

字段名

字段类型

字段描述

has_more

bool

last_id

str

first_id

str

data

list[Run]

Run对象列表

列出运行任务的关联步骤

HTTP

代码示例

用于创建某个run的多个run step

curl --location 'https://dashscope.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

run 所传入的thread id

str

run id

创建的run step所隶属的run id

str

limit

创建message file的数量

int

order

按照创建时间升序或降序

str

否(默认为“desc")

返回结果

{
    "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"
}

输出参数

输出list run step 类,并包含除了用户输入以外参数的其他额外字段:

  • list run step 类

  • request_id :请求id

SDK

代码示例

from dashscope import Steps
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;

public class Main {
    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

-

所属Run id

thread_id

str

-

所属Thread id.

limit

str

order

after

before

workspace

str

None

DashScope workspace id

api_key

str

None

DashScope api key,可以通过环境变量等方法设置。

输出参数

字段名

字段类型

字段描述

has_more

bool

last_id

str

first_id

str

data

list[Step]

Step对象列表

检索运行任务

HTTP

代码示例

curl --location 'https://dashscope.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

待检索的run id 所传入的thread id

str

run_id

待检索的run_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": "你是一个智能小助手,你可以根据用户的需求去调用不同的工具,进而给出回答。请酌情使用工具。",
    "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"
}

输出参数

输出检索到的message类,并包含除了用户输入以外参数的其他额外字段:

  • id :run id

  • thread id: 运行的run内包含的thread id

  • assistant id:所运行的assistant id

  • request_id :请求id

SDK

代码示例

from dashscope import Runs
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;

public class Main {
    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

-

指定要查询的Run id

thread_id

str

-

指定Run所属Thread id

workspace

str

None

DashScope workspace id

api_key

str

None

DashScope api key,可以通过环境变量等方法设置。

输出参数

参考create结果

检索运行任务的关联步骤

HTTP

代码示例

curl --location 'https://dashscope.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

待检索的run 所传入的thread id

str

run_id

待检索的run step的所属run id

str

step_id

待检索的run step 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"
}

输出参数

输出检索到的run step类,并包含除了用户输入以外参数的其他额外字段:

  • id :run step id

  • request_id :请求id

SDK

代码示例

from dashscope import Steps
step = Steps.retrieve('step_id',
                      thread_id='thread_id',
                      run_id='run_id')
from dashscope import Steps
step = Steps.retrieve('step_id',
                      thread_id='thread_id',
                      run_id='run_id')

输入参数

参数

类型

默认值

说明

step_id

str

-

指定要查询的Step id

run_id

str

-

指定要查询的Step所属Run id

thread_id

str

-

指定要查询的Step所属Thread id

workspace

str

None

DashScope workspace id

api_key

str

None

DashScope API key,可以通过环境变量等方法设置。

返回结果

{
    "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

-

Step id

assistant_id

str

-

Step所属Assiatant id

run_id

str

-

Step所属Run id

thread_id

str

-

Step所属Thread id

step_details

Dict

StepDetails信息

created_at

timestamp

创建时间

gmt_created

datetime

2024-03-22 17:12:31

gmt_modified

datetime

2024-03-22 17:12:31

code

str

表示请求失败,表示错误码,成功忽略。

python only

message

str

失败,表示失败详细信息,成功忽略。

python only

修改运行任务

HTTP

代码示例

curl --location 'https://dashscope.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

待修改的thread_id

str

run_id

待修改的run_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": "你是一个智能小助手,你可以根据用户的需求去调用不同的工具,进而给出回答。请酌情使用工具。",
    "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"
}

输出参数

输出修改后run类,并包含除了用户输入以外参数的其他额外字段:

  • id :run_id

  • request_id :请求id

SDK

代码示例

from dashscope import Runs
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;

public class Main {
    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

-

要更新的Run id

thread_id

str

-

Run所属的Thread id

metadata

Dict

None

assistant关联信息

workspace

str

None

DashScope workspace id

api_key

str

None

DashScope api key,可以通过环境变量等方法设置。

输出参数

同create,参考create

提交工具输出并继续执行任务

HTTP

代码示例

在使用Function call过程中,会需要把中间function运行结果返回给run,并将run的状态从“require_action" 切换回正常状态。此时,“required_action.type” 的值为“submit_tool_outputs”。

非流式

curl --location 'https://dashscope.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.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

待修改的thread_id

str

run_id

待修改的run_id

str

tool_outputs

一个tools外部调用的输出结果的列表

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\": \"西安\"}"
                    },
                    "id": "",
                    "type": "function"
                }
            ]
        }
    },
    "last_error": {},
    "expires_at": 0,
    "started_at": 0,
    "cancelled_at": 0,
    "failed_at": 0,
    "completed_at": 0,
    "model": "qwen-max",
    "instructions": "你是一个智能小助手,你可以根据用户的需求去调用不同的工具,进而给出回答。请酌情使用工具。",
    "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"}

event:thread.run.queued
data:{"id":"run_36d6fba9-0bf1-47f5-8e7b-f8c8140beda9","object":"thread.run","created_at":1714448903737,"thread_id":"thread_c6ac2eb0-ed95-4834-befb-ea66eba85b9f","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":"你是一个智能小助手,你可以根据用户的需求去调用不同的工具,进而给出回答。请酌情使用工具。","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":"6d1573a4-3cbb-9066-99cc-7247ec17cb30"}

event:thread.run.in_progress
data:{"id":"run_36d6fba9-0bf1-47f5-8e7b-f8c8140beda9","object":"thread.run","created_at":1714448903737,"thread_id":"thread_c6ac2eb0-ed95-4834-befb-ea66eba85b9f","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":"你是一个智能小助手,你可以根据用户的需求去调用不同的工具,进而给出回答。请酌情使用工具。","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":"6d1573a4-3cbb-9066-99cc-7247ec17cb30"}

event:thread.run.step.created
data:{"id":"step_d74df5eb-dcb8-4fc2-a943-d06ccda0e2e3","object":"thread.run.step","created_at":1714449008828,"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":"message_creation","status":"in_progress","step_details":{"type":"message_creation","message_creation":{"message_id":"message_965b3bb5-dbe2-434a-a36e-f7cda3845360"}},"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.step.in_progress
data:{"id":"step_d74df5eb-dcb8-4fc2-a943-d06ccda0e2e3","object":"thread.run.step","created_at":1714449008828,"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":"message_creation","status":"in_progress","step_details":{"type":"message_creation","message_creation":{"message_id":"message_965b3bb5-dbe2-434a-a36e-f7cda3845360"}},"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.message.created
data:{"id":"message_965b3bb5-dbe2-434a-a36e-f7cda3845360","object":"thread.message","created_at":1714449008828,"thread_id":"thread_c6ac2eb0-ed95-4834-befb-ea66eba85b9f","incomplete_details":null,"completed_at":null,"incomplete_at":null,"role":"assistant","content":[],"assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","run_id":"run_36d6fba9-0bf1-47f5-8e7b-f8c8140beda9","file_ids":[],"metadata":{},"request_id":"6d1573a4-3cbb-9066-99cc-7247ec17cb30"}

event:thread.message.in_progress
data:{"id":"message_965b3bb5-dbe2-434a-a36e-f7cda3845360","object":"thread.message","created_at":1714449008828,"thread_id":"thread_c6ac2eb0-ed95-4834-befb-ea66eba85b9f","incomplete_details":null,"completed_at":null,"incomplete_at":null,"role":"assistant","content":[],"assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","run_id":"run_36d6fba9-0bf1-47f5-8e7b-f8c8140beda9","file_ids":[],"metadata":{},"request_id":"6d1573a4-3cbb-9066-99cc-7247ec17cb30"}

event:thread.message.delta
data:{"id":"message_965b3bb5-dbe2-434a-a36e-f7cda3845360","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"西","annotations":[]}}],"file_ids":[]},"request_id":"6d1573a4-3cbb-9066-99cc-7247ec17cb30"}

event:thread.message.delta
data:{"id":"message_965b3bb5-dbe2-434a-a36e-f7cda3845360","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"安目前的天气是","annotations":[]}}],"file_ids":[]},"request_id":"6d1573a4-3cbb-9066-99cc-7247ec17cb30"}

event:thread.message.delta
data:{"id":"message_965b3bb5-dbe2-434a-a36e-f7cda3845360","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"雨天。","annotations":[]}}],"file_ids":[]},"request_id":"6d1573a4-3cbb-9066-99cc-7247ec17cb30"}

event:thread.message.delta
data:{"id":"message_965b3bb5-dbe2-434a-a36e-f7cda3845360","object":"thread.message.delta","delta":{"role":"assistant","content":[{"type":"text","text":{"value":"","annotations":[]}}],"file_ids":[]},"request_id":"6d1573a4-3cbb-9066-99cc-7247ec17cb30"}

event:thread.message.completed
data:{"id":"message_965b3bb5-dbe2-434a-a36e-f7cda3845360","object":"thread.message","created_at":1714449008828,"thread_id":"thread_c6ac2eb0-ed95-4834-befb-ea66eba85b9f","incomplete_details":null,"completed_at":1714449009919,"incomplete_at":null,"role":"assistant","content":[{"type":"text","text":{"value":"西安目前的天气是雨天。","annotations":[]}}],"assistant_id":"asst_5378d83a-f612-4fc1-9034-915351d67d98","run_id":"run_36d6fba9-0bf1-47f5-8e7b-f8c8140beda9","file_ids":[],"metadata":{},"request_id":"6d1573a4-3cbb-9066-99cc-7247ec17cb30"}

event:thread.run.step.completed
data:{"id":"step_d74df5eb-dcb8-4fc2-a943-d06ccda0e2e3","object":"thread.run.step","created_at":1714449008828,"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":"message_creation","status":"completed","step_details":{"type":"message_creation","message_creation":{"message_id":"message_965b3bb5-dbe2-434a-a36e-f7cda3845360"}},"last_error":null,"expired_at":null,"cancelled_at":null,"failed_at":null,"completed_at":1714449009919,"metadata":{},"usage":null,"request_id":"6d1573a4-3cbb-9066-99cc-7247ec17cb30"}

event:thread.run.completed
data:{"id":"run_36d6fba9-0bf1-47f5-8e7b-f8c8140beda9","object":"thread.run","created_at":1714448903737,"thread_id":"thread_c6ac2eb0-ed95-4834-befb-ea66eba85b9f","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":"你是一个智能小助手,你可以根据用户的需求去调用不同的工具,进而给出回答。请酌情使用工具。","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":439,"output_tokens":11,"total_tokens":450},"temperature":null,"top_p":null,"top_k":null,"max_tokens":null,"truncation_strategy":null,"request_id":"6d1573a4-3cbb-9066-99cc-7247ec17cb30"}

event:done
data:[DONE]

输出参数

输出修改后run类,并包含除了用户输入以外参数的其他额外字段:

  • id :run_id

  • request_id :请求id

SDK

import json
import sys
from http import HTTPStatus

from dashscope import Assistants, Messages, Runs, Steps, Threads



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()
    print(assistant)
    verify_status_code(assistant)

    # create thread.
    thread = Threads.create()
    print(thread)
    verify_status_code(thread)

    # create a message.
    message = Messages.create(thread.id, content='bigAdd 87787 to 788988737.')
    print(message)
    verify_status_code(message)

    # create run to run message

    message_run = Runs.create(thread.id, assistant_id=assistant.id)
    print(message_run)
    verify_status_code(message_run)

    # get run statue
    run_status = Runs.get(message_run.id, thread_id=thread.id)
    print(run_status)
    verify_status_code(run_status)

    # wait for run completed or requires_action
    run_status = Runs.wait(message_run.id, thread_id=thread.id)
    print(run_status)

    # if prompt input tool result, submit tool result.
    # should call big_add
    if run_status.required_action:
        tool_outputs = [{
            'tool_call_id':
            run_status.required_action.submit_tool_outputs.tool_calls[0].id,
            'output':
            '789076524'
        }]
        run = Runs.submit_tool_outputs(message_run.id,
                                       thread_id=thread.id,
                                       tool_outputs=tool_outputs)
        print(run)
        verify_status_code(run)

        # wait for run completed or requires_action
        run_status = Runs.wait(message_run.id, thread_id=thread.id)
        print(run_status)
        verify_status_code(run_status)

    run_status = Runs.get(run.id, thread_id=thread.id)
    print(run_status)
    verify_status_code(run_status)

    run_steps = Steps.list(run.id,  thread_id=thread.id)

    print(run_steps)
    verify_status_code(run_steps)

    # get the thread messages.
    msgs = Messages.list(thread.id)
    print(msgs)
    print(json.dumps(msgs, default=lambda o: o.__dict__, sort_keys=True, indent=4))
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.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.AssistantThread;
import com.alibaba.dashscope.threads.ThreadParam;
import com.alibaba.dashscope.threads.Threads;
import com.alibaba.dashscope.threads.messages.Messages;
import com.alibaba.dashscope.threads.messages.TextMessageParam;
import com.alibaba.dashscope.threads.messages.ThreadMessage;
import com.alibaba.dashscope.threads.runs.RequiredAction;
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.threads.runs.SubmitToolOutputsParam;
import com.alibaba.dashscope.threads.runs.ToolOutput;
import com.alibaba.dashscope.tools.FunctionDefinition;
import com.alibaba.dashscope.tools.ToolCallBase;
import com.alibaba.dashscope.utils.JsonUtils;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.victools.jsonschema.generator.Option;
import com.github.victools.jsonschema.generator.OptionPreset;
import com.github.victools.jsonschema.generator.SchemaGenerator;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfig;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.generator.SchemaVersion;
import com.alibaba.dashscope.tools.ToolCallFunction;

public class AssistantFunctionCall {
    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() {
        SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12,
                OptionPreset.PLAIN_JSON);
        SchemaGeneratorConfig config = configBuilder.with(Option.EXTRA_OPEN_API_FORMAT_VALUES)
                .without(Option.FLATTENED_ENUMS_FROM_TOSTRING).build();
        SchemaGenerator generator = new SchemaGenerator(config);

        // generate jsonSchema of function.
        ObjectNode jsonSchema = generator.generateSchema(AddFunctionTool.class);

        // call with tools of function call, jsonSchema.toString() is jsonschema String.
        FunctionDefinition fd = FunctionDefinition.builder().name("add").description("add two number")
                .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必须填写
        .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();
        AssistantThread assistantThread = threads.create(ThreadParam.builder().build());    
        
        Runs runs = new Runs();
        // create a new message
        TextMessageParam textMessageParam = TextMessageParam.builder().role("user").content("Add 87787 to 788988737.").build();
        Messages messages = new Messages();
        ThreadMessage threadMessage = messages.create(assistantThread.getId(), textMessageParam);
        System.out.println(threadMessage);
        RunParam runParam = RunParam.builder().assistantId(assistantId).build();
        Run run = runs.create(assistantThread.getId(), runParam);
        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(assistantThread.getId(), run.getId());
        }
        if(run.getStatus().equals(Run.Status.REQUIRES_ACTION)){   
            // submit action output.
            RequiredAction requiredAction = run.getRequiredAction();
            if(requiredAction.getType().equals("submit_tool_outputs")){
                ToolCallBase toolCall = requiredAction.getSubmitToolOutputs().getToolCalls().get(0);
                if (toolCall.getType().equals("function")) {
                    // get function call name and argument, both String.
                    String functionName = ((ToolCallFunction) toolCall).getFunction().getName();
                    String functionId = ((ToolCallFunction)toolCall).getId();
                    String functionArgument = ((ToolCallFunction) toolCall).getFunction().getArguments();
                    if (functionName.equals("add")) {
                      // Create the function object.
                      AddFunctionTool addFunction =
                          JsonUtils.fromJson(functionArgument, AddFunctionTool.class);
                      // call function.
                      int sum = addFunction.call();
                      System.out.println(sum);
                      SubmitToolOutputsParam submitToolOutputsParam = SubmitToolOutputsParam.builder()
                      .toolOutput(ToolOutput.builder().toolCallId(functionId).output(String.valueOf(sum)).build())
                      .build();
                      run = runs.submitToolOutputs(assistantThread.getId(), run.getId(), submitToolOutputsParam);
                    }
                  }
            }    
        }
        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(assistantThread.getId(), run.getId());
        }        

        GeneralListParam listParam = GeneralListParam.builder().limit(100l).build();
        ListResult<ThreadMessage> threadMessages = messages.list(assistantThread.getId(), listParam);
        for(ThreadMessage threadMessage2: threadMessages.getData()){
            System.out.println(threadMessage2);
        }

    }

    public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
        Assistant assistant = createAssistant();
        run(assistant.getId());
    }
}

等待运行任务结束

等待run执行,run为终态返回,终止状态包含['cancelled', 'failed', 'completed', 'expired', 'requires_action']

HTTP

SDK

代码示例

from dashscope import Runs
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;

public class AssistantFunctionCall {

    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

-

指定要取消的Run id

thread_id

str

-

Run所属Thread id

workspace

str

None

DashScope workspace id

api_key

str

None

DashScope API key,可以通过环境变量等方法设置。

输出参数

同Create