Prompts2026年4月8日·1 分钟阅读

DSPy — Programming Foundation Models Declaratively

Replace hand-written prompts with modular programs. DSPy compiles declarative AI pipelines into optimized prompts automatically, boosting reliability and performance.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 64/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Prompt
安装
Single
信任
信任等级:Community
入口
DSPy — Programming Foundation Models Declaratively
先审查命令
npx -y tokrepo@latest install 023c142e-eba9-40ff-b598-4c0774814726 --target codex

先 dry-run,确认写入项后再运行此命令。

TL;DR
DSPy lets you define AI pipelines declaratively and compiles them into optimized prompts, replacing manual prompt engineering.
§01

What it is

DSPy is a framework for programming (not prompting) language models. Instead of tweaking prompt strings, you define what your AI pipeline should do declaratively using signatures and modules, then DSPy optimizes the prompts automatically through compilation. It treats LLM calls as optimizable modules, similar to how PyTorch treats neural network layers.

DSPy is created at Stanford NLP and targets AI engineers building reliable LLM pipelines who want systematic optimization over manual prompt engineering.

§02

How it saves time or tokens

Manual prompt engineering is trial and error. DSPy replaces this with automatic compilation that optimizes prompts based on evaluation metrics. The optimizer tries different prompt strategies and selects the one that scores highest on your test set. This produces better prompts in less time, and the resulting prompts are often more token-efficient because the optimizer removes unnecessary instructions.

§03

How to use

  1. Install DSPy:
pip install dspy
  1. Configure a language model:
import dspy

lm = dspy.LM('openai/gpt-4o-mini')
dspy.configure(lm=lm)
  1. Define a signature and module:
qa = dspy.ChainOfThought('question -> answer')
result = qa(question='What is the capital of France?')
print(result.answer)
§04

Example

Building a fact-checking pipeline with compilation:

import dspy

class FactCheck(dspy.Signature):
    claim = dspy.InputField(desc='A factual claim to verify')
    evidence = dspy.OutputField(desc='Supporting or refuting evidence')
    verdict = dspy.OutputField(desc='True, False, or Uncertain')

class FactChecker(dspy.Module):
    def __init__(self):
        self.check = dspy.ChainOfThought(FactCheck)

    def forward(self, claim):
        return self.check(claim=claim)

# Compile with training examples
from dspy.teleprompt import BootstrapFewShot

optimizer = BootstrapFewShot(metric=your_metric)
compiled_checker = optimizer.compile(FactChecker(), trainset=train_data)

# Use the optimized pipeline
result = compiled_checker(claim='The Earth is flat')
print(result.verdict)
§05

Related on TokRepo

§06

Common pitfalls

  • Compilation requires a training set with labeled examples; without good examples, the optimizer cannot improve over the default prompt
  • DSPy adds abstraction overhead; for simple one-shot tasks, direct prompting is simpler and equally effective
  • The optimizer runs multiple LLM calls during compilation, which can be expensive; start with a small training set and a cheaper model

常见问题

What models does DSPy support?+

DSPy works with OpenAI (GPT-4, GPT-4o), Anthropic Claude, Google Gemini, and local models via Ollama or vLLM. The dspy.LM class accepts model identifiers in the format provider/model-name. Any model with a chat or completion API is compatible.

What is a DSPy Signature?+

A Signature defines the input and output fields of an LLM call. It can be a simple string like 'question -> answer' or a class with typed InputField and OutputField attributes. Signatures describe what the module does without specifying how.

How does compilation work?+

Compilation runs an optimizer (like BootstrapFewShot or MIPRO) that tries different prompt strategies on your training data. It evaluates each strategy using your metric function and selects the one that scores highest. The result is an optimized module with better prompts.

Can I use DSPy with RAG pipelines?+

Yes. DSPy has built-in retrieval modules that integrate with search engines and vector stores. You can compose retrieval and generation modules into a single optimizable pipeline, and the compiler optimizes both the retrieval query and the generation prompt.

How is DSPy different from LangChain?+

LangChain provides building blocks for LLM applications (chains, tools, memory). DSPy focuses on automatic prompt optimization through compilation. They serve different needs: LangChain for application architecture, DSPy for systematic prompt engineering. They can be used together.

引用来源 (3)
  • DSPy GitHub— DSPy is a framework for programming foundation models
  • DSPy Paper— Created at Stanford NLP for systematic prompt optimization
  • DSPy Docs— Compilation optimizes prompts automatically
🙏

来源与感谢

stanfordnlp/dspy — 22k+ stars, MIT

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产