文档

通用排序模型API

更新时间:

1. 参数

1.1 入参

一级参数

参数

类型

是否必传

说明

algorithm

string

标识符

model

string

模型版本。可选择:

gte-rerank

input

map<string, object>

输入参数

parameters

map<string, object>

配置参数

debug

boolean

调试模式,默认false

二级参数

二级参数

类型

是否必传

说明

input.query

string

待处理的用户当前轮输入的query

input.documents

array(map/dict/json)

待排序的文档列表:

[

"黑龙江离xxx很近",

"哈尔滨是中国黑龙江省的省会,位于中国东北",

"you are the hero"

]

parameters.return_documents

boolean

是否返回文档内容

parameters.top_n

int

返回排序后前n个文档

{
    "model": "gte-rerank",
    "input":{
        "query": "哈尔滨在哪?",
         "documents": [
            "黑龙江离xxx很近",
            "哈尔滨是中国黑龙江省的省会,位于中国东北",
            "you are the hero"
        ]
    },
    "parameters": {
        "return_documents": false,
        "top_n": 5
    }
}

1.2 出参

一级参数

参数

类型

说明

requestId

string

请求id

status

int

请求结果状态

message

string

响应信息

data

map<string, object>

排序结果

debugInfo

map<string, object>

调试信息

二级参数

二级参数

类型

说明

data.rerank

array(map/dict/json)

排序结果

(index文档索引,relevance_score相关性分数)

data.time_cost

float

服务时间消耗,单位ms

data.input_tokens

int

输入token数

data.output_tokens

int

输出token数

{
    "requestId": null,
    "data": {
        "outputTokens": 0,
        "rerank": [
            {
                "index": 1,
                "relevance_score": 0.7161281827133217
            },
            {
                "index": 0,
                "relevance_score": 0.5855924673844218
            },
            {
                "index": 2,
                "relevance_score": 0.014008649815412342
            }
        ],
        "timeCost": null,
        "inputTokens": 49
    },
    "debugInfo": null,
    "message": null,
    "status": 0
}

2. SDK调用

2.1 Java SDK

2.1.1 Maven依赖

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>alinlp20200629</artifactId>
    <version>3.1.0</version>
</dependency>

<!--出现java.lang.NoSuchMethodError: com.aliyun.credentials.Client.getCredential()Lcom/aliyun/credentials/models/CredentialModel;异常则引入-->
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>credentials-java</artifactId>
    <version>0.3.0</version>
</dependency>

2.1.2 调用示例

/**
 * 使用AK&SK初始化账号Client
 * @param accessKeyId
 * @param accessKeySecret
 * @return Client
 * @throws Exception
 */
public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
    Config config = new Config()
    // 必填,您的 AccessKey ID
    .setAccessKeyId(accessKeyId)
    // 必填,您的 AccessKey Secret
    .setAccessKeySecret(accessKeySecret);
    // Endpoint 请参考 https://api.aliyun.com/product/alinlp
    config.endpoint = "alinlp.cn-beijing.aliyuncs.com";
    return new Client(config);
}

public static void main(String[] args) throws Exception {
    String accessKeyId = "xxx";
    String accessKeySecret = "xxx";
    Client client = createClient(accessKeyId, accessKeySecret);
    PostISRerankRequest request = new PostISRerankRequest();
    request.setAlgorithm("rerank");
    request.setModel("gte-rerank");
    String input = "{\n" +
                "    \"query\": \"哈尔滨在哪?\",\n" +
                "    \"documents\": [\n" +
                "        \"黑龙江离xxx很近\",\n" +
                "        \"哈尔滨是中国黑龙江省的省会,位于中国东北\",\n" +
                "        \"you are the hero\"\n" +
                "    ]\n" +
                "}";
    Map<String, Object> inputMap = JSONObject.parseObject(input, Map.class);
    String parameters = "{\n" +
            "        \"return_documents\": false,\n" +
            "        \"top_n\": 5\n" +
            "    }";
    Map<String, Object> parametersMap = JSONObject.parseObject(parameters, Map.class);
    request.setInput(inputMap);
    request.setParameters(parametersMap);
    PostISRerankResponse response = client.postISRerank(request);
    System.out.println(JSONObject.toJSONString(response.getBody()));
}

2.2 Python SDK

2.2.1 pip源

pip install alibabacloud-alinlp20200629==3.1.0

2.2.2 调用示例

import json
from alibabacloud_alinlp20200629 import client
from alibabacloud_tea_openapi import models as api_models
from alibabacloud_alinlp20200629 import models


def rerank():
    config = api_models.Config(
        access_key_id='xxx',
        access_key_secret='xxx',
        region_id="cn-beijing")
    nlp_client = client.Client(config)
    request = models.PostISRerankRequest()
    input = {
        "query": "哈尔滨在哪?",
        "documents": [
            "黑龙江离xxx很近",
            "哈尔滨是中国黑龙江省的省会,位于中国东北",
            "you are the hero"
        ]
    }
    parameters = {
        "return_documents": False,
        "top_n": 5
    }
    request.input = input
    request.parameters = parameters
    request.algorithm = 'rerank'
    request.model = 'gte-rerank'
    response = nlp_client.post_isrerank(request)
    print(json.dumps(response.body.data, ensure_ascii=False))


if __name__ == '__main__':
    rerank()