Use CortexDB as a retriever and persistent memory in DSPy programs.

DSPy Integration

DSPy programs are typed signatures over LM calls. Wrap CortexDB as a dspy.Retrieve-compatible module so recall results plug straight into your modules.

Install

pip install cortexdbai dspy-ai

Pattern

import os
import dspy
from cortexdb.v1 import V1Client

client = V1Client(api_url="https://api-v1.cortexdb.ai", actor="user:alice",
                  bearer=os.environ["CORTEX_TOKEN"])
SCOPE = "org:acme/user:alice"


class CortexRetriever(dspy.Retrieve):
    def __init__(self, k: int = 8):
        super().__init__(k=k)

    def forward(self, query: str, k: int | None = None) -> dspy.Prediction:
        pack = client.recall(scope=SCOPE, view="holistic", query=query,
                             include=["facts", "episodes"],
                             budgets={"max_tokens": 4000})
        passages = []
        for fact in pack["layers"].get("facts", [])[: k or self.k]:
            passages.append(f"{fact['subject']['name']} {fact['predicate']} {fact['object']['value']}")
        return dspy.Prediction(passages=passages)


dspy.settings.configure(rm=CortexRetriever())

Use as any other DSPy retriever inside ChainOfThought, ReAct, etc.

See also