SDK usage guide
For more information, see Getting Started.
NLP Basic Service 2.0 dependencies
npm install @alicloud/alinlp20200629 -S
Configure access credentials using environment variables
Notes:
An AccessKey for an Alibaba Cloud account has full access permissions for all APIs, which poses a high security threat. We recommend that you create and use a Resource Access Management (RAM) user for API calls and routine operations and maintenance (O&M). To create a RAM user, log on to the RAM console.
Do not hardcode your AccessKey ID and AccessKey secret in your code. This can lead to security risks if your keys are leaked. We recommend that you use environment variables to store and access your credentials.
Configuration for Linux and macOS
export NLP_AK_ENV=<access_key_id> export NLP_SK_ENV=<access_key_secret>Replace <access_key_id> with your AccessKey ID and <access_key_secret> with your AccessKey secret. For more information about how to obtain an AccessKey ID and an AccessKey secret, see Step 2: Obtain an AccessKey for your account.
Configuration method for Windows
Create the NLP_AK_ENV and NLP_SK_ENV environment variables. Set the value of
NLP_AK_ENVto your AccessKey ID and the value ofNLP_SK_ENVto your AccessKey secret.You can restart the Windows system.
Code examples
TypeScript
import Client, * as $Alinlp from '@alicloud/alinlp20200629';
import * as $OpenApi from '@alicloud/openapi-client';
export default class Test {
static async main(): Promise<void> {
const accessKeyId = process.env.NLP_AK_ENV;
const accessKeySecret = process.env.NLP_SK_ENV;
const config = new $OpenApi.Config({
/**
* An AccessKey for an Alibaba Cloud account has full permissions for all APIs, which poses a high security threat. Create and use a RAM user for API calls and daily O&M. To create a RAM user, log on to the RAM console.
* This example shows how to store your AccessKey ID and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
* Do not hard-code your AccessKey ID and AccessKey secret in your code. This can lead to security risks.
*/
accessKeyId: process.env.NLP_AK_ENV,
accessKeySecret: process.env.NLP_SK_ENV,
// The region to access
regionId: 'cn-hangzhou',
endpoint: 'alinlp.cn-hangzhou.aliyuncs.com'
});
const client = new Client(config);
const request = new $Alinlp.GetNerChEcomRequest({
serviceCode: "alinlp",
text: "Electric multi-function grinder"
});
const resp = await client.getNerChEcom(request);
console.log(resp.body);
}
}
Test.main();
Note: In the code, GetNerChEcom in GetNerChEcomRequest is the action name for the algorithm. You must replace it with the action name of the algorithm that you want to call. You can find the action name in the API reference. It is the Sample value of the Action request parameter.
For example, to call the General Chinese Word Segmentation (Basic Edition) service, see Chinese Word Segmentation (Basic Edition). Find the sample value as shown in the following figure and replace GetNerChEcomRequest with GetWsChGeneralRequest. After you replace the request, you must also update the request parameters for the new algorithm. For more information, see the relevant API reference.
JavaScript
const { default: Client, GetNerChEcomRequest } = require('@alicloud/alinlp20200629');
const { Config } = require('@alicloud/openapi-client');
async function main() {
let config = new Config({
/**
* An AccessKey for an Alibaba Cloud account has full permissions for all APIs, which poses a high security threat. Create and use a RAM user for API calls and daily O&M. To create a RAM user, log on to the RAM console.
* This example shows how to store your AccessKey ID and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
* Do not hard-code your AccessKey ID and AccessKey secret in your code. This can lead to security risks.
*/
accessKeyId: process.env.NLP_AK_ENV,
accessKeySecret: process.env.NLP_SK_ENV,
// The region to access
regionId: 'cn-hangzhou',
endpoint: 'alinlp.cn-hangzhou.aliyuncs.com'
});
const client = new Client(config);
const request = new GetNerChEcomRequest({
serviceCode: "alinlp",
text: "Electric multi-function grinder"
});
const resp = await client.getNerChEcom(request);
console.log(resp.body);
}
main();
Note: To call other algorithms, you must also replace GetNerChEcomRequest in the code using the method described in the preceding section.
Troubleshoot call exceptions
If an exception occurs when you call the service, see Troubleshoot call exceptions (error codes). This document contains a table of error codes. You can find the error code in the table to identify the cause of the error and the corresponding solution.