RAGLibrary
The primary facade class for cognity-ai. Single entry-point for ingestion, querying, knowledge lifecycle management, and plugin registration.
Constructor
All parameters are optional. Passing no arguments gives a zero-config instance that auto-detects available backends from installed packages and environment variables.
class RAGLibrary:
def __init__(
self,
# Component selection
rag_method: str = "hybrid_graph",
chunker: str = "sentence",
embedder: str = "gemini",
vector_store: str = "chroma",
graph_store: str = "neo4j",
llm: str = "gemini",
extraction: str = "hybrid",
ocr: str = "gemini_vision",
page_index: str = "hybrid",
# API credentials
gemini_api_key: str | None = None,
openai_api_key: str | None = None,
anthropic_api_key: str | None = None,
azure_openai_endpoint: str | None = None,
azure_openai_key: str | None = None,
aws_region: str = "us-east-1",
neo4j_uri: str = "bolt://localhost:7687",
neo4j_user: str = "neo4j",
neo4j_password: str | None = None,
cohere_api_key: str | None = None,
ollama_base_url: str = "http://localhost:11434",
# Advanced: full config override
config: LibraryConfig | None = None,
)
config object is supplied it takes full precedence; individual keyword arguments are ignored. Use keyword arguments for quick setup and LibraryConfig for production deployments with fine-grained control.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
rag_method |
str |
"hybrid_graph" |
Default retrieval strategy used for all query() calls unless overridden per-call. |
chunker |
str |
"sentence" |
Text chunking strategy applied during ingestion. |
embedder |
str |
"gemini" |
Embedding provider used to vectorize chunks and queries. |
vector_store |
str |
"chroma" |
Backend for storing and searching dense vector embeddings. |
graph_store |
str |
"neo4j" |
Graph database backend for entity-relationship storage. |
llm |
str |
"gemini" |
LLM provider used for answer generation. |
extraction |
str |
"hybrid" |
Entity and relation extraction strategy during ingestion. |
ocr |
str |
"gemini_vision" |
OCR provider for image and scanned-document ingestion. |
page_index |
str |
"hybrid" |
Strategy for indexing page-level structure in multi-page documents. |
gemini_api_key |
str | None |
None |
Google Gemini API key. Falls back to GEMINI_API_KEY env var. |
openai_api_key |
str | None |
None |
OpenAI API key. Falls back to OPENAI_API_KEY env var. |
anthropic_api_key |
str | None |
None |
Anthropic API key. Falls back to ANTHROPIC_API_KEY env var. |
azure_openai_endpoint |
str | None |
None |
Azure OpenAI resource endpoint URL. |
azure_openai_key |
str | None |
None |
Azure OpenAI API key. |
aws_region |
str |
"us-east-1" |
AWS region for Bedrock API calls. Uses standard AWS credential chain. |
neo4j_uri |
str |
"bolt://localhost:7687" |
Neo4j Bolt connection URI. |
neo4j_user |
str |
"neo4j" |
Neo4j username. |
neo4j_password |
str | None |
None |
Neo4j password. Falls back to NEO4J_PASSWORD env var. |
cohere_api_key |
str | None |
None |
Cohere API key. Falls back to COHERE_API_KEY env var. |
ollama_base_url |
str |
"http://localhost:11434" |
Base URL for a locally running Ollama server. |
config |
LibraryConfig | None |
None |
Full configuration object. When supplied, all other keyword arguments are ignored. |
Valid String Values
The highlighted pill is the default value for each parameter.
rag_methodchunkerembeddervector_storegraph_storellmextractionocrpage_indexIngestion Methods
All ingestion methods parse documents, chunk text, extract entities, embed chunks, and write to both the vector store and graph store in a single call.
Ingest a single file. The file format is auto-detected from the file extension. Supports PDF, DOCX, XLSX, PPTX, TXT, MD, HTML, CSV, JSON, JSONL, and any image format supported by the configured OCR provider.
| Parameter | Type | Description |
|---|---|---|
path | str | Path | Filesystem path to the file to ingest. |
Ingest all matching files in a directory. Files that cannot be parsed are skipped with a warning. Returns the total count of successfully ingested files.
| Parameter | Type | Default | Description |
|---|---|---|---|
directory | str | Path | — | Root directory to scan. |
recursive | bool | True | When True, descend into subdirectories. |
glob | str | "*" | Glob pattern to filter filenames, e.g. "*.pdf" or "report_*.docx". |
Ingest raw text directly, bypassing the loader step. Useful for programmatically generated content, API responses, or when you have already extracted text from a custom source.
| Parameter | Type | Description |
|---|---|---|
text | str | The raw text content to ingest. |
doc_id | str | A unique identifier for this document. Used for lifecycle operations (confirm, deprecate). |
metadata | dict | None | Optional key-value metadata to attach (e.g. source URL, author, timestamp). |
Run community detection over the knowledge graph using the Leiden or Louvain algorithm (selected automatically based on graph size). Communities are stored back into the graph store and used by the microsoft_graphrag and hybrid_graph retrieval methods to answer global/thematic queries.
Call this method after bulk ingestion. Incremental ingestion does not automatically rebuild communities.
ingest_dir() first, then build_communities() once at the end. Re-running build_communities() after adding new documents is cheap — only the changed subgraph is recomputed.
Query Methods
All query methods accept a plain-language question and return answers grounded in the ingested knowledge base. The method argument overrides the default rag_method for a single call.
Answer a natural-language question using the configured RAG pipeline. Returns the generated answer as a plain string. For source attribution use query_with_sources() instead.
| Parameter | Type | Default | Description |
|---|---|---|---|
question | str | — | The question to answer. |
top_k | int | 10 | Maximum number of chunks to retrieve and pass to the LLM context. |
method | str | None | None | Per-query RAG method override. Accepts same values as the rag_method constructor argument. |
Answer a question and return the answer together with full provenance — source document metadata and the raw chunks that were used to construct the answer.
The returned dictionary has the following shape:
{
"answer": str, # Generated answer text
"sources": list[dict], # Source document metadata dicts
"chunks": list[SemanticChunk], # Raw chunks passed to the LLM
}
Retrieve the most relevant chunks for a query without generating an LLM answer. Useful for inspection, custom re-ranking pipelines, or building your own prompt.
| Parameter | Type | Default | Description |
|---|---|---|---|
query | str | — | The search query. |
top_k | int | 10 | Maximum number of results to return. |
method | str | None | None | Retrieval strategy override for this call. |
Lifecycle Methods
cognity-ai tracks the authority and freshness of each ingested document. Lifecycle methods let you mark sources as authoritative, retire outdated knowledge, detect conflicts, and prune low-confidence information.
Mark a document as confirmed and authoritative. Confirmed sources receive a higher confidence weight during retrieval ranking, making their chunks more likely to appear in answers.
Mark a document as deprecated. Deprecated chunks are excluded from retrieval but remain in the stores so they can be restored or audited. Use this to retire superseded reports or outdated policy documents without deleting them.
Find conflicting facts about a named entity in the knowledge graph. Returns a list of conflict records, each containing the two contradicting claims, their source documents, and a conflict severity score.
| Parameter | Type | Description |
|---|---|---|
entity | str | The entity name to check for conflicting assertions (e.g. "Apple Inc", "CEO"). |
Remove chunks and graph nodes whose confidence score falls below threshold. Confidence is computed from source status, citation frequency, and conflict history. Returns the number of items pruned.
| Parameter | Type | Default | Description |
|---|---|---|---|
threshold | float | 0.5 | Confidence threshold in [0, 1]. Items below this value are permanently deleted. |
Return a summary of the current knowledge base state. Useful for monitoring ingestion progress and graph quality.
{
"entities": int, # Total graph entities (nodes)
"relations": int, # Total graph relationships (edges)
"chunks": int, # Total chunks in the vector store
"communities": int, # Graph communities detected (0 if not yet built)
"documents": int, # Total source documents ingested
"deprecated": int, # Documents marked deprecated
"confirmed": int, # Documents marked confirmed/authoritative
}
prune() permanently deletes data from both the vector store and graph store. Consider calling health_report() first to understand what will be removed, and lower the threshold incrementally.
Plugin Registration
Register custom implementations at runtime. All registration methods follow the same pattern: supply a string key (used in constructor arguments) and a class that extends the corresponding base class.
register_loader(extension: str, loader_class: type[BaseLoader]) -> None
register_embedder(key: str, embedder_class: type[BaseEmbedder]) -> None
register_generator(key: str, generator_class: type[BaseGenerator]) -> None
register_retriever(key: str, retriever_class: type[BaseRetriever]) -> None
register_vector_store(key: str, store_class: type[BaseVectorStore]) -> None
register_graph_store(key: str, store_class: type[BaseGraphStore]) -> None
| Method | Key format | Base class | Import from |
|---|---|---|---|
register_loader | File extension incl. dot, e.g. ".xyz" | BaseLoader | cognity_ai.loaders |
register_embedder | Snake-case string, e.g. "my_embedder" | BaseEmbedder | cognity_ai.embedders |
register_generator | Snake-case string | BaseGenerator | cognity_ai.generators |
register_retriever | Snake-case string | BaseRetriever | cognity_ai.retrievers |
register_vector_store | Snake-case string | BaseVectorStore | cognity_ai.stores |
register_graph_store | Snake-case string | BaseGraphStore | cognity_ai.stores |
"openai" for an embedder) will override the built-in implementation for that instance only. Other RAGLibrary instances are unaffected.
Full Usage Example
A complete walkthrough from configuration to querying and lifecycle management.
from cognity_ai import RAGLibrary
# --- Minimal setup: zero-config, auto-detects available backends ---
rag = RAGLibrary()
# --- Explicit configuration ---
rag = RAGLibrary(
rag_method="hybrid_graph",
embedder="openai",
vector_store="qdrant",
graph_store="neo4j",
llm="anthropic",
openai_api_key="sk-...",
anthropic_api_key="sk-ant-...",
neo4j_uri="bolt://localhost:7687",
neo4j_password="password",
)
# --- Ingest multiple formats ---
rag.ingest("report.pdf")
rag.ingest("data.xlsx")
rag.ingest("slides.pptx")
rag.ingest("photo.jpg") # OCR via multimodal LLM
count = rag.ingest_dir("./documents/") # Batch ingest, returns file count
# --- Build knowledge graph communities (run after bulk ingestion) ---
communities = rag.build_communities()
print(f"Built {communities} communities")
# --- Query ---
answer = rag.query("What are the key findings?")
result = rag.query_with_sources("Who is the CEO?", method="hybrid_graph")
print(result["answer"])
for src in result["sources"]:
print(src["doc_id"], src["source_path"])
# --- Per-query method override ---
summary = rag.query("Summarize all themes", method="microsoft_graphrag")
# --- Retrieval only (no LLM call) ---
results = rag.retrieve("quarterly revenue", top_k=5)
for r in results:
print(f"[{r.score:.3f}] {r.chunk.text[:120]}...")
# --- Lifecycle management ---
rag.confirm("report_2024") # Mark as authoritative
rag.deprecate("old_report_2022") # Exclude from retrieval
conflicts = rag.detect_conflicts("Apple Inc")
pruned = rag.prune(threshold=0.4)
print(f"Pruned {pruned} low-confidence items")
report = rag.health_report()
# {"entities": 1243, "relations": 892, "chunks": 3421, "communities": 47,
# "documents": 28, "deprecated": 2, "confirmed": 5}
# --- Custom plugin: register a loader for a proprietary format ---
from my_loaders import MyCustomLoader
rag.register_loader(".myformat", MyCustomLoader)
rag.ingest("data.myformat") # Now handled by MyCustomLoader
# --- Custom retriever registered and used immediately ---
from my_retrievers import HybridBM25Retriever
rag.register_retriever("bm25_hybrid", HybridBM25Retriever)
answer = rag.query("latest financials", method="bm25_hybrid")