Skip to main content

Initialize the SDK

web end 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 it also supports importing by modules.
import cloudbase from "@cloudbase/js-sdk";

const app = cloudbase.init({
env: env: "your-env", // Replace with the 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 Client-Side Initialization

WeChat Mini Program basic library and AI SDK, which one should I use?

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, offering a consistent user experience across various platforms. If you require cross-platform compatibility, you can opt for the AI SDK.

However, using the AI SDK in Mini Programs is subject to certain limitations:

  1. The Mini Program package size is limited. Introducing the AI SDK will increase the package size to some extent.
  2. Mini Programs require server domain name configuration. After configuration is complete, the AI SDK can make requests.

The WeChat Mini Program basic library comes with built-in Cloud Development AI capabilities, which do not occupy package size and require no server domain configuration. For developers focused on Mini Programs, using the WeChat Mini Program basic library is a recommended choice. Without installing any external packages, you can fully access the AI+ capabilities provided by Cloud Development.

The WeChat Mini Program basic library and the AI SDK have distinctions in usage. Please refer to the documentation for details.

Using the WeChat Mini Program Basic Library

wx.cloud.init({
env: 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.

Use the AI SDK

// Before you begin, ensure you have configured the legitimate request domain for the Mini Program. For details, refer to https://docs.cloudbase.net/quick-start/baas/wx-mini
// In the directory containing the mini-program's package.json (typically the miniprogram directory), run 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 a per-module basis as needed, and it also supports full import.
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: env: "your-env", // Replace with the 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.

Node.js Cloud Function Initialization

The runtime environment for cloud functions is Node.js. Before using the AI SDK in cloud functions to access large models and Agent, the following initialization work needs to be completed.

Node.js Version Requirement

Node.js requires version 18 or above. Please ensure to select a Node.js version that meets the requirements when creating a new cloud function.

// 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 it also supports importing by modules.
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: env: "your-env", // Replace with the actual environment id
});
/**
* When initializing auth, pass storage and captchaOptions.openURIWithCallback
* Otherwise, the default will be used, which is platform-dependent and may cause errors in the nodejs 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();
// You can now call the methods provided by the ai module.
};

Initializing in WeDa

In WeDa applications, you can obtain a cloudbase instance via $w.cloud.getCloudInstance without worrying about login or other logic.

async function initAi() {
const app = await $w.cloud.getCloudInstance();
const ai = app.ai();
// You can now call the methods provided by the ai module.
}

After Initialization

After completing the SDK initialization, you can start utilizing various capabilities of AI+. Refer to the following articles for usage:

The AI SDK is built on Cloudbase BaaS. Before development, it is recommended to understand various BaaS configurations such as security domains and login permissions. For details, refer to: