Skip to main content

Moss semantic search retrieval module for DSPy

Project description

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

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

dspy_moss-0.0.1.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

dspy_moss-0.0.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file dspy_moss-0.0.1.tar.gz.

File metadata

  • Download URL: dspy_moss-0.0.1.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for dspy_moss-0.0.1.tar.gz
Algorithm Hash digest
SHA256 ef121db1a49d7492f6a76b1f3cd1336fdd202e67f8acf01829afceef56f41b88
MD5 298d75c980c7bcf098f969055f8ea9c7
BLAKE2b-256 7dbd6296384346d3a268fb842ba73940b47a18bff85922a0515526ac42ffb7d7

See more details on using hashes here.

File details

Details for the file dspy_moss-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: dspy_moss-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for dspy_moss-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d7ab06fd9a745d97f787c9bd8b7de78f5cc3d2c20f744a206427645ce76055cf
MD5 25a5aa8c72772bf019b7f817dd266e8f
BLAKE2b-256 5ee55a0b165cba27d415ff800fe616b19882808464b656826ac16be38302374b

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