tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Generative AI > Prompt Engineering > Automatic Prompt Optimization with DSPy

Automatic Prompt Optimization with DSPy

Author: Venkata Sudhakar

DSPy (Declarative Self-improving Python) replaces manual prompt engineering with automated optimization. Instead of hand-crafting the perfect prompt for ShopMax India's product category classifier, you define the task as a DSPy signature, provide a small labeled dataset, and let DSPy compile the optimal prompt automatically. The framework runs multiple prompt variants against your examples and selects the one with the highest accuracy.

The core DSPy workflow has three parts: a Signature that defines inputs and outputs, a Module (like ChainOfThought or Predict) that wraps the LLM call, and an Optimizer (like BootstrapFewShot) that searches for the best prompt. You also need a metric function that scores each prediction. DSPy then runs the optimizer against your training set and returns a compiled program with the tuned prompt baked in - ready for production.

The example below shows ShopMax India using DSPy to optimize a product category classifier. The signature takes a product title and returns a category. BootstrapFewShot finds the best few-shot examples automatically from the training set.


It gives the following output,

Product: Panasonic 43-inch Full HD LED TV
Category: TV

Product: Blue Star 1T Window AC 3 Star
Category: AC

Product: IFB 6kg Fully Automatic Top Load
Category: Washing Machine

At ShopMax India, run DSPy optimization on a labeled dataset of at least 50-100 examples per category for reliable results - small datasets produce unstable prompts. Save the compiled program with compiled.save('categorizer_v2.json') so you can reload it without re-running optimization. Re-optimize quarterly or whenever new product lines are added to the catalog. Compare compiled program accuracy against your hand-crafted baseline before switching to production - DSPy usually wins on precision but may surprise you on edge cases specific to your domain.


 
  


  
bl  br