Skip to main content

Overview

Provides Cloud Development AI integration capabilities, enabling rapid access to large language models and AI agents.


Basic Usage Examples

Publishable Key can be generated in Cloud Development Platform/API Key Configuration

Type Declaration

function ai(): AI;

Return Value

Returns the newly created AI instance.

import cloudbase from "@cloudbase/js-sdk";

// Initialization
const app = cloudbase.init({
env: "your-env-id", // Replace with your environment ID
region: "ap-shanghai", // region, defaults to Shanghai
accessKey: "", // Enter the generated Publishable Key
});

// If the accessKey is filled in, this step is not required
await app.auth.signInAnonymously();

const ai = app.ai();

AI

Class used to create AI models.

createModel

function createModel(model: string): ChatModel;

Create the specified AI model.

  • Create a new AI model instance
  • Return a model instance that implements the ChatModel abstract class
  • This instance provides capabilities related to AI-generated text.

参数

model
string

Model identifier, such as 'hunyuan-exp', 'hunyuan-lite', etc.

返回

ChatModel
ChatModel

A model instance that implements the ChatModel abstract class, providing capabilities related to AI-generated text

示例

// Create Hunyuan exp model
const model = ai.createModel("hunyuan-exp");

// Create Hunyuan lite model
const liteModel = ai.createModel("hunyuan-lite");

bot

An instance of the Bot class is mounted, which aggregates a series of methods for interacting with the Agent. For details, refer to the Bot class documentation.

Usage Examples

const agentList = await ai.bot.list({ pageNumber: 1, pageSize: 10 });

registerFunctionTool

function registerFunctionTool(functionTool: FunctionTool): void;

Register function tools. When making large model calls, you can inform the large model of the available function tools. If the large model's response is parsed as a tool call, the corresponding function tool will be automatically invoked.

参数

functionTool
FunctionTool

Definition of the function tool to be registered

返回

void
undefined

No return value

示例

// Define a tool to get weather
const getWeatherTool = {
name: "get_weather",
description: "Returns the weather information for a specific city. Invocation example: get_weather({city: 'Beijing'})",
fn: ({ city }) => `The weather in ${city} is: clear and crisp autumn weather!`,
parameters: {
type: "object",
properties: {
city: {
type: "string",
description: "City to query",
},
},
required: ["city"],
},
};

// Register a tool
ai.registerFunctionTool(getWeatherTool);

// Use tools for conversation
const model = ai.createModel("hunyuan-exp");
const result = await model.generateText({
model: "hunyuan-turbo",
tools: [getWeatherTool],
messages: [
{
role: "user",
content: "Please tell me the weather conditions in Beijing",
},
],
});

console.log(result.text);

ChatModel

This abstract class describes the interface provided by the AI text generation model class.

generateText

function generateText(data: BaseChatModelInput): Promise<{
rawResponses: Array<unknown>;
text: string;
messages: Array<ChatModelMessage>;
usage: Usage;
error?: unknown;
}>;

Invoking large models to generate text.

  • Send a message to the large model and retrieve the generated text response
  • Supports complete dialogue context management
  • Return detailed invocation information and token consumption statistics

参数

data
BaseChatModelInput

Large model input parameters, including model configuration and message content

返回

Promise
object

Generated text response from the large model

示例

const model = ai.createModel("hunyuan-exp");
const result = await model.generateText({
model: "hunyuan-lite",
messages: [{ role: "user", content: "Hello, please introduce Li Bai." }],
});

console.log("Generated text:", result.text);
console.log("Consumed tokens:", result.usage);

streamText

function streamText(data: BaseChatModelInput): Promise<StreamTextResult>;

Streaming invocation of large models to generate text.

  • During streaming invocation, the generated text and other response data will be returned via SSE. The return value of this interface encapsulates SSE to varying degrees, allowing developers to retrieve both the text stream and the complete data stream according to their actual needs.
  • Streaming invocation of large models to generate text, supporting real-time retrieval of incremental content

参数

data
BaseChatModelInput

Large model input parameters, including model configuration and message content

返回

Promise
StreamTextResult

Results of streaming text generation, including text streams and data streams

示例

const model = ai.createModel("hunyuan-exp");
const result = await model.streamText({
model: "hunyuan-lite",
messages: [{ role: "user", content: "Hello, please introduce Li Bai." }],
});

