Skip to main content

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

  1. Enable the CloudBase Environment:

    • Log in to the Tencent Cloud Console, enable the CloudBase environment, and obtain the envId.
  2. 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
      });

2. Calling AI Large Models

  1. 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
      }