Skip to main content

dspy-moss

Moss semantic search retrieval module for DSPy.

Provides MossRM — a dspy.Retrieve subclass that plugs into DSPy's RM interface for sub-10ms knowledge retrieval with no external embedder.

Installation

pip install dspy-moss
# or
uv add dspy-moss

Quickstart

import dspy
from dspy_moss import MossRM

# Reads MOSS_PROJECT_ID and MOSS_PROJECT_KEY from environment
rm = MossRM(index_name="my-index")
rm.load_index()  # load into this process's memory before querying
dspy.configure(lm=dspy.LM("openai/gpt-4o"), rm=rm)

retrieve = dspy.Retrieve(k=3)
result = retrieve("What is the refund policy?")
for passage in result.passages:
    print(f"[{passage['score']:.3f}] {passage['long_text']}")

Usage patterns

As a configured retriever

Set MossRM as the default retriever for all dspy.Retrieve calls in your program:

import dspy
from dspy_moss import MossRM

rm = MossRM(
    index_name="support-kb",
    k=5,
    alpha=0.8,  # 1.0 = semantic only, 0.0 = keyword only
)
dspy.configure(lm=dspy.LM("openai/gpt-4o"), rm=rm)

# Any dspy.Retrieve() now uses Moss
class RAG(dspy.Module):
    def __init__(self):
        self.retrieve = dspy.Retrieve(k=3)
        self.generate = dspy.ChainOfThought("context, question -> answer")

    def forward(self, question):
        context = self.retrieve(question).passages
        return self.generate(context=context, question=question)

rag = RAG()
print(rag("How long do refunds take?").answer)

As a ReAct tool

MossRM.forward() is already sync, so pass the instance directly — no wrapper needed:

import dspy
from dspy_moss import MossRM

rm = MossRM(index_name="support-kb", k=5)
rm.load_index()
agent = dspy.ReAct(signature="question -> answer", tools=[rm])
print(agent(question="What payment methods do you accept?").answer)

With an explicit client

from moss import MossClient
from dspy_moss import MossRM

client = MossClient("proj-id", "proj-key")
rm = MossRM(index_name="my-index", moss_client=client, k=5)

Configuration

MossRM

Parameter Default Description
index_name (required) Name of the Moss index to query
moss_client None Existing MossClient. When omitted, one is created from credentials
project_id MOSS_PROJECT_ID env var Moss project ID
project_key MOSS_PROJECT_KEY env var Moss project key
k 3 Default number of passages per query
alpha 0.8 Search blend: 1.0 = semantic only, 0.0 = keyword only

Passage format

Each entry in result.passages is a dict with:

Key Type Description
long_text str Document text (DSPy's standard passage field)
id str Document ID
score float Relevance score
metadata dict Key-value metadata stored with the document

Mutable index helpers

MossRM also exposes two optional helpers for agents that write to the knowledge base:

# Read documents
objects = rm.get_objects(num_samples=10)

# Add / upsert documents
rm.insert([{"id": "doc-1", "text": "New fact.", "metadata": {"source": "agent"}}])

License

BSD 2-Clause — see LICENSE.

Support

Metadata

Release files for dspy-moss 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dspy-moss 0.0.1
File Size Uploaded
dspy_moss-0.0.1.tar.gz 7.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dspy-moss 0.0.1
File Interpreter ABI Platform
dspy_moss-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 14.3 kB

Release files / dspy_moss-0.0.1.tar.gz

Download URL dspy_moss-0.0.1.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ef121db1a49d7492f6a76b1f3cd1336fdd202e67f8acf01829afceef56f41b88
BLAKE2b-256 checksum
How to use checksums
7dbd6296384346d3a268fb842ba73940b47a18bff85922a0515526ac42ffb7d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Release files / dspy_moss-0.0.1-py3-none-any.whl

Download URL dspy_moss-0.0.1-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d7ab06fd9a745d97f787c9bd8b7de78f5cc3d2c20f744a206427645ce76055cf
BLAKE2b-256 checksum
How to use checksums
5ee55a0b165cba27d415ff800fe616b19882808464b656826ac16be38302374b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page