// Get text stream
for await (let chunk of result.textStream) {
console.log("Received text chunk:", chunk);
}

// 1
// Add
// 1
// Result
// Is
// 2
// .

// Get data stream
for await (let data of result.dataStream) {
console.log("Received data block:", data);
}

// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}
// {created: 1723013866, id: "a95a54b5c5d2144eb700e60d0dfa5c98", model: "hunyuan-lite", version: "202404011000", choices: Array(1), …}

Bot

A class for interacting with the Agent.

get

function get(props: { botId: string }): Promise<BotInfo>;

Retrieve information about an Agent.

  • Retrieve detailed Agent information based on Agent ID.
  • Return complete Agent information including basic configuration, welcome message, avatar, etc.

参数

props
object

Parameters for retrieving Agent information

返回

Promise
BotInfo

Detailed information of the Agent

示例

const res = await ai.bot.get({ botId: "botId-xxx" });
console.log("Agent information:", res);

list

function list(props: {
name: string;
introduction: string;
information: string;
enable: boolean;
pageSize: number;
pageNumber: number;
}): Promise<AgentListResult>;

Batch retrieval of information for multiple Agents.

  • Query and filter the available Agent list.
  • Support paginated queries and conditional filtering
  • Return Agent's basic information and configuration details
  • Applicable for building applications such as Agent selectors and Agent management interfaces.

参数

props
object

Parameters for querying the Agent list

返回

Promise
AgentListResult

Agent list query result

示例

const agentList = await ai.bot.list({
pageNumber: 1,
pageSize: 10,
name: "",
enable: true,
information: "",
introduction: "",
});

console.log("Total Agents:", agentList.total);
console.log("Agent list:", agentList.botList);

sendMessage

function sendMessage(props: {
botId: string;
msg: string;
history: Array<{
role: string;
content: string;
}>;
}): Promise<StreamResult>;

Converse with the Agent.

  • Response will be returned via SSE. The return value of this interface encapsulates SSE to varying degrees, allowing developers to retrieve both the text stream and the complete data stream according to their actual needs.
  • Supports multi-turn conversation context management
  • Returns streaming responses, supporting real-time retrieval of Agent replies
  • Applicable for building applications such as chatbots and intelligent assistants.

参数

props
object

Parameters for sending messages

返回

Promise
StreamResult

Results of streaming conversation, including text streams and data streams

示例

const res = await ai.bot.sendMessage({
botId: "botId-xxx",
history: [{ content: "You are Li Bai.", role: "user" }],
msg: "Hello",
});

// Get text stream
for await (let text of res.textStream) {
console.log("Agent reply:", text);
}

// Get data stream
for await (let data of res.dataStream) {
console.log("Detailed data:", data);
}

getChatRecords

function getChatRecords(props: {
botId: string;
sort: string;
pageSize: number;
pageNumber: number;
}): Promise<ChatRecordsResult>;

Retrieve chat history.

  • Retrieve the chat history of the specified Agent.
  • Support paginated queries and sorting
  • Return complete conversation history information
  • Suitable for building chat history viewing features

参数

props
object

parameters for retrieving chat history

返回

Promise
ChatRecordsResult

Chat history query result

示例

const records = await ai.bot.getChatRecords({
botId: "botId-xxx",
pageNumber: 1,
pageSize: 10,
sort: "asc",
});

console.log("Total records count:", records.total);
console.log("Record list:", records.recordList);

sendFeedback

function sendFeedback(props: {
userFeedback: IUserFeedback;
botId: string;
}): Promise<void>;

Send feedback for a specific chat message.

  • Evaluate and provide feedback for specified chat messages.
  • Supports various feedback methods such as rating, commenting, and tagging.
  • Help improve the quality of Agent responses
  • Suitable for building user feedback systems

参数

props
object

Parameters for sending feedback

返回

Promise
void

No return value

示例

const res = await ai.bot.sendFeedback({
userFeedback: {
botId: "botId-xxx",
recordId: "recordId-xxx",
comment: "Excellent",
rating: 5,
tags: ["Graceful"],
aiAnswer: "Fallen petals are scattered in profusion",
input: "Give me an idiom",
type: "upvote",
},
botId: "botId-xxx",
});

console.log("Feedback sent successfully");

getFeedback

function getFeedback(props: {
botId: string;
type: string;
sender: string;
senderFilter: string;
minRating: number;
maxRating: number;
from: number;
to: number;
pageSize: number;
pageNumber: number;
}): Promise<FeedbackResult>;

Retrieve existing feedback.

  • Query the user feedback records of the specified Agent.
  • Support multiple filter conditions: time range, score range, user filtering, etc.
  • Return paginated feedback results and statistics
  • Suitable for building feedback analysis and management systems

参数

props
object

Parameters for querying feedback information

返回

Promise
FeedbackResult

Feedback query results

示例

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",
});

console.log("Total Feedback:", res.total);
console.log("Feedback List:", res.feedbackList);

uploadFiles

function uploadFiles(props: {
botId: string;
fileList: Array<{
fileId: string;
fileName: string;
type: "file";
}>;
}): Promise<void>;

Upload files from cloud storage to the Agent for document-based chatting.

  • Supports uploading files from cloud storage to the specified Agent
  • Uploaded files can be used for document chat functionality.
  • Supports batch uploading of multiple files.
  • Applicable for building application scenarios such as document Q&A and file analysis.

参数

props
object

File upload parameters

返回

Promise
void

No return value

示例

// Upload a single file to the specified Agent.
await ai.bot.uploadFiles({
botId: "botId-xxx",
fileList: [
{
fileId: "cloud://document.docx",
fileName: "document.docx",
type: "file",
},
],
});

console.log("File uploaded successfully");

getRecommendQuestions

function getRecommendQuestions(props: {
botId: string;
name: string;
introduction: string;
agentSetting: string;
msg: string;
history: Array<{
role: string;
content: string;
}>;
}): Promise<StreamResult>;

Generating recommended questions based on conversation context to intelligently generate relevant question suggestions.

  • Intelligently recommend relevant questions based on current conversation content and historical records
  • Supports streaming responses for real-time retrieval of recommended questions
  • Applicable for building applications such as intelligent conversational assistants and chatbots.
  • Help users discover more related topics to enhance the conversation experience

参数

props
object

Parameters for retrieving recommended questions

返回

Promise
StreamResult

streaming recommended questions

示例

// Retrieve recommended questions
const res = await ai.bot.getRecommendQuestions({
botId: "botId-xxx",
name: "Smart Assistant",
introduction: "I am an intelligent conversation assistant",
agentSetting: "Professional and friendly AI assistant",
msg: "Hello, I'd like to learn about artificial intelligence.",
history: [
{ content: "Who are you?", role: "user" },
{ content: "I am an intelligent assistant that can help you answer various questions", role: "assistant" },
],
});

// Retrieve recommended questions via streaming
for await (let question of res.textStream) {
console.log("Recommended questions:", question);
}

createConversation

function createConversation(props: {
botId: string;
title?: string;
}): Promise<IConversation>;

Create a new conversation with the Agent to establish an independent conversation session.

  • Create a new conversation session for the specified Agent
  • Supports custom conversation titles for easy management and identification
  • Each conversation session independently stores conversation history
  • Suitable for multi-user, multi-scenario conversation management requirements

参数

props
object

Parameters for creating a conversation

返回

Promise
IConversation

Created conversation information

示例

// Create a new conversation session
const conversation = await ai.bot.createConversation({
botId: "botId-xxx",
title: "Technical Consultation Conversation",
});

console.log("Conversation created successfully:", conversation.conversationId);
console.log("Conversation title:", conversation.title);

getConversation

function getConversation(props: {
botId: string;
pageSize?: number;
pageNumber?: number;
isDefault?: boolean;
}): Promise<ConversationListResult>;

Retrieve the Agent's conversation list, supporting pagination and conditional filtering.

  • Query all conversation records of the specified Agent.
  • Support pagination to facilitate the management of large volumes of conversations.
  • Filter default or all conversations.
  • Suitable for scenarios such as conversation management and historical record viewing

参数

props
object

Parameters for retrieving the conversation list

返回

Promise
ConversationListResult

Conversation list query result

示例

// Get conversation list
const result = await ai.bot.getConversation({
botId: "botId-xxx",
pageSize: 10,
pageNumber: 1,
isDefault: false,
});

