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.js
file 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.AI
to invoke AI capabilities:In pages or components that require calling AI large models, use the APIs provided by
wx.cloud.extend.AI
to make the call.For example, invoke the
hunyuan
model 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
}
Similar Issues
- What are the prerequisites for invoking AI large models in Mini Programs?
- How to activate WeChat Cloud Development AI+ capabilities?
- What are the steps for invoking AI large models in Mini Programs?
- What APIs does
wx.cloud.extend.AI
provide? - How to use the
hunyuan
model in Mini Programs? - How to initialize the cloud environment when invoking AI large models?
- What are the code examples for invoking AI large models?
- What should be noted when invoking AI large models?
- How to handle the return results of invoking AI large models?
- What is the performance of invoking AI large models?
Hope the above information helps you better understand how to invoke AI large models in WeChat Mini Programs. If you have further questions, feel free to ask!