文档

智能体

更新时间:
一键部署

创建智能体

前提条件

  • 已开通服务并获得API-KEY:获取API-KEY

  • 我们推荐您将API-KEY配置到环境变量中以降低API-KEY的泄漏风险,详情可参考通过环境变量配置API-KEY。您也可以在代码中配置API-KEY,但是泄漏风险会增加。

  • Dashscope Python SDK 需要1.18.0及以上版本,您可以通过pip install -U dashscope更新。Dashscope Java SDK 需要2.14.2及以上版本。

from dashscope import Assistants
import os

assistant = Assistants.create(
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        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': 'search'
        }, {
            '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']
                }
            }
        }],
)
print(assistant)
import com.alibaba.dashscope.assistants.Assistant;
import com.alibaba.dashscope.assistants.AssistantParam;
import com.alibaba.dashscope.assistants.Assistants;
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 java.lang.System;
import com.alibaba.dashscope.utils.JsonUtils;

public class Main {
    public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
        Assistants assistants = new Assistants();
        // build assistant parameters
        AssistantParam param = AssistantParam.builder()
                .model("qwen-max")
                .name("intelligent guide")
                .description("a smart guide")
                .instructions("You are a helpful assistant.")
                .build();
        Assistant assistant = assistants.create(param);
        System.out.println(JsonUtils.toJson(assistant));
        // use assistant

    }
}

返回结果

结果为智能体对象,json化后的示例内容为:

{'tools': [{'type': 'search'},
  {'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']}},
   'type': 'function'}],
 'id': 'asst_xxx',
 'object': 'assistant',
 'created_at': xxx,
 'model': 'qwen-max',
 'name': 'smart helper',
 'description': 'A tool helper.',
 'instructions': 'You are a helpful assistant. When asked a question, use tools wherever possible.',
 'file_ids': [],
 'metadata': {},
 'temperature': None,
 'top_p': None,
 'top_k': None,
 'max_tokens': None,
 'request_id': 'xxx',
 'status_code': 200}

输入参数配置

参数

类型

默认值

说明

model

string

-

指定用于智能体所使用的模型

name

string

-

指定智能体名称

description

string

-

智能体描述

instructions

string

-

指定智能体功能信息

tools

array

[]

智能体使用的tools

说明

自定义tool鉴权信息传递

{

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

}

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

file_ids

array

[]

智能体使用的文件id,

说明

目前file_ids需要和插件配合使用,目前只有PDF解析器支持传文件,单独使用assistant传文件不会起作用。

metadata

object

None

智能体关联信息

workspace

string

None

DashScope workspace id

api_key

string

None

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

输出智能体对象字段说明

字段名

字段类型

字段描述

status_code

integer

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

name

string

智能体名称。

id

string

智能体id,为uuid字符串。

model

string

智能体使用的模型名称。

description

string

智能体描述信息。

instructions

string

指定智能体功能信息。

metadata

object

key,value对对象信息,描述智能体信息。

tools

array

智能体可以用的tool列表。

file_ids

array

智能体关联file id,file可以通过file命令和Files接口上传。

created_at

integer

智能体创建的时间戳表示。

code

string

表示请求失败,表示错误码,请求成功时忽略。只有在通过Python调用失败时会显示。

message

string

失败,表示失败详细信息,成功忽略。只有在通过Python调用失败时会显示。

检索智能体

您可以通过智能体id来检索智能体。在示例代码中,请用您的智能体id替换assistant_id

from dashscope import Assistants
assistant = Assistants.retrieve('assistant_id')
import com.alibaba.dashscope.assistants.Assistant;
import com.alibaba.dashscope.assistants.Assistants;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.InvalidateParameter;
import com.alibaba.dashscope.exception.NoApiKeyException;

public class Main {
    public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
      Assistants assistants = new Assistants();
      Assistant assistant = assistants.retrieve("assistant_id");
      // use assistant
    }
}

输入参数

参数

类型

默认值

说明

assistant_id

string

-

指定要查询的智能体id

workspace

string

None

百炼的workspace id,如果为主账号空间则无需指定

api_key

string

None

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

输出参数

请参考返回结果

列出智能体

from dashscope import Assistants
assistants = Assistants.list(limit=1,
                             order='desc',
                             after='',
                             before='')
import com.alibaba.dashscope.assistants.Assistant;
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;

public class Main {
    public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
      Assistants assistants = new Assistants();
      GeneralListParam listParam = GeneralListParam.builder().limit(10l).build();
      ListResult<Assistant> assistant = assistants.list(listParam);
    }
}

请求参数

参数

类型

默认值

说明

limit

string

-

指定列出assistant的数目

order

string

排序方式,示例值:desc、asc

after

string

before

string

workspace

string

None

百炼的workspace id,如果为主账号空间则无需指定

api_key

string

None

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

返回参数

字段名

字段类型

字段描述

has_more

boolean

last_id

string

data中最后一个assistant的id。

first_id

string

data中第一个assistant的id。

data

array

智能体对象列表。

object

string

data的数据格式,如"list"

request_id

string

本次请求的id。

status_code

integer

请求状态码。

更新智能体

from dashscope import Assistants
assistants = Assistants.update('assistant_id', model='new_model_name')
import com.alibaba.dashscope.assistants.Assistant;
import com.alibaba.dashscope.assistants.AssistantParam;
import com.alibaba.dashscope.assistants.Assistants;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.InvalidateParameter;
import com.alibaba.dashscope.exception.NoApiKeyException;

public class Main {
    public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
      Assistants assistants = new Assistants();
      AssistantParam param = AssistantParam.builder()
      .model("qwen-max")
      .name("intelligent guide")
      .description("a smart guide")
      .instructions("You are a helpful assistant.  When asked a question, use tools wherever possible.")
      .build();
      
      Assistant assistant = assistants.update("assistant_id", param);
    }
}

请求参数

参数

类型

默认值

说明

assistant_id

string

-

指定需要更新的assistant id

model

string

-

指定用于智能体所使用的模型

name

string

None

指定智能体名称

description

string

None

智能体描述

instructions

string

指定智能体功能信息。

tools

array

[]

智能体使用的tools

file_ids

array

[]

智能体使用的文件id

metadata

object

None

智能体关联信息

workspace

string

None

百炼的workspace id,如果为主账号空间则无需指定

api_key

string

None

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

返回参数

请参考返回结果

删除智能体

from dashscope import Assistants
assistants = Assistants.delete('assistant_id')
import com.alibaba.dashscope.assistants.Assistants;
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;

public class Main {
    public static void main(String[] args) throws ApiException, NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
      Assistants assistants = new Assistants();     
      DeletionStatus assistant = assistants.delete("assistant_id");
    }
}

请求参数

参数

类型

默认值

说明

assistant_id

string

-

指定要删除的智能体 id

workspace

string

None

百炼的workspace id,如果为主账号空间则无需指定

api_key

string

None

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

返回参数

字段名

字段类型

字段描述

id

string

删除的对象的id

object

string

本次请求目标,示例:'assistant.deleted'

deleted

boolean

是否删除

request_id

string

本次请求的id。

status_code

integer

请求状态码。