teff.rag.tool¶
teff.rag.tool
¶
RAG tool — retrieve context from a vector store for LLM use.
Classes:
| Name | Description |
|---|---|
RAGTool |
Tool that searches a vector store and returns ranked results. |
Functions:
| Name | Description |
|---|---|
load_documents_csv |
Load documents from a CSV file. |
load_documents_excel |
Load documents from an Excel file, one per row (requires |
load_documents_pdf |
Load text from a PDF, one document per page (requires |
load_documents_txt |
Load documents from |
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 | |
load_documents_csv
¶
load_documents_csv(path, text_column='text', delimiter=',')
Load documents from a CSV file.
Returns a list of (text, metadata) tuples. The text_column
column becomes the document text; all other columns become metadata.
Source code in teff/rag/tool.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
load_documents_excel
¶
load_documents_excel(path, text_column='text', sheet=0, skip_header=True)
Load documents from an Excel file, one per row (requires openpyxl).
Source code in teff/rag/tool.py
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 | |
load_documents_pdf
¶
load_documents_pdf(path)
Load text from a PDF, one document per page (requires pypdf).
Source code in teff/rag/tool.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
load_documents_txt
¶
load_documents_txt(path, encoding='utf-8')
Load documents from .txt files (glob supported).
Each matched file becomes one (text, {"id": stem, "path": ...}) doc.
Source code in teff/rag/tool.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |