diff --git a/packages/api/src/files/mistral/crud.ts b/packages/api/src/files/mistral/crud.ts index fefe4a4675..3d124aa4c5 100644 --- a/packages/api/src/files/mistral/crud.ts +++ b/packages/api/src/files/mistral/crud.ts @@ -317,6 +317,23 @@ function getModelConfig(ocrConfig?: TCustomConfig['ocr']): string { return modelConfig.trim(); } +/** + * Gets the region configuration for Vertex AI + */ +function getRegionConfig(ocrConfig?: TCustomConfig['ocr']): string | undefined { + const regionConfig = ocrConfig?.region || ''; + + if (!regionConfig.trim()) { + return undefined; + } + + if (envVarRegex.test(regionConfig)) { + return extractEnvVariable(regionConfig) || undefined; + } + + return regionConfig.trim(); +} + /** * Determines document type based on file */ @@ -606,15 +623,18 @@ async function performGoogleVertexOCR({ accessToken, projectId, model, + region, documentType = 'document_url', }: { url: string; accessToken: string; projectId: string; model: string; + region?: string; documentType?: 'document_url' | 'image_url'; }): Promise { - const location = process.env.GOOGLE_LOC || 'us-central1'; + // Priority: function parameter > MISTRAL_VERTEX_REGION > GOOGLE_LOC > default + const location = region || process.env.MISTRAL_VERTEX_REGION || process.env.GOOGLE_LOC || 'us-central1'; const modelId = model || 'mistral-ocr-2505'; let baseURL: string; @@ -693,6 +713,7 @@ export const uploadGoogleVertexMistralOCR = async ( try { const { serviceAccount, accessToken } = await loadGoogleAuthConfig(); const model = getModelConfig(context.req.config?.ocr); + const region = getRegionConfig(context.req.config?.ocr); const { content: buffer } = await readFileAsBuffer(context.file.path, { fileSize: context.file.size, @@ -706,6 +727,7 @@ export const uploadGoogleVertexMistralOCR = async ( accessToken, projectId: serviceAccount.project_id!, model, + region, documentType, }); diff --git a/packages/data-provider/src/config.ts b/packages/data-provider/src/config.ts index 45c964cbd8..2b68964630 100644 --- a/packages/data-provider/src/config.ts +++ b/packages/data-provider/src/config.ts @@ -882,6 +882,7 @@ export const ocrSchema = z.object({ mistralModel: z.string().optional(), apiKey: z.string().optional().default('${OCR_API_KEY}'), baseURL: z.string().optional().default('${OCR_BASEURL}'), + region: z.string().optional().default('${MISTRAL_VERTEX_REGION}'), strategy: z.nativeEnum(OCRStrategy).default(OCRStrategy.MISTRAL_OCR), });