dspy-core

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

DSPy Core

DSPy 核心

Core guidance for working with the DSPy framework: signatures, modules, programs, compilation, and testing.
DSPy框架使用核心指南:签名、模块、程序、编译与测试。

Environment Setup

环境搭建

This skill requires
uv
as the Python package manager. Always ensure a
uv
virtual environment is active before running any DSPy code.
bash
undefined
本技能需要**
uv
**作为Python包管理器。在运行任何DSPy代码之前,请确保始终激活
uv
虚拟环境。
bash
undefined

Create and activate a uv virtual environment

创建并激活uv虚拟环境

uv venv source .venv/bin/activate
uv venv source .venv/bin/activate

Install dspy

安装dspy

uv pip install dspy

> **Rule**: Never use raw `pip` or `python -m pip`. Always use `uv pip` for package installation and `uv run` for script execution.
uv pip install dspy

> **规则**:永远不要使用原生`pip`或`python -m pip`。安装包请始终使用`uv pip`,执行脚本请使用`uv run`。

Quick Start

快速开始

Creating a new signature

创建新签名

python
import dspy

class MySignature(dspy.Signature):
    """Input and output fields with descriptions."""
    input_field = dspy.InputField(desc="Description of input")
    output_field = dspy.OutputField(desc="Description of output")
python
import dspy

class MySignature(dspy.Signature):
    """输入和输出字段及描述。"""
    input_field = dspy.InputField(desc="输入描述")
    output_field = dspy.OutputField(desc="输出描述")

Compiling a program

编译程序

python
undefined
python
undefined

Use the compile-dspy script for safe compilation

使用compile-dspy脚本进行安全编译

uv run scripts/compile-dspy.py --module my_module --teleprompter teleprompter_name
undefined
uv run scripts/compile-dspy.py --module my_module --teleprompter teleprompter_name
undefined

When to Use This Skill

适用场景

Use this skill when:
  • Creating or modifying DSPy signatures
  • Building new DSPy programs or modules
  • Running DSPy compilation
  • Testing DSPy signatures and programs
  • Learning DSPy fundamentals
在以下场景中使用本技能:
  • 创建或修改DSPy签名
  • 构建新的DSPy程序或模块
  • 执行DSPy编译
  • 测试DSPy签名和程序
  • 学习DSPy基础知识

Core Concepts

核心概念

Signatures

签名

DSPy signatures define the input/output contract for your programs. See signatures.md for:
  • Signature design patterns
  • InputField vs OutputField usage
  • Type hints and validation
  • Hint and description best practices
DSPy签名定义了程序的输入/输出契约。请查看signatures.md了解:
  • 签名设计模式
  • InputField与OutputField的使用
  • 类型提示与验证
  • 提示与描述的最佳实践

Programs & Modules

程序与模块

DSPy programs are composed of modules that process inputs. See programs.md for:
  • Building DSPy programs
  • Module composition
  • Compilation workflows
  • Chain-of-thought and other patterns
DSPy程序由处理输入的模块组成。请查看programs.md了解:
  • 构建DSPy程序
  • 模块组合
  • 编译工作流
  • 思维链及其他模式

Scripts

脚本

  • compile-dspy.py: Compile DSPy modules with proper caching
  • test-signature.py: Validate signature structure and types
  • clear-cache.py: Clear DSPy cache safely
  • compile-dspy.py:使用适当的缓存编译DSPy模块
  • test-signature.py:验证签名结构与类型
  • clear-cache.py:安全清除DSPy缓存

Templates

模板

The
assets/templates/
directory provides boilerplate:
  • signature-template.py: Starting point for new signatures
  • program-template.py: Starting point for new programs
assets/templates/
目录提供了模板代码:
  • signature-template.py:创建新签名的起始模板
  • program-template.py:创建新程序的起始模板

Critical Rules

重要规则

  1. Always use
    uv
    : Use
    uv venv
    for environment creation,
    uv pip
    for installs, and
    uv run
    for script execution
  2. Compilation is offline-only: Never compile at runtime in production
  3. Clear cache after changes: Run
    clear-cache.py
    after modifying DSPy modules
  1. 始终使用
    uv
    :使用
    uv venv
    创建环境,
    uv pip
    安装包,
    uv run
    执行脚本
  2. 编译仅支持离线操作:生产环境中切勿在运行时进行编译
  3. 修改后清除缓存:修改DSPy模块后,请运行
    clear-cache.py

Related Skills

相关技能

  • dspy-optimization: Optimization, metrics, evaluation, and teleprompters
  • dspy-fleet-rlm: fleet-rlm-specific DSPy patterns, debugging, and integration
  • dspy-optimization:优化、指标、评估与提示器(teleprompters)
  • dspy-fleet-rlm:fleet-rlm专属DSPy模式、调试与集成

Progressive Disclosure

渐进式披露

  1. SKILL.md (this file): Quick reference and navigation
  2. references/: Detailed technical docs loaded as needed
  3. scripts/: Executable tools (can be run without reading)
  4. assets/: Templates for new work
  1. SKILL.md(本文档):快速参考与导航
  2. references/:按需加载的详细技术文档
  3. scripts/:可直接运行的工具(无需阅读即可使用)
  4. assets/:用于新工作的模板