openharmony 鸿蒙 js-apis-data-intelligence

2025-06-12 浏览 (1)

@ohos.data.intelligence (ArkData Intelligence Platform)

The ArkData Intelligence Platform (AIP) provides on-device data intelligence capabilities, enabling AI-powered data processing data on devices. To underpin AI-powered processing on devices, the AIP aims to build the following capabilities:

  • Multimodal embedding model: leverages embedding models to generate vector representations for multimodal data, mapping text, images, and other data into a unified vector space for semantic-based multimodal knowledge retrieval.
  • Multimodal data storage: supports on-device storage for vectors, inverted indexes, and other multimodal data, eliminating the need to send raw data to the server for processing. This reduces the risk of data leakage.
  • Knowledge retrieval: enables semantic retrieval of user knowledge based on capabilities such as semantic indexing, knowledge graphs, recall, and re-ranking.
  • Knowledge generation and collation: implements efficient data organization and knowledge generation based on user data such as user documents, messages, emails, photos, videos, calendar events, and screen context, enabling the transformation of data into knowledge.

NOTE

The initial APIs of this module are supported since API version 15. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Considering the significant computing workload and resources of data vectorization processing, the APIs are only available to 2-in-1 device applications.

Modules to Import

import { intelligence } from '@kit.ArkData';

intelligence.getTextEmbeddingModel

getTextEmbeddingModel(config: ModelConfig): Promise<TextEmbedding>

Obtains a text embedding model. This API uses a promise to return the result.

System capability: SystemCapability.DistributedDataManager.DataIntelligence.Core

Parameters

NameTypeMandatoryDescription
configModelConfigYesConfiguration of the embedded model to obtain.

Return value

TypeDescription
Promise<TextEmbedding>Promise used to return the text embedding model object.

Error codes

For details about the error codes, see Common Error Codes and AIP Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
801Capability not supported.
31300000Inner error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let textConfig:intelligence.ModelConfig = {
  version:intelligence.ModelVersion.BASIC_MODEL,
  isNpuAvailable:false,
  cachePath:"/data"
}
let textEmbedding:intelligence.TextEmbedding;

intelligence.getTextEmbeddingModel(textConfig)
  .then((data:intelligence.TextEmbedding) => {
    console.info("Succeeded in getting TextModel");
    textEmbedding = data;
  })
  .catch((err:BusinessError) => {
    console.error("Failed to get TextModel and code is " + err.code);
  })

intelligence.getImageEmbeddingModel

getImageEmbeddingModel(config: ModelConfig): Promise<ImageEmbedding>

Obtains an image embedding model. This API uses a promise to return the result.

System capability: SystemCapability.DistributedDataManager.DataIntelligence.Core

Parameters

NameTypeMandatoryDescription
configModelConfigYesConfiguration of the embedded model to obtain.

Return value

TypeDescription
Promise<ImageEmbedding>Promise used to return the image embedding model object.

Error codes

For details about the error codes, see Common Error Codes and AIP Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
801Capability not supported.
31300000Inner error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let imageConfig:intelligence.ModelConfig = {
    version:intelligence.ModelVersion.BASIC_MODEL,
    isNpuAvailable:false,
    cachePath:"/data"
}
let imageEmbedding:intelligence.ImageEmbedding;

intelligence.getImageEmbeddingModel(imageConfig)
  .then((data:intelligence.ImageEmbedding) => {
    console.info("Succeeded in getting ImageModel");
    imageEmbedding = data;
  })
  .catch((err:BusinessError) => {
    console.error("Failed to get ImageModel and code is " + err.code);
  })

intelligence.splitText

splitText(text: string, config: SplitConfig): Promise<Array<string>>

Splits text.

System capability: SystemCapability.DistributedDataManager.DataIntelligence.Core

Parameters

NameTypeMandatoryDescription
textstringYesText to split, which can be any value.
configSplitConfigYesConfiguration for splitting the text.

Return value

TypeDescription
Promise<Array<string>>Promise used to return the blocks of the text.

