buildfastwithaibuildfastwithai
AI WorkshopsAll blogsPartner with usFDE Program
Unrot mascotGet yourselfupskilled in AIfor free.Get the appNext live workshopFree AI WorkshopLive session, recording includedReserve a seat

Newsletter

Stay ahead

AI tools and tips. No spam.

Share
Back to blogs
Tools
Tutorials

DSPy: Master AI Systems with a Comprehensive Guide

By Satvik Paramkusham
December 13, 2024
4 min read
Share:
DSPy: Master AI Systems with a Comprehensive Guide
Share:

DSPy (Data Science Prompting) is a framework designed to streamline the process of working with language models. It shifts the focus from "prompting" (manually crafting queries) to "programming" (building modular AI systems).

DSPy: Master AI Systems with a Comprehensive Guide illustration

It offers tools for:

  • Modular design.
  • Optimizing prompts and weights.
  • Creating systems like classifiers, retrieval-augmented generation (RAG) pipelines, and agent loops.

Setup and Installation

1.Install Required Libraries

pip install httpx==0.27.2 dspy faiss-cpu
  • httpx : A high-performance HTTP client for Python.
  • dspy : The core library for building AI systems with DSPy.
  • faiss-cpu :A library for efficient similarity search and clustering, often used in RAG task

2.Configure OpenAI API

import os
from google.colab import userdata
os.environ['OPENAI_API_KEY'] = userdata.get('OPENAI_API_KEY')
OPENAIKEY = os.getenv('OPENAI_API_KEY')
  • Sets up the OpenAI API key. This key is required to access GPT-based models.
  • userdata.get() fetches the key securely (useful in Colab environments).

3.Setting Up DSPy

import dspy
lm = dspy.OpenAI(model='gpt-4o', api_key=OPENAIKEY, model_type='chat', max_tokens=500)
dspy.settings.configure(lm=lm)
  • Initializes DSPy with OpenAI's GPT-4o model.
  • Configures DSPy to use this model for all subsequent tasks.

Loading the Dataset

  • Dataset: HotPotQA, a popular dataset for multi-hop question answering.
  • Code:
from dspy.datasets import HotPotQA

# Load the dataset.
dataset = HotPotQA(train_seed=1, train_size=20, eval_seed=2023, dev_size=50, test_size=0)

trainset = [x.with_inputs('question') for x in dataset.train]
devset = [x.with_inputs('question') for x in dataset.dev]
  • HotPotQA() : Loads a dataset with a specific configuration.
  • train_size=2 0 : Use 20 examples for training.
  • dev_size=50 : Use 50 examples for development (validation).
  • test_size=0 : No examples for testing in this case.
  • x.with_inputs('question') : Processes each example to focus on the "question" field.

Inspecting a Training Example

train_example = trainset[0]
print(train_example)
print(f"Question: {train_example.question}")
print(f"Answer: {train_example.answer}")
  • trainset[0] : Fetches the first training example.
  • Displays the question and the expected answer for inspection.

Defining the BasicQA Signature

class BasicQA(dspy.Signature):
    """Answer questions with short factoid answers."""

    question = dspy.InputField()
    answer = dspy.OutputField(desc="often between 1 and 5 words")

Signature: A blueprint for specifying inputs and outputs of a module.

Components:

  • question : Defines the input field.
  • answer : Defines the output field with a description.

 Creating the BasicQABot Module

class BasicQABot(dspy.Module):
    def __init__(self):
        super().__init__()

        self.generate = dspy.Predict(BasicQA)

    def forward(self, question):
        prediction = self.generate(question=question)
        return dspy.Prediction(answer=prediction.answer)
  • BasicQABot:
  • A simple question-answering bot.
  • __init__:
  • Initializes the BasicQABot with a prediction model (self.generate).
  • forward():
  • Takes a question as input.
  • Uses self.generate() to predict an answer.
  • Returns the predicted answer.

Querying the QA Bot

qa_bot = BasicQABot()
pred = qa_bot.forward("In the 10th Century A.D. Ealhswith had a son called Æthelweard by which English king?")
print(pred.answer)
  • Functionality:
  • Creates an instance of BasicQABot.
  • Queries it with a historical question.
  • Outputs the predicted answer.

Retrieval-Augmented Generation (RAG)

  • RAG: Combines retrieval systems (to fetch relevant context) with generative models (to generate responses).

DSPy: Master AI Systems with a Comprehensive Guide illustration

Configuring DSPy for RAG

import dspy
lm = dspy.LM('openai/gpt-4o-mini')
dspy.configure(lm=lm)
  • Configures DSPy to use a smaller version of GPT-4o.

