Multi-turn query rewriting API
1. Parameters
1.1 Request parameters
Top-level parameters
Parameter | Type | Required | Description |
algorithm | string | Yes | Identifier |
model | string | No | The model version. This parameter is optional. Valid values: dashscope-conv-rewrite-1.8b dashscope-conv-rewrite-14b |
input | map<string, object> | Yes | Input parameters |
parameters | map<string, object> | No | Configuration parameters |
debug | boolean | No | The debug mode. The default value is false. |
Second-level parameters
Secondary Parameters | Type | Required | Description |
input.query | string | Yes | The input content for the current turn. |
input.history | array(map/dict/json) | No | The conversation history.
[ {"role": "user", "content": "user_content"}, {"role": "assistant", "content": "assistant_content"} ] |
{
"algorithm": "conversation_rewriter",
"model": "dashscope-conv-rewrite-14b",
"input": {
"query": "Is it expensive?",
"history": [
{
"role": "user",
"content": "What is black catalpa wood?"
},
{
"role": "assistant",
"content": "Black catalpa wood is one of the most widely used types of wood. In northeastern China, it is also known as stinky paulownia. It is widely distributed and has a large output. Many decorative components are made from black catalpa wood."
}
]
}
}1.2 Response parameters
Top-level parameters
Parameter | Type | Description |
requestId | string | The request ID. |
status | int | The status of the request. |
message | string | The response message. |
data | map<string, object> | The result of multi-turn rewriting. |
debugInfo | map<string, object> | The debug information. |
Secondary parameters
Second-level parameters | Type | Description |
data.conversationRewriter | array(string) | The list of queries after multi-turn rewriting. |
data.timeCost | float | The time consumed by the service, in ms. |
data.inputTokens | int | The number of input tokens. |
data.outputTokens | int | The number of output tokens. |
{
"requestId": null,
"data": {
"conversationRewriter": [
"Is the price of black catalpa wood expensive?"
],
"outputTokens": 8,
"timeCost": 424.08,
"inputTokens": 61
},
"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.0.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 a client using an AccessKey pair.
* @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);
PostISConvRewriterRequest request = new PostISConvRewriterRequest();
request.setAlgorithm("conversation_rewriter");
String input = "{\n" +
" \"query\": \"Is it expensive?\",\n" +
" \"history\": [\n" +
" {\n" +
" \"role\": \"user\",\n" +
" \"content\": \"What is black catalpa wood?\"\n" +
" },\n" +
" {\n" +
" \"role\": \"assistant\",\n" +
" \"content\": \"Black catalpa wood is one of the most widely used types of wood. In northeastern China, it is also known as stinky paulownia. It is widely distributed and has a large output. Many decorative components are made from black catalpa wood.\"\n" +
" }\n" +
" ]\n" +
"}";
Map<String, Object> inputMap = JSONObject.parseObject(input, Map.class);
request.setInput(inputMap);
PostISConvRewriterResponse response = client.postISConvRewriter(request);
System.out.println(JSONObject.toJSONString(response.getBody()));
}2.2 Python SDK
2.2.1 pip source
pip install alibabacloud-alinlp20200629==3.0.02.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 conversation_rewriter():
config = api_models.Config(
access_key_id='xxx',
access_key_secret='xxx',
region_id="cn-beijing")
nlp_client = client.Client(config)
request = models.PostISConvRewriterRequest()
input_str = '{\n' + \
' \"query\": \"Is it expensive?\",\n' + \
' \"history\": [\n' + \
' {\n' + \
' \"role\": \"user\",\n' + \
' \"content\": \"What is black catalpa wood?\"\n' + \
' },\n' + \
' {\n' + \
' \"role\": \"assistant\",\n' + \
' \"content\": \"Black catalpa wood is one of the most widely used types of wood. In northeastern China, it is also known as stinky paulownia. It is widely distributed and has a large output. Many decorative components are made from black catalpa wood.\"\n' + \
' }\n' + \
' ]\n' + \
'}'
request.input = json.loads(input_str)
request.algorithm = 'conversation_rewriter'
response = nlp_client.post_isconv_rewriter(request)
print(json.dumps(response.body.data, ensure_ascii=False))
if __name__ == '__main__':
conversation_rewriter()