跳转到内容

AI 代理

client.aiAgent(TypeScript)/ client.ai_agent(Python)提供聊天流式传输、嵌入管理、parquet 生成和 AI 代理的分布式追踪。

如需逐步指南,请参阅 SDK 指南中的 AI Agent。


Schema

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 事件流解析出的纯文本 chunk(要求代理设置 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 schema
处理嵌入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。响应体要么是 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":"..."}),并以字符串形式逐个产出每个文本 chunk。要求代理在创建时设置了 streaming: true — 否则服务器返回 text/plain(没有 data: 行),此生成器将产出 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

在父会话内对子代理进行范围限定的流式聊天。

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 schema 和字段类型。

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