Agent Framework
Moss + DSPy: Sub-10ms Retrieval for Optimized LLM Programs
DSPy replaces hand-written prompts with compiled, optimized LLM programs. Moss provides MossRM, a retriever module that extends dspy.Retrieve to give DSPy programs sub-10ms semantic search. MossRM also works as a ReAct agent tool via the moss_search function, letting DSPy agents autonomously decide when to search.
Benefits
Why Use Moss with DSPy
MossRM extends dspy.Retrieve - use as a drop-in retriever in any DSPy program
Sub-10ms retrieval for DSPy RAG pipelines, ChainOfThought, and MultiHop programs
moss_search function for ReAct agent tool calling: agents decide when to search
Hybrid search via alpha parameter blends semantic and BM25 keyword matching
Works with DSPy optimizers like BootstrapFewShot and MIPRO for compiled retrieval programs
Integration
Quick Start
import dspy
from moss import MossClient, QueryOptions
# Configure Moss as DSPy's default retriever
class MossRM(dspy.Retrieve):
def __init__(self, moss_client, index_name, k=3, alpha=0.5):
super().__init__(k=k)
self.client = moss_client
self.index_name = index_name
self.alpha = alpha
def forward(self, query, k=None):
k = k or self.k
results = self.client.query_sync(
self.index_name, query,
QueryOptions(top_k=k, alpha=self.alpha)
)
return [dspy.Example(long_text=d.text) for d in results.docs]
client = MossClient("your-project-id", "your-project-key")
dspy.settings.configure(rm=MossRM(client, "knowledge-base"))
# Use in any DSPy program
retrieve = dspy.Retrieve(k=3)
results = retrieve("What is the refund policy?")Setup
Get Started in 3 Steps
Install dependencies
Run pip install moss dspy nest-asyncio python-dotenv to set up your environment.
Create a MossRM retriever
Initialize MossRM with your MossClient, index name, and search parameters. Set it as the default retriever with dspy.settings.configure(rm=...).
Use in DSPy programs
Call dspy.Retrieve(k=3) in your DSPy modules. MossRM handles the search. For agentic use, pass moss_search as a ReAct tool.
FAQ
Frequently asked questions
Explore