Azure Module (src/azure/azure.module.ts)¶
Overview¶
The AzureModule is the main entry point for Azure Cloud integration in the BidScript backend. This module bundles all Azure-related services and provides configuration for interacting with various Azure services like OpenAI, Blob Storage, Form Recognizer, and SQL Database.
Module Structure¶
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AzureService } from './azure.service';
import { BlobService } from './blob.service';
import { DocumentParseService } from './document-parse.service';
import { AzureSqlService } from './azure-sql.service';
@Module({
imports: [ConfigModule],
providers: [
AzureService,
BlobService,
DocumentParseService,
AzureSqlService
],
exports: [
AzureService,
BlobService,
DocumentParseService,
AzureSqlService
]
})
export class AzureModule {}
Key Components¶
Module Imports¶
- ConfigModule: Provides access to configuration variables for Azure services.
Module Providers¶
- AzureService: Core service for Azure OpenAI integration, providing AI-powered text generation, document analysis, summarization, and question answering.
- BlobService: Manages document storage and retrieval using Azure Blob Storage.
- DocumentParseService: Extracts text and structure from various document formats using Azure's Form Recognizer service.
- AzureSqlService: Provides a simplified interface for interacting with Azure SQL databases.
Module Exports¶
All providers are exported, making them available to any module that imports AzureModule.
Usage¶
Importing the Module¶
To use Azure services in another module, import the AzureModule:
import { Module } from '@nestjs/common';
import { AzureModule } from '../azure/azure.module';
import { YourService } from './your.service';
@Module({
imports: [AzureModule],
providers: [YourService],
exports: [YourService]
})
export class YourModule {}
Service Injection¶
After importing the module, you can inject any of its exported services:
import { Injectable } from '@nestjs/common';
import { AzureService } from '../azure/azure.service';
import { BlobService } from '../azure/blob.service';
import { DocumentParseService } from '../azure/document-parse.service';
@Injectable()
export class YourService {
constructor(
private azureService: AzureService,
private blobService: BlobService,
private documentParseService: DocumentParseService
) {}
async processDocument(file: Buffer): Promise<string> {
// Upload document to Azure Blob Storage
const uploadResult = await this.blobService.uploadDocument(file, {
title: 'Document',
type: 'application/pdf'
});
// Parse document using Form Recognizer
const parseResult = await this.documentParseService.parseDocument(uploadResult.id);
// Generate summary using Azure OpenAI
const summary = await this.azureService.summarizeDocument(parseResult.content);
return summary;
}
}
Configuration¶
The module relies on environment variables for configuration. Each service has its own configuration requirements:
Azure OpenAI Configuration¶
AZURE_OPENAI_API_KEY=your-api-key
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com/
AZURE_OPENAI_DEPLOYMENT_NAME=your-deployment
Azure Blob Storage Configuration¶
AZURE_STORAGE_CONNECTION_STRING=DefaultEndpointsProtocol=https;AccountName=youraccount;AccountKey=yourkey;EndpointSuffix=core.windows.net
AZURE_STORAGE_CONTAINER=documents
Azure Form Recognizer Configuration¶
AZURE_FORM_RECOGNIZER_ENDPOINT=https://your-instance.cognitiveservices.azure.com/
AZURE_FORM_RECOGNIZER_API_KEY=your-api-key
Azure SQL Configuration¶
AZURE_SQL_CONNECTION_STRING=Server=your-server.database.windows.net;Database=your-database;User Id=your-username;Password=your-password;
Integration with Other Modules¶
The Azure Module integrates with multiple other modules in the BidScript platform:
- AuthModule: For authentication with Azure services and Azure AD integration
- RAGModule: For document processing, storage, and AI-powered retrieval
- ChatModule: For AI-powered conversation features
- EditorModule: For document editing and processing
- VectorstoreModule: For document embedding and semantic search
Example Use Cases¶
Document Processing Pipeline¶
// Upload a document
const uploadResult = await blobService.uploadDocument(fileBuffer, {
type: 'contract',
userId: '123'
});
// Parse the document
const parseResult = await documentParseService.parseDocument(uploadResult.id);
// Generate a summary
const summary = await azureService.summarizeDocument(parseResult.content, {
maxLength: 500
});
// Store metadata in SQL database
await azureSqlService.execute(
'INSERT INTO Documents (id, title, summary, userId, created) VALUES (@id, @title, @summary, @userId, @created)',
{
id: uploadResult.id,
title: 'Contract Document',
summary,
userId: '123',
created: new Date()
}
);
Question Answering System¶
// Retrieve document from storage
const document = await blobService.getDocument('document-id');
// Parse document if needed
const parseResult = await documentParseService.parseDocument('document-id');
// Answer question using document context
const answer = await azureService.answerQuestion('What are the payment terms?', parseResult.content, {
includeReferences: true
});
return answer;
Error Handling and Logging¶
The module implements centralized error handling and logging for all Azure services. Common errors are:
- Connection issues with Azure services
- Authentication errors
- Service rate limits
- Transient failures
Example of error handling implementation:
try {
await service.performOperation();
} catch (error) {
if (error.code === 'AuthenticationFailed') {
// Handle authentication error
} else if (error.code === 'ServiceUnavailable') {
// Handle service unavailability
} else if (error.code === 'RateLimitExceeded') {
// Handle rate limiting
} else {
// Handle other errors
}
}