Error codes

For details about the error codes, see Common Error Codes and AIP Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
801Capability not supported.
31300000Inner error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let splitConfig:intelligence.SplitConfig = {
  size:10,
  overlapRatio:0.1
}
let splitText = 'text';

intelligence.splitText(splitText, splitConfig)
  .then((data:Array<string>) => {
    console.info("Succeeded in splitting Text");
  })
  .catch((err:BusinessError) => {
    console.error("Failed to split Text and code is " + err.code);
  })

ModelConfig

Represents the configuration an embedded model.

System capability: SystemCapability.DistributedDataManager.DataIntelligence.Core

NameTypeMandatoryDescription
versionModelVersionYesVersion of the model.
isNpuAvailablebooleanYesWhether to use the NPU to accelerate the vectorization process. The value true means to use the NPU, and the value false means the opposite. If this parameter is set to true but the device does not support NPUs, loading an embedding model will trigger error 31300000.
cachePathstringNoLocal directory for model caching if the NPU is used. The value is in the /xxx/xxx/xxx format, for example, /data. The path cannot exceed 512 characters.
Default value: ""

ModelVersion

Enumerates the model versions.

System capability: SystemCapability.DistributedDataManager.DataIntelligence.Core

NameValueDescription
BASIC_MODEL0Basic embedding model version.

Image

type Image = string

Represents the URI of an image, which is of the string type.

System capability: SystemCapability.DistributedDataManager.RelationalStore.Core

TypeDescription
stringImage URI, which cannot exceed 512 characters.

SplitConfig

Represents the configuration for text splitting.

System capability: SystemCapability.DistributedDataManager.DataIntelligence.Core

NameTypeMandatoryDescription
sizenumberYesMaximum size of a block. The value is a non-negative integer.
overlapRationumberYesOverlap ratio between adjacent blocks.
Value range: [0,1]
The value 0 indicates the lowest overlap ratio, and 1 indicates the highest overlap ratio.

TextEmbedding

Provides APIs for manipulating text embedding models.

Before calling any of the following APIs, you must obtain a TextEmbedding instance by using intelligence.getTextEmbeddingModel.

System capability: SystemCapability.DistributedDataManager.DataIntelligence.Core

loadModel

loadModel(): Promise<void>

Loads this embedding model. This API uses a promise to return the result.

System capability: SystemCapability.DistributedDataManager.DataIntelligence.Core

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Common Error Codes and AIP Error Codes.

IDError Message
801Capability not supported.
31300000Inner error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

textEmbedding.loadModel()
  .then(() => {
    console.info("Succeeded in loading Model");
  })
  .catch((err:BusinessError) => {
    console.error("Failed to load Model and code is " + err.code);
  })

releaseModel

releaseModel(): Promise<void>

Releases this embedding model. This API uses a promise to return the result.

System capability: SystemCapability.DistributedDataManager.DataIntelligence.Core

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Common Error Codes and AIP Error Codes.

IDError Message
801Capability not supported.
31300000Inner error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

textEmbedding.releaseModel()
  .then(() => {
    console.info("Succeeded in releasing Model");
  })
  .catch((err:BusinessError) => {
    console.error("Failed to release Model and code is " + err.code);
  })

getEmbedding

getEmbedding(text: string): Promise<Array<number>>

Obtains the embedding vector of the given text.

Before calling this API, ensure that an embedding model is successfully loaded by using loadModel.

System capability: SystemCapability.DistributedDataManager.DataIntelligence.Core

Parameters

NameTypeMandatoryDescription
textstringYesText for the embedding model. It cannot exceed 512 characters.

Return value

TypeDescription
Promise<Array<number>>Promise used to return the vectorization result.

Error codes

For details about the error codes, see Common Error Codes and AIP Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
801Capability not supported.
31300000Inner error.

Example

import { BusinessError } from '@kit.BasicServicesKit';


