This topic describes how to use Java to access the Table Q&A service.
Installation
The pom dependencies are as follows:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.6.3</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-alinlp</artifactId>
<version>1.8.10</version>
</dependency>Call the service using a common request
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
public class TableQA {
public static void main(String[] args) {
// Create and initialize a DefaultAcsClient instance.
// An AccessKey for an Alibaba Cloud account has permissions to access all APIs. This poses a high security risk.
// Create and use a Resource Access Management (RAM) user for API calls and routine O&M.
// Log on to the RAM console to create a RAM user.
// This example shows how to store the AccessKey ID and AccessKey secret in environment variables.
// You can also store them in a configuration file as needed.
// Do not hard-code the AccessKey ID and AccessKey secret in your code. This can lead to security risks.
String accessKeyId = System.getenv("NLP_AK_ENV");
String accessKeySecret = System.getenv("NLP_SK_ENV");
DefaultProfile profile = DefaultProfile.getProfile(
"cn-hangzhou",
accessKeyId,
accessKeySecret);
DefaultAcsClient acsClient = new DefaultAcsClient(profile);
// Create a request to query service information and set the parameters.
CommonRequest serviceInfoRequest = new CommonRequest();
// The domain and version are static fields.
serviceInfoRequest.setSysDomain("alinlp.cn-hangzhou.aliyuncs.com");
serviceInfoRequest.setSysVersion("2020-06-29");
serviceInfoRequest.putQueryParameter("ServiceCode","alinlp");
serviceInfoRequest.putQueryParameter("TokenizerId","MAINSE");
// The action to query service information: GetTableQAServiceInfoById.
serviceInfoRequest.setSysAction("GetTableQAServiceInfoById");
// The service ID. This is the service ID displayed in the console list.
serviceInfoRequest.putQueryParameter("ServiceId", "95");
String botId = "";
try {
CommonResponse response = acsClient.getCommonResponse(serviceInfoRequest);
JSONObject responseObject = JSON.parseObject(response.getData());
// Obtain the botId from the service information.
botId = responseObject.getJSONObject("Data").getJSONObject("data").getString("botId");
System.out.println(botId);
} catch (ClientException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Create a table Q&A request and set the parameters.
CommonRequest tableqaRequest = new CommonRequest();
// The domain and version are static fields.
tableqaRequest.setSysDomain("alinlp.cn-hangzhou.aliyuncs.com");
tableqaRequest.setSysVersion("2020-06-29");
tableqaRequest.putQueryParameter("ServiceCode","alinlp");
tableqaRequest.putQueryParameter("TokenizerId","MAINSE");
tableqaRequest.setSysAction("RequestTableQAOnline");
JSONObject params = new JSONObject();
// botId: Use the botId obtained from the previous query.
params.put("bot_id",botId);
// question: The question to ask.
params.put("question","I want to see yesterday's fund quota");
tableqaRequest.putQueryParameter("Params",params.toString());
try {
CommonResponse tableqaResponse = acsClient.getCommonResponse(tableqaRequest);
System.out.println(tableqaResponse.getData());
} catch (ClientException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}Note: The bot_id parameter for RequestTableQAOnline is retrieved from the query result of GetTableQAServiceInfoById. Before you use the Table Q&A service, call GetTableQAServiceInfoById to retrieve the latest bot_id associated with the service.
Troubleshoot call exceptions
If an exception occurs during a call, find the corresponding description in Troubleshoot call exceptions (error code summary). The document describes the cause of the error and provides a solution.
该文章对您有帮助吗?