AI SDK
Initialize SDK
For initialization methods, please refer to Initialize SDK.
app.ai
After initialization, you can use the ai method mounted on the cloudbase instance to create an AI instance for subsequent model creation.
Usage Example
app = cloudbase.init({ env: "your-env" });
const ai = app.ai();
Type Declaration
function ai(): AI;
Return Value
AI
Returns a newly created AI instance.
AI
A class for creating AI models.
createModel()
Creates a specified AI model.
Usage Example
const model = ai.createModel("cloudbase");
Type Declaration
function createModel(model: string): ChatModel;
Returns a model instance that implements the ChatModel abstract class, providing AI text generation capabilities.
createImageModel()
Creates a specified image generation model.
Usage Example
const imageModel = ai.createImageModel("hunyuan-image");
Type Declaration
function createImageModel(provider: string): ImageModel;
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
| provider | Yes | string | Model provider name, e.g., "hunyuan-image" |
Return Value
Returns an ImageModel instance that provides AI image generation capabilities.
bot
An instance of the Bot class mounted here, which includes a series of methods for interacting with Agents. For details, refer to the Bot class documentation.
Usage Example
const agentList = await ai.bot.list({ pageNumber: 1, pageSize: 10 });
registerFunctionTool()
Registers a function tool. When calling the LLM, you can inform it of available function tools. When the LLM's response is parsed as a tool call, the corresponding function tool will be automatically invoked.
Usage Example
// Omitting AI SDK initialization...
// 1. Define the weather tool, see FunctionTool type for details
const getWeatherTool = {
name: "get_weather",
description: "Returns weather information for a city. Example call: get_weather({city: 'Beijing'})",
fn: ({ city }) => `The weather in ${city} is: Clear autumn skies!!!`, // Define the tool execution content here
parameters: {
type: "object",
properties: {
city: {
type: "string",
description: "The city to query",
},
},
required: ["city"],
},
};
// 2. Register the tool we just defined
ai.registerFunctionTool(getWeatherTool);
// 3. Send a message to the LLM and inform it that a weather tool is available
const model = ai.createModel("cloudbase");
const result = await model.generateText({
model: "hy3-preview",
tools: [getWeatherTool], // Here we pass in the weather tool
messages: [
{
role: "user",
content: "Please tell me the weather in Beijing",
},
],
});
console.log(result.text);
Type Declaration
function registerFunctionTool(functionTool: FunctionTool);
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
| functionTool | Yes | FunctionTool | See FunctionTool for details |
Return Value
undefined
Parameters
| Parameter | Required | Type | Example | Description |
|---|---|---|---|---|
| data | Yes | BaseChatModelInput | {model: "deepseek-v4-flash", messages: [{ role: "user", content: "Hello, please introduce Li Bai" }]} | Parameter type is defined as BaseChatModelInput as the basic input definition. In practice, different LLM providers have their own unique input parameters. Developers can pass additional parameters not defined in this type based on the LLM's official documentation to fully utilize the LLM's capabilities. Other parameters will be passed through to the LLM interface, and the SDK does not perform additional processing on them. |
Return Value
| Property | Type | Example | Description |
|---|---|---|---|
| text | string | "Li Bai was a Tang Dynasty poet." | Text generated by the LLM. |
| rawResponses | unknown[] | [{"choices": [{"finish_reason": "stop","message": {"role": "assistant", "content": "Hello, is there anything I can help you with?"}}], "usage": {"prompt_tokens": 14, "completion_tokens": 9, "total_tokens": 23}}] | Complete response from the LLM, containing more detailed data such as message creation time. Since different LLMs have varying return values, please use according to your situation. |
| res.messages | ChatModelMessage[] | [{role: 'user', content: 'Hello'},{role: 'assistant', content: 'Hello! Nice to chat with you. Is there anything I can help you with? Whether it\'s about life, work, study, or other areas, I\'ll do my best to assist you.'}] | Complete message list for this call. |
| usage | Usage | {"completion_tokens":33,"prompt_tokens":3,"total_tokens":36} | Tokens consumed in this call. |
| error | unknown | Errors that occurred during the call. |
streamText()
Calls the LLM to generate text in streaming mode. During streaming calls, the generated text and other response data are returned via SSE. The return value of this interface provides different levels of encapsulation for SSE, allowing developers to obtain text streams and complete data streams according to their needs.
Usage Example
const hy = ai.createModel("cloudbase"); // Create model
const res = await hy.streamText({
model: "hy3-preview",
messages: [{ role: "user", content: "Hello, please introduce Li Bai" }],
});
for await (let str of res.textStream) {
console.log(str); // Print the generated text
}
for await (let data of res.dataStream) {
console.log(data); // Print the complete data for each response
}
Type Declaration
function streamText(data: BaseChatModelInput): Promise<StreamTextResult>;
Parameters
| Parameter | Required | Type | Example | Description |
|---|---|---|---|---|
| data | Yes | BaseChatModelInput | {model: "deepseek-v4-flash", messages: [{ role: "user", content: "Hello, please introduce Li Bai" }]} | Parameter type is defined as BaseChatModelInput as the basic input definition. In practice, different LLM providers have their own unique input parameters. Developers can pass additional parameters not defined in this type based on the LLM's official documentation to fully utilize the LLM's capabilities. Other parameters will be passed through to the LLM interface, and the SDK does not perform additional processing on them. |
Return Value
| StreamTextResult Property | Type | Description |
|---|---|---|
| textStream | ReadableStream<string> | LLM-generated text returned in streaming mode. Refer to the usage example to get the incremental generated text. |
| dataStream | ReadableStream<DataChunk> | LLM response data returned in streaming mode. Refer to the usage example to get the incremental generated data. Since different LLMs have varying response values, please use according to your situation. |
| messages | Promise<ChatModelMessage[]> | Complete message list for this call. |
| usage | Promise<Usage> | Tokens consumed in this call. |
| error | unknown | Errors that occurred during this call. |
| DataChunk Property | Type | Description |
|---|---|---|
| choices | Array<object> | |
| choices[n].finish_reason | string | Reason for model inference termination. |
| choices[n].delta | ChatModelMessage | Message for this request. |
| usage | Usage | Tokens consumed in this request. |
| rawResponse | unknown | Raw response from the LLM. |
Example
const hy = ai.createModel("cloudbase");
const res = await hy.streamText({
model: "hy3-preview",
messages: [{ role: "user", content: "What is 1+1" }],
});
// Text stream
for await (let str of res.textStream) {
console.log(str);
}
// 1
// plus
// 1
// equals
// 2
// .
// Data stream
for await (let str of res.dataStream) {
console.log(str);
}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hy3-preview", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hy3-preview", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hy3-preview", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hy3-preview", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hy3-preview", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hy3-preview", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hy3-preview", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hy3-preview", version: "202404011000", choices: Array(1), …}
ImageModel
This class describes the interface provided by AI image generation model classes.
generateImageSubUrlConfig
Configures the API sub-path for different provider and model combinations. When calling generateImage(), the SDK looks up the sub-path based on input.model. If not found, it uses the default path images/generations.
Type Declaration
generateImageSubUrlConfig: Record<string, Record<string, string>>
Default Value
{
'hunyuan-image': {
'hunyuan-image-v3.0-v1.0.4': 'images/ar/generations',
'hunyuan-image-v3.0-v1.0.1': 'images/ar/generations',
},
}
Usage Example
const imageModel = ai.createImageModel("hunyuan-image");
// Custom sub-path configuration
imageModel.generateImageSubUrlConfig['hunyuan-image']['custom-model'] = 'images/custom/generations';
generateImage()
Calls the LLM to generate images.
Usage Example
const imageModel = ai.createImageModel("hunyuan-image");
const res = await imageModel.generateImage({
model: "hunyuan-image-v3.0-v1.0.4",
prompt: "A cute cat playing on the grass",
});
console.log(res.data[0].url); // Print the generated image URL
Type Declaration
function generateImage(input: HunyuanARGenerateImageInput): Promise<HunyuanARGenerateImageOutput>;
function generateImage(input: HunyuanGenerateImageInput): Promise<HunyuanGenerateImageOutput>;
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
| input | Yes | HunyuanARGenerateImageInput | HunyuanGenerateImageInput | Image generation parameters, see HunyuanARGenerateImageInput or HunyuanGenerateImageInput for details |
Return Value
Promise<HunyuanARGenerateImageOutput> or Promise<HunyuanGenerateImageOutput>
| Property | Type | Description |
|---|---|---|
| id | string | ID of this request |
| created | number | Unix timestamp |
| data | Array<object> | Returned image generation content |
| data[n].url | string | Generated image URL, valid for 24 hours |
| data[n].revised_prompt | string | Text after prompt revision (if revise is false, it's the original prompt) |
Bot
A class for interacting with Agents.
get()
Gets information about a specific Agent.
Usage Example
const res = await ai.bot.get({ botId: "botId-xxx" });
console.log(res);
Type Declaration
function get(props: { botId: string });
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
| props.botId | Yes | string | ID of the Agent to get info for |
Return Value
| Property | Type | Example | Description |
|---|---|---|---|
| botId | string | "bot-27973647" | Agent ID |
| name | string | "Translator" | Agent name |
| introduction | string | Agent introduction | |
| welcomeMessage | string | Agent welcome message | |
| avatar | string | Agent avatar URL | |
| background | string | Agent chat background image URL | |
| isNeedRecommend | boolean | Whether to recommend questions after Agent responds | |
| type | string | Agent type |
list()
Gets information about multiple Agents in batch.
Usage Example
await ai.bot.list({
pageNumber: 1,
pageSize: 10,
name: "",
enable: true,
information: "",
introduction: "",
});
Type Declaration
function list(props: {
name: string;
introduction: string;
information: string;
enable: boolean;
pageSize: number;
pageNumber: number;
});
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
| props.pageNumber | Yes | number | Page index |
| props.pageSize | Yes | number | Page size |
| props.enable | Yes | boolean | Whether the Agent is enabled |
| props.name | Yes | string | Agent name, for fuzzy search |
| props.information | Yes | string | Agent information, for fuzzy search |
| props.introduction | Yes | string | Agent introduction, for fuzzy search |
Return Value
| Property | Type | Example | Description |
|---|---|---|---|
| total | number | --- | Total number of Agents |
| botList | Array<object> | Agent list | |
botList[n].botId | string | "bot-27973647" | Agent ID |
botList[n].name | string | "Translator" | Agent name |
botList[n].introduction | string | Agent introduction | |
botList[n].welcomeMessage | string | Agent welcome message | |
botList[n].avatar | string | Agent avatar URL | |
botList[n].background | string | Agent chat background image URL | |
botList[n].isNeedRecommend | boolean | Whether to recommend questions after Agent responds | |
botList[n].type | string | Agent type |
sendMessage()
Sends a message to an Agent for conversation. The response is returned via SSE, and this interface's return value provides different levels of encapsulation for SSE, allowing developers to obtain text streams and complete data streams according to their needs.
Usage Example
const res = await ai.bot.sendMessage({
botId: "botId-xxx",
history: [{ content: "You are Li Bai.", role: "user" }],
msg: "Hello",
});
for await (let str of res.textStream) {
console.log(str);
}
for await (let data of res.dataStream) {
console.log(data);
}
Type Declaration
function sendMessage(props: {
botId: string;
msg: string;
history: Array<{
role: string;
content: string;
}>;
}): Promise<StreamResult>;
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
| props.botId | Yes | string | Agent ID |
| props.msg | Yes | string | Message to send in this conversation |
| props.history | Yes | [] | Chat history before this conversation |
props.history[n].role | Yes | string | Role of the message sender |
props.history[n].content | Yes | string | Content of the message |
Return Value
Promise<StreamResult>
| StreamResult Property | Type | Description |
|---|---|---|
| textStream | AsyncIterable<string> | Agent-generated text returned in streaming mode. Refer to the usage example to get the incremental generated text. |
| dataStream | AsyncIterable<AgentStreamChunk> | Agent-generated data returned in streaming mode. Refer to the usage example to get the incremental generated data. |
| AgentStreamChunk Property | Type | Description |
|---|---|---|
| created | number | Conversation timestamp |
| record_id | string | Conversation record ID |
| model | string | LLM type |
| version | string | LLM version |
| type | string | Reply type: text: main answer content, thinking: thinking process, search: search results, knowledge: knowledge base |
| role | string | Conversation role, fixed as assistant in responses |
| content | string | Conversation content |
| finish_reason | string | Conversation end flag, continue means conversation is ongoing, stop means conversation ended |
| reasoning_content | string | Deep thinking content (only non-empty for deepseek-r1) |
| usage | object | Token usage |
| usage.prompt_tokens | number | Number of prompt tokens, remains unchanged across multiple returns |
| usage.completion_tokens | number | Total completion tokens. In streaming returns, represents the cumulative total of all completion tokens so far |
| usage.total_tokens | number | Sum of prompt_tokens and completion_tokens |
| knowledge_base | string[] | Knowledge bases used in the conversation |
| search_info | object | Search result information, requires web search to be enabled |
| search_info.search_results | object[] | Search citation information |
| search_info.search_results[n].index | string | Search citation index |
| search_info.search_results[n].title | string | Search citation title |
| search_info.search_results[n].url | string | Search citation URL |
getChatRecords()
Gets chat records.
Usage Example
await ai.bot.getChatRecords({
botId: "botId-xxx",
pageNumber: 1,
pageSize: 10,
sort: "asc",
});
Type Declaration
function getChatRecords(props: {
botId: string;
sort: string;
pageSize: number;
pageNumber: number;
});
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
| props.botId | Yes | string | Agent ID |
| props.sort | Yes | string | Sort order |
| props.pageSize | Yes | number | Page size |
| props.pageNumber | Yes | number | Page index |
Return Value
| Property | Type | Description |
|---|---|---|
| total | number | Total number of conversations |
| recordList | Array<object> | Conversation list |
recordList[n].botId | string | Agent ID |
recordList[n].recordId | string | Conversation ID, system-generated |
recordList[n].role | string | Role in conversation |
recordList[n].content | string | Conversation content |
recordList[n].conversation | string | User identifier |
recordList[n].type | string | Conversation data type |
recordList[n].image | string | Image URL generated in conversation |
recordList[n].triggerSrc | string | Conversation trigger source |
recordList[n].replyTo | string | Record ID being replied to |
recordList[n].createTime | string | Conversation time |
sendFeedback()
Sends feedback for a specific chat record.
Usage Example
const res = await ai.bot.sendFeedback({
userFeedback: {
botId: "botId-xxx",
recordId: "recordId-xxx",
comment: "Excellent",
rating: 5,
tags: ["Beautiful"],
aiAnswer: "falling petals",
input: "Give me an idiom",
type: "upvote",
},
botId: "botId-xxx",
});
Type Declaration
function sendFeedback(props: { userFeedback: IUserFeedback; botId: string });
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
| props.userFeedback | Yes | IUserFeedback | User feedback, see IUserFeedback type definition |
| props.botId | Yes | string | ID of the Agent to provide feedback for |
getFeedback()
Gets existing feedback information.
Usage Example
const res = await ai.bot.getFeedback({
botId: "botId-xxx",
from: 0,
to: 0,
maxRating: 4,
minRating: 3,
pageNumber: 1,
pageSize: 10,
sender: "user-a",
senderFilter: "include",
type: "upvote",
});
Type Declaration
function sendFeedback(props: {
botId: string;
type: string;
sender: string;
senderFilter: string;
minRating: number;
maxRating: number;
from: number;
to: number;
pageSize: number;
pageNumber: number;
});
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
| props.botId | Yes | string | Agent ID |
| props.type | Yes | string | User feedback type, upvote or downvote |
| props.sender | Yes | string | Feedback creator user |
| props.senderFilter | Yes | string | Feedback creator user filter: include, exclude, equal, unequal, prefix |
| props.minRating | Yes | number | Minimum rating |
| props.maxRating | Yes | number | Maximum rating |
| props.from | Yes | number | Start timestamp |
| props.to | Yes | number | End timestamp |
| props.pageSize | Yes | number | Page size |
| props.pageNumber | Yes | number | Page index |
Return Value
| Property | Type | Description |
|---|---|---|
| feedbackList | object[] | Feedback query results |
| feedbackList[n].recordId | string | Conversation record ID |
| feedbackList[n].type | string | User feedback type, upvote or downvote |
| feedbackList[n].botId | string | Agent ID |
| feedbackList[n].comment | string | User comment |
| feedbackList[n].rating | number | User rating |
| feedbackList[n].tags | string[] | User feedback tags array |
| feedbackList[n].input | string | User input question |
| feedbackList[n].aiAnswer | string | Agent's answer |
| total | number | Total number of feedbacks |
uploadFiles()
Uploads files from cloud storage to an Agent for document-based chat.
Usage Example
// Upload files
await ai.bot.uploadFiles({
botId: "botId-xxx",
fileList: [
{
fileId: "cloud://xxx.docx",
fileName: "xxx.docx",
type: "file",
},
],
});
// Conduct document-based chat
const res = await ai.bot.sendMessage({
botId: "your-bot-id",
msg: "What is the content of this file",
files: ["xxx.docx"], // Array of file fileIds
});
for await (let text of res.textStream) {
console.let(text);
}
Type Declaration
function uploadFiles(props: {
botId: string;
fileList: Array<{
fileId: "string";
fileName: "string";
type: "file";
}>;
});
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
| props.botId | Yes | string | Agent ID |
| props.fileList | Yes | string | File list |
| props.fileList[n].fileId | Yes | string | Cloud storage file ID |
| props.fileList[n].fileName | Yes | string | File name |
| props.fileList[n].type | Yes | string | Currently only supports "file" |
getRecommendQuestions()
Gets recommended questions.
Usage Example
const res = ai.bot.getRecommendQuestions({
botId: "botId-xxx",
history: [{ content: "Who are you", role: "user" }],
msg: "Hello",
agentSetting: "",
introduction: "",
name: "",
});
for await (let str of res.textStream) {
console.log(str);
}
Type Declaration
function getRecommendQuestions(props: {
botId: string;
name: string;
introduction: string;
agentSetting: string;
msg: string;
history: Array<{
role: string;
content: string;
}>;
}): Promise<StreamResult>;
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
| props.botId | Yes | string | Agent ID |
| props.name | Yes | string | Agent name |
| props.introduction | Yes | string | Agent introduction |
| props.agentSetting | Yes | string | Agent settings |
| props.msg | Yes | string | User message |
| props.history | Yes | Array | History messages |
props.history[n].role | Yes | string | History message role |
props.history[n].content | Yes | string | History message content |
Return Value
Promise<StreamResult>
| StreamResult Property | Type | Description |
|---|---|---|
| textStream | AsyncIterable<string> | Agent-generated text returned in streaming mode. Refer to the usage example to get the incremental generated text. |
| dataStream | AsyncIterable<AgentStreamChunk> | Agent-generated data returned in streaming mode. Refer to the usage example to get the incremental generated data. |
| AgentStreamChunk Property | Type | Description |
|---|---|---|
| created | number | Conversation timestamp |
| record_id | string | Conversation record ID |
| model | string | LLM type |
| version | string | LLM version |
| type | string | Reply type: text: main answer content, thinking: thinking process, search: search results, knowledge: knowledge base |
| role | string | Conversation role, fixed as assistant in responses |
| content | string | Conversation content |
| finish_reason | string | Conversation end flag, continue means conversation is ongoing, stop means conversation ended |
| reasoning_content | string | Deep thinking content (only non-empty for deepseek-r1) |
| usage | object | Token usage |
| usage.prompt_tokens | number | Number of prompt tokens, remains unchanged across multiple returns |
| usage.completion_tokens | number | Total completion tokens. In streaming returns, represents the cumulative total of all completion tokens so far |
| usage.total_tokens | number | Sum of prompt_tokens and completion_tokens |
| knowledge_base | string[] | Knowledge bases used in the conversation |
| search_info | object | Search result information, requires web search to be enabled |
| search_info.search_results | object[] | Search citation information |
| search_info.search_results[n].index | string | Search citation index |
| search_info.search_results[n].title | string | Search citation title |
| search_info.search_results[n].url | string | Search citation URL |
createConversation()
Creates a new conversation with an Agent.
Usage Example
const res = await ai.bot.createConversation({
botId: "botId-xxx",
title: "My Conversation",
}): Promise<IConversation>;
Type Declaration
function createConversation(props: IBotCreateConversation);
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
| props.botId | Yes | string | Agent ID |
| props.title | No | string | Conversation title |
Return Value
Promise<IConversation>
See related type: IConversation
getConversation()
Gets the conversation list.
Usage Example
const res = await ai.bot.getConversation({
botId: "botId-xxx",
pageSize: 10,
pageNumber: 1,
isDefault: false,
});
Type Declaration
function getConversation(props: IBotGetConversation);
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
| props.botId | Yes | string | Agent ID |
| props.pageSize | No | number | Page size, default 10 |
| props.pageNumber | No | number | Page index, default 1 |
| props.isDefault | No | boolean | Whether to only get default conversation |
deleteConversation()
Deletes a specified conversation.
Usage Example
await ai.bot.deleteConversation({
botId: "botId-xxx",
conversationId: "conv-123",
});
Type Declaration
function deleteConversation(props: IBotDeleteConversation);
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
| props.botId | Yes | string | Agent ID |
| props.conversationId | Yes | string | ID of conversation to delete |
speechToText()
Converts speech to text.
Usage Example
const res = await ai.bot.speechToText({
botId: "botId-xxx",
engSerViceType: "16k_zh",
voiceFormat: "mp3",
url: "https://example.com/audio.mp3",
});
Type Declaration
function speechToText(props: IBotSpeechToText);
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
| props.botId | Yes | string | Agent ID |
| props.engSerViceType | Yes | string | Engine type, e.g., "16k_zh" |
| props.voiceFormat | Yes | string | Audio format, e.g., "mp3" |
| props.url | Yes | string | Audio file URL |
| props.isPreview | No | boolean | Whether in preview mode |
textToSpeech()
Converts text to speech.
Usage Example
const res = await ai.bot.textToSpeech({
botId: "botId-xxx",
voiceType: 1,
text: "Hello, I am an AI assistant",
});
Type Declaration
function textToSpeech(props: IBotTextToSpeech);
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
| props.botId | Yes | string | Agent ID |
| props.voiceType | Yes | number | Voice type |
| props.text | Yes | string | Text to convert |
| props.isPreview | No | boolean | Whether in preview mode |
getTextToSpeechResult()
Gets the text-to-speech result.