Prompts2026年3月31日·1 分钟阅读

DSPy — Program LLMs Instead of Prompting

DSPy is a Python framework for programming language models instead of prompting them. 33.3K+ GitHub stars. Build modular AI systems — classifiers, RAG pipelines, agent loops — and let DSPy optimize pr

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 96/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Prompt
安装
Single
信任
信任等级:Established
入口
DSPy — Program LLMs Instead of Prompting
直接安装命令
npx -y tokrepo@latest install 88a2e60f-54d1-48cb-9173-23d56a4f3f20 --target codex

先 dry-run 确认安装计划,再运行此命令。

TL;DR
DSPy lets you write modular AI programs and auto-optimizes the prompts behind them.
§01

What it is

DSPy is a Python framework that replaces manual prompt engineering with programmatic modules. Instead of writing fragile prompt strings, you define typed signatures (input/output fields), compose modules into pipelines, and let DSPy optimize the underlying prompts or fine-tuning weights automatically against a metric you specify.

DSPy targets ML engineers and AI application developers building classifiers, RAG pipelines, agent loops, and multi-step reasoning systems. It decouples your application logic from the specific prompt text, making systems portable across models and more robust to model updates.

§02

Why it saves time or tokens

Manual prompt engineering is trial-and-error. DSPy's optimizers (formerly 'teleprompters') search over prompt variations, few-shot example selections, and even fine-tuning data to find the configuration that maximizes your metric. This automated search replaces hours of manual tweaking. Because the optimizer finds efficient prompts, the final system often uses fewer tokens per call than hand-written prompts that include unnecessary instructions.

§03

How to use

  1. Install DSPy: pip install dspy
  2. Define a signature: class QA(dspy.Signature): question = dspy.InputField(); answer = dspy.OutputField()
  3. Create a module, compile it with an optimizer, and run inference
§04

Example

import dspy

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

class Summarize(dspy.Signature):
    '''Summarize the document in one paragraph.'''
    document = dspy.InputField()
    summary = dspy.OutputField()

summarizer = dspy.ChainOfThought(Summarize)
result = summarizer(document='DSPy is a framework for...')
print(result.summary)

This defines a typed summarization module with chain-of-thought reasoning. DSPy generates the actual prompt from the signature and docstring.

ConceptPurpose
SignatureTyped input/output contract
ModuleComposable building block (ChainOfThought, ReAct)
OptimizerAuto-tunes prompts against a metric
MetricEvaluation function you define
§05

Related on TokRepo

§06

Common pitfalls

  • DSPy optimizers need labeled examples or a reliable metric function; without good evaluation data, optimization produces inconsistent results
  • The abstraction hides prompt text, which makes debugging harder; use dspy.inspect_history() to see what prompts DSPy actually sends
  • Compiling with a large optimizer on an expensive model can burn significant API credits; start with a small training set and a cheap model

常见问题

How does DSPy differ from LangChain?+

LangChain provides building blocks (chains, tools, retrievers) that you wire together with manually written prompts. DSPy replaces the manual prompts with typed signatures and auto-optimizes them. DSPy focuses on making prompts a compiled artifact rather than a hand-written string, while LangChain focuses on the orchestration and integration layer.

What optimizers does DSPy provide?+

DSPy includes BootstrapFewShot (selects few-shot examples), BootstrapFewShotWithRandomSearch (adds random search), MIPRO (multi-instruction proposal optimizer), and others. Each optimizer searches over different prompt configurations to maximize your specified metric. You can also write custom optimizers.

Can DSPy work with local models?+

Yes. DSPy supports any model accessible through a supported LM class, including local models via Ollama, vLLM, or Hugging Face Transformers. You configure the model endpoint and DSPy handles prompt formatting. Local models benefit from DSPy optimization just as cloud models do.

What is a DSPy signature?+

A signature is a typed contract defining the inputs and outputs of an AI module. It replaces the prompt template concept. You declare input fields and output fields as Python class attributes with optional descriptions. DSPy compiles these declarations into actual prompts during optimization.

Is DSPy production-ready?+

DSPy is used in production by multiple organizations for classification, RAG, and agent workflows. The compiled modules are deterministic given the same optimizer output. However, the API surface is still evolving, so pin your DSPy version and test thoroughly when upgrading.

引用来源 (3)
  • DSPy GitHub— DSPy is a framework for programming language models
  • DSPy Docs— DSPy replaces prompt engineering with typed signatures and optimizers
  • Stanford NLP— DSPy originated from Stanford NLP research
🙏

来源与感谢

Created by Stanford NLP. Licensed under MIT. stanfordnlp/dspy — 33,300+ GitHub stars

讨论

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

相关资产