Coverage for src / lexigram / contracts / ai / embeddings.py: 73%

15 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-08-19 05:41 +0800

1"""Embedding contracts for Lexigram. 

2 

3Defines embeddings classes analogous to LangChain's embeddings. 

4""" 

5 

6from __future__ import annotations 

7 

8from abc import ABC, abstractmethod 

9 

10 

11class Embeddings(ABC): 

12 """Embeddings base class (like LangChain's Embeddings). 

13 

14 Provides interface for embedding documents and queries. 

15 """ 

16 

17 @abstractmethod 

18 def embed_documents(self, texts: list[str]) -> list[list[float]]: 

19 """Embed a list of documents. 

20 

21 Args: 

22 texts: List of text documents. 

23 

24 Returns: 

25 List of embedding vectors. 

26 """ 

27 ... 

28 

29 @abstractmethod 

30 def embed_query(self, text: str) -> list[float]: 

31 """Embed a single query. 

32 

33 Args: 

34 text: Query text. 

35 

36 Returns: 

37 Embedding vector. 

38 """ 

39 ... 

40 

41 

42class FakeEmbeddings(Embeddings): 

43 """Fake embeddings for testing (like LangChain's FakeEmbeddings).""" 

44 

45 def embed_documents(self, texts: list[str]) -> list[list[float]]: 

46 """Embed documents with fake embeddings.""" 

47 dimension = 384 

48 return [[0.1] * dimension for _ in texts] 

49 

50 def embed_query(self, text: str) -> list[float]: 

51 """Embed query with fake embeddings.""" 

52 dimension = 384 

53 return [0.1] * dimension 

54 

55 

56__all__ = [ 

57 "Embeddings", 

58 "FakeEmbeddings", 

59]