Initialize the SDK
web Initialization
// In the root directory of the Web project, use npm or yarn to install the required packages:
// npm i @cloudbase/js-sdk
// Import the SDK. Here we import the full clousebase-js-sdk, and modular imports are also supported.
import cloudbase from "@cloudbase/js-sdk";
const app = cloudbase.init({
env: "your-env", // Replace with your actual environment id
});
const auth = app.auth();
await auth.signInAnonymously(); // Or use other login methods.
const ai = app.ai();
// Now you can call the methods provided by the ai module.
Mini Program Initialization
The AI capabilities provided by the AI SDK and the WeChat Mini Program Basic Library are identical.
The AI SDK is a multi-platform SDK provided by cloud development, delivering a consistent user experience across various platforms. If you require a single codebase for multiple platforms, you may opt for the AI SDK.
However, using the AI SDK on Mini Programs is subject to certain limitations:
- The Mini Program package size is limited, and introducing the AI SDK will increase the package size to some extent.
- The Mini Program requires server domain name configuration, and only after this can the AI SDK make requests.
The WeChat Mini Program Basic Library has built-in CloudBase AI capabilities, which do not occupy package size nor require server domain name configuration. For developers focused on Mini Programs, using the WeChat Mini Program Basic Library is a good choice. Without installing any external packages, they can fully access the AI+ capabilities provided by CloudBase.
The WeChat Mini Program Basic Library and the AI SDK differ in usage. Please refer to the documentation.
Using the WeChat Mini Program Basic Library
wx.cloud.init({
env: "lowcode-2gp2855c5ce22e35", // Specify the CloudBase environment ID
});
const ai = wx.cloud.extend.AI;
// Now you can call the methods provided by the ai module.
Using the AI SDK
// Before starting, ensure that the Mini Program request valid domain has been configured. For details, refer to https://docs.cloudbase.net/quick-start/baas/wx-mini
// In the directory where the mini-program's package.json is located (usually the miniprogram directory), execute the command to install the npm package:
// npm i @cloudbase/js-sdk
// After installation is complete, click "Tools -> Build npm" in the menu bar of the WeChat Developer Tools.
// Import the SDK. Here we import cloudbase-js-sdk on demand in a modular form, and full import is also supported.
import cloudbase from "@cloudbase/js-sdk/app";
import { registerAuth } from "@cloudbase/js-sdk/auth";
import { registerAi } from "@cloudbase/js-sdk/ai";
registerAuth(cloudbase);
registerAi(cloudbase);
const app = cloudbase.init({
env: "your-env", // Replace with your actual environment id
});
const auth = app.auth({ persistence: "local" });
await auth.signInWithOpenId(); // Or use other login methods.
const ai = app.ai();
// Now you can call the methods provided by the ai module.
Cloud Function Node.js Client Initialization
The runtime environment for Cloud Functions is Node.js. Before using the AI SDK to access large models and Agent in Cloud Functions, the following initialization tasks need to be completed.
Node.js version 18 or above is required. When creating a new cloud function, please ensure to select a Node.js version that meets this requirement.
// In the root directory of the Node.js project, use npm or yarn to install the required packages:
// npm i @cloudbase/js-sdk
// npm i @cloudbase/adapter-node
// Import the SDK. Here we import the full clousebase-js-sdk, and modular imports are also supported.
const cloudbase = require("@cloudbase/js-sdk");
// Import the node.js adapter. For details, refer to https://docs.cloudbase.net/api-reference/webv3/adapter#%E4%B8%80%E5%A5%97%E4%BB%A3%E7%A0%81%E5%A4%9A%E7%AB%AF%E9%80%82%E9%85%8D
const adapter = require("@cloudbase/adapter-node");
const { sessionStorage } = adapter.genAdapter();
cloudbase.useAdapters(adapter);
const app = cloudbase.init({
env: "your-env", // Replace with your actual environment id
});
/**
* During auth initialization, you need to pass storage and captchaOptions.openURIWithCallback.
* Otherwise, it will use the default platform-dependent behavior and throw an error in the Node.js environment.
*/
const auth = app.auth({
storage: sessionStorage,
captchaOptions: {
openURIWithCallback: () => console.log("open uri with callback"),
},
});
exports.main = async (event, context) => {
await auth.signInAnonymously(); // Or use other login methods.
const ai = app.ai();
// Now you can call the methods provided by the ai module.
};
Initialization in WeDa
In WeDa applications, you can obtain a cloudbase instance via $w.cloud.getCloudInstance
without having to worry about login logic or other related concerns.
async function initAi() {
const app = await $w.cloud.getCloudInstance();
const ai = app.ai();
// Now you can call the methods provided by the ai module.
}
After Initialization
After completing SDK initialization, you can start using various capabilities of AI+. Refer to the following articles for usage:
Reference Links
The AI SDK is built on Cloudbase BaaS. Before development, it is recommended to understand various configurations of BaaS, such as security domains, login permissions, etc. For details, refer to: