The localChatModel module is an on-device Q&A component in Data Augmentation Kit. It provides methods for accessing the on-device Q&A model and is applicable to application scenarios requiring on-device intelligent Q&A to enable local data processing.
You can use the init and chat methods provided by the localChatModel module to implement the preceding capability.
Since: 6.0.0(20)
Configuration of whether streaming Q&A is supported.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DataAugmentation.LocalChatModel
Since: 6.0.0(20)
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| isStream | boolean | No | No | Indicates whether streaming Q&A is supported. If this parameter is set to true, results can be returned in streaming mode, which is suitable for scenarios that require real-time response. If this parameter is set to false, complete results are returned at once, which is suitable for scenarios that require complete results and can tolerate delays. |
Example:
import { localChatModel } from '@kit.DataAugmentationKit';
let localConfig: localChatModel.Config = {
isStream: true
} Question used to interact with the on-device Q&A model.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DataAugmentation.LocalChatModel
Since: 6.0.0(20)
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| questionId | number | No | No | ID of the question that interacts with the on-device Q&A model. The value range is [0, 65535]. If the value is out of range, error code 1021900005 is reported. When the same app is running, the value of a questionId must be unique. You are advised to use an ascending sequence to generate unique IDs to prevent problems caused by duplicate IDs in concurrent requests. |
| content | string | No | No | Question content for interacting with the on-device Q&A model. Due to on-device model performance restrictions, to ensure model processing efficiency, you are advised to keep the content length within 4500 bytes and avoid redundant information. If the question is too long, the performance of some devices may deteriorate, or even the model may become abnormal. |
Example:
import { localChatModel } from '@kit.DataAugmentationKit';
let questionInfo: localChatModel.QuestionInfo = {
questionId: 1,
content: "Question content"
} Result returned after interaction with the on-device Q&A model.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DataAugmentation.LocalChatModel
Since: 6.0.0(20)
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| questionId | number | No | No | Question ID corresponding to the result returned by the Q&A model, which is the same as the value of questionId. |
| content | string | No | No | Answer to the question corresponding to questionId, which is provided by the Q&A model. |
| isFinished | boolean | No | No | Whether the result is complete. The value true indicates that all results have been returned and the current Q&A session ends. The value false indicates that there are subsequent results and the current callback returns incremental content. |
Example:
import { localChatModel } from '@kit.DataAugmentationKit';
let answer: localChatModel.Answer = {
questionId: 1,
content: "Answer content",
isFinished: true
} init(): Promise<boolean>
Initializes the on-device Q&A model and starts the model management app. This API returns the result asynchronously through a promise.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DataAugmentation.LocalChatModel
Since: 6.0.0(20)
Returns:
| Type | Description |
|---|---|
| Promise<boolean> | Promise object, which returns the model initialization result. The value true indicates that the initialization is successful and the model is ready. The value false indicates that the initialization fails and the model is unavailable. |
Example:
import { localChatModel } from '@kit.DataAugmentationKit';
async function init() {
try {
const result = await localChatModel.init();
console.info('init result: ', result);
} catch (err) {
console.error('init err: ', err);
}
} chat(info: QuestionInfo, config: Config, callback: AsyncCallback<Answer>): Promise<void>
Sends a question to the on-device Q&A model and obtains the answer. This method is applicable to local intelligent Q&A scenarios and uses a promise to return the result asynchronously.
Before calling the chat method, you must call the init method to initialize the model to ensure that the on-device Q&A model is available. If the model is not initialized, the chat method will throw the error code 1021900002 because the model is not ready.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DataAugmentation.LocalChatModel
Since: 6.0.0(20)
Parameters:
| Name | Type | Mandatory | Description |
|---|---|---|---|
| info | QuestionInfo | Yes | Question information used to interact with the on-device Q&A model. |
| config | Config | Yes | Question configuration item. |
| callback | AsyncCallback<Answer> | Yes | Callback used to return the Q&A result. |
Returns:
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Error codes:
For details about the error codes, please refer to ArkTS API Error Codes.
| ID | Error Message |
|---|---|
| 1021900001 | A timeout occurs when the local chat model is called. |
| 1021900002 | A loading failure occurs when the local chat model is called. |
| 1021900003 | A request failure occurs when the local chat model is called. |
| 1021900004 | The local chat model is busy. |
| 1021900005 | Some parameters do not meet the specified constraints. |
Example:
import { BusinessError } from "@kit.BasicServicesKit";
import { localChatModel } from '@kit.DataAugmentationKit';
async function chat() {
// `questionId` is used to identify the question ID, and `content` is used to set the question content.
let questionInfo: localChatModel.QuestionInfo = {
questionId: 1,
content: "You are a translation assistant that can translate multiple languages. Please translate the following content into 36 languages: PC model manager."
}
// `isStream: true` indicates that streaming results are enabled, which is suitable for real-time response scenarios.
let localConfig: localChatModel.Config = {
isStream: true
}
let localChatCallback = async (err: BusinessError, ans: localChatModel.Answer): Promise<void> => {
if (err) {
// Error code related to model running.
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
return;
}
if (!ans) {
console.error('Received null or undefined answer. Skipping processing.');
return;
}
// `questionId` indicates the question ID, `content` indicates the answer content, and `isFinished` indicates whether the Q&A result is complete. The value true indicates that all results have been returned.
console.info('questionId: ' + ans.questionId + ', content: ' + ans.content + ', isFinished: ' + ans.isFinished);
};
try {
await localChatModel.chat(questionInfo, localConfig, localChatCallback);
} catch (err) {
// Error code related to input parameters.
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
}