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("hunyuan-exp");
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("hunyuan-exp"); // Create model
const res = await hy.streamText({
data: {
model: "hunyuan-turbos-latest",
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: "hunyuan-turbos-latest", 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("hunyuan-exp"); // Create model
const res = await hy.generateText({
model: "hunyuan-turbos-latest",
messages: [{ role: "user", content: "Hello" }],
});
console.log(res);
// {
// "id": "27dae91f4e9a4777782c61f89acf8ea4",
// "object": "chat.completion",
// "created": 1737602298,
// "model": "hunyuan-turbos-latest",
// "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: "hunyuan-turbos-latest", 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.
Agent
AI.bot.get()
Get information about an Agent.
Usage Example
await wx.cloud.extend.AI.bot.get({ botId: "botId-xxx" });
Type Declaration
function get(props: { botId: string });
Parameters
| Parameter Name | Required | Type | Description |
|---|---|---|---|
| props.botId | Yes | string | The id of the Agent to get information about |
Return Value
| Property Name | Type | Example | Description |
|---|---|---|---|
| botId | string | "bot-27973647" | Agent ID |
| name | string | "Translation" | Agent name |
| introduction | string | Agent introduction | |
| welcomeMessage | string | Agent welcome message | |
| avatar | string | Agent avatar link | |
| background | string | Agent chat background image link | |
| isNeedRecommend | boolean | Whether Agent recommends questions after answering | |
| type | string | Agent type |
AI.bot.list()
Get information about multiple Agents in bulk.
Usage Example
await wx.cloud.extend.AI.bot.list({
pageNumber: 1,
pageSize: 10,
name: "",
enable: true,
information: "",
introduction: "",
});
Type Declaration
function list(props: {
name: string;
introduction: string;
information: string;
enable: boolean;
pageSize: number;
pageNumber: number;
});
Parameters
| Parameter Name | Required | Type | Description |
|---|---|---|---|
| props.pageNumber | Yes | number | Page number |
| props.pageSize | Yes | number | Page size |
| props.enable | Yes | boolean | Whether Agent is enabled |
| props.name | Yes | string | Agent name for fuzzy query |
| props.information | Yes | string | Agent information for fuzzy query |
| props.introduction | Yes | string | Agent introduction for fuzzy query |
Return Value
| Property Name | Type | Example | Description |
|---|---|---|---|
| total | number | --- | Total number of Agents |
| botList | Array<object> | Agent list | |
botList[n].botId | string | "bot-27973647" | Agent ID |
botList[n].name | string | "Translation" | Agent name |
botList[n].introduction | string | Agent introduction | |
botList[n].welcomeMessage | string | Agent welcome message | |
botList[n].avatar | string | Agent avatar link | |
botList[n].background | string | Agent chat background image link | |
botList[n].isNeedRecommend | boolean | Whether Agent recommends questions after answering | |
botList[n].type | string | Agent type |
AI.bot.sendMessage()
Chat with an Agent.
Usage Example
const res = await wx.cloud.extend.AI.bot.sendMessage({
data: {
botId: 'xxx-bot-id',
msg: "Who are you"
}
})
for await (let x of res.textStream) {
console.log(x)
}
Type Declaration
function sendMessage(props: {
data: {
botId: string;
msg: string;
history: Array<{
role: string;
content: string;
}>;
files?: Array<string>,
},
onText: OnText,
onEvent: OnEvent,
onFinish: OnFinish
});
Parameters
| Parameter Name | Required | Type | Description |
|---|---|---|---|
| props.data.botId | Yes | string | Agent id |
| props.data.msg | Yes | string | Message to send in this conversation |
| props.data.history | Yes | boolean | Chat history before this conversation |
props.data.history[n].role | Yes | string | Role of this chat message sender |
props.data.history[n].content | Yes | string | Content of this chat message |
props.data.files | No | Array<string> | Files attached to this message, need to fill in cloud storage file cloud id |
| props.onText | No | (text: string) => unknown; | Callback function triggered when new text is received, parameter is incremental text |
| props.onEvent | No | (prop: { data: string }) => unknown; | Callback function triggered when new event is received, parameter is event, prop.data is the string of data contained in this event |
| props.onFinish | No | (text: string) => unknown; | Callback function triggered when this call is completely finished, parameter is the complete text returned by this call |
AI.bot.getChatRecords()
Get chat history.
Usage Example
await wx.cloud.extend.AI.bot.getChatRecords({
botId: "botId-xxx",
pageNumber: 1,
pageSize: 10,
sort: "asc",
});
Type Declaration
function getChatRecords(props: {
botId: string;
sort: string;
pageSize: number;
pageNumber: number;
});
Parameters
| Parameter Name | Required | Type | Description |
|---|---|---|---|
| props.botId | Yes | string | Agent id |
| props.sort | Yes | string | Sort method |
| props.pageSize | Yes | number | Page size |
| props.pageNumber | Yes | number | Page number |
Return Value
| Property Name | Type | Description |
|---|---|---|
| total | number | Total conversations |
| recordList | Array<object> | Conversation list |
recordList[n].botId | string | Agent ID |
recordList[n].recordId | string | Conversation ID, generated by system |
recordList[n].role | string | Role in conversation |
recordList[n].content | string | Conversation content |
recordList[n].conversation | string | User identifier |
recordList[n].type | string | Conversation data type |
recordList[n].image | string | Image link generated in conversation |
recordList[n].triggerSrc | string | Conversation trigger source |
recordList[n].replyTo | string | Record ID being replied to |
recordList[n].createTime | string | Conversation time |
AI.bot.sendFeedback()
Send feedback for a chat record.
Usage Example
const res = await wx.cloud.extend.AI.bot.sendFeedback({
userFeedback: {
botId: "botId-xxx",
recordId: "recordId-xxx",
comment: "Excellent",
rating: 5,
tags: ["Beautiful"],
aiAnswer: "Falling petals in profusion",
input: "Give me an idiom",
type: "upvote",
},
});
Type Declaration
function sendFeedback(props: { userFeedback: IUserFeedback, botId: string });
Parameters
| Parameter Name | Required | Type | Description |
|---|---|---|---|
| props.userFeedback | Yes | IUserFeedback | User feedback, see IUserFeedback type definition |
| props.botId | Yes | string | Agent id to provide feedback for |
AI.bot.getFeedBack()
Get existing feedback information.
Usage Example
const res = await wx.cloud.extend.AI.bot.getFeedBack({
botId: "botId-xxx",
from: 0,
to: 0,
maxRating: 4,
minRating: 3,
pageNumber: 1,
pageSize: 10,
sender: "user-a",
senderFilter: "include",
type: "upvote",
});
Type Declaration
function getFeedBack(props: {
botId: string;
type: string;
sender: string;
senderFilter: string;
minRating: number;
maxRating: number;
from: number;
to: number;
pageSize: number;
pageNumber: number;
});
Parameters
| Parameter Name | Required | Type | Description |
|---|---|---|---|
| props.botId | Yes | string | Agent id |
| props.type | Yes | string | User feedback type, upvote or downvote |
| props.sender | Yes | string | User who created comment |
| props.senderFilter | Yes | string | User filter relation include:contains exclude:not contains equal:equals unequal:not equals prefix:prefix |
| props.minRating | Yes | number | Minimum rating |
| props.maxRating | Yes | number | Maximum rating |
| props.from | Yes | number | Start timestamp |
| props.to | Yes | number | End timestamp |
| props.pageSize | Yes | number | Page size |
| props.pageNumber | Yes | number | Page number |
Return Value
| Property Name | Type | Description |
|---|---|---|
| feedbackList | object[] | Feedback query results |
| feedbackList[n].recordId | string | Chat record ID |
| feedbackList[n].type | string | User feedback type, upvote or downvote |
| feedbackList[n].botId | string | Agent ID |
| feedbackList[n].comment | string | User comment |
| feedbackList[n].rating | number | User rating |
| feedbackList[n].tags | string[] | User feedback tags array |
| feedbackList[n].input | string | User input question |
| feedbackList[n].aiAnswer | string | Agent's answer |
| total | number | Total feedback |
AI.bot.getRecommendQuestions()
Get recommended questions.
Usage Example
const res = await wx.cloud.extend.AI.bot.getRecommendQuestions({
data: {
botId: "xxx-bot-id",
msg: "Who are you"
}
})
for await (let x of res.textStream) {
console.log(x)
}
Type Declaration
function getRecommendQuestions(props: {
data: {
botId: string;
name: string;
introduction: string;
agentSetting: string;
msg: string;
history: Array<{
role: string;
content: string;
}>;
},
onText: OnText,
onEvent: OnEvent,
onFinish: OnFinish
});
Parameters
| Parameter Name | Required | Type | Description |
|---|---|---|---|
| props.data.botId | Yes | string | Agent id |
| props.data.name | Yes | string | Agent name |
| props.data.introduction | Yes | string | Agent introduction |
| props.data.agentSetting | Yes | string | Agent settings |
| props.data.msg | Yes | string | User sent message |
| props.data.history | Yes | Array | Historical conversation info |
props.data.history[n].role | Yes | string | Role of historical message |
props.data.history[n].content | Yes | string | Content of historical message |
| props.onText | No | (text: string) => unknown; | Callback function triggered when new text is received, parameter is incremental text |
| props.onEvent | No | (prop: { data: string }) => unknown; | Callback function triggered when new event is received, parameter is event, prop.data is the string of data contained in this event |
| props.onFinish | No | (text: string) => unknown; | Callback function triggered when this call is completely finished, parameter is the complete text returned by this call |
AI.bot.uploadFiles()
Upload files from cloud storage to Agent for document chat.
Usage Example
await wx.cloud.extend.AI.bot.uploadFiles({
botId: 'bot-xx',
fileList: [{
fileId: 'cloud://xxx.pdf',
fileName: 'xxx.pdf',
type: 'file'
}]
})
Type Declaration
function uploadFiles(props: {
botId: string,
fileList: Array<{
fileId: string;
fileName: string;
type: string;
}>
});
Parameters
| Parameter Name | Required | Type | Description |
|---|---|---|---|
| props.botId | Yes | string | Agent id |
| props.fileList | Yes | Array | File list |
| props.fileList[n].fileId | Yes | string | Cloud storage cloud id of file |
| props.fileList[n].fileName | Yes | string | File name |
| props.fileList[n].type | Yes | string | Currently only supports "file" |