How mpairtech/emventory-ai-backend works
1. This repository is a FastAPI-based AI backend centered on semantic search and RAG. The main runtime entrypoint is app/main.py, which wires the API, configuration, dependency injection, and startup DB initialization.
2. The HTTP boundary lives under app/api/v1/. router.py aggregates the versioned routers, while the feature routers split the API by concern: search, content, voice_router, speech_router, and revoke_router. Request/response contracts are defined in app/api/v1/schemas.py.
3. The core application layer is in app/core/. It holds cross-cutting concerns that sit below the API and above the domain modules: configuration loading, exceptions, dependency providers, Redis cache client/service, LLM adapters, and vector-store helpers.
4. Persistence is PostgreSQL with SQLAlchemy models in app/db/ and app/db/models/. The vector-specific model is app/db/models/vector.py, and app/core/vector/pgvector.py indicates the semantic index is implemented with pgvector inside Postgres rather than a separate search engine.
5. Redis is a second infrastructure dependency, wrapped by app/core/cache/redis_client.py and cache_service.py. Architecturally, this is a side cache alongside Postgres, likely used to speed up repeated lookups or store transient AI/search state.
6. The main domain logic is organized under app/modules/. search/ is the most important module and appears to own the AI search pipeline: service.py orchestrates the flow, repository.py handles data access, embeddings.py handles embedding generation, and stt.py suggests speech-to-text support is part of the search experience.
7. app/modules/content/service.py is a separate business module for content operations. In architecture terms, it likely represents the ingestion/update side of the system: content enters here, is persisted in Postgres, and can then be indexed for semantic retrieval.
8. Model generation and AI inference are abstracted behind provider-specific clients in app/core/llm/. There are separate integrations for Gemini and OpenAI, which means the backend can switch or compare LLM providers without changing the API layer. The README confirms these are used for embeddings and RAG generation.
9. Speech-related functionality is split out from search. app/modules/speech/huggingface_service.py suggests a dedicated speech AI component backed by Hugging Face models, while the voice/speech routers expose it through the API. This keeps audio processing isolated from the core search and content flows.
10. The data flow is: client request → API router → domain service → repository/vector/cache/LLM dependencies → Postgres/pgvector, Redis, and external AI providers → API response. The key architectural boundary is that routers stay thin, while the domain modules and core adapters own the AI and storage behavior.
11. Operationally, the repo is container-friendly: there is a Dockerfile, docker-compose.yml, environment templates, and YAML config defaults under config/ for local and production settings. Startup DB initialization and migration utilities live in scripts/, which suggests the schema and vector index setup are part of the deployment workflow.
12. There are also supporting artifacts that matter for understanding the system shape: streamlit_test_app.py for a local UI/test client, app/tests/ for basic verification, and several docs focused on architecture, RAG, and pgvector checks. These are auxiliary to the runtime but help confirm the repo is an AI search backend rather than a general-purpose web app.
Main entry points: service.py Search Service · router.py API Router · dependencies.py Dependencies · main.py Main
Components 26
API
HTTP boundary and request contracts
- Main · Application startup and wiring
app/main.py - API Router · Aggregates versioned HTTP routes
app/api/v1/router.py - Search · Semantic search and RAG endpoints
app/api/v1/routers/search.py - Content · Content ingestion and management endpoints
app/api/v1/routers/content.py - Voice · Voice interaction endpoints
app/api/v1/routers/voice_router.py - Speech · Speech processing endpoints
app/api/v1/speech_router.py - Revoke · Revocation and cancellation endpoints
app/api/v1/routers/revoke_router.py - Schemas · Request and response models
app/api/v1/schemas.py
Core
Cross-cutting configuration, adapters, and infrastructure helpers
- Config · Environment and app configuration
app/core/config.py - Dependencies · FastAPI dependency wiring
app/core/dependencies.py - Errors · Shared API and domain exceptions
app/core/exceptions.py - Redis Client · Redis connection wrapper
app/core/cache/redis_client.py - Cache Service · Redis-backed caching operations
app/core/cache/cache_service.py - Gemini · Google Gemini integration
app/core/llm/gemini.py - OpenAI · OpenAI integration
app/core/llm/openai.py - pgvector · Postgres vector search helpers
app/core/vector/pgvector.py
Modules
Domain services for search, content, and speech
- Search Service · Coordinates query, retrieval, and generation
app/modules/search/service.py - Search Repo · Search persistence and retrieval access
app/modules/search/repository.py - Embeddings · Embedding generation for semantic index
app/modules/search/embeddings.py - STT · Speech transcription support within search
app/modules/search/stt.py - Content Service · Content ingestion and update logic
app/modules/content/service.py - HuggingFace Speech · Dedicated speech model integration
app/modules/speech/huggingface_service.py
Data
Persistence models and database session
- DB Session · Database session management
app/db/session.py - Vector Model · Vector-enabled persistence model
app/db/models/vector.py
Connections 40
- Main → API Router: mounts API · Main application mounts the versioned router
- Main → Config: loads · Startup reads runtime configuration
- Main → Dependencies: wires DI · Main app initializes dependency providers
- Main → Init DB: initializes · Startup database setup path
- API Router → Search: includes · Version router aggregates search endpoints
- API Router → Content: includes · Version router aggregates content endpoints
- API Router → Voice: includes · Version router aggregates voice endpoints
- API Router → Speech: includes · Version router aggregates speech endpoints
- API Router → Revoke: includes · Version router aggregates revoke endpoints
- Search → Schemas: uses · Search API depends on request/response contracts
- Content → Schemas: uses · Content API depends on request/response contracts
- Voice → Schemas: uses · Voice API depends on request/response contracts
- Speech → Schemas: uses · Speech API depends on request/response contracts
- Search → Search Service: calls · Search requests are handled by the domain service
- Content → Content Service: calls · Content requests are handled by the content service
- Voice → HuggingFace Speech: calls · Voice endpoints delegate to speech AI service
- Speech → HuggingFace Speech: calls · Speech endpoints delegate to speech AI service
- Search Service → Search Repo: queries · Search orchestration reads/writes through repository
- Search Service → Embeddings: generates · Search flow needs embeddings for semantic retrieval
- Search Service → STT: transcribes · Search flow can accept speech input
- Search Service → Cache Service: caches · Repeated or transient search state can be cached
- Search Service → Gemini: generates · LLM-backed RAG generation can use Gemini
- Search Service → OpenAI: generates · LLM-backed RAG generation can use OpenAI
- Search Repo → DB Session: persists · Repository uses SQLAlchemy sessions
- Search Repo → Vector Model: stores · Repository works with vector-enabled records
- Search Repo → pgvector: indexes · Semantic retrieval uses pgvector helpers
- Content Service → DB Session: persists · Content workflow writes through the database session
- Content Service → Vector Model: updates · Content changes can affect indexed vector records
- Cache Service → Redis Client: uses · Cache service is backed by Redis client
- Dependencies → Cache Service: provides · Dependency layer exposes cache service
- Dependencies → DB Session: provides · Dependency layer exposes database sessions
- Dependencies → Gemini: provides · Dependency layer can select Gemini adapter
- Dependencies → OpenAI: provides · Dependency layer can select OpenAI adapter
- Dependencies → Search Service: injects · Services are injected into HTTP handlers
- Errors → Search: guards · Shared error handling applies to routes
- Errors → Content: guards · Shared error handling applies to routes
- Errors → Voice: guards · Shared error handling applies to routes
- Test UI → API Router: calls · Local test client exercises the HTTP API
- Init DB → DB Session: prepares · Initialization scripts prepare the database layer
- Init DB → pgvector: enables · Bootstrap sets up vector support in Postgres