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.2 - Primary supported extraction path:
paralabelgenLLM concept extraction - First semantic-reranking embedding provider:
sentence-transformers
Install
pip install labelrag
If you want to use the spaCy-backed extraction path, install a compatible English pipeline such as:
python -m spacy download en_core_web_sm
en_core_web_sm is a convenient local option, but 0.1.0 release validation
targets the paralabelgen==0.2.2 LLM concept-extraction pipeline as the
primary supported extraction path.
The first shipped semantic-reranking provider uses sentence-transformers.
Its model weights may be downloaded on first use if they are not already cached
locally.
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.",
]
config = RAGPipelineConfig()
config.labelgen.extractor_mode = "heuristic"
config.labelgen.use_graph_community_detection = False
pipeline = RAGPipeline(config)
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.",
]
config = RAGPipelineConfig()
config.labelgen.extractor_mode = "heuristic"
config.labelgen.use_graph_community_detection = False
pipeline = RAGPipeline(config)
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 still label-driven at the candidate-generation stage.
fit(...)delegates paragraph analysis tolabelgen.LabelGeneratorfit(...)also builds paragraph embeddingsbuild_context(...)maps the question into the fitted label space- retrieval uses greedy coverage over query label IDs
- semantic similarity is used as a secondary ranking signal inside greedy selection
- label-free queries can use configurable fallback strategies
require_full_label_coverage=Truesuppresses partial retrieval results while preserving attempted coverage trace in metadata
Greedy selection order is:
- larger overlap with remaining query labels
- larger semantic similarity
- larger overlap on query concept IDs
- larger total paragraph label count
- lexicographically smaller
paragraph_id
0.1.1 supports three label-free fallback strategies:
concept_overlap_onlyconcept_overlap_semantic_reranksemantic_only
The default is concept_overlap_semantic_rerank.
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,LabelRecord,ConceptRecordQueryAnalysis,RetrievedParagraphRetrievalResult,RAGAnswerResultGeneratedAnswer,AnswerGeneratorOpenAICompatibleAnswerGenerator,OpenAICompatibleConfig- convenience re-export:
Paragraph
RAGPipeline also exposes record-oriented inspection helpers for
paragraph/label/concept lookup workflows:
get_paragraph(...)get_label(...)get_paragraph_labels(...)get_paragraph_concepts(...)get_label_paragraphs(...)get_concept_paragraphs(...)
Lower-level ID-oriented helpers remain available when you only need stable IDs:
get_label_paragraph_ids(...)get_paragraph_label_ids(...)get_paragraph_concept_ids(...)get_concept_paragraph_ids(...)
Detailed API notes are available in docs/public_api.md.
Embedding Notes
RAGPipeline.fit(...)now requires an embedding provider- the default runtime path is
RAGPipeline(config)and resolves the embedding provider fromconfig.embedding - explicit
embedding_provider=is still available as an advanced override - the first shipped provider is
SentenceTransformerEmbeddingProvider - the default model is
sentence-transformers/all-MiniLM-L6-v2 - the model may be downloaded on first use
- offline environments should pre-cache the embedding model before running
fit(...)
Common runtime failures:
- missing
sentence-transformerspackage:- reinstall project dependencies, for example
pip install -e .
- reinstall project dependencies, for example
- model load/download failure:
- verify the configured model name
- ensure the model is already cached locally or that the environment can reach Hugging Face
Examples
Runnable examples are available in examples/:
examples/basic_usage.pyexamples/custom_config.pyexamples/inspection_api.pyexamples/fallback_policies.pyexamples/semantic_rerank.pyexamples/save_and_load.pyexamples/provider_answer.py
Example note:
- the runnable example scripts use a tiny local demo embedding provider so they stay runnable offline
- production usage should prefer
SentenceTransformerEmbeddingProvider
Persistence Notes
save(path) produces a human-inspectable directory containing:
manifest.jsonconfig.jsonlabel_generator.jsoncorpus_index.jsonfit_result.jsonparagraph_embeddings.npz
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.
Snapshots written by the current release include a lightweight manifest describing the saved version, persistence format, and expected artifacts.
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
Legacy snapshot note:
- loading pre-embedding snapshots remains a best-effort compatibility path
- when older snapshots are missing derived concept inspection tables,
load()may rebuild them from paragraph-side concept data that is still present - when older snapshots predate
paragraph_embeddings.npz,load()may rebuild paragraph embeddings from persisted paragraph texts if an embedding provider is available - persisted manifests include a non-empty
labelrag_version save()fails explicitly if the current package version cannot be determined for manifest writing
Configuration Notes
RetrievalConfig.max_paragraphssets the hard retrieval limitRetrievalConfig.allow_label_free_fallbackenables deterministic concept fallback behavior for label-free queriesRetrievalConfig.label_free_fallback_strategyselects one of:concept_overlap_onlyconcept_overlap_semantic_reranksemantic_only
RetrievalConfig.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.1.1.tar.gz.
File metadata
- Download URL: labelrag-0.1.1.tar.gz
- Upload date:
- Size: 20.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6bb0ce147c1fcce3de9621187bfbef1f09a01a032c4db835ab712df31e1ba216
|
|
| MD5 |
a93b69bdca22b28f27338e34a602fad0
|
|
| BLAKE2b-256 |
0c20047936e92373d78620dfd907708cac8342b5c759a7c8c9a5662b94ce9056
|
Provenance
The following attestation bundles were made for labelrag-0.1.1.tar.gz:
Publisher:
publish.yml on HuRuilizhen/labelrag
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
labelrag-0.1.1.tar.gz -
Subject digest:
6bb0ce147c1fcce3de9621187bfbef1f09a01a032c4db835ab712df31e1ba216 - Sigstore transparency entry: 1294078495
- Sigstore integration time:
-
Permalink:
HuRuilizhen/labelrag@2c960cae2a6f21fa5ac911c6624c0e4cd116d924 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/HuRuilizhen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2c960cae2a6f21fa5ac911c6624c0e4cd116d924 -
Trigger Event:
push
-
Statement type:
File details
Details for the file labelrag-0.1.1-py3-none-any.whl.
File metadata
- Download URL: labelrag-0.1.1-py3-none-any.whl
- Upload date:
- Size: 27.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b43bdf9cf9c4c92e25e9cfaef401a43fef03512bf0b13d8df965ab96b493527
|
|
| MD5 |
599668367aa4b7513c053d6e4bb9ea03
|
|
| BLAKE2b-256 |
a7dff0f9592a889776b6bed2c778679647b108cb1f7e05a07b972c3c5e0d4b0e
|
Provenance
The following attestation bundles were made for labelrag-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on HuRuilizhen/labelrag
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
labelrag-0.1.1-py3-none-any.whl -
Subject digest:
5b43bdf9cf9c4c92e25e9cfaef401a43fef03512bf0b13d8df965ab96b493527 - Sigstore transparency entry: 1294078562
- Sigstore integration time:
-
Permalink:
HuRuilizhen/labelrag@2c960cae2a6f21fa5ac911c6624c0e4cd116d924 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/HuRuilizhen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2c960cae2a6f21fa5ac911c6624c0e4cd116d924 -
Trigger Event:
push
-
Statement type: