| name | ai-model-nodejs |
| description | Use this skill when developing Node.js backend services or CloudBase cloud functions (Express/Koa/NestJS, serverless, backend APIs) that need AI capabilities. Features text generation (generateText), streaming (streamText), AND image generation (generateImage) via @cloudbase/node-sdk ≥3.16.0. Built-in models include Hunyuan (hunyuan-2.0-instruct-20251111 recommended), DeepSeek (deepseek-v3.2 recommended), and hunyuan-image for images. This is the ONLY SDK that supports image generation. NOT for browser/Web apps (use ai-model-web) or WeChat Mini Program (use ai-model-wechat). |
| alwaysApply | false |
When to use this skill
Use this skill for calling AI models in Node.js backend or CloudBase cloud functions using @cloudbase/node-sdk.
Use it when you need to:
- Integrate AI text generation in backend services
- Generate images with Hunyuan Image model
- Call AI models from CloudBase cloud functions
- Server-side AI processing
Do NOT use for:
- Browser/Web apps → use
ai-model-web skill
- WeChat Mini Program → use
ai-model-wechat skill
- HTTP API integration → use
http-api skill
Available Providers and Models
CloudBase provides these built-in providers and models:
| Provider | Models | Recommended |
|---|
hunyuan-exp | hunyuan-turbos-latest, hunyuan-t1-latest, hunyuan-2.0-thinking-20251109, hunyuan-2.0-instruct-20251111 | ✅ hunyuan-2.0-instruct-20251111 |
deepseek | deepseek-r1-0528, deepseek-v3-0324, deepseek-v3.2 | ✅ deepseek-v3.2 |
Installation
npm install @cloudbase/node-sdk
⚠️ AI feature requires version 3.16.0 or above. Check with npm list @cloudbase/node-sdk.
Initialization
In Cloud Functions
const tcb = require('@cloudbase/node-sdk');
const app = tcb.init({ env: '<YOUR_ENV_ID>' });
exports.main = async (event, context) => {
const ai = app.ai();
};
Cloud Function Configuration for AI Models
⚠️ Important: When creating cloud functions that use AI models (especially generateImage() and large language model generation), set a longer timeout as these operations can be slow.
Using MCP Tool manageFunctions(action="createFunction"):
Legacy compatibility: if an older prompt still says createFunction, keep the same payload shape but execute it through manageFunctions(action="createFunction").
Set the timeout parameter in the func object:
- Parameter:
func.timeout (number)
- Unit: seconds
- Range: 1 - 900
- Default: 20 seconds (usually too short for AI operations)
Recommended timeout values:
- Text generation (
generateText): 60-120 seconds
- Streaming (
streamText): 60-120 seconds
- Image generation (
generateImage): 300-900 seconds (recommended: 900s)
- Combined operations: 900 seconds (maximum allowed)
In Regular Node.js Server
const tcb = require('@cloudbase/node-sdk');
const app = tcb.init({
env: '<YOUR_ENV_ID>',
secretId: '<YOUR_SECRET_ID>',
secretKey: '<YOUR_SECRET_KEY>'
});
const ai = app.ai();
generateText() - Non-streaming
const model = ai.createModel("hunyuan-exp");
const result = await model.generateText({
model: "hunyuan-2.0-instruct-20251111",
messages: [{ role: "user", content: "你好,请你介绍一下李白" }],
});
console.log(result.text);
console.log(result.usage);
console.log(result.messages);
console.log(result.rawResponses);
streamText() - Streaming
const model = ai.createModel("hunyuan-exp");
const res = await model.streamText({
model: "hunyuan-2.0-instruct-20251111",
messages: [{ role: "user", content: "你好,请你介绍一下李白" }],
});
for await (let text of res.textStream) {
console.log(text);
}
for await (let data of res.dataStream) {
console.log(data);
}
const messages = await res.messages;
const usage = await res.usage;
generateImage() - Image Generation
⚠️ Image generation is only available in Node SDK, not in JS SDK (Web) or WeChat Mini Program.
const imageModel = ai.createImageModel("hunyuan-image");
const res = await imageModel.generateImage({
model: "hunyuan-image",
prompt: "一只可爱的猫咪在草地上玩耍",
size: "1024x1024",
version: "v1.9",
});
console.log(res.data[0].url);
console.log(res.data[0].revised_prompt);
Image Generation Parameters
interface HunyuanGenerateImageInput {
model: "hunyuan-image";
prompt: string;
version?: "v1.8.1" | "v1.9";
size?: string;
negative_prompt?: string;
style?: string;
revise?: boolean;
n?: number;
footnote?: string;
seed?: number;
}
interface HunyuanGenerateImageOutput {
id: string;
created: number;
data: Array<{
url: string;
revised_prompt?: string;
}>;
}
Type Definitions
interface BaseChatModelInput {
model: string;
messages: Array<ChatModelMessage>;
temperature?: number;
topP?: number;
}
type ChatModelMessage =
| { role: "user"; content: string }
| { role: "system"; content: string }
| { role: "assistant"; content: string };
interface GenerateTextResult {
text: string;
messages: Array<ChatModelMessage>;
usage: Usage;
rawResponses: Array<unknown>;
error?: unknown;
}
interface {
: <>;
: <>;
: <[]>;
: <>;
?: ;
}
{
: ;
: ;
: ;
}