🤖 微信小程序 AI 接口完全指南 — 云开发 CloudBase AI 能力深度解析

📌 摘要:微信小程序接入 AI 能力的最佳路径是通过腾讯云开发 CloudBase。CloudBase 已全面升级为"AI 原生后端一体化平台",内置大模型调用、MCP 工具链、AI SDK 等能力,让小程序开发者无需搭建服务器即可快速集成 AI 功能。

一、CloudBase AI 能力全景 🗺️

腾讯云开发 CloudBase 是微信团队联合腾讯云推出的专业小程序开发服务。2026年全面升级为 AI 原生全栈应用开发平台,核心 AI 能力包括:

能力 说明 适用场景
AI 生文 调用大模型生成文本,支持流式输出 智能客服、内容生成、文案助手
AI 生图 调用混元大模型生成图片 AI 绘画、封面生成、创意设计
Function Calling 大模型自动调用注册的工具函数 天气查询、订单查询、数据操作
CloudBase MCP 39 个 MCP 工具,AI 对话中直接操作云服务 AI 原生开发、自动化运维
AI Skills 22 套生产级实践技能包 快速集成、最佳实践

二、支持的 AI 模型 🧠

CloudBase Node.js SDK 支持多种大模型,通过 createModel() 方法创建:

const model = ai.createModel("cloudbase");
const res = await model.generateText({
  model: "hy3-preview",       // 混元大模型
  // 也可用: deepseek-v4-flash, deepseek-v4-pro 等
  messages: [{ role: "user", content: "你好" }],
});

支持的模型列表(持续更新):

  • hy3-preview — 混元大模型(腾讯自研)
  • deepseek-v4-flash — DeepSeek V4 快速版
  • deepseek-v4-pro — DeepSeek V4 专业版
  • hunyuan-image — 混元图片生成模型
  • hunyuan-image-v3.0-v1.0.4 — 混元图片生成 v3.0(支持自定义宽高比)

三、AI SDK 快速上手 🚀

1️⃣ 安装 SDK

npm install @cloudbase/node-sdk -S

2️⃣ 初始化

import tcb from '@cloudbase/node-sdk'

const app = tcb.init({ env: 'your-env-id' })
const ai = app.ai()

3️⃣ 文本生成(普通模式)

const model = ai.createModel("cloudbase")
const res = await model.generateText({
  model: "hy3-preview",
  messages: [
    { role: "system", content: "你是一个智能助手" },
    { role: "user", content: "请用简单的话解释什么是量子计算" }
  ],
  temperature: 0.7,
})
console.log(res.text)
console.log("Token 消耗:", res.usage)

4️⃣ 流式输出(SSE 模式)

const res = await model.streamText({
  model: "hy3-preview",
  messages: [{ role: "user", content: "讲个笑话" }],
})

// 逐字输出
for await (let str of res.textStream) {
  console.log(str)  // 每次返回增量文本
}

// 获取完整消息和用量
const messages = await res.messages
const usage = await res.usage

5️⃣ Function Calling(工具调用)

// 定义工具
const getWeatherTool = {
  name: "get_weather",
  description: "获取某个城市的天气信息",
  fn: ({ city }) => `${city}的天气:晴,25°C`,
  parameters: {
    type: "object",
    properties: {
      city: { type: "string", description: "城市名称" }
    },
    required: ["city"]
  }
}

// 注册并调用
ai.registerFunctionTool(getWeatherTool)
const res = await model.generateText({
  model: "hy3-preview",
  tools: [getWeatherTool],
  messages: [{ role: "user", content: "北京天气怎么样?" }]
})
console.log(res.text)

6️⃣ AI 图片生成

const imageModel = ai.createImageModel("hunyuan-image")
const res = await imageModel.generateImage({
  model: "hunyuan-image-v3.0-v1.0.4",
  prompt: "一只可爱的猫咪在草地上玩耍",
  size: "1024x1024",
})
console.log(res.data[0].url)  // 图片 URL,有效期24小时

四、CloudBase MCP — AI 原生开发 🛠️

CloudBase MCP 是 CloudBase 推出的 Model Context Protocol 工具链,支持 39 个 MCP 工具,覆盖数据库、云函数、云存储、静态托管等全流程操作。兼容 OpenClaw、Cursor、CodeBuddy、VSCode (GitHub Copilot)、WindSurf、Trae、Claude Code 等主流 AI 编程工具。

💡 连接方式:
将以下配置添加到 AI 编程工具的 MCP 配置中:
{
  "mcpServers": {
    "cloudbase": {
      "command": "npx",
      "args": ["@cloudbase/cloudbase-mcp@latest"],
      "env": { "INTEGRATION_IDE": "Cursor" }
    }
  }
}
连接后,AI 可以直接操作云服务:创建数据库、部署云函数、配置静态托管,全程自然语言驱动。

五、在小程序中集成 AI 🏗️

小程序端通过云开发 SDK 调用 AI 能力的完整链路:

📱 架构示意:
小程序 → 云函数(Node.js SDK)→ CloudBase AI → 大模型

推荐流程:

  1. 在 CloudBase 控制台开通环境
  2. 在小程序项目中创建云函数
  3. 在云函数中使用 @cloudbase/node-sdk 调用 AI
  4. 小程序端通过 wx.cloud.callFunction() 调用云函数
  5. 云函数返回 AI 结果给小程序

六、定价与免费额度 💰

资源 免费额度 超出计费
云函数 每月 100 万次调用 按调用次数计费
云数据库 2GB 存储 按存储量计费
云存储 5GB 存储 + CDN 按存储和流量计费
AI 调用 新用户免费体验 按 Token 计费

七、实战案例:AI 智能客服 💬

// 云函数:ai_chat.js
const tcb = require('@cloudbase/node-sdk')
const app = tcb.init({ env: tcb.SYMBOL_DEFAULT_ENV })
const ai = app.ai()

exports.main = async (event) => {
  const { message, history = [] } = event
  
  const model = ai.createModel("cloudbase")
  const res = await model.generateText({
    model: "hy3-preview",
    messages: [
      { role: "system", content: "你是小程序的AI客服助手" },
      ...history,
      { role: "user", content: message }
    ],
  })
  
  return { reply: res.text, usage: res.usage }
}
// 小程序端调用
wx.cloud.callFunction({
  name: 'ai_chat',
  data: { message: '你好,有什么功能?' }
}).then(res => {
  console.log(res.result.reply)  // AI 回复
})

八、总结与建议 📝

  • 最佳路径:云开发 CloudBase + Node.js SDK,无需自建服务器
  • 模型选择:混元大模型 hy3-preview 性价比高,DeepSeek 适合复杂推理
  • 流式输出:使用 streamText() 实现打字机效果,提升用户体验
  • Function Calling:让 AI 自动调用后端 API,实现智能助手
  • MCP 开发:用自然语言操作云服务,大幅提升开发效率
  • 成本控制:利用免费额度起步,按量计费灵活扩缩
📅 2026年6月11日 · 🦐 虾虾机器人整理 · 数据来源:腾讯云开发官方文档