Simple QA Chain

qa = dspy.Predict('question: str -> response: str')
response = qa(question="what are high memory and low memory on linux?")
print(response.response)
  • QA Chain:
  • Defines a simple chain that maps a question string to a response string.
  • Predicts the response using the QA chain.

Chain Of Thought Module

cot = dspy.ChainOfThought('question -> response')
cot(question="should curly braces appear on their own line?")
  • Chain of Thought (CoT):
  • Models multi-step reasoning processes.
  • Takes a question and outputs both reasoning and response.

Manipulating Examples in DSPy

data = [dspy.Example(**d).with_inputs('question') for d in data]
  • dspy.Example:
  • Converts raw JSON data into DSPy-compatible examples.
  • Focuses on the question field for processing.

Evaluation in DSPy

Semantic F1 Metric

from dspy.evaluate import SemanticF1
metric = SemanticF1(decompositional=True)
score = metric(example, pred)
  • Semantic F1:
  • Measures similarity between the predicted and gold-standard responses.
  • Useful for evaluating generated answers.

Using DSPy for Math Reasoning

  • Dataset: MATH (Mathematical reasoning benchmark).
  • Workflow: - Load the dataset
  • Use a CoT module for reasoning.
  • Evaluate predictions using DSPy utilities.

DSPy: Master AI Systems with a Comprehensive Guide illustration

DSPy Math Reasoning

Conclusion:- DSPy is a powerful framework for building modular AI systems, streamlining the process of programming with language models. From basic question-answering bots to advanced Retrieval-Augmented Generation (RAG) pipelines, DSPy offers tools and algorithms to optimize prompts, weights, and workflows efficiently. Its flexibility allows developers to iterate rapidly, evaluate models effectively, and enhance performance with integrated metrics like Semantic F1. Whether you're working on natural language processing, reasoning tasks, or AI-driven applications, DSPy simplifies complex implementations, empowering developers to unlock the full potential of AI.

LLM AGENTSRAG PIPELINESTOOL CALLINGDEPLOYMENT
Let's build

Start building AI agents with Build Fast

Explore ProgramJoin Waitlist

Resources

  • DSPy Github Repository
  • Build Fast With AI DSPy Github Repository
  • OpenAI API Documentation

--------------

Stay Updated:- Follow Build Fast with AI pages for all the latest AI updates and resources.

Experts predict 2025 will be the defining year for Gen AI implementation.Want to be ahead of the curve?

Join Build Fast with AI’s Gen AI Launch Pad 2025 - your accelerated path to mastering AI tools and building revolutionary applications.

👉 Limited Spots, join the waitlist now: www.buildfastwithai.com/genai-course 

Satvik Paramkusham
Written by

Satvik Paramkusham

Founder at Build Fast with AI. AI Engineer, educator, and researcher passionate about agentic AI workflows, LLM application architecture, and training the next generation of AI builders.

Share:

Partner with Build Fast with AI

Guest posts, sponsored articles, tool reviews and in-blog CTAs. See what we offer and get in touch.

Partner with us

Topics

  • Claude AI
  • Best AI Models
  • AI Coding Tools
  • AI Agent Frameworks
  • Agentic AI
  • Open-Source LLMs
  • RAG & Vector Databases
  • AI Industry News

Popular guides

  • 100 Best AI Photo Editing Prompts
  • 75 Best ChatGPT Images 2.5 Prompts
  • 100 Best AI Video Prompts
  • Best AI Models 2026, Tested
  • Claude AI Complete Guide
  • What Is MCP?

Build Fast with AI

  • Build Fast with AI
  • AI Workshops
  • Corporate Training
  • FDE Program
  • Partner with Us
  • Contact
© 2026 Intellify Edventures Private Limited. All rights reserved.
    You Might Also Like
    ChatGPT Images 2.6: Features, Pricing and Review
    Tutorials
    ChatGPT Images 2.6: Features, Pricing and Review

    ChatGPT Images 2.6 review covering Flare vs Sunburst, GIFs, SVG output, image editing, API pricing and practical prompts for creators.

    50 Best Diwali AI Photo Prompts 2026 for Gemini, ChatGPT & Nano Banana
    Tools
    50 Best Diwali AI Photo Prompts 2026 for Gemini, ChatGPT & Nano Banana

    Create stunning Diwali photos with 50 copy-paste AI prompts for Gemini, ChatGPT and Nano Banana. Try realistic portraits, couple photos, saree looks, diyas, rangoli and festive posters.