DEV Community

Alex Spinov
Alex Spinov

Posted on

DSPy Has a Free API — Program LLMs Instead of Prompting

DSPy: Compiler for LLM Programs

DSPy by Stanford replaces prompt engineering with programming. Define signatures, DSPy optimizes prompts automatically.

Why DSPy

  • No manual prompt tweaking
  • Composable LLM pipelines
  • Automatic optimization from examples
  • Model-agnostic

The Free API

import dspy

lm = dspy.LM("openai/gpt-4o-mini")
dspy.configure(lm=lm)

class QA(dspy.Signature):
    question: str = dspy.InputField()
    answer: str = dspy.OutputField()

qa = dspy.ChainOfThought(QA)
result = qa(question="What causes pod eviction?")
print(result.answer)
Enter fullscreen mode Exit fullscreen mode

Optimization

from dspy.teleprompt import BootstrapFewShot

trainset = [dspy.Example(question="What is a pod?", answer="Smallest K8s unit").with_inputs("question")]
optimizer = BootstrapFewShot()
optimized = optimizer.compile(qa, trainset=trainset)
Enter fullscreen mode Exit fullscreen mode

Real-World Use Case

Team spent 3 weeks tuning prompts. DSPy: define signature + 20 examples. Optimizer found best prompts in 10 minutes. Accuracy up 15%.

Quick Start

pip install dspy
Enter fullscreen mode Exit fullscreen mode

Need AI pipelines? Check out my tools on Apify or email spinov001@gmail.com.

Top comments (0)