Database Services
MongoDBClient
Singleton MongoDB connection manager using environment variables. Environment Variables:DB_USERNAME
DB_PASSWORD
DB_HOST
(default:localhost
)DB_PORT
(default:27017
)DB_DATABASE
(default:customai
)DB_CONNECTION
(default:mongodb
)
get_database(db_name=None)
- Returns MongoDB database instance
Database Utilities
get_db_instance(db_name=None)
Retrieves singleton database instance across the application.
mongodb_connection_error_handler(exc)
Global error handler for MongoDB connection issues.
Callback Handlers
MongoDBCallbackHandler
Post-processing handler for LLM operations. Methods:on_llm_end(response: LLMResult)
- Handles token tracking, memory updates, and chat history
- Usage tracking
- Credit updates
- Memory management
- Chat history persistence
Encryption Services
MessageEncryptor
AES encryption utility for plaintext messages.encrypt(plaintext: str) -> str
- Returns Base64-encoded ciphertext
MessageDecryptor
AES decryption utility for encrypted messages.decrypt(ciphertext: str) -> str
- Returns decrypted plaintext
Model Repositories
DefaultGeminiModelRepository
Manages default Gemini model configurations per company. Methods:get_default_model_key()
get_encrypt_key()
get_decrypt_key()
DefaultAnthropicModelRepository
Handles default Anthropic model configurations. Methods:get_default_model_key()
get_encrypt_key()
get_decrypt_key()
DefaultOpenAIModelRepository
Manages default OpenAI model configurations. Methods:get_default_model_key()
Notification Services
EmailService
Email sending abstraction for AWS SES or SMTP.Provider selection via
EMAIL_PROVIDER
environment variable.Firebase
Push notification service using Firebase Cloud Messaging.send_push_notification(tokens: list, title: str, body: str = None)
Security Services
LLMAPIKeyDecryptionHandler
Secure API key decryption from MongoDB.initialization(api_key_id: str, collection_name: str)
decrypt() -> bytes
Ensure correct decrypt key is configured in environment variables.
Streaming Utilities
StreamingResponseWithStatusCode
Enhanced FastAPI StreamingResponse with dynamic status code updates.- Dynamic status code changes during streaming
- Mid-stream error handling
- Compatible with FastAPI streaming responses
stream_response(send)
- Streams content with status code management