textEmbedding.loadModel();
let text = 'text';
textEmbedding.getEmbedding(text)
  .then((data:Array<number>) => {
    console.info("Succeeded in getting Embedding");
  })
  .catch((err:BusinessError) => {
    console.error("Failed to get Embedding and code is " + err.code);
  })

getEmbedding

getEmbedding(batchTexts: Array<string>): Promise<Array<Array<number>>>

Obtains the embedding vector of a given batch of texts.

Before calling this API, ensure that an embedding model is successfully loaded by using loadModel.

System capability: SystemCapability.DistributedDataManager.DataIntelligence.Core

Parameters

NameTypeMandatoryDescription
batchTextsArray<string>YesBatch of texts, each of which cannot exceed 512 characters.

Return value

TypeDescription
Promise<Array<Array<number>>>Promise used to return the vectorization result.

Error codes

For details about the error codes, see Common Error Codes and AIP Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
801Capability not supported.
31300000Inner error.

Example

import { BusinessError } from '@kit.BasicServicesKit';


textEmbedding.loadModel();
let batchTexts = ['text1','text2'];
textEmbedding.getEmbedding(batchTexts)
  .then((data:Array<Array<number>>) => {
    console.info("Succeeded in getting Embedding");
  })
  .catch((err:BusinessError) => {
    console.error("Failed to get Embedding and code is " + err.code);
  })

ImageEmbedding

Provides APIs for manipulating image embedding models.

Before calling any of the following APIs, you must obtain a ImageEmbedding instance by using intelligence.getImageEmbeddingModel.

System capability: SystemCapability.DistributedDataManager.DataIntelligence.Core

loadModel

loadModel(): Promise<void>

Loads this embedding model. This API uses a promise to return the result.

System capability: SystemCapability.DistributedDataManager.DataIntelligence.Core

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Common Error Codes and AIP Error Codes.

IDError Message
801Capability not supported.
31300000Inner error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

imageEmbedding.loadModel()
  .then(() => {
    console.info("Succeeded in loading Model");
  })
  .catch((err:BusinessError) => {
    console.error("Failed to load Model and code is " + err.code);
  })

releaseModel

releaseModel(): Promise<void>

Releases this embedding model. This API uses a promise to return the result.

System capability: SystemCapability.DistributedDataManager.DataIntelligence.Core

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Common Error Codes and AIP Error Codes.

IDError Message
801Capability not supported.
31300000Inner error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

imageEmbedding.releaseModel()
  .then(() => {
    console.info("Succeeded in releasing Model");
  })
  .catch((err:BusinessError) => {
    console.error("Failed to release Model and code is " + err.code);
  })

getEmbedding

getEmbedding(image: Image): Promise<Array<number>>

Obtains the embedding vector of the given image.

Before calling this API, ensure that an embedding model is successfully loaded by using loadModel.

System capability: SystemCapability.DistributedDataManager.DataIntelligence.Core

Parameters

NameTypeMandatoryDescription
imageImageYesURI of the target image.

Return value

TypeDescription
Promise<Array<number>>Promise used to return the vectorization result.

Error codes

For details about the error codes, see Common Error Codes and AIP Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
801Capability not supported.
31300000Inner error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

imageEmbedding.loadModel();
let image = 'file://<packageName>/data/storage/el2/base/haps/entry/files/xxx.jpg';
imageEmbedding.getEmbedding(image)
  .then((data:Array<number>) => {
    console.info("Succeeded in getting Embedding");
  })
  .catch((err:BusinessError) => {
    console.error("Failed to get Embedding and code is " + err.code);
  })

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkData (ArkData Management)

harmony 鸿蒙Data

harmony 鸿蒙OH_Cursor

harmony 鸿蒙OH_Predicates

harmony 鸿蒙OH_Rdb_Config

harmony 鸿蒙OH_Rdb_Store

harmony 鸿蒙OH_VBucket

harmony 鸿蒙OH_VObject

harmony 鸿蒙Preferences

harmony 鸿蒙_r_d_b

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/RWwcgH