Chuyển đến nội dung

Document AI

Document AI sử dụng vision models (ví dụ: GPT-4o) để trích xuất dữ liệu có cấu trúc từ PDF và hình ảnh — hóa đơn, biểu mẫu, biên lai, hợp đồng, hoặc bất kỳ tài liệu nào có văn bản nhìn thấy được.

SDK hiển thị hai namespace:

  • client.chatAi / client.chat_ai — xử lý tài liệu trực tiếp và liệt kê model
  • client.documentAi / client.document_ai — xử lý tài liệu dựa trên agent với quản lý schema, CRUD agent và điều phối đầu cuối

Khởi tạo client trước (xem Cài đặt hoặc Quick Start).


client.chatAi — Xử lý Tài liệu

Liệt kê model có sẵn

const models = await client.chatAi.listDocumentModels();

Xử lý tài liệu trực tiếp

const result = await client.chatAi.processDocument({
modelName: "gpt-4o",
url: "https://example.com/invoice.pdf",
organizationId: "org_xxx",
});

Tham số tham chiếu

Tham sốTrường TSTham số PythonLoạiBắt buộc
Tên modelmodelNamemodel_namestring / str
URL tài liệuurlurlstring / str
ID tổ chứcorganizationIdorganization_idstring / str
ID BoardboardIdboard_idstring / strKhông
Ngôn ngữlanguagelanguagestring / strKhông
Hướng dẫn bổ sungadditionalInstructionsadditional_instructionsstring / strKhông
Hướng dẫn tài liệu bổ sungadditionalDocumentInstructionsadditional_document_instructionsstring / strKhông
Tên model xử lýprocessModelNameprocess_model_namestring / strKhông
URL tệp để điềnfileUrlToFillfile_url_to_fillstring / strKhông
Công cụtoolstoolsRecord<string, unknown>[] / List[Dict]Không
UTC offsetutcutcnumber / intKhông
Kích thước chunkchunkSizechunk_sizenumber / intKhông
Tối đa đồng thờimaxConcurrentmax_concurrentnumber / intKhông
Số lần thử lại tối đamaxRetriesmax_retriesnumber / intKhông
Xử lý nâng caouseEnhancedProcessinguse_enhanced_processingboolean / boolKhông

Ví dụ điền form PDF

const result = await client.chatAi.processDocument({
modelName: "gpt-4o",
url: "https://example.com/blank-invoice.pdf",
organizationId: "org_xxx",
fileUrlToFill: "https://example.com/blank-invoice.pdf",
language: "en",
});
if (result.success && result.data.filledPdfUrl) {
console.log("Filled PDF:", result.data.filledPdfUrl);
}

client.documentAi — CRUD Agent

Document AI Agents lưu trữ schema trích xuất, hướng dẫn và cấu hình model để sử dụng lặp lại.

Liệt kê agent

// Tất cả agent
const agents = await client.documentAi.listAgents();
// Lọc theo tên
const filtered = await client.documentAi.listAgents({ nameContains: "Invoice" });
// Chỉ Document AI agents (được tạo qua createFull hoặc webapp)
const docAiAgents = await client.documentAi.listAgents({ documentAiOnly: true });

Lấy agent

const agent = await client.documentAi.getAgent("agent_id");

Tạo agent

const agent = await client.documentAi.createAgent({
name: "Invoice Extractor",
instructions: "Extract invoice fields. Dates as YYYY-MM-DD.",
model_id: "gpt-4o",
schema: {
invoice_number: { type: "string", description: "Invoice ID" },
total: { type: "number" },
date: { type: "string", format: "date" },
},
});

Cập nhật agent

await client.documentAi.updateAgent("agent_id", {
name: "Invoice Extractor v2",
instructions: "Updated extraction logic.",
});

Xóa agent

await client.documentAi.deleteAgent("agent_id");

client.documentAi — Xử lý với Agent

Xử lý tài liệu bằng agent đã cấu hình (tra cứu model + hướng dẫn từ agent).

const result = await client.documentAi.process({
agentId: "agent_id",
url: "https://example.com/invoice.pdf",
organizationId: "org_xxx",
});

Bạn cũng có thể ghi đè model hoặc hướng dẫn của agent:

const result = await client.documentAi.process({
agentId: "agent_id",
url: "https://example.com/invoice.pdf",
organizationId: "org_xxx",
modelName: "gpt-4o", // ghi đè model của agent
instructions: "Custom prompt", // ghi đè hướng dẫn của agent
});

client.documentAi — Đề xuất Schema

Tự động đề xuất JSON schema bằng cách phân tích tài liệu mẫu.

const schema = await client.documentAi.suggestSchema({
url: "https://example.com/invoice.pdf",
organizationId: "org_xxx",
modelName: "gpt-4o", // tùy chọn, mặc định "gpt-4o"
});

client.documentAi — Tạo Đầy đủ (Orchestrator)

Tạo Document AI agent từ đầu đến cuối (giống như webapp iMBRACE): tạo board với schema trích xuất, sau đó tạo UseCase + AI Agent liên kết với board đó.

const result = await client.documentAi.createFull({
name: "Invoice Extractor",
instructions: "Extract invoice fields. Dates as YYYY-MM-DD.",
schemaFields: [
{ name: "invoice_number", type: "ShortText", description: "Invoice ID" },
{ name: "total", type: "Number", description: "Total amount" },
{ name: "date", type: "Date", description: "Invoice date" },
],
modelId: "gpt-4o",
providerId: "system",
});
console.log(result.board_id); // "brd_xxx"
console.log(result.ai_agent_id); // UUID của AI Agent đã tạo
console.log(result.usecase_id); // UUID của UseCase đã tạo

Tùy chọn Tạo Đầy đủ

Tham sốTrường TSTham số PythonLoạiMặc định
Tênnamenamestring / str
Hướng dẫninstructionsinstructionsstring / str
Trường schemaschemaFieldsschema_fieldsCreateBoardFieldInput[] / List[Dict]
Model IDmodelIdmodel_idstring / str
Provider IDproviderIdprovider_idstring / str
Mô tảdescriptiondescriptionstring / strNone
Model VLMvlmModelvlm_modelstring / strmodelId
Provider VLMvlmProviderIdvlm_provider_idstring / strproviderId
Ngôn ngữ nguồnsourceLanguagessource_languagesstring[] / List[str]["English"]
Hỗ trợ chữ viết tayhandwritingSupporthandwriting_supportboolean / boolfalse
Offset thời giantimeOffsettime_offsetstring / str"UTC+00:00"
Tiếp tục khi lỗicontinueOnFailurecontinue_on_failureboolean / boolfalse
Thời gian thử lạiretryTimeretry_timenumber / int2
Nhiệt độtemperaturetemperaturenumber / float0.1
URL DemodemoUrldemo_urlstring / strNone
ID nhómteamIdsteam_idsstring[] / List[str][]
Trường AI Agent bổ sungextraAiAgentextra_ai_agentRecord<string, unknown> / DictNone

Sử dụng bất đồng bộ (Python)

from imbrace import AsyncImbraceClient
async with AsyncImbraceClient() as client:
# Xử lý trực tiếp (chat_ai)
models = await client.chat_ai.list_document_models()
result = await client.chat_ai.process_document(
model_name="gpt-4o",
url="https://example.com/invoice.pdf",
organization_id="org_xxx",
)
# Xử lý dựa trên agent (document_ai)
agents = await client.document_ai.list_agents(document_ai_only=True)
result2 = await client.document_ai.process(
agent_id=agents[0]["_id"],
url="https://example.com/receipt.pdf",
organization_id="org_xxx",
)

Xem thêm