创建线程
import json
from dashscope import Threads
thread = Threads.create(messages=[{"role": "user", "content": "How does AI work? Explain it in simple terms."}])
print(json.dumps(thread, default=lambda o: o.__dict__, sort_keys=True, indent=4))
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;
public class Main {
public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
Threads threads = new Threads();
AssistantThread assistantThread = threads.create(ThreadParam.builder().build());
}
}
返回结果
结果为Thread对象,json化内容为:
{
"account_id": "sk-6bddfc116de744c3aa1d66893cc87b20",
"created_at": 1711338305031,
"id": "thread_97934051-2c15-44bf-97de-310039d873f9",
"metadata": {},
"object": "thread",
"request_id": "982d4b9a-b982-9d53-9c79-a75b32f7168a",
"status_code": 200
}
输入参数配置
参数 | 类型 | 默认值 | 说明 |
messages | List[Dict] | None | Thread初始messages |
metadata | Dict | None | 与该Thread关联的key/value信息 |
workspace | str | None | DashScope workspace id |
api_key | str | None | DashScope api key,可以通过环境变量等方法设置。 |
输出Thread对象字段说明
字段名 | 字段类型 | 字段描述 |
status_code | int | 为调用http status code,200表示调用成功,其他表示调用出错 |
id | str | thread id,为uuid字符串 |
metadata | Dict | 和这个Thread关联的key/value信息 |
created_at | timestamp | assistant创建时间 |
code | str | 表示请求失败,显示错误码,成功忽略。 python only |
message | str | 失败,表示失败的详细信息,成功则忽略。 python only |
检索线程
from dashscope import Threads
thread = Threads.retrieve('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.AssistantThread;
import com.alibaba.dashscope.threads.Threads;
public class Main {
public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
Threads threads = new Threads();
AssistantThread assistantThread = threads.retrieve("thread_id");
}
}
输入参数
参数 | 类型 | 默认值 | 说明 |
thread_id | str | - | 指定要查询的thread id |
workspace | str | None | DashScope workspace id |
api_key | str | None | DashScope api key,可以通过环境变量等方法设置。 |
输出参数
参考create结果
修改线程
from dashscope import Threads
thread = Threads.update('thread_id',
metadata={'key': 'value'})
请求参数
参数 | 类型 | 默认值 | 说明 |
thread_id | str | - | 指定需要update的thread id |
metadata | Dict | None | Thread关联信息 |
workspace | str | None | DashScope workspace id |
api_key | str | None | DashScope api key,可以通过环境变量等方法设置。 |
返回参数
同create,参考create
删除线程
from dashscope import Threads
thread = Threads.delete('thread_id')
import com.alibaba.dashscope.common.DeletionStatus;
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.Threads;
public class Main {
public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
Threads threads = new Threads();
DeletionStatus assistantThread = threads.delete("thread_id");
}
}
请求参数
参数 | 类型 | 默认值 | 说明 |
thread_id | str | - | 指定要删除的thread id |
workspace | str | None | DashScope workspace id |
api_key | str | None | DashScope API key,可以通过环境变量等方法设置。 |
返回参数
字段名 | 字段类型 | 字段描述 |
id | str | 删除的对象的id |
deleted | bool | 是否删除 |