Files
landing_page/qdrant-landing/content/documentation/frameworks/dspy.md
T

2.9 KiB

title, short_description, description, aliases
title short_description description aliases
Stanford DSPy Use Qdrant as the retrieval model in Stanford DSPy programs to ground prompting, reasoning, and self-improving LLM pipelines in your data. Configure DSPy to use Qdrant as its retrieval model, grounding declarative LLM programs and RAG modules in fast vector search over your collections.
../integrations/dspy/

Stanford DSPy

DSPy is the framework for solving advanced tasks with language models (LMs) and retrieval models (RMs). It unifies techniques for prompting and fine-tuning LMs — and approaches for reasoning, self-improvement, and augmentation with retrieval and tools.

  • Provides composable and declarative modules for instructing LMs in a familiar Pythonic syntax.

  • Introduces an automatic compiler that teaches LMs how to conduct the declarative steps in your program.

Qdrant can be used as a retrieval mechanism in the DSPy flow.

Installation

For the Qdrant retrieval integration, include dspy-ai with the qdrant extra:

pip install dspy-ai dspy-qdrant fastembed

Usage

We can configure DSPy settings to use the Qdrant retriever model like so:

import os
import dspy
from dspy_qdrant import QdrantRM
from qdrant_client import QdrantClient

lm = dspy.LM("gpt-4o-mini", max_tokens=512,api_key=os.environ.get("OPENAI_API_KEY"))
client = QdrantClient(url=os.environ.get("QDRANT_CLOUD_URL"), api_key=os.environ.get("QDRANT_API_KEY"))
collection_name = "collection_name"
rm = QdrantRM(
    qdrant_collection_name=collection_name, 
    qdrant_client=client, 
    vector_name="dense",                 # <-- MATCHES your vector name
    document_field="passage_text",        # <-- MATCHES your payload field
    k=20)

dspy.settings.configure(lm=lm, rm=rm)

Using the retriever is pretty simple. The dspy.Retrieve(k) module will search for the top-k passages that match a given query.

retrieve = dspy.Retrieve(k=3)
question = "Some question about my data"
topK_passages = retrieve(question).passages

print(f"Top {retrieve.k} passages for question: {question} \n", "\n")

for idx, passage in enumerate(topK_passages):
    print(f"{idx+1}]", passage, "\n")

With Qdrant configured as the retriever for contexts, you can set up a DSPy module like so:

class RAG(dspy.Module):
    def __init__(self, num_passages=3):
        super().__init__()

        self.retrieve = dspy.Retrieve(k=num_passages)
        ...

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

With the generic RAG blueprint now in place, you can add the many interactions offered by DSPy with context retrieval powered by Qdrant.

Next steps