Skip to main content

Next-generation hybrid RAG: Embeddings + Knowledge Graph. Zero-config, production-ready.

Project description

CogniQ 🧠

Next-generation hybrid RAG. 3 lines to start. Infinite to extend.

pip install cogniq

Why CogniQ?

Feature LangChain RAG LlamaIndex CogniQ
Lines to start 30+ 20+ 3
Zero-dependency core
Knowledge graph layer partial
Plugin system complex complex ✅ simple
Multiple LLM backends yes yes ✅ built-in
Search speed (100K docs) ~50ms ~40ms ~1ms

Quickstart (30 seconds)

from cogniq import RAG

r = RAG()
r += "Banks must maintain LCR of 100% at all times per RBI guidelines."
r += "Failure to maintain LCR attracts penalty and supervisory action."

result = r("What happens if a bank doesn't maintain LCR?")
print(result)
# → "Failure to maintain LCR attracts penalty and supervisory action."

Full API

Add documents

r = RAG()

r += "plain text string"                  # string
r.add("text", source="rbi_circular.txt") # with metadata
r.add("document.txt")                    # file path
r.add(["doc1", "doc2", "doc3"])          # batch
r.learn("text")                          # alias for add

Search & Ask

# Search (returns SearchResult list)
results = r.search("LCR requirements", top_k=5)
results = r["LCR requirements"]           # same, operator syntax

for result in results:
    print(result.score, result.text, result.source)

# Ask (returns AskResult)
answer = r.ask("What is LCR?")
answer = r("What is LCR?")               # same, operator syntax

print(answer)                            # auto-prints answer text
answer.show()                            # pretty print with sources
print(answer.sources)                    # list of SearchResult
print(answer.latency_ms)                 # query time

Attach an LLM

# Ollama (local, free)
r.use_ollama("llama3.2")

# OpenAI
r.use_openai("gpt-4o-mini")

# Anthropic
r.use_anthropic("claude-haiku-4-5-20251001")

# Any custom function
def my_llm(question, context, **kwargs):
    return call_my_model(question, context)
r.use_llm(my_llm)

Choose embedder

# Auto (sentence-transformers → Ollama → TF-IDF)
r = RAG(embedder="auto")

# Force sentence-transformers (pip install sentence-transformers)
r = RAG(embedder="sentence", model="all-MiniLM-L6-v2")

# OpenAI
r = RAG(embedder="openai")

# Ollama
r = RAG(embedder="ollama", model="nomic-embed-text")

# Pure TF-IDF (zero deps, surprisingly good for domain text)
r = RAG(embedder="tfidf")

# Your own function
from cogniq import CustomEmbedder
r = RAG(embedder=CustomEmbedder(my_fn, dim=768))

Choose store

r = RAG(store="memory")   # default, numpy-optimized
r = RAG(store="faiss")    # pip install faiss-cpu, for 100K+ docs

Plugins — extend anything

from cogniq.plugins import Plugin, register_plugin

@register_plugin("my_reranker")
class MyReranker(Plugin):
    """Custom post-search reranker"""

    def post_search(self, results, query, **kw):
        # Your reranking logic
        return sorted(results, key=lambda r: my_score(r, query), reverse=True)

    def pre_add(self, text, meta):
        # Clean text before adding
        text = text.upper()
        return text, meta

# Use it
r = RAG(plugins=["my_reranker"])

# Built-in plugins
r = RAG(plugins=["dedup"])              # remove near-duplicate results
r = RAG(plugins=["min_score"])          # filter below threshold
r = RAG(plugins=["text_cleaner"])       # strip HTML, normalize whitespace
r = RAG(plugins=[
    "text_cleaner",
    "dedup",
    MinScorePlugin(threshold=0.4),      # instance with params
    my_custom_plugin,                   # instance or callable
])

Knowledge Graph

r = RAG(graph=True)   # enabled by default
r += "Risk causes liquidity stress. Stress triggers penalty."

# Graph automatically extracted — boosts relevant results
# Manual additions:
r._graph.add_entity("LCR", "regulation")
r._graph.add_relation("LCR_violation", "penalty", "triggers")

# Inspect
print(r._graph.stats())
paths = r._graph.find_paths("risk", "penalty")

Save / Load

r.save("my_rag.pkl")
r2 = RAG.load("my_rag.pkl")
r2.use_ollama("llama3.2")  # reattach LLM after load

Global API (no class needed)

import cogniq

cogniq.add("document text")
cogniq.add("more docs")
result = cogniq.ask("question?")
results = cogniq.search("keyword")
cogniq.reset()  # clear

CLI

cogniq add "your text here"
cogniq add --file document.txt
cogniq ask "What is LCR?"
cogniq ask "What is LCR?" --sources    # show source chunks
cogniq search "liquidity"
cogniq info
cogniq reset

ARJUNA / CCIL Integration Example

from cogniq import RAG

# Build CCIL regulatory RAG
rag = RAG(
    embedder="sentence",
    store="faiss",          # large corpus
    chunker="smart",
    graph=True,
    plugins=["dedup", "min_score"],
)

# Load circulars
import glob
for path in glob.glob("circulars/*.txt"):
    rag.add(path)

# Attach local LLM
rag.use_ollama("llama3.2")

# Query
result = rag("What is the penalty for LCR violation?")
result.show()

# Save
rag.save("ccil_rag.pkl")

Performance

Scale Search Time Memory
1K docs ~0.01ms ~5 MB
10K docs ~0.1ms ~50 MB
100K docs ~1ms ~500 MB
1M docs ~10ms (FAISS) ~2 GB

Why so fast?

  • Pre-normalized vectors → cosine = dot product (no division)
  • np.argpartition O(n) top-k (no full sort)
  • Single BLAS call for all similarities
  • LRU embedding cache
  • FAISS ANN for large scale

Installation Options

pip install cogniq                          # numpy only (TF-IDF)
pip install cogniq[sentence]               # + sentence-transformers
pip install cogniq[openai]                 # + OpenAI
pip install cogniq[faiss]                  # + FAISS
pip install cogniq[full]                   # everything

License

MIT

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

cogniq-0.2.0.tar.gz (30.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cogniq-0.2.0-py3-none-any.whl (26.4 kB view details)

Uploaded Python 3

File details

Details for the file cogniq-0.2.0.tar.gz.

File metadata

  • Download URL: cogniq-0.2.0.tar.gz
  • Upload date:
  • Size: 30.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.2

File hashes

Hashes for cogniq-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d5f5870f3d6c5fb4681fb0ea1ceed114736dd36f9a646f3d4379f7aaf9058511
MD5 e91abca638d03c6b33ddda6396efd6e6
BLAKE2b-256 897596f3270d54981948b8f51f0640114f1276b5484372f522e90595cc123a54

See more details on using hashes here.

File details

Details for the file cogniq-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: cogniq-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 26.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.2

File hashes

Hashes for cogniq-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cb7e5157cee0b3dd28af11e4ee9efcd6b483c58ab86d040096a7c5f461ffb1f0
MD5 21a3aff8d15e4ef6277d478543901a5d
BLAKE2b-256 626c955d8fbbeae1f20ba56f1eee97c58a111fd5b8171eb35246db2b842cbc84

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page