teff.rag.base¶
teff.rag.base
¶
Vector store abstract base and similarity utilities.
Classes:
| Name | Description |
|---|---|
VectorStore |
Abstract interface for vector storage and similarity search. |
Functions:
| Name | Description |
|---|---|
blend_scores |
Blend a cosine score with a lexical overlap score. |
cosine_similarity |
Compute cosine similarity between two vectors. |
finalize_results |
Apply the filter, optional hybrid blending, rank, and cap at k. |
match_filter |
Return |
VectorStore
¶
Bases: ABC
Abstract interface for vector storage and similarity search.
Implementations must provide add, search, and delete. The
extended operations (count, list, get, update_metadata,
clear) default to :class:NotImplementedError and are implemented
by the built-in stores.
Methods:
| Name | Description |
|---|---|
add |
Store vectors with IDs and metadata. |
clear |
Remove all stored vectors. |
count |
Return the number of stored vectors. |
delete |
Remove vectors by ID. |
entries |
Return |
get |
Return |
search |
Search for the k nearest neighbours. |
update_metadata |
Merge metadata into the metadata of an existing ID. |
Source code in teff/rag/base.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
add
abstractmethod
async
¶
add(vectors)
Store vectors with IDs and metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vectors
|
list[tuple[str, list[float], dict]]
|
List of |
required |
Source code in teff/rag/base.py
92 93 94 95 96 97 98 99 | |
clear
async
¶
clear()
Remove all stored vectors.
Source code in teff/rag/base.py
151 152 153 | |
count
async
¶
count()
Return the number of stored vectors.
Source code in teff/rag/base.py
131 132 133 | |
delete
abstractmethod
async
¶
delete(ids)
Remove vectors by ID.
Source code in teff/rag/base.py
126 127 128 129 | |
entries
async
¶
entries(limit=100, offset=0)
Return (id, metadata) pairs with pagination.
Source code in teff/rag/base.py
135 136 137 138 139 | |
get
async
¶
get(ids)
Return (id, metadata) pairs for existing IDs.
Source code in teff/rag/base.py
141 142 143 | |
search
abstractmethod
async
¶
search(query, k=10, filter=None, hybrid=False, query_text=None)
Search for the k nearest neighbours.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
list[float]
|
Query embedding. |
required |
k
|
int
|
Maximum number of results. |
10
|
filter
|
dict | None
|
Optional metadata filter DSL (see :func: |
None
|
hybrid
|
bool
|
When |
False
|
query_text
|
str | None
|
Original query text, required for |
None
|
Returns:
| Type | Description |
|---|---|
list[tuple[str, float, dict]]
|
List of |
list[tuple[str, float, dict]]
|
descending. Scores are similarity-like (higher = more similar). |
Source code in teff/rag/base.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
update_metadata
async
¶
update_metadata(id, metadata)
Merge metadata into the metadata of an existing ID.
Source code in teff/rag/base.py
145 146 147 148 149 | |
blend_scores
¶
blend_scores(cosine, text, query, alpha=0.4)
Blend a cosine score with a lexical overlap score.
Used for hybrid search: alpha weights the lexical (keyword) share,
1 - alpha the semantic (cosine) share. When the query has no
alphabetic tokens the cosine score is returned unchanged.
Source code in teff/rag/base.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
cosine_similarity
¶
cosine_similarity(a, b)
Compute cosine similarity between two vectors.
Source code in teff/rag/base.py
156 157 158 159 160 161 162 163 | |
finalize_results
¶
finalize_results(candidates, k, filter=None, hybrid=False, query_text=None)
Apply the filter, optional hybrid blending, rank, and cap at k.
Stores that retrieve candidates (e.g. brute-force scans) use this to
post-process results consistently: drop non-matching metadata, blend
a lexical score for hybrid search, sort descending, and trim.
Source code in teff/rag/base.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
match_filter
¶
match_filter(metadata, filter)
Return True if metadata satisfies the filter DSL.
A filter is a dict of field -> condition pairs:
- scalar value — equality:
{"category": "news"} - list value — membership:
{"category": ["news", "tech"]}; when the stored field value is itself a list, any shared element matches "$and"/"$or"keys combine sub-filters (lists of filters).
A missing field never matches a scalar or list condition.
Source code in teff/rag/base.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |