An introduction to DSPy - a framework for programming language models rather than prompting them.
This repository contains educational materials and tutorials for learning DSPy (Declarative Self-improving Python)[1], a framework developed at Stanford that allows developers to build modular AI systems by writing compositional Python code instead of brittle prompts[1].
DSPy enables you to iterate fast on building structured AI applications and offers algorithms for optimizing prompts and weights, whether you're building simple classifiers, sophisticated RAG pipelines, or complex Agent loops[1].
DSPy is a declarative framework for building modular AI software that allows you to iterate fast on structured code, rather than brittle strings[2]. Instead of manually crafting prompts, DSPy lets you focus on building AI pipelines by:
- Programming, not prompting: Write compositional Python code instead of fragile prompt strings[1]
- Modular design: Build complex systems using composable modules[3]
- Automatic optimization: Let DSPy optimize prompts and model weights for you[1]
- Structured outputs: Get reliable, structured responses from language models[4]
This repository includes three comprehensive Jupyter notebooks that progressively introduce DSPy concepts:
01_Intro to DSPy.ipynb- Introduction to DSPy fundamentals02_Custom Modules and Tool Calling.ipynb- Building custom modules and integrating tools03_Deploying with Databricks.ipynb- Production deployment strategies
- Predict: Basic predictor that handles key forms of learning[3]
- ChainOfThought: Teaches the LM to think step-by-step before responding[3]
- ProgramOfThought: Generates and executes Python code for complex reasoning[3]
- ReAct: Agent that can use tools to implement given signatures[3]
- Retrieve: Handles retrieval for RAG applications[3]
- Signatures: Define input/output interfaces for your AI modules[4]
- Custom Modules: Build specialized components for your use case
- Tool Integration: Connect external APIs and services
- Production Deployment: Scale your DSPy applications
- Python 3.7+
- Jupyter Notebook or JupyterLab
- Basic understanding of Python programming
pip install dspyTo install the latest development version:
pip install git+https://github.com/stanfordnlp/dspy.gitimport dspy
# Configure your language model
lm = dspy.LM('gpt-3.5-turbo')
dspy.configure(lm=lm)
# Define a signature
class BasicQA(dspy.Signature):
"""Answer questions with short factoid answers."""
question = dspy.InputField()
answer = dspy.OutputField(desc="often between 1 and 5 words")
# Create a predictor
generate_answer = dspy.ChainOfThought(BasicQA)
# Use it
question = "What is the color of the sky?"
pred = generate_answer(question=question)
print(pred.answer)Follow the notebooks in sequence:
- Start with Basics: Begin with
01_Intro to DSPy.ipynbto understand core concepts - Build Custom Solutions: Move to
02_Custom Modules and Tool Calling.ipynbfor advanced techniques - Deploy to Production: Complete with
03_Deploying with Databricks.ipynbfor real-world applications
DSPy is particularly effective for:
- Information Extraction: Extract structured data from unstructured text[5]
- Question Answering: Build intelligent Q&A systems[4]
- RAG Pipelines: Create retrieval-augmented generation systems[5]
- Agent Development: Build tool-using AI agents[5]
- Code Generation: Generate code from natural language descriptions[5]
- Official Documentation: dspy.ai[2]
- GitHub Repository: stanfordnlp/dspy[1]
- Community: Join the DSPy Discord for support and discussions[1]
This is an educational repository. If you find errors or have suggestions for improvements, please open an issue or submit a pull request.
Please refer to the original DSPy project for licensing information.