Embedding Ownership¶
Split-Ownership Principle¶
The Loryth platform has two distinct embedding use cases, owned by different services:
| Concern | Owner | Table | Purpose |
|---|---|---|---|
| Conversation vectors | loryth-ai | ai_conversation_vector | Semantic search across chat history |
| Document vectors | loryth-kb | kb_document_vector (future) | Knowledge base document retrieval |
Each service manages its own embedding lifecycle — generation, storage, indexing, and search. The EmbeddingProvider abstraction is shared, but each service calls it independently with its own content.
ai_conversation_vector¶
Stores one embedding per message for semantic similarity search across conversations.
| Column | Type | Description |
|---|---|---|
id | BIGSERIAL | Primary key |
uuid | uuid | Unique identifier |
message_id | BIGINT | FK → ai_message.id (CASCADE) |
space_id | uuid | Space for query scoping |
conversation_id | uuid | Conversation UUID for result grouping |
embedding_model_ref | VARCHAR(255) | Model identifier (e.g., text-embedding-ada-002) |
content_hash | VARCHAR(128) | SHA hash of embedded content (dedup) |
embedding | vector(1536) | pgvector column (1536 dimensions) |
created_at | TIMESTAMP | Creation timestamp |
Index¶
CREATE INDEX idx_ai_conversation_vector__embedding
ON ai_conversation_vector
USING ivfflat (embedding vector_cosine_ops);
IVFFlat is used for approximate nearest neighbor search with cosine similarity. The index works with any number of rows (including zero).
EmbeddingProvider Abstraction¶
class EmbeddingProvider(ABC):
@abstractmethod
def embed(self, texts: list[str]) -> list[list[float]]:
"""Generate embeddings for a list of texts."""
...
Implementations¶
| Provider | Dimensions | Status |
|---|---|---|
StubEmbeddingProvider | 1536 | Available (zero vectors, for dev/test) |
OpenAI text-embedding-ada-002 | 1536 | Planned |
OpenAI text-embedding-3-small | 1536 | Planned |
The dimension count (1536) is fixed in v1 by the vector(1536) column definition. Supporting variable dimensions would require a schema migration.
Embedding Generation Flow¶
- Client calls
POST /embeddings/generatewithconversation_id EmbeddingServiceloads all messages for the conversation- For each message, compute
content_hash(SHA-256 of content) - Skip messages that already have a vector with matching
content_hash(unlessforce=true) - Batch remaining texts through
EmbeddingProvider.embed() - Store resulting
ConversationVectorrecords
Search Flow¶
- Client calls
POST /conversations/searchwithspace_id,query, andlimit EmbeddingServiceembeds the query text viaEmbeddingProvider.embed()- Execute pgvector cosine similarity search scoped to
space_id: - Return ranked results with
conversation_id,message_id,content, andsimilarityscore