mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-18 13:14:56 +03:00
feat(sse): add Vertex AI DeepSeek OCR transformation to the registry
Adds VERTEX_DEEPSEEK_TRANSFORMATION (request/response mapping for the Vertex AI DeepSeek OCR MaaS endpoint) and registers the "vertex-deepseek-ocr" provider in OCR_PROVIDERS, modeled on litellm's VertexAIDeepSeekOCRConfig. buildRequest treats the resolved baseUrl as the complete Vertex endpoint URL (project/location resolved upstream), matching the existing Mistral passthrough pattern.
This commit is contained in:
@@ -104,6 +104,80 @@ export const AZURE_DI_TRANSFORMATION: OcrTransformation = {
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Vertex AI DeepSeek OCR (deepseek-ai/deepseek-ocr-maas), served through Vertex's generic
|
||||
* OpenAI-compatible partner endpoint ("openapi/chat/completions"). Modeled on litellm's
|
||||
* VertexAIDeepSeekOCRConfig (litellm/llms/vertex_ai/ocr/deepseek_transformation.py):
|
||||
* - request: OpenAI chat-completions shape, model prefixed with "deepseek-ai/", the OCR
|
||||
* document sent as a single image_url content part (document_url documents are mapped to
|
||||
* the same image_url shape — Vertex accepts both gs:// and https:// URLs there).
|
||||
* - response: an OpenAI chat-completions body whose choices[0].message.content is either a
|
||||
* JSON string already in the canonical {pages,model,usage_info} shape, or plain markdown
|
||||
* text — both are normalized into OcrResponseShape.
|
||||
*
|
||||
* The full project/location endpoint URL is resolved into credentials.baseUrl upstream (see
|
||||
* resolveOcrCredentials in src/app/api/v1/ocr/route.ts, the same pattern Azure DI uses for its
|
||||
* resource endpoint) — buildRequest treats baseUrl as the complete URL, exactly like Mistral.
|
||||
*/
|
||||
function vertexDeepseekOcrContent(document: Record<string, unknown> | undefined): {
|
||||
type: string;
|
||||
image_url: string;
|
||||
} {
|
||||
const url = String(document?.document_url ?? document?.image_url ?? "");
|
||||
return { type: "image_url", image_url: url };
|
||||
}
|
||||
|
||||
export const VERTEX_DEEPSEEK_TRANSFORMATION: OcrTransformation = {
|
||||
buildRequest({ baseUrl, token, body, modelId }) {
|
||||
return {
|
||||
url: baseUrl,
|
||||
init: {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}` },
|
||||
body: JSON.stringify({
|
||||
model: `deepseek-ai/${modelId}`,
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: [vertexDeepseekOcrContent(body.document as Record<string, unknown>)],
|
||||
},
|
||||
],
|
||||
}),
|
||||
},
|
||||
};
|
||||
},
|
||||
parseResponse(raw) {
|
||||
const r = raw as {
|
||||
model?: string;
|
||||
choices?: Array<{ message?: { content?: unknown } }>;
|
||||
usage?: Record<string, unknown>;
|
||||
};
|
||||
const model = r.model ?? "deepseek-ocr-maas";
|
||||
const content = r.choices?.[0]?.message?.content;
|
||||
|
||||
if (typeof content === "string") {
|
||||
const trimmed = content.trim();
|
||||
if (trimmed.startsWith("{")) {
|
||||
try {
|
||||
const parsed = JSON.parse(trimmed) as Partial<OcrResponseShape>;
|
||||
if (Array.isArray(parsed.pages)) {
|
||||
return {
|
||||
pages: parsed.pages,
|
||||
model: parsed.model ?? model,
|
||||
usage_info: parsed.usage_info ?? r.usage,
|
||||
};
|
||||
}
|
||||
} catch {
|
||||
// Not JSON after all — fall through and treat it as plain markdown.
|
||||
}
|
||||
}
|
||||
return { pages: [{ index: 0, markdown: content }], model, usage_info: r.usage };
|
||||
}
|
||||
|
||||
return { pages: [{ index: 0, markdown: "" }], model, usage_info: r.usage };
|
||||
},
|
||||
};
|
||||
|
||||
export const OCR_PROVIDERS: Record<string, OcrProvider> = {
|
||||
mistral: {
|
||||
id: "mistral",
|
||||
@@ -120,6 +194,14 @@ export const OCR_PROVIDERS: Record<string, OcrProvider> = {
|
||||
models: [{ id: "prebuilt-read", name: "Azure Document Intelligence (Read)" }],
|
||||
transformation: AZURE_DI_TRANSFORMATION,
|
||||
},
|
||||
"vertex-deepseek-ocr": {
|
||||
id: "vertex-deepseek-ocr",
|
||||
baseUrl: "",
|
||||
authType: "apikey",
|
||||
authHeader: "bearer",
|
||||
models: [{ id: "deepseek-ocr-maas", name: "DeepSeek OCR (Vertex AI MaaS)" }],
|
||||
transformation: VERTEX_DEEPSEEK_TRANSFORMATION,
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
OCR_PROVIDERS,
|
||||
getOcrTransformation,
|
||||
MISTRAL_PASSTHROUGH,
|
||||
VERTEX_DEEPSEEK_TRANSFORMATION,
|
||||
} from "../../open-sse/config/ocrRegistry.ts";
|
||||
|
||||
test("mistral resolves the passthrough transformation by default", () => {
|
||||
@@ -72,3 +73,94 @@ test("azure DI maps base64/image_url documents to base64Source/urlSource", () =>
|
||||
const sent = JSON.parse(String(init.body));
|
||||
assert.equal(sent.base64Source, "AAAA");
|
||||
});
|
||||
|
||||
// ── Vertex AI DeepSeek OCR ──────────────────────────────────────────────────
|
||||
// URL/body/response shapes verified against the upstream reference
|
||||
// (litellm/llms/vertex_ai/ocr/deepseek_transformation.py): the endpoint is the
|
||||
// generic Vertex "openapi/chat/completions" partner endpoint, the model id is
|
||||
// prefixed with "deepseek-ai/", and the OCR document is sent as an
|
||||
// OpenAI-chat-shaped image_url content part.
|
||||
|
||||
test("vertex-deepseek-ocr resolves its own transformation (not the passthrough)", () => {
|
||||
const t = getOcrTransformation("vertex-deepseek-ocr");
|
||||
assert.equal(t, VERTEX_DEEPSEEK_TRANSFORMATION);
|
||||
});
|
||||
|
||||
test("vertex-deepseek-ocr builds an OpenAI-chat-shaped request against the resolved endpoint", () => {
|
||||
const t = getOcrTransformation("vertex-deepseek-ocr");
|
||||
const { url, init } = t.buildRequest({
|
||||
// resolveOcrCredentials (src/app/api/v1/ocr/route.ts) resolves the full
|
||||
// project/location endpoint into credentials.baseUrl before this runs —
|
||||
// buildRequest treats baseUrl as the complete URL, mirroring Mistral.
|
||||
baseUrl:
|
||||
"https://aiplatform.googleapis.com/v1/projects/proj-1/locations/us-central1/endpoints/openapi/chat/completions",
|
||||
token: "ya29.mock",
|
||||
body: { document: { type: "image_url", image_url: "https://x/y.png" } },
|
||||
modelId: "deepseek-ocr-maas",
|
||||
});
|
||||
assert.equal(
|
||||
url,
|
||||
"https://aiplatform.googleapis.com/v1/projects/proj-1/locations/us-central1/endpoints/openapi/chat/completions"
|
||||
);
|
||||
assert.equal(init.method, "POST");
|
||||
assert.equal((init.headers as Record<string, string>).Authorization, "Bearer ya29.mock");
|
||||
const sent = JSON.parse(String(init.body));
|
||||
assert.equal(sent.model, "deepseek-ai/deepseek-ocr-maas");
|
||||
assert.deepEqual(sent.messages, [
|
||||
{ role: "user", content: [{ type: "image_url", image_url: "https://x/y.png" }] },
|
||||
]);
|
||||
});
|
||||
|
||||
test("vertex-deepseek-ocr maps a document_url document to the same image_url content shape", () => {
|
||||
const t = getOcrTransformation("vertex-deepseek-ocr");
|
||||
const { init } = t.buildRequest({
|
||||
baseUrl:
|
||||
"https://aiplatform.googleapis.com/v1/projects/p/locations/us-central1/endpoints/openapi/chat/completions",
|
||||
token: "t",
|
||||
body: { document: { type: "document_url", document_url: "https://x/d.pdf" } },
|
||||
modelId: "deepseek-ocr-maas",
|
||||
});
|
||||
const sent = JSON.parse(String(init.body));
|
||||
assert.deepEqual(sent.messages[0].content, [{ type: "image_url", image_url: "https://x/d.pdf" }]);
|
||||
});
|
||||
|
||||
test("vertex-deepseek-ocr parseResponse extracts a JSON pages payload embedded in choices[0].message.content", () => {
|
||||
const t = getOcrTransformation("vertex-deepseek-ocr");
|
||||
const raw = {
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: JSON.stringify({
|
||||
pages: [{ index: 0, markdown: "# hi" }],
|
||||
model: "deepseek-ocr-maas",
|
||||
usage_info: { pages_processed: 1 },
|
||||
}),
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
const parsed = t.parseResponse(raw);
|
||||
assert.deepEqual(parsed.pages, [{ index: 0, markdown: "# hi" }]);
|
||||
assert.equal(parsed.model, "deepseek-ocr-maas");
|
||||
assert.deepEqual(parsed.usage_info, { pages_processed: 1 });
|
||||
});
|
||||
|
||||
test("vertex-deepseek-ocr parseResponse wraps plain markdown content into a single page (Mistral shape)", () => {
|
||||
const t = getOcrTransformation("vertex-deepseek-ocr");
|
||||
const raw = {
|
||||
model: "deepseek-ocr-maas",
|
||||
choices: [{ message: { content: "# just markdown, not JSON" } }],
|
||||
usage: { total_tokens: 42 },
|
||||
};
|
||||
const parsed = t.parseResponse(raw);
|
||||
assert.deepEqual(parsed.pages, [{ index: 0, markdown: "# just markdown, not JSON" }]);
|
||||
assert.equal(parsed.model, "deepseek-ocr-maas");
|
||||
assert.deepEqual(parsed.usage_info, { total_tokens: 42 });
|
||||
});
|
||||
|
||||
test("vertex-deepseek-ocr parseResponse tolerates a missing/empty choices array", () => {
|
||||
const t = getOcrTransformation("vertex-deepseek-ocr");
|
||||
const parsed = t.parseResponse({ model: "deepseek-ocr-maas", choices: [] });
|
||||
assert.deepEqual(parsed.pages, [{ index: 0, markdown: "" }]);
|
||||
assert.equal(parsed.model, "deepseek-ocr-maas");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user