DSPy


DSPy = Declarative Self-improving Python.

Components

1. language model — LLM that will answer our questions,

2. signature —a declaration of the program’s input and output (what task we want to solve),

 - 1. inline

 - 2. class

dspy.InputField()

List[Literal['', '', '']] = dspy.OutputField()

3. module — the prompting technique (how we want to solve the task).

 - Building blocks

 - different prompting strategies, 

  - 1. dspy.Predict

  - 2. dspy.ChainOfThought 

  - 3. dspy.ReAct (to add tools = function calling

4. Optimiser

  - 1. Automatic few-shot learning (e.g. BootstrapFewShot or BootstrapFewShotWithRandomSearch)

  - 2. Automatic instructions optimisation (e.g. MIPROv2) 

  - 3. Automatic fine-tuning (e.g, BootstrapFinetune) 

Other points

  • dspy.inspect_history for logs
  • Caching

# 1. updating config

dspy.configure_cache(enable_memory_cache=False, enable_disk_cache=False)

# 2. not using cache for specific module

math = dspy.Predict("question -> answer: float", cache = False)

  • dspy.configure(adapter=dspy.JSONAdapter()) 

  • DSPy is integrated with MLFlow (an observability tool)
Reference

Article
https://miptgirl.medium.com/programming-not-prompting-a-hands-on-guide-to-dspy-04ea2d966e6d
https://github.com/miptgirl/miptgirl_medium/blob/main/dspy_example/nps_topic_modelling.ipynb

https://www.dbreunig.com/2025/06/10/let-the-model-write-the-prompt.html

https://thedataquarry.com/blog/learning-dspy-1-the-power-of-good-abstractions/
https://thedataquarry.com/blog/learning-dspy-2-understanding-the-internals/
https://thedataquarry.com/blog/learning-dspy-3-working-with-optimizers/

https://thenewstack.io/goodbye-manual-prompting-hello-programming-with-dspy/

https://dzone.com/static/csrfAttackDetected.html

Paper
https://arxiv.org/abs/2310.03714

Website
https://dspy.ai/
https://dspy.ai/tutorials/games/

Course
https://www.deeplearning.ai/short-courses/dspy-build-optimize-agentic-apps/

Github
https://github.com/stanfordnlp/dspy

0 comments:

Post a Comment