1e41f4b71Sopenharmony_ci# AI Framework Development Guide 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## **Overview** 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci### Introduction 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciThe AI subsystem is the part of OpenHarmony that provides native distributed AI capabilities. At the heart of the subsystem is a unified AI engine framework, which implements quick integration of AI algorithm plug-ins. 8e41f4b71Sopenharmony_ciThe framework consists of the plug-in management, module management, and communication management modules, fulfilling lifecycle management and on-demand deployment of AI algorithms. Specifically, plug-in management implements lifecycle management, on-demand deployment, and quick integration of AI algorithm plug-ins; module management implements task scheduling and client instance management; communication management manages inter-process communication (IPC) between the client and server and data transmission between the AI engine and plug-ins. Under this framework, AI algorithm APIs will be standardized to facilitate distributed calling of AI capabilities. In addition, unified inference APIs will be provided to adapt to different inference framework hierarchies. 9e41f4b71Sopenharmony_ciThe following figure shows the AI engine framework. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci **Figure 1** AI engine framework 13e41f4b71Sopenharmony_ci  14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci### Setting Up the Environment 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci1. Prepare a development board, which can be Hi3516D V300 or Hi3518E V300. 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci2. [Download the source code.](../get-code/sourcecode-acquire.md) 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci## Technical Specifications 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci### Code Management 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ciThe AI engine framework consists of three modules: **client**, **server**, and **common**. The client module provides the server connection management function. An OpenHarmony SDK needs to encapsulate and call the public APIs provided by the client in the algorithm's external APIs. The server module provides functions such as plug-in loading and task management. Plug-ins are integrated using the plug-in APIs provided by the server. The common module provides platform-related operation methods, engine protocols, and tool classes for other modules. 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ciThe following figure shows the code dependency between modules of the AI engine framework. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci **Figure 2** Code dependency 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci  35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci#### Recommendation: Develop plug-ins and OpenHarmony SDKs in the directories specified by the AI engine. 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ciIn the overall planning of the AI engine framework, OpenHarmony SDKs are a part of the client, and plug-ins are called by the server and are considered a part of the server. Therefore, the following directories have been planned for plug-in and OpenHarmony SDK development in the AI engine framework: 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci- SDK code directory: //foundation/ai/engine/services/client/algorithm_sdk 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci Example 1: //foundation/ai/engine/services/client/algorithm_sdk/cv 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci Example 2: //foundation/ai/engine/services/client/algorithm_sdk/nlu 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci- Plug-in code directory: //foundation/ai/engine/services/server/plugin 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci Example 1: //foundation/ai/engine/services/server/plugin/cv 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci Example 2: //foundation/ai/engine/services/server/plugin/nlu 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci#### Rule: Store all external APIs provided by plug-ins in the **interfaces/kits** directory of the AI subsystem. 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ciThe AI subsystem exposes its capabilities through external APIs of OpenHarmony SDKs. According to API management requirements of OpenHarmony, store all external APIs of the SDK in the **interfaces/kits** directory of the subsystem. Currently, the external APIs of plug-ins of the AI subsystem are stored in the **//foundation/ai/engine/interfaces/kits** directory. You can add a sub-directory for each newly added plug-in in this directory. For example, if you add a CV plug-in, then store its external APIs in the **//foundation/ai/engine/interfaces/kits/cv** directory. 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci#### Rule: Make sure that plug-in build results are stored in the **/usr/lib** directory. 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ciPlug-in loading on the server uses the dlopen mode and can only be performed in the** /usr/lib** directory. Therefore, when compiling the **.so** file of a plug-in, set the output directory as **/usr/lib** in the build configuration file. 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci### Naming rule 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci#### Rule: Name an SDK in the format of **domain_keyword<_other information 1_other information 2_...>_sdk.so**. 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ciYou are advised to use the commonly known abbreviations for domains. For example, use **cv** for image and video, **asr** for voice recognition, and **translation** for text translation. Add one if there is no available abbreviation for a domain. Use keywords that accurately describe the algorithm capability of the plug-in. For example, use **keyword\_spotting** for wakeup keyword spotting (KWS). Add other information, such as the supported chip type and applicable region, between **keyword** and **sdk**, with each of them separated by an underscore (\_). Note that the name of an SDK must end with **\_sdk**. 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ciFor example, if the SDK for the KWS plug-in supports only the Kirin 9000 chipset and is applicable only in China, then name the SDK as follows: **asr\_keyword\_spotting\_kirin9000\_china\_sdk.so**. 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci#### Rule: Name a plug-in in the format of **domain_keyword<_other information 1_other information 2_...>.so**. 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ciThere is a one-to-one mapping between plug-ins and SDKs. Therefore, the definitions and requirements of terms such as the domain, keyword, and other information in plug-in names are the same as those in SDK names. The only difference is that the name of the SDK ends with **\_sdk** additionally. For example, if the plug-in is named **asr\_keyword\_spotting.so**, the corresponding SDK is named **asr\_keyword\_spotting\_sdk.so**. 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ciFor example, if the SDK for the KWS plug-in supports only the Kirin 9000 chipset and is applicable only in China, then name the plug-in as follows: **asr\_keyword\_spotting\_kirin9000\_china.so**. 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci### API Development 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci#### Rule: Encapsulate the external APIs provided by the client based on the algorithm call sequence. For the SDK of an asynchronous plug-in, implement the **IClientCb** API provided by the client. 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ciThe external APIs provided by the client of the AI engine include **AieClientInit**, **AieClientPrepare**, **AieClientSyncProcess**, **AieClientAsyncProcess**, **AieClientRelease**, **AieClientDestroy**, **AieClientSetOption**, and **AieClientGetOption**. The SDK needs to encapsulate at least the following five APIs in sequence: **AieClientInit**, **AieClientPrepare**, **AieClientSyncProcess** (or **AieClientAsyncProcess**), **AieClientRelease**, and **AieClientDestroy**. Otherwise, a call failure or memory leakage may occur. For example, if the **AieClientPrepare** API is omitted during encapsulation, the server cannot load the plug-in. As a result, APIs that follow it cannot be called. 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ciFor an asynchronous plug-in, the SDK needs to implement the **IClientCb** API to receive the algorithm inference result from the client and return the result to the third-party caller. 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci#### Rule: Save all common data related to client interaction in the SDK during API implementation. 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ciThe client of the AI engine uses the singleton pattern for API implementation. If the client is connecting to multiple SDKs, each SDK needs to store all common data exchanged with the client so that they can connect to the server to perform operations such as task inference and return the result. Common data usually includes **clientInfo**, **algorithmInfo**, and **configInfo**, which are defined in the SDK's member variables. 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci#### Recommendation: Enable the SDK to implement the **IServiceDeadCb** API defined by the client. 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ciThe processes running on the server are system resident processes. The server provides services for clients by way of system capabilities. The **IServiceDeadCb** API is called if a server process is abnormally killed. The SDK can implement related operations in this API, for example, stopping process call or restarting the server. 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ciThe following is an example of **IServiceDeadCb** API implementation: 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci``` 100e41f4b71Sopenharmony_ciclass ServiceDeadCb : public IServiceDeadCb { 101e41f4b71Sopenharmony_cipublic: 102e41f4b71Sopenharmony_ciServiceDeadCb() = default; 103e41f4b71Sopenharmony_ci~ServiceDeadCb() override = default; 104e41f4b71Sopenharmony_civoid OnServiceDead() override 105e41f4b71Sopenharmony_ci{ 106e41f4b71Sopenharmony_ciprintf("[ServiceDeadCb]OnServiceDead Callback happens"); 107e41f4b71Sopenharmony_ci} 108e41f4b71Sopenharmony_ci}; 109e41f4b71Sopenharmony_ci``` 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ciAs shown above, the SDK can implement its own operations in the **OnServiceDead()** function, for example, stopping API call. 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci#### Rule: Convert dedicated algorithm data into common data of the AI engine if the SDK and plug-ins need to use the codec module. 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ciFor plug-ins, inference data is transmitted by the third-party caller to them through the client and server. The required data type varies according to algorithms. For example, the CV algorithm requires image data, and the ASR algorithm requires audio data. To address this issue, the AI engine provides the codec capabilities to convert different types of data into common data that can be used by it. 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ciThe encoded data is as follows: 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci``` 122e41f4b71Sopenharmony_cistruct DataInfo { 123e41f4b71Sopenharmony_ciunsigned char *data; 124e41f4b71Sopenharmony_ciint length; 125e41f4b71Sopenharmony_ci} DataInfo; 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci``` 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ciAs shown above, **DataInfo** consists of two variables: a pointer to the data memory, and the data length. 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ciTo use the APIs of the AI engine framework, you need to: 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci1. Add the dependency header file utils/encdec/include/encdec.h. 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci2. Add the dependency items in the **build.gn** file. 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ciAdd **//foundation/ai/engine/services/common** to **include\_dirs**. 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ciAdd **//foundation/ai/engine/services/common/utils/encdec:encdec** to **deps**. 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci3. Convert different types of data through codec. The following is an example: 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci``` 145e41f4b71Sopenharmony_ci// Example function for encoding: arg1, arg2, and arg3 are variables to be encoded, and dataInfo is the encoding result. 146e41f4b71Sopenharmony_ciretCode = ProcessEncode(dataInfo, arg1, arg2, arg3) // The number of parameters can be flexible. 147e41f4b71Sopenharmony_ci// Example function for decoding: dataInfo is the data to be decoded, and arg1, arg2, and arg3 are the decoding result. 148e41f4b71Sopenharmony_ciretCode = ProcessDecode(dataInfo, arg1, arg2, arg3) // The number of parameters can be flexible. 149e41f4b71Sopenharmony_ci``` 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ciNote: 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci- The sequence of parameters must be the same during encoding and decoding. 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci- After encoding, the memory used by **dataInfo** needs to be manually released by the caller. 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci- The memory is managed and released separately on the server and the client. 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci- If a pointer contains the shared memory, no extra processing is required. 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci- If other types of pointers are used, you need to dereference them before using **ProcessEncode** or **ProcessDecode**. 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci- The codec module has not been adapted to the **class** data type and therefore it is not recommended. 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci#### Rule: Release the memory used by the encoded or decoded parameters in the SDK. Otherwise, a memory leakage occurs. 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ciEncoding is essentially a process of encapsulating different types of data in the same memory space and then encapsulating the start address and length of the memory into the body. A plug-in is unable to release the memory that has been allocated to output parameter data, which is returned to the SDK through encoding. To obtain the data, the SDK first needs to release the memory. 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ciThe following is an example of releasing the memory: 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci``` 174e41f4b71Sopenharmony_ciDataInfo outputInfo = { 175e41f4b71Sopenharmony_ci.data = nullptr, 176e41f4b71Sopenharmony_ci.length = 0, 177e41f4b71Sopenharmony_ci}; 178e41f4b71Sopenharmony_ciAieClientPrepare(clientInfo_, algorithmInfo_, inputInfo, outputInfo, nullptr); 179e41f4b71Sopenharmony_ciif (outputInfo.data != nullptr) { 180e41f4b71Sopenharmony_cifree(outputInfo.data); 181e41f4b71Sopenharmony_cioutputInfo.data = nullptr; 182e41f4b71Sopenharmony_cioutputInfo.length = 0; 183e41f4b71Sopenharmony_ci} 184e41f4b71Sopenharmony_ci``` 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci#### Rule: Enable plug-ins to implement the **IPlugin** API defined by the server and use the **PLUGIN\_INTERFACE\_IMPL** statement to provide the function pointer for external systems. 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ciThe server manages a variety of plug-ins, and the API implementation logic varies according to plug-ins. To unify the plug-in loading process, the AI engine provides the **IPlugin** API. In the runtime environment, a plug-in is loaded as a dynamic link library (DLL) by the AI engine framework in dlopen mode. Therefore, the plug-in needs to use the **PLUGIN\_INTERFACE\_IMPL** statement to expose the function pointer. Otherwise, the plug-in cannot be properly loaded. 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci#### Rule: Use the unified data channel provided by the AI engine for plug-ins. 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ciThe AI engine provides a unified data channel between the server and plug-ins to send inference requests from the SDK and returned results from plug-ins. Plug-ins need to obtain the request data and encapsulate the inference result over the data channel when calling the inference API. 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ciThe following is an example of using the data channel: 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci``` 200e41f4b71Sopenharmony_ciint SyncProcess(IRequest *request, IResponse *&response) 201e41f4b71Sopenharmony_ci{ 202e41f4b71Sopenharmony_ciHILOGI("[IvpPlugin]Begin SyncProcess"); 203e41f4b71Sopenharmony_ciif (request == nullptr) { 204e41f4b71Sopenharmony_ciHILOGE("[IvpPlugin]SyncProcess request is nullptr"); 205e41f4b71Sopenharmony_cireturn RETCODE_NULL_PARAM; 206e41f4b71Sopenharmony_ci} 207e41f4b71Sopenharmony_ciDataInfo inputInfo = request->GetMsg(); 208e41f4b71Sopenharmony_ciif (inputInfo.data == nullptr) { 209e41f4b71Sopenharmony_ciHILOGE("[IvpPlugin]InputInfo data is nullptr"); 210e41f4b71Sopenharmony_cireturn RETCODE_NULL_PARAM; 211e41f4b71Sopenharmony_ci} 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci... 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ciresponse = IResponse::Create(request); 216e41f4b71Sopenharmony_ciresponse->SetResult(outputInfo); 217e41f4b71Sopenharmony_cireturn RETCODE_SUCCESS; 218e41f4b71Sopenharmony_ci} 219e41f4b71Sopenharmony_ci``` 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ciIn the example, the request and response are the data body sent over the data channel. The server encapsulates data in the request and sends it to the plug-in. After completing algorithm processing, the plug-in encapsulates the result into the response and returns it to the server over the data channel. 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci## Development Guidelines 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci### Developing the SDK 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ciThe function of the SDK header file is implemented by mapping SDK API calls to client API calls. The following table lists the APIs provided by the client. 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ci **Table 1** Client APIs 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ci| API| Description| Parameters| 233e41f4b71Sopenharmony_ci| -------- | -------- | -------- | 234e41f4b71Sopenharmony_ci| int **AieClientInit**(const ConfigInfo &configInfo,<br> ClientInfo &clientInfo, const AlgorithmInfo<br> &algorithmInfo, IServiceDeadCb \*cb) | **Function**: Links and initializes the engine service and activates IPC call.<br>**Return value**: **0** if the operation is successful; a non-zero value otherwise.| **configInfo**: engine-related initial configuration data. This parameter must not be null.<br>**clientInfo**: engine client information. This parameter must not be null.<br>**algorithmInfo**: information about the called algorithm. This parameter must not be null.<br>**cb**: death callback object. This parameter can be null. | 235e41f4b71Sopenharmony_ci| int **AieClientPrepare**(const ClientInfo &clientInfo<br>, const AlgorithmInfo &algorithmInfo, const DataInfo<br> &inputInfo, DataInfo &outputInfo, IClientCb \*cb) | **Function**: Loads an algorithm plug-in.<br>**Return value**: **0** if the operation is successful; a non-zero value otherwise.| **clientInfo**: engine client information. This parameter must not be null.<br>**algorithmInfo**: information about the called algorithm. This parameter must not be null.<br>**inputInfo**: input information specified for algorithm plug-in loading. This parameter can be null.<br>**outputInfo**: information returned after algorithm plug-in loading, if any. This parameter can be null.<br>**cb**: return result of the asynchronous algorithm. This parameter must not be null for the asynchronous algorithm. For the synchronous algorithm, this parameter must be null. | 236e41f4b71Sopenharmony_ci| int **AieClientAsyncProcess**(const ClientInfo &clientInfo,<br> const AlgorithmInfo &algorithmInfo, const DataInfo<br> &inputInfo) | **Function**: Executes an asynchronous algorithm.<br>**Return value**: **0** if the operation is successful; a non-zero value otherwise.| **clientInfo**: engine client information. This parameter must not be null.<br>**algorithmInfo**: information about the called algorithm. This parameter must not be null.<br>**inputInfo**: input information specified for algorithm operations. This parameter can be null. | 237e41f4b71Sopenharmony_ci| int **AieClientSyncProcess**(const ClientInfo &clientInfo,<br> const AlgorithmInfo &algorithmInfo, const<br> DataInfo &inputInfo, DataInfo &outputInfo) | **Function**: Executes a synchronous algorithm.<br>**Return value**: **0** if the operation is successful; a non-zero value otherwise.| **clientInfo**: engine client information. This parameter must not be null.<br>**algorithmInfo**: information about the called algorithm. This parameter must not be null.<br>**inputInfo**: input information specified for algorithm operations. This parameter can be null.<br>**outputInfo**: output information in the return result of the synchronous algorithm. This parameter can be null. | 238e41f4b71Sopenharmony_ci| int **AieClientRelease**(const ClientInfo &clientInfo,<br> const AlgorithmInfo &algorithmInfo, const<br> DataInfo &inputInfo) | **Function**: Uninstalls an algorithm plug-in.<br>**Return value**: **0** if the operation is successful; a non-zero value otherwise.| **clientInfo**: engine client information. This parameter must not be null.<br>**algorithmInfo**: information about the algorithm plug-in to be uninstalled. This parameter must not be null.<br>**inputInfo**: input information specified for algorithm plug-in uninstallation. This parameter can be null. | 239e41f4b71Sopenharmony_ci| int **AieClientDestroy**(ClientInfo &clientInfo) | **Function**: Disconnects from the server and releases the cache.<br>**Return value**: **0** if the operation is successful; a non-zero value otherwise.| **clientInfo**: engine client information. This parameter must not be null. | 240e41f4b71Sopenharmony_ci| int **AieClientSetOption**(const ClientInfo &clientInfo,<br> int optionType, const DataInfo &inputInfo) | **Function**: Sets configuration items. You can use this API to pass algorithm's extended information to plug-ins.<br>**Return value**: **0** if the operation is successful; a non-zero value otherwise.| **clientInfo**: engine client information. This parameter must not be null.<br>**optionType**: algorithm used for obtaining the configuration item information. An algorithm plug-in can use this parameter as needed. This parameter must not be null.<br>**inputInfo**: Indicates algorithm parameter information. An algorithm plug-in can use this parameter as needed. This parameter can be null. | 241e41f4b71Sopenharmony_ci| int **AieClientGetOption**(const ClientInfo &clientInfo,<br> int optionType, const DataInfo &inputInfo,<br> DataInfo &outputInfo) | **Function**: Obtains configuration item information based on the specified **optionType** and **inputInfo**.<br>**Return value**: **0** if the operation is successful; a non-zero value otherwise.| **clientInfo**: engine client information. This parameter must not be null.<br>**optionType**: algorithm used for obtaining the configuration item information. This parameter must not be null.<br>**inputInfo**: input information specified for obtaining configuration item information of the algorithm. This parameter can be null.<br>**outputInfo**: configuration item information in the return result. This parameter can be null. | 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ciThe following table describes the data structure of **ConfigInfo**, **ClientInfo**, **AlgorithmInfo**, and **DataInfo**. 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci **Table 2** Data structure of ConfigInfo, ClientInfo, AlgorithmInfo, and DataInfo 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ci| Data Structure| Description| Attributes| 250e41f4b71Sopenharmony_ci| -------- | -------- | -------- | 251e41f4b71Sopenharmony_ci| ConfigInfo | Algorithm configuration item information| **const char \*description**: body of configuration item information. | 252e41f4b71Sopenharmony_ci| ClientInfo | Client information.| **long long clientVersion**: client version number. This parameter is not used currently.<br>**int clientId**: client ID.<br>**int sessionId**: session ID.<br>**uid\_t serverUid**: server UID.<br>**uid\_t clientUid**: client UID.<br>**int extendLen**: length of the extended information (**extendMsg**).<br>**unsigned char \*extendMsg**: body of the extended information. | 253e41f4b71Sopenharmony_ci| AlgorithmInfo | Algorithm information| **long long clientVersion**: client version number. This parameter is not used currently.<br>**bool isAsync**: whether asynchronous execution is used.<br>**int algorithmType**: algorithm type ID allocated by the AI engine framework based on the plug-in loading sequence.<br>**long long algorithmVersion**: algorithm version number.<br>**bool isCloud**: whether to migrate data to the cloud. This parameter is not used currently.<br>**int operateId**: execution ID. This parameter is not used currently.<br>**int requestId**: request ID, which identifies each request and corresponds to the execution result.<br>**int extendLen**: length of the extended information (**extendMsg**).<br>**unsigned char \*extendMsg**: body of the extended information. | 254e41f4b71Sopenharmony_ci| DataInfo | Algorithm input parameter configuration information (**inputInfo**)<br>and output parameter configuration information (**outputInfo**)| **unsigned char \*data**: data subject.<br>**int length**: data length. | 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ciFor details about the development process, see the development example of the [KWS SDK](#kws-sdk). 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ci### Developing a Plug-in 260e41f4b71Sopenharmony_ciThe AI engine framework has defined a set of algorithm plug-in access specifications. Each plug-in needs to implement specified APIs to implement functions such as obtaining the plug-in version information and algorithm inference type, executing synchronous and asynchronous algorithms, loading algorithm plug-ins, uninstalling algorithm plug-ins, setting algorithm configuration information, and obtaining specified algorithm configuration information. Specifically, implement the **SyncProcess** API for the synchronous algorithm and the **AsyncProcess** API for the asynchronous algorithm. 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ci 263e41f4b71Sopenharmony_ciThe following table describes the **IPlugin** APIs. 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ci **Table 3** IPlugin APIs 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci| API| Description| Parameters| 269e41f4b71Sopenharmony_ci| -------- | -------- | -------- | 270e41f4b71Sopenharmony_ci| const long long GetVersion() const; | **Function**: Obtains the plug-in version information.<br>**Return value**: version number (in the **long long** format)| - | 271e41f4b71Sopenharmony_ci| const char \*GetInferMode() const; | **Function**: Obtains the algorithm inference type.<br>**Return value**: "SYNC" or "ASYNC"| - | 272e41f4b71Sopenharmony_ci| int SyncProcess(IRequest \*request,<br>IResponse \*&response); | **Function**: Executes a synchronous algorithm.<br>**Return value**: **0** if the operation is successful; a non-zero value otherwise.| **request**: used to pass the request content to the algorithm plug-in over the data channel between the engine server and the plug-in. This parameter must not be null.<br>**response**: used to receive the synchronous algorithm execution result returned by the algorithm plug-in over the data channel between the engine server and the plug-in. This parameter must not be null.| 273e41f4b71Sopenharmony_ci| int AsyncProcess(IRequest \*request,<br>IPluginAlgorithmCallback \*callback); | **Function**: Executes an asynchronous algorithm.<br>**Return value**: **0** if the operation is successful; a non-zero value otherwise.| **request**: used to pass the request content to the algorithm plug-in over the data channel between the engine server and the plug-in. This parameter must not be null.<br>**callback**: used to return the asynchronous algorithm execution result to the engine server. This parameter must not be null.| 274e41f4b71Sopenharmony_ci| int Prepare(long long transactionId,<br>const DataInfo &inputInfo, DataInfo<br>&outputInfo); | **Function**: Loads an algorithm plug-in.<br>**Return value**: **0** if the operation is successful; a non-zero value otherwise.| **transactionId**: transaction ID, which is used to identify the client and session. This parameter must not be null.<br>**inputInfo**: input information specified for algorithm plug-in loading. This parameter can be null.<br>**outputInfo**: output information in the return result of algorithm plug-in loading. This parameter can be null.| 275e41f4b71Sopenharmony_ci| int Release(bool isFullUnload, long long<br>transactionId, const DataInfo &inputInfo); | **Function**: Uninstalls an algorithm plug-in.<br>**Return value**: **0** if the operation is successful; a non-zero value otherwise.| **isFullUnload**: whether a plug-in is called by only one client. A plug-in can be uninstalled only when it is called by only one client. This parameter must not be null.<br>**transactionId**: transaction ID, which is used to identify the client and session. This parameter must not be null.<br>**inputInfo**: input information specified for algorithm plug-in uninstallation. This parameter can be null.| 276e41f4b71Sopenharmony_ci| int SetOption(int optionType, const<br>DataInfo &inputInfo); | **Function**: Sets configuration items. You can use this API to pass algorithm's extended information to plug-ins.<br>**Return value**: **0** if the operation is successful; a non-zero value otherwise.| **optionType**: algorithm used for obtaining the configuration item information. An algorithm plug-in can use this parameter as needed. This parameter must not be null.<br>**inputInfo**: algorithm parameter information. An algorithm plug-in can use this parameter as needed. This parameter can be null.| 277e41f4b71Sopenharmony_ci| int GetOption(int optionType, const<br>DataInfo &inputInfo, DataInfo<br>&outputInfo); | **Function**: Obtains configuration item information based on the specified **optionType** and **inputInfo**.<br>**Return value**: **0** if the operation is successful; a non-zero value otherwise.| **optionType**: algorithm used for obtaining the configuration item information. This parameter must not be null.<br>**inputInfo**: input information specified for obtaining configuration item information of the algorithm. This parameter can be null.<br>**outputInfo**: configuration item information in the return result. This parameter can be null.| 278e41f4b71Sopenharmony_ci 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ciAlgorithm plug-in APIs including **Prepare**, **SyncProcess**, **AsyncProcess**, **Release**, **SetOption**, and **GetOption** are in 1:1 mapping with the client APIs including **AieClientPrepare**, **AieClientSyncProcess**, **AieClientAsyncProcess**, **AieClientRelease**, **AieClientSetOption**, and **AieClientGetOption**. The **GetInferMode** API is used to return the algorithm execution type, which can be synchronous or asynchronous. 281e41f4b71Sopenharmony_ci 282e41f4b71Sopenharmony_ci 283e41f4b71Sopenharmony_ciThe following table describes the **IPluginCallback** APIs. 284e41f4b71Sopenharmony_ci 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ci **Table 4** IPluginCallback APIs 287e41f4b71Sopenharmony_ci 288e41f4b71Sopenharmony_ci| API| Description| Parameters| 289e41f4b71Sopenharmony_ci| -------- | -------- | -------- | 290e41f4b71Sopenharmony_ci| void OnEvent(PluginEvent event,<br>IResponse \*response); | **Function**: Returns the asynchronous algorithm execution result.| **event**: used to enumerate the algorithm execution result. The value can be **ON\_PLUGIN\_SUCCEED** or **ON\_PLUGIN\_FAIL**.<br>**response**: used to return the algorithm execution result.| 291e41f4b71Sopenharmony_ci 292e41f4b71Sopenharmony_ci 293e41f4b71Sopenharmony_ciThe **Request** and **Response** classes define the requests and responses used for communication between the AI engine server and algorithm plug-ins. A request encapsulates the request content and input data of the caller. The plug-in returns the calculation result to the AI engine server through a response. 294e41f4b71Sopenharmony_ci 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_ciThe following table describes the attributes of the **Request** class. 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ci **Table 5** Attributes of the Request class 300e41f4b71Sopenharmony_ci 301e41f4b71Sopenharmony_ci| Attribute| Description| Default Value| 302e41f4b71Sopenharmony_ci| -------- | -------- | -------- | 303e41f4b71Sopenharmony_ci| innerSequenceId_ | **Type**: long long<br>**Function**: reserved| 0 | 304e41f4b71Sopenharmony_ci| requestId_ | **Type**: int<br>**Function**: Indicates the request sequence, which is used to bind the return result.| 0 | 305e41f4b71Sopenharmony_ci| operationId_ | **Type**: int<br>**Function**: reserved| 0 | 306e41f4b71Sopenharmony_ci| transactionId_ | Type: long long<br>**Function**: Indicates the transaction ID, which is the combination of **clientId** and **sessionId**.| 0 | 307e41f4b71Sopenharmony_ci| algoPluginType_ | **Type**: int<br>**Function**: Indicates the algorithm type ID allocated by the AI engine framework based on the plug-in loading sequence.| 0 | 308e41f4b71Sopenharmony_ci| msg_ | **Type**: DataInfo<br>**Function**: Stores the input parameters for calling the algorithm API.| .data = nullptr<br>.length = 0 | 309e41f4b71Sopenharmony_ci 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ciThe following table describes the attributes of the **Response** class. 312e41f4b71Sopenharmony_ci 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_ci **Table 6** Attributes of the Response class 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_ci| Attribute| Description| Default Value| 317e41f4b71Sopenharmony_ci| -------- | -------- | -------- | 318e41f4b71Sopenharmony_ci| innerSequenceId_ | **Type**: long long<br>**Function**: reserved| 0 | 319e41f4b71Sopenharmony_ci| requestId_ | **Type**: int<br>**Function**: Indicates the request sequence, which is used to bind the return result.| 0 | 320e41f4b71Sopenharmony_ci| retCode__ | **Type**: int<br>**Function**: Indicates the inference result code of the asynchronous algorithm.| 0 | 321e41f4b71Sopenharmony_ci| retDesc_ | **Type**: string<br>**Function**: reserved| - | 322e41f4b71Sopenharmony_ci| transactionId_ | Type: long long<br>**Function**: Indicates the transaction ID, which is the combination of **clientId** and **sessionId**.| 0 | 323e41f4b71Sopenharmony_ci| algoPluginType_ | **Type**: int<br>**Function**: Indicates the algorithm type ID allocated by the AI engine framework based on the plug-in loading sequence.| INVALID_ALGO_PLUGIN_TYPE(-1) | 324e41f4b71Sopenharmony_ci| result_ | **Type**: DataInfo<br>**Function**: Stores the inference result of the asynchronous algorithm.| .data = nullptr<br>.length = 0 | 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ciFor details about the development process, see the development example of the [KWS plug-in](#kws-plug-in). 328e41f4b71Sopenharmony_ci 329e41f4b71Sopenharmony_ci### Preparing the Configuration File 330e41f4b71Sopenharmony_ci 331e41f4b71Sopenharmony_ciThe SDK identifies the plug-in type based on **algorithmVersion** and **algorithmType** in the **AlgorithmInfo** structure so it can call the plug-in capabilities. Therefore, you need to develop the plug-in configuration file as follows: 332e41f4b71Sopenharmony_ci 333e41f4b71Sopenharmony_ci 334e41f4b71Sopenharmony_ci1. Add the plug-in configuration file to the **//foundation/ai/engine/services/common/protocol/plugin\_config/plugin\_config\_ini/** directory. 335e41f4b71Sopenharmony_ci 336e41f4b71Sopenharmony_ci2. Add the algorithm type to the **aie\_algorithm_type.h** file in the **//foundation/ai/engine/services/common/protocol/plugin\_config/** directory. 337e41f4b71Sopenharmony_ci 338e41f4b71Sopenharmony_ci3. Add the name of the KWS algorithm and its sequence number in **ALGORITHM\_TYPE\_ID\_LIST** to the **aie\_plugin\_info.h** file in the **//foundation/ai/engine/services/server/plugin\_manager/include/** directory. 339e41f4b71Sopenharmony_ci 340e41f4b71Sopenharmony_ciFor details about the development process, see the development example of the [configuration file](#kws-configuration-file). 341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ci## Development Example 343e41f4b71Sopenharmony_ci 344e41f4b71Sopenharmony_ci### KWS SDK 345e41f4b71Sopenharmony_ci 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ci1. Add the API definition of the KWS SDK to the **//foundation/ai/engine /interfaces/kits** directory. This API can be called by third-party applications. The following code snippet is an example API for the KWS SDK. The reference code is available at the **//foundation/ai/engine /interfaces/kits/asr/keyword\_spotting** directory. 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ci ``` 350e41f4b71Sopenharmony_ci class KWSSdk { 351e41f4b71Sopenharmony_ci public: 352e41f4b71Sopenharmony_ci KWSSdk(); 353e41f4b71Sopenharmony_ci virtual ~KWSSdk(); 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci // Create a KWS SDK instance. 356e41f4b71Sopenharmony_ci int32_t Create(); 357e41f4b71Sopenharmony_ci 358e41f4b71Sopenharmony_ci // Synchronously execute the KWS task. 359e41f4b71Sopenharmony_ci int32_t SyncExecute(const Array<int16_t> &audioInput); 360e41f4b71Sopenharmony_ci 361e41f4b71Sopenharmony_ci // Set the KWS callback. 362e41f4b71Sopenharmony_ci int32_t SetCallback(const std::shared_ptr<KWSCallback> &callback); 363e41f4b71Sopenharmony_ci 364e41f4b71Sopenharmony_ci // Destroy the KWS SDK instance to release the session engaged with the plug-in. 365e41f4b71Sopenharmony_ci int32_t Destroy(); 366e41f4b71Sopenharmony_ci }; 367e41f4b71Sopenharmony_ci ``` 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_ci2. Add the API implementation of the SDK to the **//foundation/ai/engine/services/client/algorithm\_sdk** directory and call the APIs provided by the client to use the algorithm plug-in capabilities. The following code snippet is an example implementation of the **create** method in the API of the KWS SDK. For more details, see the reference code at **//foundation/ai/engine/services/client/algorithm\_sdk/asr/keyword\_spotting**. 370e41f4b71Sopenharmony_ci 371e41f4b71Sopenharmony_ci ``` 372e41f4b71Sopenharmony_ci int32_t KWSSdk::KWSSdkImpl::Create() 373e41f4b71Sopenharmony_ci { 374e41f4b71Sopenharmony_ci if (kwsHandle_ != INVALID_KWS_HANDLE) { 375e41f4b71Sopenharmony_ci HILOGE("[KWSSdkImpl]The SDK has been created"); 376e41f4b71Sopenharmony_ci return KWS_RETCODE_FAILURE; 377e41f4b71Sopenharmony_ci } 378e41f4b71Sopenharmony_ci if (InitComponents() != RETCODE_SUCCESS) { 379e41f4b71Sopenharmony_ci HILOGE("[KWSSdkImpl]Fail to init sdk components"); 380e41f4b71Sopenharmony_ci return KWS_RETCODE_FAILURE; 381e41f4b71Sopenharmony_ci } 382e41f4b71Sopenharmony_ci // Call the AieClientInit API provided by the client to initialize the engine service and activate IPC call. 383e41f4b71Sopenharmony_ci int32_t retCode = AieClientInit(configInfo_, clientInfo_, algorithmInfo_, nullptr); 384e41f4b71Sopenharmony_ci if (retCode != RETCODE_SUCCESS) { 385e41f4b71Sopenharmony_ci HILOGE("[KWSSdkImpl]AieClientInit failed. Error code[%d]", retCode); 386e41f4b71Sopenharmony_ci return KWS_RETCODE_FAILURE; 387e41f4b71Sopenharmony_ci } 388e41f4b71Sopenharmony_ci if (clientInfo_.clientId == INVALID_CLIENT_ID) { 389e41f4b71Sopenharmony_ci HILOGE("[KWSSdkImpl]Fail to allocate client id"); 390e41f4b71Sopenharmony_ci return KWS_RETCODE_FAILURE; 391e41f4b71Sopenharmony_ci } 392e41f4b71Sopenharmony_ci DataInfo inputInfo = { 393e41f4b71Sopenharmony_ci .data = nullptr, 394e41f4b71Sopenharmony_ci .length = 0, 395e41f4b71Sopenharmony_ci }; 396e41f4b71Sopenharmony_ci DataInfo outputInfo = { 397e41f4b71Sopenharmony_ci .data = nullptr, 398e41f4b71Sopenharmony_ci .length = 0, 399e41f4b71Sopenharmony_ci }; 400e41f4b71Sopenharmony_ci // Call the AieClientPrepare API provided by the client to load the algorithm plug-in. 401e41f4b71Sopenharmony_ci retCode = AieClientPrepare(clientInfo_, algorithmInfo_, inputInfo, outputInfo, nullptr); 402e41f4b71Sopenharmony_ci if (retCode != RETCODE_SUCCESS) { 403e41f4b71Sopenharmony_ci HILOGE("[KWSSdkImpl]AieclientPrepare failed. Error code[%d]", retCode); 404e41f4b71Sopenharmony_ci return KWS_RETCODE_FAILURE; 405e41f4b71Sopenharmony_ci } 406e41f4b71Sopenharmony_ci if (outputInfo.data == nullptr || outputInfo.length <= 0) { 407e41f4b71Sopenharmony_ci HILOGE("[KWSSdkImpl]The data or length of output info is invalid"); 408e41f4b71Sopenharmony_ci return KWS_RETCODE_FAILURE; 409e41f4b71Sopenharmony_ci } 410e41f4b71Sopenharmony_ci MallocPointerGuard<unsigned char> pointerGuard(outputInfo.data); 411e41f4b71Sopenharmony_ci retCode = PluginHelper::UnSerializeHandle(outputInfo, kwsHandle_); 412e41f4b71Sopenharmony_ci if (retCode != RETCODE_SUCCESS) { 413e41f4b71Sopenharmony_ci HILOGE("[KWSSdkImpl]Get handle from inputInfo failed"); 414e41f4b71Sopenharmony_ci return KWS_RETCODE_FAILURE; 415e41f4b71Sopenharmony_ci } 416e41f4b71Sopenharmony_ci return KWS_RETCODE_SUCCESS; 417e41f4b71Sopenharmony_ci } 418e41f4b71Sopenharmony_ci ``` 419e41f4b71Sopenharmony_ci 420e41f4b71Sopenharmony_ci The preceding code is the specific API implementation. The **create** function in the API of the KWS SDK calls the open **AieClientInit** and **AieClientPrepare** APIs provided by the client to connect to the server and load the algorithm model. 421e41f4b71Sopenharmony_ci 422e41f4b71Sopenharmony_ci > **NOTE** 423e41f4b71Sopenharmony_ci > 424e41f4b71Sopenharmony_ci > The sequence for the SDK to call client APIs of the AI engine is as follows: AieClientInit -> AieClientPrepare -> AieClientSyncProcess/AieClientAsyncProcess -> AieClientRelease -> AieClientDestroy. An exception will be thrown if the call sequence is violated. 425e41f4b71Sopenharmony_ci 426e41f4b71Sopenharmony_ci### KWS Plug-in 427e41f4b71Sopenharmony_ciAdd the API definition (that is, **IPlugin**) of the KWS plug-in to the **//foundation/ai/engine/services/server/plugin** directory. This API is used to call AI capabilities. The following code snippet is an example API implementation of the KWS plug-in. The reference code is available at the **//foundation/ai/engine/services/server/plugin/asr/keyword\_spotting** directory. 428e41f4b71Sopenharmony_ci 429e41f4b71Sopenharmony_ci ``` 430e41f4b71Sopenharmony_ci #include "plugin/i_plugin.h 431e41f4b71Sopenharmony_ci class KWSPlugin : public IPlugin { 432e41f4b71Sopenharmony_ci public: 433e41f4b71Sopenharmony_ci KWSPlugin(); 434e41f4b71Sopenharmony_ci ~KWSPlugin(); 435e41f4b71Sopenharmony_ci const long long GetVersion() const override; 436e41f4b71Sopenharmony_ci const char* GetName() const override; 437e41f4b71Sopenharmony_ci const char* GetInferMode() const override; 438e41f4b71Sopenharmony_ci int32_t Prepare(long long transactionId, const DataInfo &amp;inputInfo, DataInfo &amp;outputInfo) override; 439e41f4b71Sopenharmony_ci int32_t SetOption(int optionType, const DataInfo &amp;inputInfo) override; 440e41f4b71Sopenharmony_ci int32_t GetOption(int optionType, const DataInfo &amp;inputInfo, DataInfo &amp;outputInfo) override; 441e41f4b71Sopenharmony_ci int32_t SyncProcess(IRequest *request, IResponse *&amp;response) override; 442e41f4b71Sopenharmony_ci int32_t AsyncProcess(IRequest *request, IPluginCallback*callback) override; 443e41f4b71Sopenharmony_ci int32_t Release(bool isFullUnload, long long transactionId, const DataInfo &amp;inputInfo) override; 444e41f4b71Sopenharmony_ci }; 445e41f4b71Sopenharmony_ci ``` 446e41f4b71Sopenharmony_ciThe preceding code implements the **IPlugin** APIs provided by the server. The following table shows the mapping between the client APIs and the plug-in APIs. 447e41f4b71Sopenharmony_ci 448e41f4b71Sopenharmony_ci**Table 7** Mapping between the client APIs and the plug-in APIs 449e41f4b71Sopenharmony_ci 450e41f4b71Sopenharmony_ci| Client API| Plug-in API| Description| 451e41f4b71Sopenharmony_ci| -------- | -------- | -------- | 452e41f4b71Sopenharmony_ci| AieClientPrepare | Prepare | Initializes the inference algorithm plug-in. For KWS, this API loads the KWS model from the fixed location (**/sdcard/wenwen\_inst.wk**) to the memory.| 453e41f4b71Sopenharmony_ci| AieClientSyncProcess | SyncProcess | Executes the inference algorithm synchronously. For KWS, this API synchronously executes the audio inference algorithm to determine whether the specified wakeup keyword exists in the audio.| 454e41f4b71Sopenharmony_ci| AieClientAsyncProcess | AsyncProcess | Executes the inference algorithm asynchronously. Currently, this API is not used in KWS. However, you can implement the API based on your use case.| 455e41f4b71Sopenharmony_ci| AieClientSetOption | SetOption | Sets algorithm-related configuration items, such as the confidence threshold and delay. Currently, this API is not used in KWS. However, you can implement the API based on your use case.| 456e41f4b71Sopenharmony_ci| AieClientGetOption | GetOption | Obtains algorithm-related configuration items. For KWS, this API can obtain the input and output scale of the KWS model. The input scale is the MFCC feature (fixed value: **4000**) required by the KWS model, and the output scale is the confidence (fixed value: **2**) of the result.| 457e41f4b71Sopenharmony_ci| AieClientRelease | Release | Releases the algorithm model. For KWS, this API releases the specified algorithm model and clears the dynamic memory in the feature processor.| 458e41f4b71Sopenharmony_ci 459e41f4b71Sopenharmony_ci Note: 460e41f4b71Sopenharmony_ci 461e41f4b71Sopenharmony_ci 1. The **AieClientInit** and **AieClientDestroy** APIs are used to connect to and disconnect from the server, respectively. They are not called in the plug-in algorithm and therefore do not need to be defined in the plug-in. 462e41f4b71Sopenharmony_ci 463e41f4b71Sopenharmony_ci 2. The KWS plug-in needs to use the **PLUGIN\_INTERFACE\_IMPL** statement to expose the function pointer. Otherwise, the plug-in cannot be properly loaded. 464e41f4b71Sopenharmony_ci 465e41f4b71Sopenharmony_ci 466e41f4b71Sopenharmony_ci ``` 467e41f4b71Sopenharmony_ci PLUGIN_INTERFACE_IMPL(KWSPlugin); 468e41f4b71Sopenharmony_ci ``` 469e41f4b71Sopenharmony_ci 470e41f4b71Sopenharmony_ci### KWS Configuration File 471e41f4b71Sopenharmony_ci 472e41f4b71Sopenharmony_ci1. Add the KWS configuration file to the **//foundation/ai/engine/services/common/protocol/plugin\_config/plugin\_config\_ini/** directory. 473e41f4b71Sopenharmony_ci 474e41f4b71Sopenharmony_ci ``` 475e41f4b71Sopenharmony_ci [base] 476e41f4b71Sopenharmony_ci supported_boards = hi3516dv300 477e41f4b71Sopenharmony_ci related_sessions = asr_keyword_spotting+20001002 478e41f4b71Sopenharmony_ci 479e41f4b71Sopenharmony_ci // Naming rule: [algorithm name+algorithm version], for example, [asr\_keyword\_spotting+20001002] 480e41f4b71Sopenharmony_ci [asr_keyword_spotting+20001002] 481e41f4b71Sopenharmony_ci AID = asr_keyword_spotting 482e41f4b71Sopenharmony_ci VersionCode = 20001002 483e41f4b71Sopenharmony_ci VersionName = 2.00.01.002 484e41f4b71Sopenharmony_ci XPU = NNIE 485e41f4b71Sopenharmony_ci District = China 486e41f4b71Sopenharmony_ci // Location of the complied **.so** file of the plug-in 487e41f4b71Sopenharmony_ci FullPath = /usr/lib/libasr_keyword_spotting.so 488e41f4b71Sopenharmony_ci Chipset = ALL 489e41f4b71Sopenharmony_ci ChkSum = '' 490e41f4b71Sopenharmony_ci Key = '' 491e41f4b71Sopenharmony_ci ``` 492e41f4b71Sopenharmony_ci 493e41f4b71Sopenharmony_ci2. Add the type ID of the KWS algorithm to the **aie\_algorithm_type.h** file in the **//foundation/ai/engine/services/common/protocol/plugin\_config/** directory. 494e41f4b71Sopenharmony_ci 495e41f4b71Sopenharmony_ci ``` 496e41f4b71Sopenharmony_ci // Make sure that the type ID maps to the sequence number in **ALGORITHM\_TYPE\_ID\_LIST**. 497e41f4b71Sopenharmony_ci const int ALGORITHM_TYPE_KWS = 3; 498e41f4b71Sopenharmony_ci ``` 499e41f4b71Sopenharmony_ci 500e41f4b71Sopenharmony_ci3. Add the name of the KWS algorithm and its sequence number in **ALGORITHM\_TYPE\_ID\_LIST** to the **aie\_plugin\_info.h** file in the **//foundation/ai/engine/services/server/plugin\_manager/include/** directory. 501e41f4b71Sopenharmony_ci 502e41f4b71Sopenharmony_ci ``` 503e41f4b71Sopenharmony_ci const std::string ALGORITHM_ID_SAMPLE_1 = "sample_plugin_1"; 504e41f4b71Sopenharmony_ci const std::string ALGORITHM_ID_SAMPLE_2 = "sample_plugin_2"; 505e41f4b71Sopenharmony_ci const std::string ALGORITHM_ID_IVP = "cv_human_detect"; 506e41f4b71Sopenharmony_ci // Add the name of the KWS algorithm: asr\_keyword\_spotting. 507e41f4b71Sopenharmony_ci // Name the algorithm variable in the same way as the algorithm type ID in ALGORITHM\_TYPE\_ID\_LIST, for example, ALGORITHM_ID_KWS. 508e41f4b71Sopenharmony_ci const std::string ALGORITHM_ID_KWS = "asr_keyword_spotting"; 509e41f4b71Sopenharmony_ci const std::string ALGORITHM_ID_IC = "cv_image_classification"; 510e41f4b71Sopenharmony_ci const std::string ALGORITHM_ID_INVALID = "invalid algorithm id"; 511e41f4b71Sopenharmony_ci 512e41f4b71Sopenharmony_ci const std::vector<std::string> ALGORITHM_TYPE_ID_LIST = { 513e41f4b71Sopenharmony_ci ALGORITHM_ID_SAMPLE_1, 514e41f4b71Sopenharmony_ci ALGORITHM_ID_SAMPLE_2, 515e41f4b71Sopenharmony_ci ALGORITHM_ID_IVP, 516e41f4b71Sopenharmony_ci // Add the sequence number of the KWS algorithm to ALGORITHM\_TYPE\_ID\_LIST, so that the name of the KWS algorithm can be obtained based on the sequence number. 517e41f4b71Sopenharmony_ci // Make sure that the algorithm name maps to the sequence number in ALGORITHM\_TYPE\_ID\_LIST. 518e41f4b71Sopenharmony_ci ALGORITHM_ID_KWS, 519e41f4b71Sopenharmony_ci ALGORITHM_ID_IC, 520e41f4b71Sopenharmony_ci }; 521e41f4b71Sopenharmony_ci ``` 522