Skip to main content

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 NameRequiredTypeExampleDescription
props.dataYesunknown{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.toolsNoobject--Tool calling related parameters
props.tools.autoExecuteNobooleantrueWhether to automatically call tools, default true
props.tools.maxStepNonumber10Maximum times to auto-execute, default 10
props.tools.listNoArray--Tool list
props.tools.list[n].nameYesstring"get_weather"Tool name
props.tools.list[n].descriptionNostringReturns temperature information for a city on a specific day. Example: get_weather({city: 'Beijing',date: '03-19'})Tool description
props.tools.list[n].fnYesfunction({ city, date }) => city + " on " + date + " temperature is: " + (20 + (Math.random() * 10))Callback function executed by the tool
props.tools.list[n].parametersNo{"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.onToolEventNofunctionconsole.warnListen for events when the large model executes tool_call
props.onTextNo(text: string) => unknown;(text) => console.log(text)Callback function triggered when new text is received, parameter is incremental text
props.onEventNo(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.onFinishNo(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 PropertyTypeDescription
textStreamAsyncIterable<string>Streamed large model generated text. Refer to usage examples to get incremental generated text.
eventStreamAsyncIterable<{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 NameRequiredTypeExampleDescription
dataYesunknown{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 NameRequiredTypeDescription
props.botIdYesstringThe id of the Agent to get information about

Return Value

Property NameTypeExampleDescription
botIdstring"bot-27973647"Agent ID
namestring"Translation"Agent name
introductionstringAgent introduction
welcomeMessagestringAgent welcome message
avatarstringAgent avatar link
backgroundstringAgent chat background image link
isNeedRecommendbooleanWhether Agent recommends questions after answering
typestringAgent 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 NameRequiredTypeDescription
props.pageNumberYesnumberPage number
props.pageSizeYesnumberPage size
props.enableYesbooleanWhether Agent is enabled
props.nameYesstringAgent name for fuzzy query
props.informationYesstringAgent information for fuzzy query
props.introductionYesstringAgent introduction for fuzzy query

Return Value

Property NameTypeExampleDescription
totalnumber---Total number of Agents
botListArray<object>Agent list
botList[n].botIdstring"bot-27973647"Agent ID
botList[n].namestring"Translation"Agent name
botList[n].introductionstringAgent introduction
botList[n].welcomeMessagestringAgent welcome message
botList[n].avatarstringAgent avatar link
botList[n].backgroundstringAgent chat background image link
botList[n].isNeedRecommendbooleanWhether Agent recommends questions after answering
botList[n].typestringAgent 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 NameRequiredTypeDescription
props.data.botIdYesstringAgent id
props.data.msgYesstringMessage to send in this conversation
props.data.historyYesbooleanChat history before this conversation
props.data.history[n].roleYesstringRole of this chat message sender
props.data.history[n].contentYesstringContent of this chat message
props.data.filesNoArray<string>Files attached to this message, need to fill in cloud storage file cloud id
props.onTextNo(text: string) => unknown;Callback function triggered when new text is received, parameter is incremental text
props.onEventNo(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.onFinishNo(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 NameRequiredTypeDescription
props.botIdYesstringAgent id
props.sortYesstringSort method
props.pageSizeYesnumberPage size
props.pageNumberYesnumberPage number

Return Value

Property NameTypeDescription
totalnumberTotal conversations
recordListArray<object>Conversation list
recordList[n].botIdstringAgent ID
recordList[n].recordIdstringConversation ID, generated by system
recordList[n].rolestringRole in conversation
recordList[n].contentstringConversation content
recordList[n].conversationstringUser identifier
recordList[n].typestringConversation data type
recordList[n].imagestringImage link generated in conversation
recordList[n].triggerSrcstringConversation trigger source
recordList[n].replyTostringRecord ID being replied to
recordList[n].createTimestringConversation 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 NameRequiredTypeDescription
props.userFeedbackYesIUserFeedbackUser feedback, see IUserFeedback type definition
props.botIdYesstringAgent 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 NameRequiredTypeDescription
props.botIdYesstringAgent id
props.typeYesstringUser feedback type, upvote or downvote
props.senderYesstringUser who created comment
props.senderFilterYesstringUser filter relation include:contains exclude:not contains equal:equals unequal:not equals prefix:prefix
props.minRatingYesnumberMinimum rating
props.maxRatingYesnumberMaximum rating
props.fromYesnumberStart timestamp
props.toYesnumberEnd timestamp
props.pageSizeYesnumberPage size
props.pageNumberYesnumberPage number

Return Value

Property NameTypeDescription
feedbackListobject[]Feedback query results
feedbackList[n].recordIdstringChat record ID
feedbackList[n].typestringUser feedback type, upvote or downvote
feedbackList[n].botIdstringAgent ID
feedbackList[n].commentstringUser comment
feedbackList[n].ratingnumberUser rating
feedbackList[n].tagsstring[]User feedback tags array
feedbackList[n].inputstringUser input question
feedbackList[n].aiAnswerstringAgent's answer
totalnumberTotal 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 NameRequiredTypeDescription
props.data.botIdYesstringAgent id
props.data.nameYesstringAgent name
props.data.introductionYesstringAgent introduction
props.data.agentSettingYesstringAgent settings
props.data.msgYesstringUser sent message
props.data.historyYesArrayHistorical conversation info
props.data.history[n].roleYesstringRole of historical message
props.data.history[n].contentYesstringContent of historical message
props.onTextNo(text: string) => unknown;Callback function triggered when new text is received, parameter is incremental text
props.onEventNo(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.onFinishNo(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 NameRequiredTypeDescription
props.botIdYesstringAgent id
props.fileListYesArrayFile list
props.fileList[n].fileIdYesstringCloud storage cloud id of file
props.fileList[n].fileNameYesstringFile name
props.fileList[n].typeYesstringCurrently only supports "file"