跳转到内容
EN

让本地编码代理把文件交给云端处理

把本地 policy.pdf 交给云端知识库,检索退款政策,再把检索结果保存为本地 result.json。字节不进入模型上下文。

准备

已按三种接入方式连接 Praxis,并能执行 codemode;密钥有同组织的 File 与 Knowledge 权限。准备一份未加密、非空、含退款政策的 PDF(不超过 100000000 字节),本地安装 curl。下面 JavaScript 均在 codemode 里执行,bash 均在本地 shell 执行;不要用云端 bash 读取本地 PDF。

占位符替换为前一步的真实返回值;每次 codemode 调用独立,不依赖上次变量。签名 URL 是凭据,只交给请求者,在关闭 shell 追踪的临时变量中使用,不写文件、日志或版本库。

1. 上传本地 PDF

终端窗口
wc -c < policy.pdf

将输出整数替换 local_byte_count,在云端签名:

import { praxis } from "praxis";
export default async () => praxis.file.uploads.createSigned({ body: {
filename: "policy.pdf", bytes: local_byte_count, content_type: "application/pdf", purpose: "user_data"
} });

等调用结束,将返回的 url 放入本地临时变量 upload_url,再运行:

终端窗口
curl --fail-with-body -T policy.pdf -H 'Content-Type: application/pdf' "$upload_url"

预期 201,得到 id(后文 file_id)。本例大小走单 PUT。成功后不要重放上传。

2. 创建知识库并入库

import { praxis } from "praxis";
export default async () => praxis.knowledge.vectorStores.create({ body: { name: "Policy transfer demo" } });

把返回的 id 替换为 store_id:

import { praxis } from "praxis";
export default async () => praxis.knowledge.vectorStores.files.create({
path: { vector_store_id: "store_id" }, body: { file_id: "file_id" }
});

预期附件 id 与 file_id 相同,状态可能为 in_progress。上传与入库是两步。

3. 等待可检索

import { praxis } from "praxis";
export default async () => praxis.knowledge.vectorStores.files.retrieve({
path: { vector_store_id: "store_id", file_id: "file_id" }
});

间隔 2 秒重复调用,最长等 5 分钟;只有 status: "completed" 才继续。failed 或 cancelled 时停止并查看 last_error;超时报告状态,不盲目创建重复附件。

4. 检索并生成结果文件

import { praxis } from "praxis";
export default async () => {
const result = await praxis.knowledge.vectorStores.search.create({
path: { vector_store_id: "store_id" },
body: { query: "What is the refund policy?", max_num_results: 5 }
});
if (!result.data.length) throw new Error("No matching chunks; check ingestion and PDF text");
const file = await praxis.file.uploadFile(
new File([JSON.stringify(result, null, 2)], "result.json", { type: "application/json" }),
{ purpose: "user_data" }
);
return { result_file_id: file.id, matches: result.data.length };
};

预期 matches 大于 0,并得到 result_file_id。结果文件包含检索响应,不是模型编写的答案。helper 等文件 active 才返回。

5. 下载并验证

把 file_id 换成上一步 result_file_id:

import { praxis } from "praxis";
export default async () => praxis.file.files.contentUrl.create({
path: { file_id: "file_id" }, body: { ttl_seconds: 3600 }
});
终端窗口
curl -fsSL -o result.json "$download_url"
终端窗口
test -s result.json
python3 -c 'import json; r=json.load(open("result.json")); assert len(r["data"]) > 0; print("search results saved")'

预期输出 search results saved,打开文件可核对退款条款来源。链接默认 900 秒,可重复下载,最长 604800 秒。

错误与清理

上传 400:核对准确字节数与 Content-Type;410 CAPABILITY_USED:上传已经成功,不要重试成功请求;链接 404:核对组织和文件状态,重新签名。若 File 为 processing,带间隔轮询 praxis.file.files.retrieve 到 active,失败或超时停止。内联 MCP FileRef(不超过 5 MiB)只进入对话,不会替你保存文件。

下载后可吊销链接,将 /c/ 后的 token 替换到下例;15 分钟内传播,不删除文件:

import { praxis } from "praxis";
export default async () => praxis.file.files.contentLinks.revoke({
path: { file_id: "RESULT_file_id" }, body: { token: "content_token" }
});

确认结果后再按组织的数据保留规则清理测试知识库、源 PDF 和结果文件;删除知识库不会自动删除源 File。

延伸

接入与完整限制 · 知识库快速开始 · 文件