mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 00:40:14 +01:00
⚙️ fix: Update OCR context to use req.config (#9367)
This commit is contained in:
parent
005a0cb84a
commit
e2a6937ca6
2 changed files with 130 additions and 169 deletions
|
|
@ -44,13 +44,12 @@ jest.mock('~/utils/axios', () => ({
|
||||||
|
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import type { Request as ExpressRequest } from 'express';
|
|
||||||
import type { Readable } from 'stream';
|
import type { Readable } from 'stream';
|
||||||
import type {
|
import type {
|
||||||
MistralFileUploadResponse,
|
MistralFileUploadResponse,
|
||||||
MistralSignedUrlResponse,
|
MistralSignedUrlResponse,
|
||||||
|
ServerRequest,
|
||||||
OCRResult,
|
OCRResult,
|
||||||
AppConfig,
|
|
||||||
} from '~/types';
|
} from '~/types';
|
||||||
import { logger as mockLogger } from '@librechat/data-schemas';
|
import { logger as mockLogger } from '@librechat/data-schemas';
|
||||||
import {
|
import {
|
||||||
|
|
@ -502,16 +501,15 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
user: { id: 'user123' },
|
user: { id: 'user123' },
|
||||||
} as unknown as ExpressRequest;
|
config: {
|
||||||
|
ocr: {
|
||||||
const appConfig = {
|
// Use environment variable syntax to ensure loadAuthValues is called
|
||||||
ocr: {
|
apiKey: '${OCR_API_KEY}',
|
||||||
// Use environment variable syntax to ensure loadAuthValues is called
|
baseURL: '${OCR_BASEURL}',
|
||||||
apiKey: '${OCR_API_KEY}',
|
mistralModel: 'mistral-medium',
|
||||||
baseURL: '${OCR_BASEURL}',
|
},
|
||||||
mistralModel: 'mistral-medium',
|
|
||||||
},
|
},
|
||||||
} as AppConfig;
|
} as unknown as ServerRequest;
|
||||||
|
|
||||||
const file = {
|
const file = {
|
||||||
path: '/tmp/upload/file.pdf',
|
path: '/tmp/upload/file.pdf',
|
||||||
|
|
@ -521,7 +519,6 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const result = await uploadMistralOCR({
|
const result = await uploadMistralOCR({
|
||||||
req,
|
req,
|
||||||
appConfig,
|
|
||||||
file,
|
file,
|
||||||
loadAuthValues: mockLoadAuthValues,
|
loadAuthValues: mockLoadAuthValues,
|
||||||
});
|
});
|
||||||
|
|
@ -604,15 +601,14 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
user: { id: 'user456' },
|
user: { id: 'user456' },
|
||||||
} as unknown as ExpressRequest;
|
config: {
|
||||||
|
ocr: {
|
||||||
const appConfig = {
|
apiKey: '${OCR_API_KEY}',
|
||||||
ocr: {
|
baseURL: '${OCR_BASEURL}',
|
||||||
apiKey: '${OCR_API_KEY}',
|
mistralModel: 'mistral-medium',
|
||||||
baseURL: '${OCR_BASEURL}',
|
},
|
||||||
mistralModel: 'mistral-medium',
|
|
||||||
},
|
},
|
||||||
} as AppConfig;
|
} as unknown as ServerRequest;
|
||||||
|
|
||||||
const file = {
|
const file = {
|
||||||
path: '/tmp/upload/image.png',
|
path: '/tmp/upload/image.png',
|
||||||
|
|
@ -622,7 +618,6 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const result = await uploadMistralOCR({
|
const result = await uploadMistralOCR({
|
||||||
req,
|
req,
|
||||||
appConfig,
|
|
||||||
file,
|
file,
|
||||||
loadAuthValues: mockLoadAuthValues,
|
loadAuthValues: mockLoadAuthValues,
|
||||||
});
|
});
|
||||||
|
|
@ -703,15 +698,14 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
user: { id: 'user123' },
|
user: { id: 'user123' },
|
||||||
} as unknown as ExpressRequest;
|
config: {
|
||||||
|
ocr: {
|
||||||
const appConfig = {
|
apiKey: '${CUSTOM_API_KEY}',
|
||||||
ocr: {
|
baseURL: '${CUSTOM_BASEURL}',
|
||||||
apiKey: '${CUSTOM_API_KEY}',
|
mistralModel: '${CUSTOM_MODEL}',
|
||||||
baseURL: '${CUSTOM_BASEURL}',
|
},
|
||||||
mistralModel: '${CUSTOM_MODEL}',
|
|
||||||
},
|
},
|
||||||
} as AppConfig;
|
} as unknown as ServerRequest;
|
||||||
|
|
||||||
// Set environment variable for model
|
// Set environment variable for model
|
||||||
process.env.CUSTOM_MODEL = 'mistral-large';
|
process.env.CUSTOM_MODEL = 'mistral-large';
|
||||||
|
|
@ -724,7 +718,6 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const result = await uploadMistralOCR({
|
const result = await uploadMistralOCR({
|
||||||
req,
|
req,
|
||||||
appConfig,
|
|
||||||
file,
|
file,
|
||||||
loadAuthValues: mockLoadAuthValues,
|
loadAuthValues: mockLoadAuthValues,
|
||||||
});
|
});
|
||||||
|
|
@ -795,16 +788,15 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
user: { id: 'user123' },
|
user: { id: 'user123' },
|
||||||
} as unknown as ExpressRequest;
|
config: {
|
||||||
|
ocr: {
|
||||||
const appConfig = {
|
// Use environment variable syntax to ensure loadAuthValues is called
|
||||||
ocr: {
|
apiKey: '${INVALID_FORMAT}', // Using valid env var format but with an invalid name
|
||||||
// Use environment variable syntax to ensure loadAuthValues is called
|
baseURL: '${OCR_BASEURL}', // Using valid env var format
|
||||||
apiKey: '${INVALID_FORMAT}', // Using valid env var format but with an invalid name
|
mistralModel: 'mistral-ocr-latest', // Plain string value
|
||||||
baseURL: '${OCR_BASEURL}', // Using valid env var format
|
},
|
||||||
mistralModel: 'mistral-ocr-latest', // Plain string value
|
|
||||||
},
|
},
|
||||||
} as AppConfig;
|
} as unknown as ServerRequest;
|
||||||
|
|
||||||
const file = {
|
const file = {
|
||||||
path: '/tmp/upload/file.pdf',
|
path: '/tmp/upload/file.pdf',
|
||||||
|
|
@ -814,7 +806,6 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
await uploadMistralOCR({
|
await uploadMistralOCR({
|
||||||
req,
|
req,
|
||||||
appConfig,
|
|
||||||
file,
|
file,
|
||||||
loadAuthValues: mockLoadAuthValues,
|
loadAuthValues: mockLoadAuthValues,
|
||||||
});
|
});
|
||||||
|
|
@ -850,14 +841,13 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
user: { id: 'user123' },
|
user: { id: 'user123' },
|
||||||
} as unknown as ExpressRequest;
|
config: {
|
||||||
|
ocr: {
|
||||||
const appConfig = {
|
apiKey: 'OCR_API_KEY',
|
||||||
ocr: {
|
baseURL: 'OCR_BASEURL',
|
||||||
apiKey: 'OCR_API_KEY',
|
},
|
||||||
baseURL: 'OCR_BASEURL',
|
|
||||||
},
|
},
|
||||||
} as AppConfig;
|
} as unknown as ServerRequest;
|
||||||
|
|
||||||
const file = {
|
const file = {
|
||||||
path: '/tmp/upload/file.pdf',
|
path: '/tmp/upload/file.pdf',
|
||||||
|
|
@ -868,7 +858,6 @@ describe('MistralOCR Service', () => {
|
||||||
await expect(
|
await expect(
|
||||||
uploadMistralOCR({
|
uploadMistralOCR({
|
||||||
req,
|
req,
|
||||||
appConfig,
|
|
||||||
file,
|
file,
|
||||||
loadAuthValues: mockLoadAuthValues,
|
loadAuthValues: mockLoadAuthValues,
|
||||||
}),
|
}),
|
||||||
|
|
@ -936,15 +925,14 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
user: { id: 'user123' },
|
user: { id: 'user123' },
|
||||||
} as unknown as ExpressRequest;
|
config: {
|
||||||
|
ocr: {
|
||||||
const appConfig = {
|
apiKey: 'OCR_API_KEY',
|
||||||
ocr: {
|
baseURL: 'OCR_BASEURL',
|
||||||
apiKey: 'OCR_API_KEY',
|
mistralModel: 'mistral-ocr-latest',
|
||||||
baseURL: 'OCR_BASEURL',
|
},
|
||||||
mistralModel: 'mistral-ocr-latest',
|
|
||||||
},
|
},
|
||||||
} as AppConfig;
|
} as unknown as ServerRequest;
|
||||||
|
|
||||||
const file = {
|
const file = {
|
||||||
path: '/tmp/upload/file.pdf',
|
path: '/tmp/upload/file.pdf',
|
||||||
|
|
@ -954,7 +942,6 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const result = await uploadMistralOCR({
|
const result = await uploadMistralOCR({
|
||||||
req,
|
req,
|
||||||
appConfig,
|
|
||||||
file,
|
file,
|
||||||
loadAuthValues: mockLoadAuthValues,
|
loadAuthValues: mockLoadAuthValues,
|
||||||
});
|
});
|
||||||
|
|
@ -1024,16 +1011,15 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
user: { id: 'user123' },
|
user: { id: 'user123' },
|
||||||
} as unknown as ExpressRequest;
|
config: {
|
||||||
|
ocr: {
|
||||||
const appConfig = {
|
// Direct values that should be used as-is, without variable substitution
|
||||||
ocr: {
|
apiKey: 'actual-api-key-value',
|
||||||
// Direct values that should be used as-is, without variable substitution
|
baseURL: 'https://direct-api-url.mistral.ai/v1',
|
||||||
apiKey: 'actual-api-key-value',
|
mistralModel: 'mistral-direct-model',
|
||||||
baseURL: 'https://direct-api-url.mistral.ai/v1',
|
},
|
||||||
mistralModel: 'mistral-direct-model',
|
|
||||||
},
|
},
|
||||||
} as AppConfig;
|
} as unknown as ServerRequest;
|
||||||
|
|
||||||
const file = {
|
const file = {
|
||||||
path: '/tmp/upload/file.pdf',
|
path: '/tmp/upload/file.pdf',
|
||||||
|
|
@ -1043,7 +1029,6 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const result = await uploadMistralOCR({
|
const result = await uploadMistralOCR({
|
||||||
req,
|
req,
|
||||||
appConfig,
|
|
||||||
file,
|
file,
|
||||||
loadAuthValues: mockLoadAuthValues,
|
loadAuthValues: mockLoadAuthValues,
|
||||||
});
|
});
|
||||||
|
|
@ -1138,16 +1123,15 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
user: { id: 'user123' },
|
user: { id: 'user123' },
|
||||||
} as unknown as ExpressRequest;
|
config: {
|
||||||
|
ocr: {
|
||||||
const appConfig = {
|
// Empty string values - should fall back to defaults
|
||||||
ocr: {
|
apiKey: '',
|
||||||
// Empty string values - should fall back to defaults
|
baseURL: '',
|
||||||
apiKey: '',
|
mistralModel: '',
|
||||||
baseURL: '',
|
},
|
||||||
mistralModel: '',
|
|
||||||
},
|
},
|
||||||
} as AppConfig;
|
} as unknown as ServerRequest;
|
||||||
|
|
||||||
const file = {
|
const file = {
|
||||||
path: '/tmp/upload/file.pdf',
|
path: '/tmp/upload/file.pdf',
|
||||||
|
|
@ -1157,7 +1141,6 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const result = await uploadMistralOCR({
|
const result = await uploadMistralOCR({
|
||||||
req,
|
req,
|
||||||
appConfig,
|
|
||||||
file,
|
file,
|
||||||
loadAuthValues: mockLoadAuthValues,
|
loadAuthValues: mockLoadAuthValues,
|
||||||
});
|
});
|
||||||
|
|
@ -1281,15 +1264,14 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
user: { id: 'user123' },
|
user: { id: 'user123' },
|
||||||
} as unknown as ExpressRequest;
|
config: {
|
||||||
|
ocr: {
|
||||||
const appConfig = {
|
apiKey: '${AZURE_MISTRAL_OCR_API_KEY}',
|
||||||
ocr: {
|
baseURL: 'https://endpoint.models.ai.azure.com/v1',
|
||||||
apiKey: '${AZURE_MISTRAL_OCR_API_KEY}',
|
mistralModel: 'mistral-ocr-2503',
|
||||||
baseURL: 'https://endpoint.models.ai.azure.com/v1',
|
},
|
||||||
mistralModel: 'mistral-ocr-2503',
|
|
||||||
},
|
},
|
||||||
} as AppConfig;
|
} as unknown as ServerRequest;
|
||||||
|
|
||||||
const file = {
|
const file = {
|
||||||
path: '/tmp/upload/file.pdf',
|
path: '/tmp/upload/file.pdf',
|
||||||
|
|
@ -1299,7 +1281,6 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
await uploadMistralOCR({
|
await uploadMistralOCR({
|
||||||
req,
|
req,
|
||||||
appConfig,
|
|
||||||
file,
|
file,
|
||||||
loadAuthValues: mockLoadAuthValues,
|
loadAuthValues: mockLoadAuthValues,
|
||||||
});
|
});
|
||||||
|
|
@ -1365,15 +1346,14 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
user: { id: 'user456' },
|
user: { id: 'user456' },
|
||||||
} as unknown as ExpressRequest;
|
config: {
|
||||||
|
ocr: {
|
||||||
const appConfig = {
|
apiKey: 'hardcoded-api-key-12345',
|
||||||
ocr: {
|
baseURL: '${CUSTOM_OCR_BASEURL}',
|
||||||
apiKey: 'hardcoded-api-key-12345',
|
mistralModel: 'mistral-ocr-latest',
|
||||||
baseURL: '${CUSTOM_OCR_BASEURL}',
|
},
|
||||||
mistralModel: 'mistral-ocr-latest',
|
|
||||||
},
|
},
|
||||||
} as AppConfig;
|
} as unknown as ServerRequest;
|
||||||
|
|
||||||
const file = {
|
const file = {
|
||||||
path: '/tmp/upload/file.pdf',
|
path: '/tmp/upload/file.pdf',
|
||||||
|
|
@ -1383,7 +1363,6 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
await uploadMistralOCR({
|
await uploadMistralOCR({
|
||||||
req,
|
req,
|
||||||
appConfig,
|
|
||||||
file,
|
file,
|
||||||
loadAuthValues: mockLoadAuthValues,
|
loadAuthValues: mockLoadAuthValues,
|
||||||
});
|
});
|
||||||
|
|
@ -1489,15 +1468,14 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
user: { id: 'user123' },
|
user: { id: 'user123' },
|
||||||
} as unknown as ExpressRequest;
|
config: {
|
||||||
|
ocr: {
|
||||||
const appConfig = {
|
apiKey: '${OCR_API_KEY}',
|
||||||
ocr: {
|
baseURL: '${OCR_BASEURL}',
|
||||||
apiKey: '${OCR_API_KEY}',
|
mistralModel: 'mistral-ocr-latest',
|
||||||
baseURL: '${OCR_BASEURL}',
|
},
|
||||||
mistralModel: 'mistral-ocr-latest',
|
|
||||||
},
|
},
|
||||||
} as AppConfig;
|
} as unknown as ServerRequest;
|
||||||
|
|
||||||
const file = {
|
const file = {
|
||||||
path: '/tmp/upload/file.pdf',
|
path: '/tmp/upload/file.pdf',
|
||||||
|
|
@ -1507,7 +1485,6 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
await uploadMistralOCR({
|
await uploadMistralOCR({
|
||||||
req,
|
req,
|
||||||
appConfig,
|
|
||||||
file,
|
file,
|
||||||
loadAuthValues: mockLoadAuthValues,
|
loadAuthValues: mockLoadAuthValues,
|
||||||
});
|
});
|
||||||
|
|
@ -1558,15 +1535,14 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
user: { id: 'user123' },
|
user: { id: 'user123' },
|
||||||
} as unknown as ExpressRequest;
|
config: {
|
||||||
|
ocr: {
|
||||||
const appConfig = {
|
apiKey: '${OCR_API_KEY}',
|
||||||
ocr: {
|
baseURL: '${OCR_BASEURL}',
|
||||||
apiKey: '${OCR_API_KEY}',
|
mistralModel: 'mistral-ocr-latest',
|
||||||
baseURL: '${OCR_BASEURL}',
|
},
|
||||||
mistralModel: 'mistral-ocr-latest',
|
|
||||||
},
|
},
|
||||||
} as AppConfig;
|
} as unknown as ServerRequest;
|
||||||
|
|
||||||
const file = {
|
const file = {
|
||||||
path: '/tmp/upload/file.pdf',
|
path: '/tmp/upload/file.pdf',
|
||||||
|
|
@ -1577,7 +1553,6 @@ describe('MistralOCR Service', () => {
|
||||||
await expect(
|
await expect(
|
||||||
uploadMistralOCR({
|
uploadMistralOCR({
|
||||||
req,
|
req,
|
||||||
appConfig,
|
|
||||||
file,
|
file,
|
||||||
loadAuthValues: mockLoadAuthValues,
|
loadAuthValues: mockLoadAuthValues,
|
||||||
}),
|
}),
|
||||||
|
|
@ -1646,15 +1621,14 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
user: { id: 'user123' },
|
user: { id: 'user123' },
|
||||||
} as unknown as ExpressRequest;
|
config: {
|
||||||
|
ocr: {
|
||||||
const appConfig = {
|
apiKey: '${OCR_API_KEY}',
|
||||||
ocr: {
|
baseURL: '${OCR_BASEURL}',
|
||||||
apiKey: '${OCR_API_KEY}',
|
mistralModel: 'mistral-ocr-latest',
|
||||||
baseURL: '${OCR_BASEURL}',
|
},
|
||||||
mistralModel: 'mistral-ocr-latest',
|
|
||||||
},
|
},
|
||||||
} as AppConfig;
|
} as unknown as ServerRequest;
|
||||||
|
|
||||||
const file = {
|
const file = {
|
||||||
path: '/tmp/upload/file.pdf',
|
path: '/tmp/upload/file.pdf',
|
||||||
|
|
@ -1665,7 +1639,6 @@ describe('MistralOCR Service', () => {
|
||||||
// Should not throw even if delete fails
|
// Should not throw even if delete fails
|
||||||
const result = await uploadMistralOCR({
|
const result = await uploadMistralOCR({
|
||||||
req,
|
req,
|
||||||
appConfig,
|
|
||||||
file,
|
file,
|
||||||
loadAuthValues: mockLoadAuthValues,
|
loadAuthValues: mockLoadAuthValues,
|
||||||
});
|
});
|
||||||
|
|
@ -1706,15 +1679,14 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
user: { id: 'user123' },
|
user: { id: 'user123' },
|
||||||
} as unknown as ExpressRequest;
|
config: {
|
||||||
|
ocr: {
|
||||||
const appConfig = {
|
apiKey: '${OCR_API_KEY}',
|
||||||
ocr: {
|
baseURL: '${OCR_BASEURL}',
|
||||||
apiKey: '${OCR_API_KEY}',
|
mistralModel: 'mistral-ocr-latest',
|
||||||
baseURL: '${OCR_BASEURL}',
|
},
|
||||||
mistralModel: 'mistral-ocr-latest',
|
|
||||||
},
|
},
|
||||||
} as AppConfig;
|
} as unknown as ServerRequest;
|
||||||
|
|
||||||
const file = {
|
const file = {
|
||||||
path: '/tmp/upload/file.pdf',
|
path: '/tmp/upload/file.pdf',
|
||||||
|
|
@ -1725,7 +1697,6 @@ describe('MistralOCR Service', () => {
|
||||||
await expect(
|
await expect(
|
||||||
uploadMistralOCR({
|
uploadMistralOCR({
|
||||||
req,
|
req,
|
||||||
appConfig,
|
|
||||||
file,
|
file,
|
||||||
loadAuthValues: mockLoadAuthValues,
|
loadAuthValues: mockLoadAuthValues,
|
||||||
}),
|
}),
|
||||||
|
|
@ -1780,15 +1751,14 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
user: { id: 'user123' },
|
user: { id: 'user123' },
|
||||||
} as unknown as ExpressRequest;
|
config: {
|
||||||
|
ocr: {
|
||||||
const appConfig = {
|
apiKey: '${OCR_API_KEY}',
|
||||||
ocr: {
|
baseURL: '${OCR_BASEURL}',
|
||||||
apiKey: '${OCR_API_KEY}',
|
mistralModel: 'mistral-ocr-latest',
|
||||||
baseURL: '${OCR_BASEURL}',
|
},
|
||||||
mistralModel: 'mistral-ocr-latest',
|
|
||||||
},
|
},
|
||||||
} as AppConfig;
|
} as unknown as ServerRequest;
|
||||||
|
|
||||||
const file = {
|
const file = {
|
||||||
path: '/tmp/upload/azure-file.pdf',
|
path: '/tmp/upload/azure-file.pdf',
|
||||||
|
|
@ -1798,7 +1768,6 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const result = await uploadAzureMistralOCR({
|
const result = await uploadAzureMistralOCR({
|
||||||
req,
|
req,
|
||||||
appConfig,
|
|
||||||
file,
|
file,
|
||||||
loadAuthValues: mockLoadAuthValues,
|
loadAuthValues: mockLoadAuthValues,
|
||||||
});
|
});
|
||||||
|
|
@ -1856,15 +1825,14 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
user: { id: 'user123' },
|
user: { id: 'user123' },
|
||||||
} as unknown as ExpressRequest;
|
config: {
|
||||||
|
ocr: {
|
||||||
const appConfig = {
|
apiKey: '${AZURE_MISTRAL_OCR_API_KEY}',
|
||||||
ocr: {
|
baseURL: 'https://endpoint.models.ai.azure.com/v1',
|
||||||
apiKey: '${AZURE_MISTRAL_OCR_API_KEY}',
|
mistralModel: 'mistral-ocr-2503',
|
||||||
baseURL: 'https://endpoint.models.ai.azure.com/v1',
|
},
|
||||||
mistralModel: 'mistral-ocr-2503',
|
|
||||||
},
|
},
|
||||||
} as AppConfig;
|
} as unknown as ServerRequest;
|
||||||
|
|
||||||
const file = {
|
const file = {
|
||||||
path: '/tmp/upload/file.pdf',
|
path: '/tmp/upload/file.pdf',
|
||||||
|
|
@ -1874,7 +1842,6 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
await uploadAzureMistralOCR({
|
await uploadAzureMistralOCR({
|
||||||
req,
|
req,
|
||||||
appConfig,
|
|
||||||
file,
|
file,
|
||||||
loadAuthValues: mockLoadAuthValues,
|
loadAuthValues: mockLoadAuthValues,
|
||||||
});
|
});
|
||||||
|
|
@ -1920,15 +1887,14 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
const req = {
|
const req = {
|
||||||
user: { id: 'user456' },
|
user: { id: 'user456' },
|
||||||
} as unknown as ExpressRequest;
|
config: {
|
||||||
|
ocr: {
|
||||||
const appConfig = {
|
apiKey: 'hardcoded-api-key-12345',
|
||||||
ocr: {
|
baseURL: '${CUSTOM_OCR_BASEURL}',
|
||||||
apiKey: 'hardcoded-api-key-12345',
|
mistralModel: 'mistral-ocr-latest',
|
||||||
baseURL: '${CUSTOM_OCR_BASEURL}',
|
},
|
||||||
mistralModel: 'mistral-ocr-latest',
|
|
||||||
},
|
},
|
||||||
} as AppConfig;
|
} as unknown as ServerRequest;
|
||||||
|
|
||||||
const file = {
|
const file = {
|
||||||
path: '/tmp/upload/file.pdf',
|
path: '/tmp/upload/file.pdf',
|
||||||
|
|
@ -1938,7 +1904,6 @@ describe('MistralOCR Service', () => {
|
||||||
|
|
||||||
await uploadAzureMistralOCR({
|
await uploadAzureMistralOCR({
|
||||||
req,
|
req,
|
||||||
appConfig,
|
|
||||||
file,
|
file,
|
||||||
loadAuthValues: mockLoadAuthValues,
|
loadAuthValues: mockLoadAuthValues,
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ import {
|
||||||
extractVariableName,
|
extractVariableName,
|
||||||
} from 'librechat-data-provider';
|
} from 'librechat-data-provider';
|
||||||
import type { TCustomConfig } from 'librechat-data-provider';
|
import type { TCustomConfig } from 'librechat-data-provider';
|
||||||
import type { Request as ServerRequest } from 'express';
|
|
||||||
import type { AxiosError } from 'axios';
|
import type { AxiosError } from 'axios';
|
||||||
import type {
|
import type {
|
||||||
MistralFileUploadResponse,
|
MistralFileUploadResponse,
|
||||||
|
|
@ -17,7 +16,7 @@ import type {
|
||||||
MistralOCRUploadResult,
|
MistralOCRUploadResult,
|
||||||
MistralOCRError,
|
MistralOCRError,
|
||||||
OCRResultPage,
|
OCRResultPage,
|
||||||
AppConfig,
|
ServerRequest,
|
||||||
OCRResult,
|
OCRResult,
|
||||||
OCRImage,
|
OCRImage,
|
||||||
} from '~/types';
|
} from '~/types';
|
||||||
|
|
@ -43,10 +42,7 @@ interface GoogleServiceAccount {
|
||||||
|
|
||||||
/** Helper type for OCR request context */
|
/** Helper type for OCR request context */
|
||||||
interface OCRContext {
|
interface OCRContext {
|
||||||
req: Pick<ServerRequest, 'user'> & {
|
req: ServerRequest;
|
||||||
user?: { id: string };
|
|
||||||
};
|
|
||||||
appConfig: AppConfig;
|
|
||||||
file: Express.Multer.File;
|
file: Express.Multer.File;
|
||||||
loadAuthValues: (params: {
|
loadAuthValues: (params: {
|
||||||
userId: string;
|
userId: string;
|
||||||
|
|
@ -238,7 +234,8 @@ async function resolveConfigValue(
|
||||||
* Loads authentication configuration from OCR config
|
* Loads authentication configuration from OCR config
|
||||||
*/
|
*/
|
||||||
async function loadAuthConfig(context: OCRContext): Promise<AuthConfig> {
|
async function loadAuthConfig(context: OCRContext): Promise<AuthConfig> {
|
||||||
const ocrConfig = context.appConfig?.ocr;
|
const appConfig = context.req.config;
|
||||||
|
const ocrConfig = appConfig?.ocr;
|
||||||
const apiKeyConfig = ocrConfig?.apiKey || '';
|
const apiKeyConfig = ocrConfig?.apiKey || '';
|
||||||
const baseURLConfig = ocrConfig?.baseURL || '';
|
const baseURLConfig = ocrConfig?.baseURL || '';
|
||||||
|
|
||||||
|
|
@ -354,7 +351,6 @@ function createOCRError(error: unknown, baseMessage: string): Error {
|
||||||
* @param params - The params object.
|
* @param params - The params object.
|
||||||
* @param params.req - The request object from Express. It should have a `user` property with an `id`
|
* @param params.req - The request object from Express. It should have a `user` property with an `id`
|
||||||
* representing the user
|
* representing the user
|
||||||
* @param params.appConfig - Application configuration object
|
|
||||||
* @param params.file - The file object, which is part of the request. The file object should
|
* @param params.file - The file object, which is part of the request. The file object should
|
||||||
* have a `mimetype` property that tells us the file type
|
* have a `mimetype` property that tells us the file type
|
||||||
* @param params.loadAuthValues - Function to load authentication values
|
* @param params.loadAuthValues - Function to load authentication values
|
||||||
|
|
@ -370,7 +366,7 @@ export const uploadMistralOCR = async (context: OCRContext): Promise<MistralOCRU
|
||||||
const authConfig = await loadAuthConfig(context);
|
const authConfig = await loadAuthConfig(context);
|
||||||
apiKey = authConfig.apiKey;
|
apiKey = authConfig.apiKey;
|
||||||
baseURL = authConfig.baseURL;
|
baseURL = authConfig.baseURL;
|
||||||
const model = getModelConfig(context.appConfig?.ocr);
|
const model = getModelConfig(context.req.config?.ocr);
|
||||||
|
|
||||||
const mistralFile = await uploadDocumentToMistral({
|
const mistralFile = await uploadDocumentToMistral({
|
||||||
filePath: context.file.path,
|
filePath: context.file.path,
|
||||||
|
|
@ -440,7 +436,7 @@ export const uploadAzureMistralOCR = async (
|
||||||
): Promise<MistralOCRUploadResult> => {
|
): Promise<MistralOCRUploadResult> => {
|
||||||
try {
|
try {
|
||||||
const { apiKey, baseURL } = await loadAuthConfig(context);
|
const { apiKey, baseURL } = await loadAuthConfig(context);
|
||||||
const model = getModelConfig(context.appConfig?.ocr);
|
const model = getModelConfig(context.req.config?.ocr);
|
||||||
|
|
||||||
const buffer = fs.readFileSync(context.file.path);
|
const buffer = fs.readFileSync(context.file.path);
|
||||||
const base64 = buffer.toString('base64');
|
const base64 = buffer.toString('base64');
|
||||||
|
|
@ -655,7 +651,7 @@ export const uploadGoogleVertexMistralOCR = async (
|
||||||
): Promise<MistralOCRUploadResult> => {
|
): Promise<MistralOCRUploadResult> => {
|
||||||
try {
|
try {
|
||||||
const { serviceAccount, accessToken } = await loadGoogleAuthConfig();
|
const { serviceAccount, accessToken } = await loadGoogleAuthConfig();
|
||||||
const model = getModelConfig(context.appConfig?.ocr);
|
const model = getModelConfig(context.req.config?.ocr);
|
||||||
|
|
||||||
const buffer = fs.readFileSync(context.file.path);
|
const buffer = fs.readFileSync(context.file.path);
|
||||||
const base64 = buffer.toString('base64');
|
const base64 = buffer.toString('base64');
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue