This module provides the capabilities of obtaining the knowledge processing object (KnowledgeProcessor) and knowledge processing status (ProcessorStatus).
The embedding model on which the knowledge processing capability depends can be deployed only on PCs/2-in-1 devices. Therefore, the knowledge processing capability is supported only on PCs/2-in-1 devices. An exception will be thrown if this module is unavailable on other device types.
Since: 6.0.0(20)
getKnowledgeProcessor(context: common.BaseContext, config: KnowledgeProcessorConfig): Promise<KnowledgeProcessor>
Obtains the knowledge processing object used for obtaining the knowledge processing status. This API uses a promise to return the result.
In the schema upgrade scenario, the cleanKnowledgeData API needs to be called before a database is opened for the first time or the getKnowledgeProcessor API is called.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DataAugmentation.KnowledgeProcessor
Since: 6.0.0(20)
Parameters:
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| context | common.BaseContext | Yes | Context of the knowledge processing object. |
| config | KnowledgeProcessorConfig | Yes | Knowledge processing configuration. |
Returns:
| Type | Description |
|---|---|
| Promise<KnowledgeProcessor> | Promise used to return the knowledge processing object. This object can be used to obtain the knowledge processing status (getStatus), start knowledge processing (startProcess), and stop knowledge processing (stopProcess). |
Error codes:
| Error Code | Error Message |
|---|---|
| 1021400000 | Internal error. |
| 1021400001 | The knowledge source is not configured. |
| 1021400002 | The knowledge schema file is not found. |
| 1021400003 | The knowledge schema content is invalid. |
| 1021400004 | An error occurred during operations on the RDB source. |
Example:
import { knowledgeProcessor } from '@kit.DataAugmentationKit';
import { common } from '@kit.AbilityKit';
import { relationalStore } from '@kit.ArkData';
const storeName: string = "testmail_store.db";
const storeConfig: relationalStore.StoreConfig = {
name: storeName, // Name of the database where knowledge processing is triggered.
securityLevel: relationalStore.SecurityLevel.S3,
tokenizer: relationalStore.Tokenizer.CUSTOM_TOKENIZER,
};
const knowledgeSourceConfig: knowledgeProcessor.KnowledgeSourceConfig = {
rdbSource: storeConfig,
};
const knowledgeProcessorConfig: knowledgeProcessor.KnowledgeProcessorConfig = {
sourceConfig: knowledgeSourceConfig,
};
// Asynchronous function for obtaining the knowledge processor.
async function getProcessor() {
const context = AppStorage.get<common.UIAbilityContext>("Context") as common.UIAbilityContext;
try {
// Obtain the knowledge processing object.
const processor = await knowledgeProcessor.getKnowledgeProcessor(context, knowledgeProcessorConfig);
return processor;
} catch (err) {
console.error("Error: " + err.message + " code: " + err.code);
return undefined;
}
} cleanKnowledgeData(context: common.Context, config: KnowledgeProcessorConfig): Promise<void>
Obtains the knowledge base information based on the knowledge processing configuration in the input parameters and clears the corresponding knowledge base. This API uses a promise to return the result.
In the schema upgrade scenario, call the cleanKnowledgeData API before opening a database for the first time or calling the getKnowledgeProcessor API. In other scenarios, calling this API may cause knowledge base data loss or data damage.
Caution
In the schema upgrade scenario, to avoid data inconsistency, you are strongly advised to call the cleanKnowledgeData API to clear old data only before opening a database for the first time or calling the getKnowledgeProcessor API. Do not call this API in non-schema upgrade scenarios. Otherwise, existing knowledge base data may be permanently lost or damaged and cannot be restored.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DataAugmentation.KnowledgeProcessor
Since: 6.1.0(23)
Parameters:
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| context | common.Context | Yes | Context of the knowledge processing object. |
| config | KnowledgeProcessorConfig | Yes | Knowledge processing configuration. |
Returns:
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Error codes:
| Error Code | Error Message |
|---|---|
| 1021400001 | The knowledge source is not configured. |
The knowledge schema file is not found. The knowledge schema content is invalid. An error occurred during operations on the RDB source. |
Example:
import { knowledgeProcessor } from '@kit.DataAugmentationKit';
import { common } from '@kit.AbilityKit';
import { relationalStore } from '@kit.ArkData';
const storeName: string = "testmail_store.db";
const storeConfig: relationalStore.StoreConfig = {
name: storeName, // Name of the database where knowledge processing is triggered.
securityLevel: relationalStore.SecurityLevel.S3,
tokenizer: relationalStore.Tokenizer.CUSTOM_TOKENIZER,
};
const knowledgeSourceConfig: knowledgeProcessor.KnowledgeSourceConfig = {
rdbSource: storeConfig,
};
const knowledgeProcessorConfig: knowledgeProcessor.KnowledgeProcessorConfig = {
sourceConfig: knowledgeSourceConfig,
};
// Asynchronous function for clearing the knowledge base.
async function cleanKnowledgeData() {
const context = AppStorage.get<common.UIAbilityContext>("Context") as common.UIAbilityContext;
try {
// Clear the knowledge base.
await knowledgeProcessor.cleanKnowledgeData(context, knowledgeProcessorConfig);
} catch (err) {
console.error("Error: " + err.message + " code: " + err.code);
return undefined;
}
} Manages knowledge processor object configurations.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DataAugmentation.KnowledgeProcessor
Since: 6.0.0(20)
| Name | Type | Mandatory | Description |
|---|---|---|---|
| sourceConfig | KnowledgeSourceConfig | Yes | Data source configuration for knowledge processing. |
| llm | ChatLLM | No | Pointer to the LLM maintained by the API caller. This parameter must be set and cannot be left empty for any handler that uses email analysis. Since: 26.0.0 |
| properties | string | No | Attribute information required for knowledge processing. The JSON character string is in the format of "{\"userName\": \"John\", \"maxCtxLen\": 30000}". This parameter must be set and cannot be left empty for any handler that uses email analysis. - userName: name of the user. The length range is [1,256]. This parameter is used to exclude to-dos that are not intended for the user from being mistakenly extracted. This parameter must be set for any handler that uses email analysis. - maxCtxLen: maximum context length of the foundation model defined by the API caller. The length range is [1,INT32_MAX]. The API caller must ensure that the context length of the foundation model called by the ChatLLM API is greater than or equal to this value. Otherwise, email analysis may fail due to the foundation model context length exceeding the limit. This parameter must be set for any handler that uses email analysis. Since: 26.0.0 |
Manages knowledge data source configurations.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DataAugmentation.KnowledgeProcessor
Since: 6.0.0(20)
| Name | Type | Mandatory | Description |
|---|---|---|---|
| rdbSource | relationalStore.StoreConfig | No | RDB configuration. This parameter is required only when the data source is an RDB. The current version supports only the RDB as the data source. If this parameter is not specified, the API returns error code 1021400001. |
Knowledge processing configuration.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DataAugmentation.KnowledgeProcessor
Since: 6.1.0(23)
| Name | Type | Mandatory | Description |
|---|---|---|---|
| mode | KnowledgeProcessMode | Yes | Knowledge processing mode. Inverted index or inverted index + vectorization modes are supported. |
chat(query: string): Promise<string>
Defines the function types encapsulated by the API caller for the expected large model Q&A process. This API uses a promise to return the result.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DataAugmentation.KnowledgeProcessor
Since: 26.0.0
Parameters:
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| query | string | Yes | Question information used to interact with the LLM Q&A model. |
Returns:
| Type | Mandatory | Description |
|---|---|---|
| Promise<string> | Yes | Promise used to return the Q&A result. If an exception occurs, an empty string is returned. |
Manages the knowledge processing mode.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DataAugmentation.KnowledgeProcessor
Since: 6.1.0(23)
| Name | Value | Description |
|---|---|---|
| INVERTED_INDEX | 1 | Inverted index processing mode. |
| INVERTED_INDEX_VECTORIZATION | 3 | Inverted index + vectorization processing mode. |
Describes a knowledge processing object used for obtaining the knowledge processing status.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DataAugmentation.KnowledgeProcessor
Since: 6.0.0(20)
getStatus(): Promise<ProcessorStatus>
Obtains the knowledge processing status. This API uses a promise to return the result.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DataAugmentation.KnowledgeProcessor
Since: 6.0.0(20)
Returns:
| Type | Description |
|---|---|
| Promise<ProcessorStatus> | Promise used to return the knowledge processing status. |
Error codes:
| Error Code | Error Message |
|---|---|
| 1021400000 | Internal error. |
| 1021400004 | An error occurred during operations on the RDB source. |
Example:
import { knowledgeProcessor } from '@kit.DataAugmentationKit';
import { common } from '@kit.AbilityKit';
import { relationalStore } from '@kit.ArkData';
const storeName: string = "testmail_store.db";
const storeConfig: relationalStore.StoreConfig = {
name: storeName, // Name of the database where knowledge processing is triggered.
securityLevel: relationalStore.SecurityLevel.S3,
tokenizer: relationalStore.Tokenizer.CUSTOM_TOKENIZER,
};
const knowledgeSourceConfig: knowledgeProcessor.KnowledgeSourceConfig = {
rdbSource: storeConfig,
};
const knowledgeProcessorConfig: knowledgeProcessor.KnowledgeProcessorConfig = {
sourceConfig: knowledgeSourceConfig,
};
// Asynchronous function for obtaining the knowledge processing status.
async function getStatus() {
const context = AppStorage.get<common.UIAbilityContext>("Context") as common.UIAbilityContext;
try {
// Obtain the knowledge processing object.
const processor = await knowledgeProcessor.getKnowledgeProcessor(context, knowledgeProcessorConfig);
// Obtain the knowledge processing status.
const status: knowledgeProcessor.ProcessorStatus = await processor.getStatus();
return status;
} catch (err) {
console.error("Error: " + err.message + " code: " + err.code);
return undefined;
}
} startProcess(config: KnowledgeProcessConfig): Promise<void>
Starts knowledge processing based on the input parameter configurations. This API uses a promise to return the result.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DataAugmentation.KnowledgeProcessor
Since: 6.1.0(23)
Parameters:
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| config | KnowledgeProcessConfig | Yes | Knowledge processing configuration. |
Returns:
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Error codes:
| Error Code | Error Message |
|---|---|
| 801 | Device type error. |
| 1021400000 | Internal error. |
| 1021400005 | Feature already active. Function called repeatedly. |
Example:
import { knowledgeProcessor } from '@kit.DataAugmentationKit';
import { common } from '@kit.AbilityKit';
import { relationalStore } from '@kit.ArkData';
const storeName: string = "testmail_store.db";
const storeConfig: relationalStore.StoreConfig = {
name: storeName, // Name of the database where knowledge processing is triggered.
securityLevel: relationalStore.SecurityLevel.S3,
tokenizer: relationalStore.Tokenizer.CUSTOM_TOKENIZER,
};
const knowledgeSourceConfig: knowledgeProcessor.KnowledgeSourceConfig = {
rdbSource: storeConfig,
};
const knowledgeProcessorConfig: knowledgeProcessor.KnowledgeProcessorConfig = {
sourceConfig: knowledgeSourceConfig,
};
// Asynchronous function for starting knowledge processing.
async function startProcess() {
const context = AppStorage.get<common.UIAbilityContext>("Context") as common.UIAbilityContext;
try {
// Obtain the knowledge processing object.
const processor = await knowledgeProcessor.getKnowledgeProcessor(context, knowledgeProcessorConfig);
// Start knowledge processing.
let processMode: knowledgeProcessor.KnowledgeProcessMode = knowledgeProcessor.KnowledgeProcessMode.INVERTED_INDEX;
let config: knowledgeProcessor.KnowledgeProcessConfig = {
mode: processMode,
};
await processor.startProcess(config);
} catch (err) {
console.error("Error: " + err.message + " code: " + err.code);
}
} stopProcess(): Promise<void>
Stops the current knowledge processing. This API uses a promise to return the result.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DataAugmentation.KnowledgeProcessor
Since: 6.1.0(23)
Returns:
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Example:
import { knowledgeProcessor } from '@kit.DataAugmentationKit';
import { common } from '@kit.AbilityKit';
import { relationalStore } from '@kit.ArkData';
const storeName: string = "testmail_store.db";
const storeConfig: relationalStore.StoreConfig = {
name: storeName, // Name of the database where knowledge processing is triggered.
securityLevel: relationalStore.SecurityLevel.S3,
tokenizer: relationalStore.Tokenizer.CUSTOM_TOKENIZER,
};
const knowledgeSourceConfig: knowledgeProcessor.KnowledgeSourceConfig = {
rdbSource: storeConfig,
};
const knowledgeProcessorConfig: knowledgeProcessor.KnowledgeProcessorConfig = {
sourceConfig: knowledgeSourceConfig,
};
// Asynchronous function for stopping knowledge processing.
async function stopProcess() {
const context = AppStorage.get<common.UIAbilityContext>("Context") as common.UIAbilityContext;
try {
// Obtain the knowledge processing object.
const processor = await knowledgeProcessor.getKnowledgeProcessor(context, knowledgeProcessorConfig);
// Stop knowledge processing.
await processor.stopProcess();
} catch (err) {
console.error("Error: " + err.message + " code: " + err.code);
}
} Obtains the knowledge processing status.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DataAugmentation.KnowledgeProcessor
Since: 6.0.0(20)
| Name | Value | Description |
|---|---|---|
| DATA_REMAINING_TO_PROCESS | 0 | Indicates that there is data to be processed. |
| DATA_PROCESSING_IN_PROGRESS | 1 | Indicates that data is being processed. |
| DATA_PROCESSING_COMPLETE | 2 | Indicates that all data has been processed. |