跳转到内容

文件、知识与费用接口

以下路径均位于 /api/v1,需要登录和相应资料权限。

方法 路径 用途
GET /knowledge/spaces 列出资料空间
POST /knowledge/spaces 创建资料空间
GET /knowledge/spaces/{space_id}/documents 列出空间文档
POST /knowledge/documents 上传文档
GET /knowledge/documents/{document_id} 查询文档状态与信息
POST /knowledge/documents/{document_id}/confirm 确认后续处理
POST /knowledge/documents/{document_id}/retry-parse 按状态重试解析
GET /knowledge/documents/{document_id}/download 下载原文档

POST /knowledge/documents 接收 multipart/form-data:

参数 位置 说明
file 表单文件 必填
knowledge_space_id 表单 可选 UUID;指定时检查空间所有权
processing_mode 表单 默认 chunk,其他模式需符合后端支持范围
source_tag 表单 可选来源标记
redact 查询参数 可选布尔值,默认 false

上传返回 KnowledgeDocumentUploadResponse,并非统一 BaseResponse。查询状态后,按流程确认;已经激活的文档会直接返回当前状态,不应为了获取结果反复确认。

方法 路径 用途
GET /sessions/{session_id}/files 列出文件
POST /sessions/{session_id}/files 上传文件并获取处理结果或预估
POST /sessions/{session_id}/files/{file_id}/confirm 确认激活
GET /sessions/{session_id}/files/{file_id}/download 下载文件

注意这里的文件路径是 /sessions/...,不是 /chat/sessions/...。会话文件上传使用 BaseResponse,其 data 内包含 file 和可能存在的 estimate。

方法 路径 用途
GET /billing/account 账户概览
GET /billing/authorization?module=knowledge 查询知识模块的工具费用授权信息
GET /billing/tasks 分页查询任务
GET /billing/tasks/{task_id} 查询任务明细
GET /billing/ledger 分页查询流水
GET /billing/prices 查询当前价格

单位范围可通过支持的接口传入 organization_id,后端仍会校验权限。任务列表支持 offset 与 limit,其中 limit 为 1–100。

const response = await fetch('/api/v1/billing/tasks?offset=0&limit=20', {
credentials: 'include',
});
if (!response.ok) throw new Error(`查询失败:${response.status}`);
const tasks = await response.json();
console.log(tasks);

上传、确认、重试与知识检索中的部分动作使用独立的工具费用授权:

请求头 说明
X-Billing-Max-Cost 用户明确确认的费用上限,人民币正数,最多六位小数
X-Billing-Request-Id 本次任务标识,1–128 个字符,可使用字母、数字、下划线、点、冒号与连字符

强制按量计费模式会检查预算,并要求能取得任务标识。不同阶段可能有服务端生成的幂等键,客户端仍应为操作提供明确的请求编号。请求内容或授权变化后,不要复用原编号伪装成同一任务。

下面的函数在已登录的应用同源环境中使用。file 来自用户选择,maxCost 必须来自用户确认;函数不会自动执行后续确认阶段。

async function uploadKnowledge(file, spaceId, maxCost, requestId) {
if (!(file instanceof File)) throw new Error('请先选择文件');
if (!maxCost || !requestId) throw new Error('需要费用授权与任务编号');
const form = new FormData();
form.append('file', file);
form.append('knowledge_space_id', spaceId);
form.append('processing_mode', 'chunk');
const response = await fetch('/api/v1/knowledge/documents', {
method: 'POST',
credentials: 'include',
headers: {
'X-Billing-Max-Cost': maxCost,
'X-Billing-Request-Id': requestId,
},
body: form,
});
if (!response.ok) throw new Error(await response.text());
return await response.json();
}

不要手动设置 multipart/form-data 的 Content-Type,浏览器会生成带边界的请求头。后续确认应先展示文件结果和预估,再独立发起授权操作。