General-purpose sorting model API

更新时间:
复制 MD 格式

1. Parameters

1.1 Request parameters

Top-level parameters

Parameter

Type

Required

Description

algorithm

string

Yes

The identifier.

model

string

Yes

The model version. The following value is available:

gte-rerank

input

map<string, object>

Yes

The input parameters.

parameters

map<string, object>

No

The configuration parameters.

debug

boolean

No

The debug mode. The default value is false.

Second-level parameters

Second-level parameter

Type

Required

Description

input.query

string

Yes

The user's query for the current turn.

input.documents

array(map/dict/json)

Yes

The list of documents to be sorted:

[

"Heilongjiang is very close to xxx",

"Harbin is the capital of China's Heilongjiang Province, located in Northeast China",

"you are the hero"

]

parameters.return_documents

boolean

No

Specifies whether to return the document content.

parameters.top_n

int

No

Returns the top n documents after sorting.

{
    "model": "gte-rerank",
    "input":{
        "query": "Where is Harbin?",
         "documents": [
            "Heilongjiang is very close to xxx",
            "Harbin is the capital of China's Heilongjiang Province, located in Northeast China",
            "you are the hero"
        ]
    },
    "parameters": {
        "return_documents": false,
        "top_n": 5
    }
}

1.2 Response parameters

Top-level parameters

Parameter

Type

Description

requestId

string

The request ID.

status

int

The status of the request result.

message

string

The response message.

data

map<string, object>

The sorting result.

debugInfo

map<string, object>

The debug information.

Second-level parameters

Second-level parameter

Type

Description

data.rerank

array(map/dict/json)

Sorting results

(index: Document index, relevance_score: Relevance score)

data.time_cost

float

The time consumed by the service. Unit: ms.

data.input_tokens

int

The number of input tokens.

data.output_tokens

int

The number of output tokens.

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

2.1 Java SDK

2.1.1 Maven dependency

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

<!--Import this dependency if a java.lang.NoSuchMethodError: com.aliyun.credentials.Client.getCredential()Lcom/aliyun/credentials/models/CredentialModel; exception occurs.-->
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>credentials-java</artifactId>
    <version>0.3.0</version>
</dependency>

2.1.2 Call example

/**
 * Initializes the account client using an AccessKey ID and an AccessKey secret.
 * @param accessKeyId
 * @param accessKeySecret
 * @return Client
 * @throws Exception
 */
public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
    Config config = new Config()
    // Required. Your AccessKey ID.
    .setAccessKeyId(accessKeyId)
    // Required. Your AccessKey secret.
    .setAccessKeySecret(accessKeySecret);
    // For the endpoint, see 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\": \"Where is Harbin?\",\n" +
                "    \"documents\": [\n" +
                "        \"Heilongjiang is very close to xxx\",\n" +
                "        \"Harbin is the capital of China's Heilongjiang Province, located in Northeast China\",\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 source

pip install alibabacloud-alinlp20200629==3.1.0

2.2.2 Call example

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": "Where is Harbin?",
        "documents": [
            "Heilongjiang is very close to xxx",
            "Harbin is the capital of China's Heilongjiang Province, located in Northeast China",
            "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()