WeChat Mini Program Basic Library API
Starting from version 3.7.1, WeChat Mini Program's basic library includes built-in CloudBase AI+ capabilities that developers can access directly through wx.cloud.extend.AI.
Initialization
Before using the basic library's AI+ capabilities, you need to initialize with the CloudBase environment.
wx.cloud.init({
env: "your-env-id"
});
After initialization, you can use CloudBase AI+ capabilities through wx.cloud.extend.AI.
Large Models
AI.createModel()
Creates a specified AI model.
Usage Example
const model = wx.cloud.extend.AI.createModel("cloudbase");
Type Declaration
function createModel(model: string): ChatModel;
Returns an object that provides AI text generation capability.
ChatModel.streamText()
Call large models with streaming to generate text. During streaming calls, generated text and other response data are returned via SSE. This interface provides different levels of encapsulation for SSE, allowing developers to obtain text streams and complete data streams according to actual needs.
Usage Example
const hy = wx.cloud.extend.AI.createModel("cloudbase"); // Create model
const res = await hy.streamText({
data: {
model: "hy3-preview",
messages: [
{
role: "user",
content: "hi"
}
]
}
});
for await (let str of res.textStream) {
console.log(str); // Print generated text
}
for await (let event of res.eventStream) {
console.log(event); // Print complete data for each return
// When the large model finishes transmitting, it usually sends [DONE] data, you can stop the loop after that
if (event.data === "[DONE]") {
break;
}
}
Type Declaration
function streamText(props: StreamTextInput): Promise<StreamTextResult>;
interface StreamTextResult {
eventStream: EventStream;
textStream: TextStream;
}
interface StreamTextInput {
data: unknown;
onEvent?: OnEvent;
onText?: OnText;
onFinish?: OnFinish;
}
interface OnEvent {
(prop: { data: string }): unknown;
}
interface OnText {
(text: string): unknown;
}
interface OnFinish {
(text: string): unknown;
}
interface EventStream {
[Symbol.asyncIterator](): AsyncIterator<{
event?: unknown;
id?: unknown;
data: string;
}>;
}
interface TextStream {
[Symbol.asyncIterator](): AsyncIterator<string>;
}
Parameters
| Parameter Name | Required | Type | Example | Description |
|---|---|---|---|---|
| props.data | Yes | unknown | {model: "deepseek-v4-flash", messages: [{ role: "user", content: "Hello, introduce Li Bai" }]} | Parameters for calling each large model differ. Pass the correct parameters according to the actual model being called. |
| props.tools | No | object | -- | Tool calling related parameters |
| props.tools.autoExecute | No | boolean | true | Whether to automatically call tools, default true |
| props.tools.maxStep | No | number | 10 | Maximum times to auto-execute, default 10 |
| props.tools.list | No | Array | -- | Tool list |
props.tools.list[n].name | Yes | string | "get_weather" | Tool name |
props.tools.list[n].description | No | string | Returns temperature information for a city on a specific day. Example: get_weather({city: 'Beijing',date: '03-19'}) | Tool description |
props.tools.list[n].fn | Yes | function | ({ city, date }) => city + " on " + date + " temperature is: " + (20 + (Math.random() * 10)) | Callback function executed by the tool |
props.tools.list[n].parameters | No | {"type":"object","properties":{"city":{"type":"string","description":"city to query"},"date":{"type":"string","description":"date to query"}},"required":["city","date"]} | -- | Schema definition for input parameters of the tool execution callback function |
| props.tools.onToolEvent | No | function | console.warn | Listen for events when the large model executes tool_call |
| props.onText | No | (text: string) => unknown; | (text) => console.log(text) | Callback function triggered when new text is received, parameter is incremental text |
| props.onEvent | No | (prop: { data: string }) => unknown; | ({data}) => console.log(data) | Callback function triggered when new event is received, parameter is event, prop.data is the data contained in this event |
| props.onFinish | No | (text: string) => unknown; | (text) => console.log(text) | Callback function triggered when this call is completely finished, parameter is the complete text returned by this call |
Return Value
| StreamTextResult Property | Type | Description |
|---|---|---|
| textStream | AsyncIterable<string> | Streamed large model generated text. Refer to usage examples to get incremental generated text. |
| eventStream | AsyncIterable<{data: string, event?: unknown, id?: unknown}> | Streamed large model response data. Can get incremental data generated. Since different model responses vary, use reasonably based on actual situation. |
ChatModel.generateText()
Call large models to generate text.
Usage Example
const hy = wx.cloud.extend.AI.createModel("cloudbase"); // Create model
const res = await hy.generateText({
model: "hy3-preview",
messages: [{ role: "user", content: "Hello" }],
});
console.log(res);
// {
// "id": "27dae91f4e9a4777782c61f89acf8ea4",
// "object": "chat.completion",
// "created": 1737602298,
// "model": "hy3-preview",
// "system_fingerprint": "",
// "choices": [
// {
// "index": 0,
// "message": {
// "role": "assistant",
// "content": "Hello! Nice to chat with you. Is there anything I can help you with? Whether it's about life, work, learning or other aspects, I'll try my best to help you."
// },
// "finish_reason": "stop"
// }
// ],
// "usage": {
// "prompt_tokens": 3,
// "completion_tokens": 33,
// "total_tokens": 36
// },
// "note": "The above content is AI generated, does not represent the developer's position, please do not delete or modify this mark"
// }
console.log(res.choices[0].message.content);
// Hello! Nice to chat with you. Is there anything I can help you with? Whether it's about life, work, learning or other aspects, I'll try my best to help you.
Type Declaration
function generateText(data: unknown): Promise<unknown>;
Parameters
| Parameter Name | Required | Type | Example | Description |
|---|---|---|---|---|
| data | Yes | unknown | {model: "deepseek-v4-flash", messages: [{ role: "user", content: "Hello, introduce Li Bai" }]} | Parameters for calling each large model differ. Pass the correct parameters according to the actual model being called. |
Return Value
This interface directly returns the actual response from calling the large model. You need to parse the needed data according to the actual response content. See the usage example above.