A label-driven RAG pipeline built on top of paralabelgen.
Project description
labelrag
labelrag is a Python library for label-driven retrieval-augmented generation
pipelines built on top of paralabelgen.
- PyPI distribution:
labelrag - Python import package:
labelrag - Core dependency target:
paralabelgen==0.2.0 - Default extraction path: spaCy via
paralabelgen
Install
pip install labelrag
If you want to use the default spaCy-backed labeling path, install a compatible English pipeline such as:
python -m spacy download en_core_web_sm
en_core_web_sm is the recommended default model, but you can point the
underlying LabelGeneratorConfig at another installed compatible spaCy
pipeline.
Quick Start
Retrieval-only workflow
from labelrag import RAGPipeline, RAGPipelineConfig
paragraphs = [
"OpenAI builds language models for developers.",
"Developers use language models in production systems.",
"Production systems need monitoring and evaluation tooling.",
]
pipeline = RAGPipeline(RAGPipelineConfig())
pipeline.fit(paragraphs)
retrieval = pipeline.build_context("How do developers use language models?")
print(retrieval.prompt_context)
print(retrieval.metadata)
Retrieval plus provider-backed answer generation
from labelrag import (
OpenAICompatibleAnswerGenerator,
OpenAICompatibleConfig,
RAGPipeline,
RAGPipelineConfig,
)
paragraphs = [
"OpenAI builds language models for developers.",
"Developers use language models in production systems.",
"Production systems need monitoring and evaluation tooling.",
]
pipeline = RAGPipeline(RAGPipelineConfig())
pipeline.fit(paragraphs)
generator = OpenAICompatibleAnswerGenerator(
OpenAICompatibleConfig(
model="mistral-small-latest",
api_key_env_var="MISTRAL_API_KEY",
base_url="https://api.mistral.ai/v1",
)
)
answer = pipeline.answer_with_generator(
"How do developers use language models?",
generator,
)
print(answer.answer_text)
print(answer.metadata)
Retrieval Model
The current retrieval layer is deterministic and label-driven.
fit(...)delegates paragraph analysis tolabelgen.LabelGeneratorbuild_context(...)maps the question into the fitted label space- retrieval uses greedy coverage over query label IDs
- label-free queries can fall back to deterministic concept overlap
require_full_label_coverage=Truesuppresses partial retrieval results while preserving attempted coverage trace in metadata
Tie-break order for greedy retrieval is:
- larger overlap with remaining query labels
- larger overlap on query concept IDs
- larger total paragraph label count
- lexicographically smaller
paragraph_id
OpenAI-Compatible Provider Notes
The built-in answer-generation adapter targets a minimal OpenAI-compatible chat-completions API surface.
It supports:
- standard base URLs such as
https://api.openai.com/v1 - full endpoint URLs such as
https://api.mistral.ai/v1/chat/completions - API key injection through explicit config or optional environment-variable lookup
- non-streaming text generation for
answer_with_generator(...)
This adapter is intended to cover providers such as OpenAI, Mistral, and Qwen when they expose an OpenAI-compatible endpoint shape.
Public API
The main public entrypoints are:
RAGPipelineRAGPipelineConfig,RetrievalConfig,PromptConfigIndexedParagraph,QueryAnalysis,RetrievedParagraphRetrievalResult,RAGAnswerResultGeneratedAnswer,AnswerGeneratorOpenAICompatibleAnswerGenerator,OpenAICompatibleConfig- convenience re-export:
Paragraph
Detailed API notes are available in docs/public_api.md.
Examples
Runnable examples are available in examples/:
examples/basic_usage.pyexamples/custom_config.pyexamples/save_and_load.pyexamples/provider_answer.py
Persistence Notes
save(path) produces a human-inspectable directory containing:
config.jsonlabel_generator.jsoncorpus_index.jsonfit_result.json
The persistence layer now supports:
jsonjson.gz
Compression is applied to the full saved snapshot rather than mixing compressed and uncompressed artifacts in one directory.
Public guarantee:
- a saved and reloaded pipeline should preserve retrieval behavior for the same fitted state, question, and config
Current update boundary:
fit(...)is batch-only- adding new paragraphs currently requires a full refit
- save/load restores a static fitted state rather than an incrementally updateable corpus state
Configuration Notes
RetrievalConfig.max_paragraphssets the hard retrieval limitRetrievalConfig.allow_label_free_fallbackenables deterministic concept overlap fallback for label-free queriesRetrievalConfig.require_full_label_coveragesuppresses partial retrieval output when not all query labels can be coveredPromptConfig.include_paragraph_idsincludes stable paragraph IDs in the rendered prompt contextPromptConfig.include_label_annotationsincludes paragraph label annotations in rendered prompt contextPromptConfig.max_context_charactersapplies a hard cap to rendered context length
Development Checks
.venv/bin/ruff check . --fix
.venv/bin/pyright
.venv/bin/pytest
Release Checks
.venv/bin/python -m build
.venv/bin/python -m twine check dist/*
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file labelrag-0.0.1.tar.gz.
File metadata
- Download URL: labelrag-0.0.1.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1dbc1eaa89be492bd4748003724c363af8be39f04bd8ae341cb39a8f13efcff6
|
|
| MD5 |
315f9143cd49ddeeb51e30d4db2e7ad5
|
|
| BLAKE2b-256 |
f6f57354458f9ddbeb587837480c038ad11008a9f16cc6730b27cd0f5ab00fca
|
File details
Details for the file labelrag-0.0.1-py3-none-any.whl.
File metadata
- Download URL: labelrag-0.0.1-py3-none-any.whl
- Upload date:
- Size: 18.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e4446340c7e4f6f67afd349597aa714f5df16045164bed5209f918d48628654
|
|
| MD5 |
04dba318308c56cf63fc7100124de9e7
|
|
| BLAKE2b-256 |
4e7e2d8d41094e45066642f050204e9d84893e91dfb72bcf434dd39bb1f821ce
|