Calling AI Large Models in Mini Programs
In WeChat Mini Programs, calling AI Large Models can be achieved through the built-in Cloud Development AI+ capability in the basic library. Below is a detailed step-by-step guide:
1. Preparations
Enable the CloudBase Environment:
- Log in to the Tencent Cloud Console, enable the CloudBase environment, and obtain the
envId.
- Log in to the Tencent Cloud Console, enable the CloudBase environment, and obtain the
Initialize the Cloud Environment:
- Initialize the cloud environment in the
app.jsfile of the Mini Program.wx.cloud.init({
env: "your-env-id", // Replace with your CloudBase environment ID
});
- Initialize the cloud environment in the
2. Calling AI Large Models
Use
wx.cloud.extend.AIto invoke AI capabilities:In pages or components that require calling AI large models, use the APIs provided by
wx.cloud.extend.AIto make the call.For example, invoke the
hunyuanmodel for text generation:const hy = wx.cloud.extend.AI.createModel("hunyuan"); // Create a model
const res = await hy.streamText({
data: {
model: "hunyuan-lite",
messages: [
{
role: "user",
content: "hi",
},
],
},
});
for await (let str of res.textStream) {
console.log(str); // Print the generated text
}
for await (let event of res.eventStream) {
console.log(event); // Print the complete data returned each time
}