快速开始
建库、上传图片并找到它。
准备 API Key,将下方代码保存为 quickstart.mjs,在 Node.js 中运行。示例建库、上传图片、按标签检索,并给出原图与缩略图地址。
npm install cloudinary@2
# Set PRAXIS_API_KEY in your environment.
node quickstart.mjsimport { v2 as cloudinary } from "cloudinary";
const base = "https://api.deepraxis.ai";
const key = process.env.PRAXIS_API_KEY;
if (!key) throw new Error("Set PRAXIS_API_KEY");
async function media(path, method = "GET", body) {
const response = await fetch(base + "/media/v1" + path, {
method,
headers: { Authorization: `Bearer ${key}`, "Content-Type": "application/json" },
...(body === undefined ? {} : { body: JSON.stringify(body) }),
});
if (!response.ok) throw new Error(`Media HTTP ${response.status}`);
return response.json();
}
const library = await media("/libraries", "POST", { name: "Agent media" });
cloudinary.config({
cloud_name: library.id,
oauth_token: key,
upload_prefix: base + "/cloudinary",
});
const asset = await cloudinary.uploader.upload(
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAIAAABMXPacAAABWklEQVR4nO3OQQ0AMBAEofVv+iqDxzRBALvtg/wgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwgzg/i/CDOD+L8IM4P4vwg7gEgaMOyrMtNTwAAAABJRU5ErkJggg==",
{ public_id: "quickstart/pixel", tags: ["docs"] },
);
const found = await cloudinary.search.expression("resource_type:image AND tags:docs").execute();
const delivery = asset.secure_url;
const thumbnail = delivery.replace("/upload/", "/upload/w_160,c_fit/");
const understandings = await media(`/libraries/${library.id}/assets/${asset.asset_id}/understandings`);
const current = await media(`/libraries/${library.id}/assets/${asset.asset_id}`);
const matches = current.semantic === "ready"
? await media(`/libraries/${library.id}/search/semantic`, "POST", { query: "a small image", top_k: 5 })
: null;
console.log({ library_id: library.id, asset_id: asset.asset_id, found, delivery, thumbnail, understandings, matches });理解异步完成。上传响应不含处理状态;用 GET 读取素材,查看 processing 与 semantic。实测量级:图片约半分钟,3 秒视频约 50 秒,raw 文件立即就绪(不做理解)。首次读取可能为空;matches 为 null 时稍后重新读取素材,semantic 为 ready 后再检索。理解与语义检索可用;上传、表达式检索和原文件读取无需等待理解完成。
示例保留库供继续使用。用完后,把 LIBRARY_ID 设为输出的 library_id 并删除:
curl -X DELETE "https://api.deepraxis.ai/media/v1/libraries/${LIBRARY_ID}" \
-H "Authorization: Bearer $PRAXIS_API_KEY"