console.log(`Total ${result.total} conversations`);
result.conversations.forEach((conversation, index) => {
console.log(
`${index + 1}. ${conversation.title} (${conversation.messageCount} messages)`
);
});

deleteConversation

function deleteConversation(props: {
botId: string;
conversationId: string;
}): Promise<void>;

Delete the specified conversation session and clear the conversation history.

  • Permanently delete the specified conversation session.
  • Clear all message records in the conversation
  • Free up storage space and optimize system performance
  • Suitable for scenarios such as conversation management and data cleanup

参数

props
object

Parameters for deleting a conversation

返回

Promise
void

Deletion operation completed, no return value

示例

// Delete the specified conversation
await ai.bot.deleteConversation({
botId: "botId-xxx",
conversationId: "conv-123",
});

console.log("Conversation deleted successfully");

speechToText

function speechToText(props: {
botId: string;
engSerViceType: string;
voiceFormat: string;
url: string;
isPreview?: boolean;
}): Promise<SpeechToTextResult>;

Convert speech files to text, supporting multiple audio formats and language recognition.

  • Supports multiple audio formats: MP3, WAV, AAC, etc.
  • Supports multiple language recognition engines
  • Can process local or remote audio files
  • Suitable for scenarios such as voice assistants, meeting minutes, voice notes, etc.

参数

props
object

Speech-to-text parameters

返回

Promise
SpeechToTextResult

ASR result

示例

// Convert speech files to text
const result = await ai.bot.speechToText({
botId: "speech-bot-id",
engSerViceType: "16k_zh",
voiceFormat: "mp3",
url: "https://example.com/audio.mp3",
});

console.log("Recognition result:", result.text);
console.log("Confidence:", result.confidence);
console.log("Audio duration:", result.duration, "seconds");
console.log("Recognized language:", result.language);

textToSpeech

function textToSpeech(props: {
botId: string;
voiceType: number;
text: string;
isPreview?: boolean;
}): Promise<TextToSpeechResult>;

Convert text to speech, supporting multiple voice types and timbre options.

  • Supports multiple voice types and timbre options
  • Adjustable parameters such as speech rate and pitch
  • Support segmented conversion of long texts
  • Suitable for scenarios such as voice broadcasting, audiobooks, voice assistants, etc.

参数

props
object

Text-to-speech parameters

返回

Promise
TextToSpeechResult

Text-to-speech result

示例

// Convert text to speech
const result = await ai.bot.textToSpeech({
botId: "tts-bot-id",
voiceType: 1, // standard female voice
text: "Hello, I am an AI voice assistant, pleased to assist you.",
});

console.log("TTS completed:");
console.log("Audio URL:", result.audioUrl);
console.log("Audio duration:", result.duration, "seconds");
console.log("File format:", result.format);
console.log("File size:", result.size, "bytes");

getTextToSpeechResult

function getTextToSpeechResult(props: {
botId: string;
taskId: string;
isPreview?: boolean;
}): Promise<TextToSpeechResult>;

Get the result of a text-to-speech task, query the completion status of the TTS task and the generated audio file.

  • Query the status and result of asynchronous TTS tasks
  • Retrieve generated audio file information
  • Supports preview mode and production mode
  • Suitable for asynchronous scenarios such as batch TTS and long-text processing

参数

props
object

Parameters for retrieving TTS results

返回

Promise
TextToSpeechResult

TTS task result

示例

// Query TTS task result
const result = await ai.bot.getTextToSpeechResult({
botId: "tts-bot-id",
taskId: "task-123456",
});

console.log("Task Status:", result.status);
console.log("Task Progress:", result.progress, "%");

if (result.status === "completed") {
console.log("Audio URL:", result.audioUrl);
console.log("Audio duration:", result.duration, "seconds");
console.log("File format:", result.format);
console.log("File size:", result.size, "bytes");
} else if (result.status === "processing") {
console.log("The task is still being processed, please try again later.");
} else if (result.status === "failed") {
console.log("Task processing failed");
}

Complete Type Definitions

IBotCreateConversation

interface IBotCreateConversation {
botId: string;
title?: string;
}

IBotGetConversation

