Implementation:ALwrity ALwrity TxtaiIntelligenceService index content
| Knowledge Sources | |
|---|---|
| Domains | Semantic_Indexing, Daily_Workflow, Search |
| Last Updated | 2026-10-24 00:00 GMT |
Overview
Concrete tool for per-user semantic indexing of content via txtai embeddings with singleton lifecycle, lazy initialisation, and a faiss backend, provided by the ALwrity intelligence subsystem (SIF).
Description
The TxtaiIntelligenceService is the core service of the Semantic Indexing Framework (SIF). It is a per-user singleton (thread-safe double-checked locking via __new__) that wraps a txtai Embeddings instance backed by the sentence-transformers/all-MiniLM-L6-v2 model and a faiss backend. The index_content method accepts a list of (id, text, metadata) tuples, upserts them into the embeddings index, and saves the index to the per-user path (workspace/workspace_{user_id}/indices/txtai). Initialisation is lazy (deferred until first use) and async-guarded per user. A fail_fast mode (env SIF_FAIL_FAST, default true) raises on missing txtai or uninitialised service rather than silently returning 0. A semantic cache (semantic_cache_manager) wraps queries for performance.
Usage
Import this service when:
- The daily workflow pipeline needs to index tasks for semantic search and self-learning.
- A podcast or blog pipeline needs to index content for retrieval.
- A per-user semantic index must be maintained with singleton lifecycle.
Code Reference
Source Location
- Repository: ALwrity
- File: backend/services/intelligence/txtai_service.py (L29-100, L346-386)
Signature
class TxtaiIntelligenceService:
DEFAULT_MODEL_PATH = "sentence-transformers/all-MiniLM-L6-v2"
def __new__(cls, user_id: str, *args, **kwargs):
"""Thread-safe per-user singleton via double-checked locking."""
def __init__(
self,
user_id: str,
model_path: Optional[str] = None,
enable_caching: bool = True,
):
"""
Args:
user_id: Clerk user ID (determines index path).
model_path: sentence-transformers model (default all-MiniLM-L6-v2).
enable_caching: Enable semantic cache (default True).
"""
async def index_content(
self,
items: List[Tuple[str, str, Dict[str, Any]]],
) -> int:
"""
Index content using incremental upsert — only processes new/changed documents.
Args:
items: List of (id, text, metadata) tuples.
Returns:
Number of items actually upserted.
"""
Import
from services.intelligence.txtai_service import TxtaiIntelligenceService
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| items | List[Tuple[str, str, Dict]] | Yes | (id, text, metadata) tuples |
| user_id | str | Yes (init) | Clerk user ID (singleton key, index path) |
| model_path | Optional[str] | No | sentence-transformers model (default all-MiniLM-L6-v2) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | int | Number of items upserted |
| side effect | txtai index | Embeddings upserted and saved to workspace/workspace_{user_id}/indices/txtai |
Usage Examples
Indexing daily workflow tasks
from services.intelligence.txtai_service import TxtaiIntelligenceService
service = TxtaiIntelligenceService(user_id="clerk_user_123")
count = await service.index_content([
("task_1", "Write a blog post about QLoRA.", {"tags": "task_memory completed blog", "status": "completed"}),
("task_2", "Analyse competitor backlinks.", {"tags": "task_memory dismissed seo", "status": "dismissed"}),
])
print(f"Indexed {count} items")
Semantic search (query the index)
# After indexing, query for similar tasks
results = service.embeddings.search("fine-tuning LLMs", limit=5)
for r in results:
print(f" id={r['id']} score={r['score']}")
Related Pages
Implements Principle
- Principle:ALwrity_ALwrity_Semantic_Indexing_Self_Learning
- Principle:ALwrity_ALwrity_Committee_Agent_Generation