Compare commits
3 Commits
main
...
feat/docum
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8711ca8b3a | ||
|
|
5dfad6b77b | ||
|
|
2bf1857bc0 |
74
api/server/services/Files/AzureDocumentIntelligence/crud.js
Normal file
74
api/server/services/Files/AzureDocumentIntelligence/crud.js
Normal file
@@ -0,0 +1,74 @@
|
||||
const axios = require('axios');
|
||||
const fs = require('fs');
|
||||
const { logger } = require('~/config');
|
||||
|
||||
/**
|
||||
* Uploads a document to Azure Document Intelligence API and returns the Markdown result.
|
||||
*
|
||||
* @param {Object} params - The parameters for the Azure Document Intelligence request.
|
||||
* @param {string} params.filePath - The path to the file on disk.
|
||||
* @param {string} params.apiKey - Azure API key.
|
||||
* @param {string} params.endpoint - Azure Document Intelligence endpoint.
|
||||
* @param {string} params.modelId - The model ID to use for analysis.
|
||||
* @returns {Promise<Object>} - The Document Intelligence result.
|
||||
*/
|
||||
async function uploadAzureDocumentIntelligence({ filePath, apiKey, endpoint, modelId }) {
|
||||
// Read and encode file
|
||||
const fileBuffer = fs.readFileSync(filePath);
|
||||
const base64Source = fileBuffer.toString('base64');
|
||||
|
||||
// Build URL (ensure no trailing slash on endpoint)
|
||||
const url = `${endpoint.replace(/\/+$/, '')}/documentModels/${modelId}:analyze?outputContentFormat=markdown`;
|
||||
|
||||
try {
|
||||
// Kick off the analysis
|
||||
const response = await axios.post(
|
||||
url,
|
||||
{ base64Source },
|
||||
{
|
||||
headers: {
|
||||
'Ocp-Apim-Subscription-Key': apiKey,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
// Axios lower-cases header keys, but allow either form
|
||||
const headers = response.headers || {};
|
||||
const operationLocation = headers['operation-location'] || headers['Operation-Location'];
|
||||
if (!operationLocation) {
|
||||
throw new Error('Missing Operation-Location header in Azure response.');
|
||||
}
|
||||
|
||||
// Poll until done
|
||||
let resultContent;
|
||||
while (true) {
|
||||
const pollResponse = await axios.get(operationLocation, {
|
||||
headers: { 'Ocp-Apim-Subscription-Key': apiKey },
|
||||
});
|
||||
|
||||
const { status, resultUrl } = pollResponse.data;
|
||||
if (status === 'succeeded') {
|
||||
const final = await axios.get(resultUrl, {
|
||||
headers: { 'Ocp-Apim-Subscription-Key': apiKey },
|
||||
});
|
||||
resultContent = final.data.analyzeResult.content;
|
||||
break;
|
||||
}
|
||||
if (status === 'failed') {
|
||||
throw new Error('Azure Document Intelligence processing failed.');
|
||||
}
|
||||
// Wait 2s before retry
|
||||
await new Promise((r) => setTimeout(r, 2000));
|
||||
}
|
||||
|
||||
return resultContent;
|
||||
} catch (error) {
|
||||
logger.error('Error performing Azure Document Intelligence:', error.message);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
uploadAzureDocumentIntelligence,
|
||||
};
|
||||
103
api/server/services/Files/AzureDocumentIntelligence/crud.spec.js
Normal file
103
api/server/services/Files/AzureDocumentIntelligence/crud.spec.js
Normal file
@@ -0,0 +1,103 @@
|
||||
const fs = require('fs');
|
||||
|
||||
const mockAxios = {
|
||||
interceptors: {
|
||||
request: { use: jest.fn(), eject: jest.fn() },
|
||||
response: { use: jest.fn(), eject: jest.fn() },
|
||||
},
|
||||
create: jest.fn().mockReturnValue({
|
||||
defaults: { proxy: null },
|
||||
get: jest.fn().mockResolvedValue({ data: {} }),
|
||||
post: jest.fn().mockResolvedValue({ data: {} }),
|
||||
put: jest.fn().mockResolvedValue({ data: {} }),
|
||||
delete: jest.fn().mockResolvedValue({ data: {} }),
|
||||
}),
|
||||
get: jest.fn().mockResolvedValue({ data: {} }),
|
||||
post: jest.fn().mockResolvedValue({ data: {} }),
|
||||
put: jest.fn().mockResolvedValue({ data: {} }),
|
||||
delete: jest.fn().mockResolvedValue({ data: {} }),
|
||||
reset: jest.fn().mockImplementation(function () {
|
||||
this.get.mockClear();
|
||||
this.post.mockClear();
|
||||
this.put.mockClear();
|
||||
this.delete.mockClear();
|
||||
this.create.mockClear();
|
||||
}),
|
||||
};
|
||||
|
||||
jest.mock('axios', () => mockAxios);
|
||||
jest.mock('fs');
|
||||
jest.mock('~/config', () => ({
|
||||
logger: { error: jest.fn() },
|
||||
}));
|
||||
|
||||
const { uploadAzureDocumentIntelligence } = require('./crud');
|
||||
|
||||
describe('AzureDocumentIntelligence Service', () => {
|
||||
beforeEach(() => {
|
||||
mockAxios.reset();
|
||||
fs.readFileSync.mockReset();
|
||||
});
|
||||
|
||||
it('should upload and poll until it gets the Markdown result', async () => {
|
||||
const mockFileBuffer = Buffer.from('test file content');
|
||||
const mockBase64 = mockFileBuffer.toString('base64');
|
||||
const mockOpLocation = 'https://azure-ocr-endpoint.com/operations/123';
|
||||
const mockResultUrl = 'https://azure-ocr-endpoint.com/results/123';
|
||||
const mockFinal = { analyzeResult: { content: 'Final analysis result' } };
|
||||
|
||||
// fs.readFileSync returns our buffer
|
||||
fs.readFileSync.mockReturnValue(mockFileBuffer);
|
||||
|
||||
// First axios.post => returns Operation-Location header
|
||||
mockAxios.post.mockResolvedValueOnce({
|
||||
headers: { 'Operation-Location': mockOpLocation },
|
||||
});
|
||||
|
||||
// First axios.get => poll success, returns status + resultUrl
|
||||
// Second axios.get => fetch final result
|
||||
mockAxios.get
|
||||
.mockResolvedValueOnce({ data: { status: 'succeeded', resultUrl: mockResultUrl } })
|
||||
.mockResolvedValueOnce({ data: mockFinal });
|
||||
|
||||
const result = await uploadAzureDocumentIntelligence({
|
||||
filePath: '/path/to/test.pdf',
|
||||
apiKey: 'azure-api-key',
|
||||
endpoint: 'https://azure-ocr-endpoint.com/',
|
||||
modelId: 'prebuilt-layout',
|
||||
});
|
||||
|
||||
// Validate read
|
||||
expect(fs.readFileSync).toHaveBeenCalledWith('/path/to/test.pdf');
|
||||
|
||||
// Validate initial POST
|
||||
expect(mockAxios.post).toHaveBeenCalledWith(
|
||||
'https://azure-ocr-endpoint.com/documentModels/prebuilt-layout:analyze?outputContentFormat=markdown',
|
||||
{ base64Source: mockBase64 },
|
||||
expect.objectContaining({
|
||||
headers: expect.objectContaining({
|
||||
'Ocp-Apim-Subscription-Key': 'azure-api-key',
|
||||
'Content-Type': 'application/json',
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
// Validate polling GET
|
||||
expect(mockAxios.get).toHaveBeenCalledWith(
|
||||
mockOpLocation,
|
||||
expect.objectContaining({
|
||||
headers: expect.objectContaining({ 'Ocp-Apim-Subscription-Key': 'azure-api-key' }),
|
||||
}),
|
||||
);
|
||||
|
||||
// Validate final fetch GET
|
||||
expect(mockAxios.get).toHaveBeenCalledWith(
|
||||
mockResultUrl,
|
||||
expect.objectContaining({
|
||||
headers: expect.objectContaining({ 'Ocp-Apim-Subscription-Key': 'azure-api-key' }),
|
||||
}),
|
||||
);
|
||||
|
||||
expect(result).toEqual('Final analysis result');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,5 @@
|
||||
const crud = require('./crud');
|
||||
|
||||
module.exports = {
|
||||
...crud,
|
||||
};
|
||||
@@ -47,6 +47,7 @@ const { uploadOpenAIFile, deleteOpenAIFile, getOpenAIFileStream } = require('./O
|
||||
const { getCodeOutputDownloadStream, uploadCodeEnvFile } = require('./Code');
|
||||
const { uploadVectors, deleteVectors } = require('./VectorDB');
|
||||
const { uploadMistralOCR } = require('./MistralOCR');
|
||||
const { uploadAzureDocumentIntelligence } = require('./AzureDocumentIntelligence'); // Import the function
|
||||
|
||||
/**
|
||||
* Firebase Storage Strategy Functions
|
||||
@@ -202,6 +203,26 @@ const mistralOCRStrategy = () => ({
|
||||
handleFileUpload: uploadMistralOCR,
|
||||
});
|
||||
|
||||
const azureOCRStrategy = () => ({
|
||||
/** @type {typeof saveFileFromURL | null} */
|
||||
saveURL: null,
|
||||
/** @type {typeof saveFileFromURL | null} */
|
||||
getFileURL: null,
|
||||
/** @type {typeof saveFileFromURL | null} */
|
||||
saveBuffer: null,
|
||||
/** @type {typeof saveFileFromURL | null} */
|
||||
processAvatar: null,
|
||||
/** @type {typeof saveFileFromURL | null} */
|
||||
handleImageUpload: null,
|
||||
/** @type {typeof saveFileFromURL | null} */
|
||||
prepareImagePayload: null,
|
||||
/** @type {typeof saveFileFromURL | null} */
|
||||
deleteFile: null,
|
||||
handleFileUpload: uploadAzureDocumentIntelligence,
|
||||
/** @type {typeof saveFileFromURL | null} */
|
||||
getDownloadStream: null,
|
||||
});
|
||||
|
||||
// Strategy Selector
|
||||
const getStrategyFunctions = (fileSource) => {
|
||||
if (fileSource === FileSources.firebase) {
|
||||
@@ -222,6 +243,8 @@ const getStrategyFunctions = (fileSource) => {
|
||||
return codeOutputStrategy();
|
||||
} else if (fileSource === FileSources.mistral_ocr) {
|
||||
return mistralOCRStrategy();
|
||||
} else if (fileSource === FileSources.azure_ocr) {
|
||||
return azureOCRStrategy();
|
||||
} else {
|
||||
throw new Error('Invalid file source');
|
||||
}
|
||||
|
||||
@@ -926,4 +926,4 @@
|
||||
"com_ui_zoom": "Zoom",
|
||||
"com_user_message": "You",
|
||||
"com_warning_resubmit_unsupported": "Resubmitting the AI message is not supported for this endpoint."
|
||||
}
|
||||
}
|
||||
|
||||
@@ -581,6 +581,7 @@ export type TStartupConfig = {
|
||||
export enum OCRStrategy {
|
||||
MISTRAL_OCR = 'mistral_ocr',
|
||||
CUSTOM_OCR = 'custom_ocr',
|
||||
AZURE_DOCUMENT_INTELLIGENCE = 'azure_document_intelligence',
|
||||
}
|
||||
|
||||
export enum SearchCategories {
|
||||
@@ -627,6 +628,7 @@ export type TWebSearchConfig = z.infer<typeof webSearchSchema>;
|
||||
|
||||
export const ocrSchema = z.object({
|
||||
mistralModel: z.string().optional(),
|
||||
documentIntelligenceModel: z.string().optional(),
|
||||
apiKey: z.string().optional().default('${OCR_API_KEY}'),
|
||||
baseURL: z.string().optional().default('${OCR_BASEURL}'),
|
||||
strategy: z.nativeEnum(OCRStrategy).default(OCRStrategy.MISTRAL_OCR),
|
||||
|
||||
@@ -4,11 +4,14 @@ import { OCRStrategy } from '../src/config';
|
||||
export function loadOCRConfig(config: TCustomConfig['ocr']): TCustomConfig['ocr'] {
|
||||
const baseURL = config?.baseURL ?? '';
|
||||
const apiKey = config?.apiKey ?? '';
|
||||
const mistralModel = config?.mistralModel ?? '';
|
||||
const mistralModel = config?.mistralModel ?? '';
|
||||
const documentIntelligenceModel = config?.documentIntelligenceModel ?? '';
|
||||
|
||||
return {
|
||||
apiKey,
|
||||
baseURL,
|
||||
mistralModel,
|
||||
documentIntelligenceModel,
|
||||
strategy: config?.strategy ?? OCRStrategy.MISTRAL_OCR,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ export enum FileSources {
|
||||
vectordb = 'vectordb',
|
||||
execute_code = 'execute_code',
|
||||
mistral_ocr = 'mistral_ocr',
|
||||
azure_ocr = 'azure_ocr',
|
||||
text = 'text',
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user