interface IBotGetConversation {
botId: string;
pageSize?: number;
pageNumber?: number;
isDefault?: boolean;
}

IBotDeleteConversation

interface IBotDeleteConversation {
botId: string;
conversationId: string;
}

IBotSpeechToText

interface IBotSpeechToText {
botId: string;
engSerViceType: string;
voiceFormat: string;
url: string;
isPreview?: boolean;
}

IBotTextToSpeech

interface IBotTextToSpeech {
botId: string;
voiceType: number;
text: string;
isPreview?: boolean;
}

IBotGetTextToSpeechResult

interface IBotGetTextToSpeechResult {
botId: string;
taskId: string;
isPreview?: boolean;
}

BaseChatModelInput

interface BaseChatModelInput {
model: string;
messages: Array<ChatModelMessage>;
temperature?: number;
topP?: number;
tools?: Array<FunctionTool>;
toolChoice?: "none" | "auto" | "custom";
maxSteps?: number;
onStepFinish?: (prop: IOnStepFinish) => unknown;
}
BaseChatModelInput Property NameTypeDescription
modelstringModel name.
messagesArray<ChatModelMessage>Message list.
temperaturenumberSampling temperature, which controls the randomness of the output.
topPnumberTemperature sampling, where the model considers tokens within the top_p probability mass.
toolsArray<FunctionTool>List of tools available for large models.
toolChoicestringSpecifies the method for large models to select tools.
maxStepsnumberMaximum number of requests to the large model.
onStepFinish(prop: IOnStepFinish) => unknownThe callback function triggered when a request to the large model is completed.

BotInfo

interface BotInfo {
botId: string;
name: string;
introduction: string;
agentSetting: string;
welcomeMessage: string;
avatar: string;
background: string;
tags: Array<string>;
isNeedRecommend: boolean;
knowledgeBase: Array<string>;
type: string;
initQuestions: Array<string>;
enable: true;
}

IUserFeedback

interface IUserFeedback {
recordId: string;
type: string;
botId: string;
comment: string;
rating: number;
tags: Array<string>;
input: string;
aiAnswer: string;
}

ChatModelMessage

type ChatModelMessage =
| UserMessage
| SystemMessage
| AssistantMessage
| ToolMessage;

UserMessage

type UserMessage = {
role: "user";
content: string;
};

SystemMessage

type SystemMessage = {
role: "system";
content: string;
};

AssistantMessage

type AssistantMessage = {
role: "assistant";
content?: string;
tool_calls?: Array<ToolCall>;
};

ToolMessage

type ToolMessage = {
role: "tool";
tool_call_id: string;
content: string;
};

ToolCall

export type ToolCall = {
id: string;
type: string;
function: { name: string; arguments: string };
};

FunctionTool

Tool definition type.

type FunctionTool = {
name: string;
description: string;
fn: CallableFunction;
parameters: object;
};
FunctionTool Property NameTypeDescription
namestringTool name.
descriptionstringDescription of the tool. A clear tool description helps the large model understand the tool's purpose.
fnCallableFunctionThe tool's execution function. When the AI SDK parses that the large model's response requires calling this tool, it invokes this function and returns the result to the large model.
parametersobjectThe input parameters for the tool's execution function, which must be defined using JSON Schema format.

IOnStepFinish

The type of input parameters for the callback function triggered after the large model responds.

interface IOnStepFinish {
messages: Array<ChatModelMessage>;
text?: string;
toolCall?: ToolCall;
toolResult?: unknown;
finishReason?: string;
stepUsage?: Usage;
totalUsage?: Usage;
}
IOnStepFinish Property NameTypeDescription
messagesArray<ChatModelMessage>List of all messages up to the current step.
textstringThe text of the current response.
toolCallToolCallThe tool called by the current response.
toolResultunknownThe corresponding tool call result.
finishReasonstringReason for termination of large model inference.
stepUsageUsageThe tokens consumed by the current step.
totalUsageUsageThe total tokens consumed up to the current step.

Usage

type Usage = {
completion_tokens: number;
prompt_tokens: number;
total_tokens: number;
};

IConversation

Agent session.

interface IConversation {
id: string;
envId: string;
ownerUin: string;
userId: string;
conversationId: string;
title: string;
startTime: string; // date-time format
createTime: string;
updateTime: string;
}