/ API Reference

RAGLibrary

The primary facade class for cognity-ai. Single entry-point for ingestion, querying, knowledge lifecycle management, and plugin registration.

Class Stable API cognity_ai.__init__

Constructor

All parameters are optional. Passing no arguments gives a zero-config instance that auto-detects available backends from installed packages and environment variables.

python
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 precedence When a 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_method
hybrid_graph naive vector_only graph_only parent_child multi_query microsoft_graphrag adaptive
chunker
sentence fixed semantic recursive parent_child hybrid
embedder
gemini vertex_ai openai azure_openai bedrock cohere sentence_transformers ollama
vector_store
chroma qdrant pinecone faiss weaviate milvus pgvector azure_search
graph_store
neo4j microsoft_graphrag memgraph arangodb networkx
llm
gemini vertex_ai openai azure_openai anthropic bedrock cohere ollama
extraction
hybrid nlp llm
ocr
gemini_vision openai_vision anthropic_vision azure_vision bedrock_vision tesseract
page_index
hybrid regex structural

Ingestion 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.

method ingest(path: str | Path) -> None

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.

ParameterTypeDescription
pathstr | PathFilesystem path to the file to ingest.
method ingest_dir(directory: str | Path, recursive: bool = True, glob: str = "*") -> int

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.

ParameterTypeDefaultDescription
directorystr | PathRoot directory to scan.
recursiveboolTrueWhen True, descend into subdirectories.
globstr"*"Glob pattern to filter filenames, e.g. "*.pdf" or "report_*.docx".
Returns int — number of files successfully ingested
method ingest_text(text: str, doc_id: str, metadata: dict | None = None) -> None

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.

ParameterTypeDescription
textstrThe raw text content to ingest.
doc_idstrA unique identifier for this document. Used for lifecycle operations (confirm, deprecate).
metadatadict | NoneOptional key-value metadata to attach (e.g. source URL, author, timestamp).
method build_communities() -> int

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.

Returns int — number of communities detected
💡
Batch ingestion pattern Call 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.

method query(question: str, top_k: int = 10, method: str | None = None) -> str

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.

ParameterTypeDefaultDescription
questionstrThe question to answer.
top_kint10Maximum number of chunks to retrieve and pass to the LLM context.
methodstr | NoneNonePer-query RAG method override. Accepts same values as the rag_method constructor argument.
Returns str — generated answer
method query_with_sources(question: str, top_k: int = 10, method: str | None = None) -> dict

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:

python
{
    "answer":  str,                 # Generated answer text
    "sources": list[dict],           # Source document metadata dicts
    "chunks":  list[SemanticChunk],  # Raw chunks passed to the LLM
}
Returns dict — answer, sources, chunks
method retrieve(query: str, top_k: int = 10, method: str | None = None) -> list[RetrievalResult]

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.

ParameterTypeDefaultDescription
querystrThe search query.
top_kint10Maximum number of results to return.
methodstr | NoneNoneRetrieval strategy override for this call.
Returns list[RetrievalResult] — ranked results, highest score first

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.

method confirm(doc_id: str) -> None

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.

method deprecate(doc_id: str) -> None

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.

method detect_conflicts(entity: str) -> list[dict]

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.

ParameterTypeDescription
entitystrThe entity name to check for conflicting assertions (e.g. "Apple Inc", "CEO").
Returns list[dict] — list of conflict records
method prune(threshold: float = 0.5) -> int

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.

ParameterTypeDefaultDescription
thresholdfloat0.5Confidence threshold in [0, 1]. Items below this value are permanently deleted.
Returns int — number of items pruned
method health_report() -> dict

Return a summary of the current knowledge base state. Useful for monitoring ingestion progress and graph quality.

python
{
    "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 is irreversible 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.

python
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_loaderFile extension incl. dot, e.g. ".xyz"BaseLoadercognity_ai.loaders
register_embedderSnake-case string, e.g. "my_embedder"BaseEmbeddercognity_ai.embedders
register_generatorSnake-case stringBaseGeneratorcognity_ai.generators
register_retrieverSnake-case stringBaseRetrievercognity_ai.retrievers
register_vector_storeSnake-case stringBaseVectorStorecognity_ai.stores
register_graph_storeSnake-case stringBaseGraphStorecognity_ai.stores
Registration replaces built-ins Registering a key that already exists (e.g. "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.

python
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")

See Also

Configuration
LibraryConfig and provider dataclasses
🔭
Retrievers
All RAG method implementations
📄
Loaders
File format loaders and BaseLoader
🗄
Stores
Vector and graph store backends
📚
API Index
All modules at a glance
🚀
Getting Started
Installation and first query