Chuyển đến nội dung

AI Agent

client.aiAgent (TypeScript) / client.ai_agent (Python) cung cấp chat streaming, quản lý embedding, sinh parquet, và distributed tracing cho AI agents.

Để xem hướng dẫn chi tiết, tham khảo AI Agent trong hướng dẫn SDK.


Schema

StreamChatBody

Tham số để khởi tạo một phiên chat streaming.

TrườngKiểuBắt buộcMô tả
assistant_idstring✓ID của AI agent để chat
messagesarray✓Lịch sử hội thoại (cặp role + content)
idstringID phiên chat có sẵn để tiếp tục
model_idstringGhi đè mô hình LLM mặc định của agent
provider_idstringGhi đè nhà cung cấp LLM mặc định của agent
user_idstringĐịnh danh người dùng bên ngoài

StreamSubAgentChatBody

TrườngKiểuBắt buộcMô tả
assistant_idstring✓ID của AI agent
session_idstring✓ID phiên cha
chat_idstring✓ID chat trong phiên
messagesarray✓Tin nhắn hội thoại

Phương thức

Phương thứcTypeScriptPythonMô tả
Stream chatstreamChatstream_chatBắt đầu phiên chat streaming với AI agent — trả về Response thô
Stream chat (văn bản đã phân tích)streamChatTextstream_chat_textBao bọc streamChat và trả về từng đoạn văn bản thuần được phân tích từ luồng sự kiện SSE (yêu cầu agent có streaming: true)
Stream chat sub-agentstreamSubAgentChatstream_sub_agent_chatStreaming chat trong phạm vi phiên sub-agent
Lấy lịch sử sub-agentgetSubAgentHistoryget_sub_agent_historyLấy lịch sử tin nhắn của sub-agent chat
Danh sách chatlistChatslist_chatsDanh sách phiên chat của tổ chức
Lấy chatgetChatget_chatLấy một phiên chat
Xóa chatdeleteChatdelete_chatXóa một phiên chat
Gợi ý promptgetAgentPromptSuggestionget_agent_prompt_suggestionLấy gợi ý prompt cho agent
Phân loại fileclassifyFileclassify_filePhân loại file vào danh mục
Gợi ý kiểu trườngsuggestFieldTypessuggest_field_typesGợi ý JSON schema từ tài liệu mẫu
Xử lý embeddingprocessEmbeddingprocess_embeddingNhúng file vào kho tri thức
Danh sách file embeddinglistEmbeddingFileslist_embedding_filesDanh sách các file đã nhúng
Lấy file embeddinggetEmbeddingFileget_embedding_fileLấy một file đã nhúng
Xem trước file embeddingpreviewEmbeddingFilepreview_embedding_fileXem trước nội dung đã nhúng
Cập nhật trạng thái embeddingupdateEmbeddingFileStatusupdate_embedding_file_statusCập nhật trạng thái nhúng file
Xóa file embeddingdeleteEmbeddingFiledelete_embedding_fileXóa file khỏi kho tri thức
Sinh parquetgenerateParquetgenerate_parquetChuyển đổi mảng dữ liệu thành file parquet
Danh sách file parquetlistParquetFileslist_parquet_filesDanh sách file parquet đã sinh
Xóa file parquetdeleteParquetFiledelete_parquet_fileXóa file parquet theo tên
Lấy tracesgetTracesget_tracesTruy vấn distributed traces
Lấy tracegetTraceget_traceLấy một trace theo ID
Lấy dịch vụ tracegetTraceServicesget_trace_servicesDanh sách dịch vụ phát ra traces

streamChat / stream_chat

Trả về một HTTP Response thô. Body có dạng text/event-stream (khi agent có streaming: true) hoặc text/plain (khi streaming: false). Với trường hợp SSE, nên dùng helper streamChatText bên dưới — nó sẽ phân tích giúp bạn.

const res = await client.aiAgent.streamChat({
assistant_id: "agent_id",
messages: [{ role: "user", content: "Hello" }],
user_id: "user_123",
});
if ((res.headers.get("content-type") ?? "").includes("text/event-stream")) {
const reader = res.body!.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
console.log(decoder.decode(value)); // raw SSE bytes
}
} else {
console.log(await res.text()); // full reply, plain text
}

streamChatText / stream_chat_text

Wrapper tiện lợi bao quanh streamChat. Phân tích Vercel-AI-SDK UI-Message-Stream (data: {"type":"text-delta","delta":"..."}) và trả về từng đoạn văn bản dưới dạng chuỗi. Yêu cầu agent đã được tạo với streaming: true — nếu không, server trả về text/plain (không có dòng data:) và generator này trả về 0 chunk.

for await (const chunk of client.aiAgent.streamChatText({
assistant_id: "agent_id",
messages: [{ role: "user", content: "Hello" }],
})) {
process.stdout.write(chunk);
}

streamSubAgentChat / stream_sub_agent_chat

Stream chat trong phạm vi sub-agent trong một phiên cha.

const response = await client.aiAgent.streamSubAgentChat({
assistant_id: "agent_id",
session_id: "session_id",
chat_id: "chat_id",
messages: [{ role: "user", content: "Follow-up question" }],
});

listChats / list_chats

const { data: chats } = await client.aiAgent.listChats({
organization_id: "org_id",
user_id: "user_123",
limit: 20,
});

getSubAgentHistory / get_sub_agent_history

const history = await client.aiAgent.getSubAgentHistory({
session_id: "session_id",
chat_id: "chat_id",
});

processEmbedding / process_embedding

Nhúng file vào kho tri thức của agent.

const result = await client.aiAgent.processEmbedding({
fileId: "file_id",
options: { chunk_size: 512 },
});

classifyFile / classify_file

Phân loại file vào danh mục định sẵn dựa trên nội dung.

const result = await client.aiAgent.classifyFile({ fileId: "file_id" });
console.log(result.category);

suggestFieldTypes / suggest_field_types

Phân tích tài liệu mẫu và gợi ý JSON schema với kiểu dữ liệu cho trích xuất tài liệu.

const schema = await client.aiAgent.suggestFieldTypes({
fileUrls: ["https://example.com/invoice.pdf"],
});
console.log(schema);

previewEmbeddingFile / preview_embedding_file

Xem trước nội dung của file đã nhúng trước khi xử lý.

const preview = await client.aiAgent.previewEmbeddingFile({ fileId: "file_id" });
console.log(preview);

updateEmbeddingFileStatus / update_embedding_file_status

Cập nhật trạng thái xử lý của một file nhúng.

const result = await client.aiAgent.updateEmbeddingFileStatus("file_id", "completed");
console.log(result);

generateParquet / generate_parquet

Chuyển đổi mảng bản ghi thành file parquet để nhập dữ liệu có cấu trúc.

const result = await client.aiAgent.generateParquet({
data: [{ name: "Alice", score: 95 }, { name: "Bob", score: 87 }],
fileName: "scores",
folderName: "results",
});

getTraces / get_traces

Truy vấn distributed traces để debug và quan sát.

const traces = await client.aiAgent.getTraces({
service: "ai-agent",
limit: 50,
timeRange: 3600,
orgId: "org_id",
});