liveBook · Manning

DSPy

As explained in How Large Language Models Work

DSPy is a programming framework for composing language-model programs while separating model, vectorization, prompt, and task-logic definitions.

DSPy is a programming framework for building language-model applications without tying task logic to one model, database, vectorization method, or prompt design. It hides routine integration and prompting details, making components easier to replace and maintain. section 5.5.2 section 5.6

A DSPy program can separate:

  • The default language model, such as GPT-3.5 or a local model such as Llama. section 5.5.2
  • The searchable database and vectorized-document method, such as ColBERTv2 over Wikipedia abstracts. section 5.5.2
  • The task pipeline: retrieve relevant passages, then generate an answer from the question and passages. section 5.5.2
Listing 5.1 Simplest RAG in DSPy
import dspy

llm = dspy.OpenAI(model='gpt-3.5-turbo')  #1




similarity_and_database = dspy.ColBERTv2(   #2
  'wiki17_abstracts'
) 

dspy.settings.configure(  #3
    lm=llm, 
    rm=similarity_and_database
) 

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

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

    
        self.generate_answer = dspy.Prediction(  #5
            "question,relevant_documents -> answer"
        )

    
    def forward(self, question):  #6
        documents = self.retrieve(
            question
        ).passages
        
        return self.generate_answer(
            question=question, 
            relevant_documents=documents
        ).answer

The smallest RAG example configures a language model and retrieval database, retrieves three passages, and passes them to an answer-generation module. section 5.5.2

How How Large Language Models Work covers it

How deep it goes
Demonstrates configuration and a simple RAG pipeline but stops short of a full DSPy tutorial.
Examples
Uses GPT-3.5, Wikipedia, ColBERTv2, three retrieved passages, and a generated answer in a minimal RAG program.
What it assumes
Assumes familiarity with language models, databases, vectorized documents, and retrieval-augmented generation.
What you can do after
Build it from scratch by configuring replaceable model and retrieval components around a simple RAG task.
How it connects
Connects large language models, vectorization, prompting, databases, and retrieval-augmented generation.

Also explained in

Compare how 3 books explain DSPy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of How Large Language Models Work. The section links point to the parts of the book it draws on.