给你的 SaaS 产品加上 AI 功能:一天搞定
2026 年,用户已经习惯了 AI 加持的产品体验。如果你的 SaaS 还没有 AI 功能,你正在失去竞争力。好消息是:接入 AI 比你想的简单得多 —— 用 OpenAI SDK 兼容的 API 网关,你可以在一天内给产品加上 AI 聊天、内容生成和智能搜索,而且成本低到可以忽略。
三种最实用的 AI 集成模式
模式一:AI 聊天组件 —— 产品内嵌智能客服 / 助手
模式二:内容生成 —— 一键生成文案、报告、摘要
模式三:智能搜索 —— 自然语言搜索替代传统关键词
模式一:AI 聊天组件
在你的产品里加一个 AI 助手对话框,用户可以用自然语言提问,AI 根据产品上下文回答。
后端 API(Node.js + TypeScript)
import OpenAI from "openai";
import { Request, Response } from "express";
const client = new OpenAI({
baseURL: "https://api.aipower.me/v1",
apiKey: process.env.AIPOWER_API_KEY,
});
// 根据用户套餐选择模型
function getModel(userTier: "free" | "pro" | "enterprise") {
const models = {
free: "zhipu/glm-4-flash", // $0.01/M — 免费用户
pro: "deepseek/deepseek-chat", // $0.34/M — 付费用户
enterprise: "anthropic/claude-opus", // $15.00/M — 企业用户
};
return models[userTier];
}
export async function handleChat(req: Request, res: Response) {
const { message, conversationHistory, userTier } = req.body;
const systemPrompt = `你是 [产品名称] 的 AI 助手。
你的职责是帮助用户使用产品、回答问题、提供建议。
请用简洁友好的语气回答,如果不确定请说明。`;
const response = await client.chat.completions.create({
model: getModel(userTier),
messages: [
{ role: "system", content: systemPrompt },
...conversationHistory,
{ role: "user", content: message },
],
max_tokens: 1024,
stream: true, // 流式返回,体验更好
});
// 流式响应
res.setHeader("Content-Type", "text/event-stream");
for await (const chunk of response) {
const content = chunk.choices[0]?.delta?.content || "";
res.write(`data: ${JSON.stringify({ content })}\n\n`);
}
res.end();
}前端组件(React)
"use client";
import { useState } from "react";
export function AIChatWidget() {
const [messages, setMessages] = useState<Array<{
role: "user" | "assistant"; content: string
}>>([]);
const [input, setInput] = useState("");
async function sendMessage() {
const userMsg = { role: "user" as const, content: input };
setMessages(prev => [...prev, userMsg]);
setInput("");
const res = await fetch("/api/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
message: input,
conversationHistory: messages,
userTier: "pro",
}),
});
// 处理流式响应
const reader = res.body!.getReader();
const decoder = new TextDecoder();
let assistantMsg = "";
setMessages(prev => [...prev, { role: "assistant", content: "" }]);
while (true) {
const { done, value } = await reader.read();
if (done) break;
const text = decoder.decode(value);
const lines = text.split("\n").filter(l => l.startsWith("data: "));
for (const line of lines) {
const { content } = JSON.parse(line.slice(6));
assistantMsg += content;
setMessages(prev => [
...prev.slice(0, -1),
{ role: "assistant", content: assistantMsg },
]);
}
}
}
return (
<div className="fixed bottom-4 right-4 w-80 bg-white rounded-xl shadow-xl">
{/* 消息列表 + 输入框 UI */}
</div>
);
}模式二:一键内容生成
让用户用一句话描述需求,AI 生成完整的文案、邮件、报告或产品描述。
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.aipower.me/v1",
apiKey: process.env.AIPOWER_API_KEY,
});
// 内容生成器:支持多种模板
async function generateContent(
type: "email" | "report" | "product_desc" | "social_post",
context: string,
userTier: "free" | "pro" | "enterprise"
) {
const templates: Record<string, string> = {
email: "根据以下内容,写一封专业的商务邮件:",
report: "根据以下数据和要求,生成一份结构清晰的分析报告:",
product_desc: "为以下产品写一段吸引人的产品描述,适合电商平台:",
social_post: "根据以下内容,写一条适合社交媒体发布的帖子:",
};
const model = {
free: "zhipu/glm-4-flash",
pro: "deepseek/deepseek-chat",
enterprise: "anthropic/claude-opus",
}[userTier];
const response = await client.chat.completions.create({
model,
messages: [
{
role: "system",
content: "你是一个专业的内容创作助手,擅长写各种商业文案。",
},
{
role: "user",
content: `${templates[type]}\n\n${context}`,
},
],
max_tokens: 2048,
});
return response.choices[0].message.content;
}
// 使用示例
const email = await generateContent(
"email",
"感谢客户上周的合作,询问是否需要续约,提供 10% 早鸟折扣",
"pro"
);模式三:智能搜索
用户输入自然语言问题,AI 理解意图后搜索你的数据库,返回精准结果。
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.aipower.me/v1",
apiKey: process.env.AIPOWER_API_KEY,
});
// 第一步:用便宜模型理解搜索意图
async function parseSearchIntent(query: string) {
const response = await client.chat.completions.create({
model: "zhipu/glm-4-flash", // 用最便宜的模型解析意图
messages: [
{
role: "system",
content: `将用户的自然语言搜索请求转换为结构化查询。
返回 JSON 格式:{ "keywords": [], "filters": {}, "sort": "" }`,
},
{ role: "user", content: query },
],
response_format: { type: "json_object" },
});
return JSON.parse(response.choices[0].message.content || "{}");
}
// 第二步:用解析结果查数据库
async function smartSearch(query: string) {
// 用 GLM-4 Flash 解析意图(成本几乎为 0)
const intent = await parseSearchIntent(query);
// 用结构化查询搜索数据库
const results = await db.search(intent);
// 如果需要,用 DeepSeek 生成摘要
if (results.length > 5) {
const summary = await client.chat.completions.create({
model: "deepseek/deepseek-chat",
messages: [
{
role: "user",
content: `用户搜索"${query}",以下是结果,请生成摘要:
${JSON.stringify(results.slice(0, 10))}`,
},
],
});
return { results, summary: summary.choices[0].message.content };
}
return { results, summary: null };
}
// 使用示例:用户输入自然语言
await smartSearch("上个月销量最高的 3 个产品");
await smartSearch("退货率超过 5% 的商品");
await smartSearch("上海地区的企业客户");多模型分级策略:控制成本的关键
不是所有请求都需要最贵的模型。合理分级可以把 AI 成本降低 100 倍。
| 用户层级 | 推荐模型 | 价格 | 适用场景 |
|---|---|---|---|
| 免费用户 | zhipu/glm-4-flash | $0.01/M | 简单问答、文本分类、格式化 |
| 付费用户 | deepseek/deepseek-chat | $0.34/M | 内容生成、代码辅助、数据分析 |
| 高级 / 企业 | anthropic/claude-opus | $15.00/M | 复杂推理、专业分析、创意写作 |
成本分析:不同用户规模花多少钱?
假设每个活跃用户每天发送 5 次 AI 请求,每次约 1,200 Token(800 输入 + 400 输出):
| 规模 | 月请求量 | GLM-4 Flash | DeepSeek V3 | Claude Opus | 混合策略 |
|---|---|---|---|---|---|
| 100 用户 | 15,000 | $0.18 | $7.08 | $630 | $1.50 |
| 1,000 用户 | 150,000 | $1.80 | $70.80 | $6,300 | $15.00 |
| 10,000 用户 | 1,500,000 | $18.00 | $708.00 | $63,000 | $150.00 |
混合策略成本计算(推荐)
假设 70% 请求走 GLM-4 Flash(免费层 + 简单任务),25% 走 DeepSeek(付费用户),5% 走 Claude Opus(企业用户)。10,000 用户月成本仅 $150 左右 —— 比全用 GPT-4o 便宜 40 倍。
完整接入方案:一个中间层搞定
import OpenAI from "openai";
// 初始化一次,全局复用
const ai = new OpenAI({
baseURL: "https://api.aipower.me/v1",
apiKey: process.env.AIPOWER_API_KEY,
});
// AI 中间层:封装模型选择 + 错误处理 + 日志
export class AIService {
private static modelMap = {
free: "zhipu/glm-4-flash",
pro: "deepseek/deepseek-chat",
enterprise: "anthropic/claude-opus",
} as const;
static async complete(params: {
userTier: "free" | "pro" | "enterprise";
system: string;
prompt: string;
maxTokens?: number;
stream?: boolean;
}) {
const model = this.modelMap[params.userTier];
try {
const response = await ai.chat.completions.create({
model,
messages: [
{ role: "system", content: params.system },
{ role: "user", content: params.prompt },
],
max_tokens: params.maxTokens ?? 1024,
stream: params.stream ?? false,
});
return response;
} catch (error) {
// 降级策略:高级模型失败时自动切换到低级模型
if (params.userTier === "enterprise") {
return ai.chat.completions.create({
model: this.modelMap.pro, // 降级到 DeepSeek
messages: [
{ role: "system", content: params.system },
{ role: "user", content: params.prompt },
],
max_tokens: params.maxTokens ?? 1024,
});
}
throw error;
}
}
}
// 在产品各处调用
await AIService.complete({
userTier: "pro",
system: "你是产品助手",
prompt: "帮我写一封营销邮件...",
});一天搞定的实施计划
上午 2h — 注册 AIPower,获取 API Key,搭建 AIService 中间层
上午 2h — 实现第一个 AI 功能(推荐从聊天组件开始)
下午 2h — 加上内容生成或智能搜索(选一个最适合你产品的)
下午 2h — 测试 + 部署 + 配置分级模型策略
常见问题
Q: 需要学习新的 SDK 吗?
不需要。AIPower 完全兼容 OpenAI SDK,如果你之前用过 OpenAI,改一行 base_url 就行。
Q: 如果 AI 回答了错误的内容怎么办?
在 system prompt 中明确限定 AI 的回答范围,对于关键操作加上人工确认环节。可以用 GLM-4 Flash 做一轮内容审核,成本几乎为零。
Q: 延迟表现如何?
GLM-4 Flash 和 DeepSeek V3 的首字延迟通常在 300-800ms,使用流式输出用户体验很好。建议用 stream: true 做所有面向用户的请求。
Q: 数据安全有保障吗?
AIPower 不存储你的对话内容,API 通信全程 HTTPS 加密。敏感数据建议在发送前做脱敏处理。
50 次免费调用 | 16 个模型 | 兼容 OpenAI SDK