teff.rag¶
teff.rag
¶
Retrieval-augmented generation primitives.
Modules:
| Name | Description |
|---|---|
base |
Vector store abstract base and similarity utilities. |
chunker |
Text chunking strategies for RAG document splitting. |
embedder |
Embedding service using provider APIs. |
image_tool |
Image extraction tool — OCR via an OpenAI-compatible vision model. |
pdf_tool |
PDF extraction tool — turn a PDF into per-page text for RAG. |
stores |
Vector store implementations. |
tool |
RAG tool — retrieve context from a vector store for LLM use. |
Classes:
| Name | Description |
|---|---|
Chunker |
Split text into chunks for embedding and retrieval. |
Embedder |
Convert text to vector embeddings using a provider API. |
ImageTool |
Extract text from an image with an OpenAI-compatible vision model. |
PDFTool |
Extract text from a PDF file, one section per page. |
RAGTool |
Tool that searches a vector store and returns ranked results. |
VectorStore |
Abstract interface for vector storage and similarity search. |
Chunker
dataclass
¶
Split text into chunks for embedding and retrieval.
Supports three strategies:
token— Split on whitespace into token windows.sentence— Split on sentence boundaries.fixed— Split by fixed character count.
Attributes:
| Name | Type | Description |
|---|---|---|
strategy |
str
|
Chunking strategy name. |
chunk_size |
int
|
Target chunk size (tokens, sentences, or chars). |
overlap |
int
|
Overlap between consecutive chunks. |
Methods:
| Name | Description |
|---|---|
chunk |
Split text into chunks using the configured strategy. |
Source code in teff/rag/chunker.py
6 7 8 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
chunk
¶
chunk(text)
Split text into chunks using the configured strategy.
Source code in teff/rag/chunker.py
26 27 28 29 30 31 32 33 34 | |
Embedder
dataclass
¶
Convert text to vector embeddings using a provider API.
Attributes:
| Name | Type | Description |
|---|---|---|
provider |
str
|
Provider name ( |
model |
str
|
Embedding model name; defaults to a per-provider model. |
base_url |
str | None
|
Optional custom API base URL. |
api_key_env |
str | None
|
Env var holding the API key; defaults to the per-provider env var. |
Methods:
| Name | Description |
|---|---|
embed |
Embed a single text string. |
embed_many |
Embed multiple texts in a single API call. |
Source code in teff/rag/embedder.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
embed
async
¶
embed(text)
Embed a single text string.
Source code in teff/rag/embedder.py
59 60 61 62 | |
embed_many
async
¶
embed_many(texts)
Embed multiple texts in a single API call.
Source code in teff/rag/embedder.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
ImageTool
¶
Bases: Tool
Extract text from an image with an OpenAI-compatible vision model.
The image is base64-encoded and sent to a chat-completions vision
endpoint (default ollama/llava; openai/gpt-4o-mini is
the API alternative). Use it for OCR on screenshots, scans, charts
and photos.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict | None
|
Optional dict with |
None
|
Methods:
| Name | Description |
|---|---|
arun |
OCR the image at path and return the transcribed text. |
Source code in teff/rag/image_tool.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 | |
arun
async
¶
arun(path, prompt=None, max_chars=50000)
OCR the image at path and return the transcribed text.
Source code in teff/rag/image_tool.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 | |
PDFTool
¶
Bases: Tool
Extract text from a PDF file, one section per page.
Text-based PDFs are read with pypdf (extra teff[rag-pdf]).
Scanned / image-only pages yield no text — feed those pages to
:class:~teff.rag.image_tool.ImageTool instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict | None
|
Optional dict. |
None
|
Methods:
| Name | Description |
|---|---|
run |
Return the PDF text as |
Source code in teff/rag/pdf_tool.py
7 8 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 | |
run
¶
run(path, max_chars=None)
Return the PDF text as --- page N --- sections.
Source code in teff/rag/pdf_tool.py
28 29 30 31 32 33 34 35 36 37 38 39 40 | |
RAGTool
¶
Bases: Tool
Tool that searches a vector store and returns ranked results.
Usage::
store = InMemoryVectorStore(dim=768)
embedder = Embedder(provider="openai")
tool = RAGTool(store, embedder)
await tool.add_document("some long text")
result = await tool.arun(query="find this")
Can also be built from a config dict (e.g. a tools: entry in a
workflow YAML)::
{
"name": "rag_docs", # optional; overrides the default "rag"
"embedder": {"provider": "ollama", "model": "nomic-embed-text"},
"store": {"type": "in_memory", "dim": 768},
"documents": [
{"type": "csv", "path": "docs.csv"},
{"type": "txt", "path": "corpus/*.txt"},
{"type": "pdf", "path": "manual.pdf"},
{"type": "excel", "path": "table.xlsx", "text_column": "content"},
],
"filter": {"topic": "news"}, # metadata filter (DSL below)
"similarity_threshold": 0.5, # drop low-score hits
"max_tokens": 1024, # context token budget
"hybrid": true, # keyword + semantic blend
"parent_chunks": true, # keep full parent text per chunk
"parent_retrieval": true, # return whole parent documents
}
Supported document types (loaders): csv, txt (glob), pdf
(teff[rag-pdf]), excel (teff[rag-excel]). Supported store
types: in_memory (default), sqlite (stdlib file persistence),
faiss, lance, chroma, qdrant, milvus, weaviate,
pgvector, pinecone (via teff[embedding]).
documents may also be a bare path (CSV shorthand) or a list of
inline {"id": ..., "text": ...} dicts. Documents are embedded
lazily on the first search.
Filter DSL: {"category": "news"} (equality), {"category":
["news", "tech"]} (membership), plus "$and" / "$or" keys
combining sub-filters.
parent_chunks stores each chunk with a parent_id and the full
parent_text; with parent_retrieval enabled, search returns
whole parent documents (deduplicated) instead of individual chunks —
the "small-to-big" pattern.
Methods:
| Name | Description |
|---|---|
add_document |
Chunk, embed, and store a document. |
add_documents |
Add multiple documents at once. |
arun |
Search documents and return formatted results. |
Source code in teff/rag/tool.py
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | |
add_document
async
¶
add_document(text, metadata=None)
Chunk, embed, and store a document.
Source code in teff/rag/tool.py
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | |
add_documents
async
¶
add_documents(docs)
Add multiple documents at once.
Source code in teff/rag/tool.py
383 384 385 386 | |
arun
async
¶
arun(
query="",
k=5,
filter=None,
similarity_threshold=None,
max_tokens=None,
parent_retrieval=None,
)
Search documents and return formatted results.
Any optional argument overrides the value from the config for
this call; None falls back to the configured default.
Source code in teff/rag/tool.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
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 | |