How yukasama/txt2vec works
Project Type and Purpose
• This is a single-service, full-stack Python backend application (a text-embedding microservice).
• It exposes REST endpoints for uploading datasets, generating embeddings, managing and retrieving datasets, and interfacing with external storage (local files or GitHub).
• The core features include: dataset ingestion & validation, AI-model inference (embeddings), data persistence (SQLite via SQLAlchemy/aiosqlite), and file-upload management.
File Structure Analysis
• Top level:
• deploy/compose.yaml – Docker-Compose configuration
• Dockerfile – container build
• docs/ – PlantUML source for ER, API, use-cases
• src/txt2vec/ – application code
• scripts/ – utility and load-testing scripts (locust)
• test_data/ and tests/ – test vectors and unit tests
• Under src/txt2vec:
• app.py & server.py – application factory and startup (uvicorn via uv run app)
• config/ – configuration loading (app.toml), DB, logging, security, seeding
• common/ – shared models, exceptions, status codes
• ai_model/ – model definitions, repository layer for embeddings, helper utils
• datasets/ – models, repository, service, router, validation & file-handling utils
• upload/ – router + schemas + services (local vs GitHub backends)
• synthesis/ – domain models for embedding results
• utils/ – file sanitization
Key Insights from README
• Uses Astral’s “uv” wrapper (likely uvicorn + FastAPI)
• SQLite via SQLAlchemy/aiosqlite
• Locust for load testing (scripts/locust.py)
• CI/CD via GitHub Actions, Docker multi-stage build
Main Components
a. Clients & Interfaces
• External clients (e.g., CLI clients, web front ends, scripts)
• Locust load-tester as a special client
b. API Layer
• Uvicorn/“uv” server
• FastAPI routers:
• /datasets – ingestion, validation, metadata
• /upload – file upload (local / GitHub)
• /embeddings or /synthesis – embedding generation endpoints
c. Service Layer
• DatasetService – orchestration of file readers, mappers, normalizers
• UploadService – local vs GitHub backends behind a strategy interface
• AIModelService – loading model from repository, exposing embed(text)
d. Persistence Layer
• SQLAlchemy ORM (aiosqlite)
• Entities: Dataset, EmbeddingResult, UploadRecord, etc.
• config/seed for initial data
e. External Systems
• GitHub API (for remote file upload)
• Local file system storage (uploads)
f. Utilities & Cross-cutting
• Logging (config/logger.py)
• Security (config/security.py)
• Error handling (error_handler.py, common/exceptions)
Interactions and Data Flows
• Client → HTTP request → Uvicorn/FastAPI server
• Server dispatches to corresponding Router
• Router calls Service → Service uses Repository → Repository interacts with DB or FS or External API
• AIModelService retrieves or caches a model, runs inference, returns embeddings
• Results persisted to DB and returned to client
Architectural Patterns & Principles
• Layered Architecture: Presentation (routers) → Business Logic (services) → Data Access (repositories)
• Strategy Pattern in upload services (local vs GitHub)
• Dependency Injection via config/app factory
• Modularization by domain (ai_model, datasets, upload, synthesis)