跳到內容

AI 代理

client.aiAgent (TypeScript) / client.ai_agent (Python) 提供聊天串流、嵌入管理、parquet 生成與 AI 代理的分散式追蹤。

如需逐步指南,請參閱 SDK 指南中的 AI Agent。


結構定義

StreamChatBody

初始化串流聊天會話的參數。

欄位類型必填說明
assistant_idstring✓要聊天的 AI 代理 ID
messagesarray✓對話歷史記錄(role + content 成對)
idstring要繼續的現有聊天會話 ID
model_idstring覆寫代理的預設 LLM 模型
provider_idstring覆寫代理的預設 LLM 提供者
user_idstring外部使用者識別碼

StreamSubAgentChatBody

欄位類型必填說明
assistant_idstring✓AI 代理 ID
session_idstring✓父會話 ID
chat_idstring✓會話中的聊天 ID
messagesarray✓對話訊息

方法

方法TypeScriptPython說明
串流聊天streamChatstream_chat與 AI 代理開始串流聊天會話 — 傳回原始 Response
串流聊天(解析後文字)streamChatTextstream_chat_text包裝 streamChat,並逐一產出從 SSE 事件串流解析出的純文字片段(需要代理設定 streaming: true)
串流子代理聊天streamSubAgentChatstream_sub_agent_chat在子代理會話範圍內進行串流聊天
取得子代理歷史getSubAgentHistoryget_sub_agent_history擷取子代理聊天的訊息歷史
列出聊天listChatslist_chats列出組織的聊天會話
取得聊天getChatget_chat取得單一聊天會話
刪除聊天deleteChatdelete_chat刪除聊天會話
提示建議getAgentPromptSuggestionget_agent_prompt_suggestion取得代理的建議提示
分類檔案classifyFileclassify_file將檔案分類至類別
建議欄位類型suggestFieldTypessuggest_field_types從範例文件建議 JSON 結構
處理嵌入processEmbeddingprocess_embedding將檔案嵌入知識庫
列出嵌入檔案listEmbeddingFileslist_embedding_files列出所有已嵌入的檔案
取得嵌入檔案getEmbeddingFileget_embedding_file取得單一已嵌入的檔案
預覽嵌入檔案previewEmbeddingFilepreview_embedding_file預覽已嵌入的內容
更新嵌入狀態updateEmbeddingFileStatusupdate_embedding_file_status更新檔案嵌入狀態
刪除嵌入檔案deleteEmbeddingFiledelete_embedding_file從知識庫移除檔案
產生 parquetgenerateParquetgenerate_parquet將資料陣列轉換為 parquet 檔案
列出 parquet 檔案listParquetFileslist_parquet_files列出已生成的 parquet 檔案
刪除 parquet 檔案deleteParquetFiledelete_parquet_file依名稱刪除 parquet 檔案
取得追蹤清單getTracesget_traces查詢分散式追蹤
取得追蹤getTraceget_trace依 ID 取得單一追蹤
取得追蹤服務getTraceServicesget_trace_services列出發出追蹤的服務

streamChat / stream_chat

傳回原始 HTTP Response。其 body 為 text/event-stream(當代理設定 streaming: true 時)或 text/plain(當 streaming: false 時)。SSE 的情況建議改用下方的 streamChatText 輔助方法 — 它會替你完成解析。

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)); // 原始 SSE 位元組
}
} else {
console.log(await res.text()); // 完整回覆,純文字
}

streamChatText / stream_chat_text

streamChat 的便利包裝。它會解析 Vercel-AI-SDK UI-Message-Stream(data: {"type":"text-delta","delta":"..."}),並將每個文字片段以字串形式逐一產出。需要代理以 streaming: true 建立 — 否則伺服器會回傳 text/plain(沒有 data: 行),此產生器將產出 0 個片段。

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

在父會話內,進行限定於子代理範圍的串流聊天。

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

將檔案嵌入代理的知識庫。

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

classifyFile / classify_file

根據檔案內容將其分類至預定義類別。

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

suggestFieldTypes / suggest_field_types

分析範例文件並建議用於文件擷取的 JSON 結構與欄位類型。

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

previewEmbeddingFile / preview_embedding_file

在處理前預覽已嵌入檔案的內容。

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

updateEmbeddingFileStatus / update_embedding_file_status

更新嵌入檔案的處理狀態。

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

generateParquet / generate_parquet

將記錄陣列轉換為 parquet 檔案以進行結構化資料匯入。

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

getTraces / get_traces

查詢分散式追蹤以進行除錯和可觀測性。

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