ai-kickoff

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Start a New AI Feature

启动全新AI功能

Create a project structure for building an AI-powered feature with DSPy:
$ARGUMENTS/
├── main.py          # Entry point — run your AI feature
├── program.py       # AI logic (DSPy module)
├── metrics.py       # How to measure if the AI is working
├── optimize.py      # Make the AI better automatically
├── evaluate.py      # Test the AI's quality
├── data.py          # Training/test data loading
└── requirements.txt # Dependencies
创建基于DSPy的AI驱动功能项目结构:
$ARGUMENTS/
├── main.py          # Entry point — run your AI feature
├── program.py       # AI logic (DSPy module)
├── metrics.py       # How to measure if the AI is working
├── optimize.py      # Make the AI better automatically
├── evaluate.py      # Test the AI's quality
├── data.py          # Training/test data loading
└── requirements.txt # Dependencies

Step 1: Gather requirements

步骤1:收集需求

Ask the user:
  1. What should the AI do? (sort content, answer questions, extract data, take actions, or describe it)
  2. What goes in and what comes out? (e.g., "customer email in, category out" or "question in, answer out")
  3. Do you have example data? (if yes, what format — CSV, JSON, database?)
  4. Which AI provider? (default: OpenAI — DSPy works with any provider)
询问用户:
  1. AI的功能是什么?(内容分类、问答、数据提取、执行操作,或其他描述)
  2. 输入和输出分别是什么?(例如:"输入客户邮件,输出分类结果"或"输入问题,输出答案")
  3. 是否有示例数据?(如果有,格式是什么——CSV、JSON、数据库?)
  4. 选择哪个AI提供商?(默认:OpenAI —— DSPy兼容所有提供商)

Step 2: Generate the project

步骤2:生成项目

requirements.txt

requirements.txt

dspy>=2.5
Add
datasets
if loading from HuggingFace. Add provider-specific packages if needed.
dspy>=2.5
如果从HuggingFace加载数据,需添加
datasets
包。如有需要,添加对应AI提供商的专属包。

data.py

data.py

Create dataset loading utilities:
python
import dspy

def load_data():
    """Load and prepare training/dev data.

    Returns:
        tuple: (trainset, devset) as lists of dspy.Example
    """
    # TODO: Replace with actual data loading
    examples = [
        dspy.Example(input_field="...", output_field="...").with_inputs("input_field"),
    ]

    split = int(0.8 * len(examples))
    return examples[:split], examples[split:]
Adapt field names to match the user's inputs/outputs.
创建数据集加载工具:
python
import dspy

def load_data():
    """Load and prepare training/dev data.

    Returns:
        tuple: (trainset, devset) as lists of dspy.Example
    """
    # TODO: Replace with actual data loading
    examples = [
        dspy.Example(input_field="...", output_field="...").with_inputs("input_field"),
    ]

    split = int(0.8 * len(examples))
    return examples[:split], examples[split:]
根据用户的输入/输出适配字段名称。

program.py

program.py

Create the DSPy module. Choose the right module based on the task:
  • Simple input/output:
    dspy.Predict
  • Needs reasoning:
    dspy.ChainOfThought
    (most tasks)
  • Math/computation:
    dspy.ProgramOfThought
  • Needs tools:
    dspy.ReAct
python
import dspy

class MySignature(dspy.Signature):
    """Describe the task here."""
    # Adapt fields to user's task
    input_field: str = dspy.InputField(desc="description")
    output_field: str = dspy.OutputField(desc="description")

class MyProgram(dspy.Module):
    def __init__(self):
        self.predict = dspy.ChainOfThought(MySignature)

    def forward(self, **kwargs):
        return self.predict(**kwargs)
创建DSPy模块。根据任务选择合适的模块:
  • 简单输入输出
    dspy.Predict
  • 需要推理
    dspy.ChainOfThought
    (适用于大多数任务)
  • 数学/计算类任务
    dspy.ProgramOfThought
  • 需要调用工具
    dspy.ReAct
python
import dspy

class MySignature(dspy.Signature):
    """Describe the task here."""
    # Adapt fields to user's task
    input_field: str = dspy.InputField(desc="description")
    output_field: str = dspy.OutputField(desc="description")

class MyProgram(dspy.Module):
    def __init__(self):
        self.predict = dspy.ChainOfThought(MySignature)

    def forward(self, **kwargs):
        return self.predict(**kwargs)

metrics.py

metrics.py

python
def metric(example, prediction, trace=None):
    """Score how good the AI's output is.

    Args:
        example: Expected output (ground truth)
        prediction: What the AI actually produced
        trace: Optional trace for optimization

    Returns:
        float: Score between 0 and 1
    """
    # TODO: Implement task-specific metric
    return prediction.output_field == example.output_field
python
def metric(example, prediction, trace=None):
    """Score how good the AI's output is.

    Args:
        example: Expected output (ground truth)
        prediction: What the AI actually produced
        trace: Optional trace for optimization

    Returns:
        float: Score between 0 and 1
    """
    # TODO: Implement task-specific metric
    return prediction.output_field == example.output_field

evaluate.py

evaluate.py

python
import dspy
from dspy.evaluate import Evaluate
from program import MyProgram
from metrics import metric
from data import load_data
python
import dspy
from dspy.evaluate import Evaluate
from program import MyProgram
from metrics import metric
from data import load_data

Configure AI provider

Configure AI provider

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

Load data

Load data

_, devset = load_data()
_, devset = load_data()

Test quality

Test quality

program = MyProgram() evaluator = Evaluate(devset=devset, metric=metric, num_threads=4, display_progress=True) score = evaluator(program) print(f"Score: {score}")
undefined
program = MyProgram() evaluator = Evaluate(devset=devset, metric=metric, num_threads=4, display_progress=True) score = evaluator(program) print(f"Score: {score}")
undefined

optimize.py

optimize.py

python
import dspy
from program import MyProgram
from metrics import metric
from data import load_data
python
import dspy
from program import MyProgram
from metrics import metric
from data import load_data

Configure AI provider

Configure AI provider

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

Load data

Load data

trainset, devset = load_data()
trainset, devset = load_data()

Automatically improve the AI's prompts

Automatically improve the AI's prompts

program = MyProgram() optimizer = dspy.BootstrapFewShot(metric=metric, max_bootstrapped_demos=4) optimized = optimizer.compile(program, trainset=trainset)
program = MyProgram() optimizer = dspy.BootstrapFewShot(metric=metric, max_bootstrapped_demos=4) optimized = optimizer.compile(program, trainset=trainset)

Check improvement

Check improvement

from dspy.evaluate import Evaluate evaluator = Evaluate(devset=devset, metric=metric, num_threads=4, display_progress=True) score = evaluator(optimized) print(f"Optimized score: {score}")
from dspy.evaluate import Evaluate evaluator = Evaluate(devset=devset, metric=metric, num_threads=4, display_progress=True) score = evaluator(optimized) print(f"Optimized score: {score}")

Save

Save

optimized.save("optimized.json")
undefined
optimized.save("optimized.json")
undefined

main.py

main.py

python
import dspy
from program import MyProgram
python
import dspy
from program import MyProgram

Configure AI provider

Configure AI provider

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

Load optimized version if available

Load optimized version if available

program = MyProgram() try: program.load("optimized.json") print("Loaded optimized program") except FileNotFoundError: print("Running unoptimized program")
program = MyProgram() try: program.load("optimized.json") print("Loaded optimized program") except FileNotFoundError: print("Running unoptimized program")

Run

Run

result = program(input_field="test input") print(result)
undefined
result = program(input_field="test input") print(result)
undefined

Step 2b: Add API serving (if the user wants a web API)

步骤2b:添加API服务(若用户需要Web API)

If the user wants to serve their AI as a web API, add these files to the project structure:
$ARGUMENTS/
├── main.py          # Entry point — run your AI feature
├── program.py       # AI logic (DSPy module)
├── server.py        # FastAPI app — routes and startup
├── models.py        # Pydantic request/response schemas
├── config.py        # Environment configuration
├── metrics.py       # How to measure if the AI is working
├── optimize.py      # Make the AI better automatically
├── evaluate.py      # Test the AI's quality
├── data.py          # Training/test data loading
├── requirements.txt # Dependencies
├── Dockerfile
└── .env.example
如果用户希望将AI作为Web API提供服务,在项目结构中添加以下文件:
$ARGUMENTS/
├── main.py          # Entry point — run your AI feature
├── program.py       # AI logic (DSPy module)
├── server.py        # FastAPI app — routes and startup
├── models.py        # Pydantic request/response schemas
├── config.py        # Environment configuration
├── metrics.py       # How to measure if the AI is working
├── optimize.py      # Make the AI better automatically
├── evaluate.py      # Test the AI's quality
├── data.py          # Training/test data loading
├── requirements.txt # Dependencies
├── Dockerfile
└── .env.example

server.py

server.py

python
from contextlib import asynccontextmanager
import dspy
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field

from program import MyProgram

@asynccontextmanager
async def lifespan(app: FastAPI):
    lm = dspy.LM("openai/gpt-4o-mini")
    dspy.configure(lm=lm)
    app.state.program = MyProgram()
    try:
        app.state.program.load("optimized.json")
    except FileNotFoundError:
        pass
    yield

app = FastAPI(title="My AI API", lifespan=lifespan)

class QueryRequest(BaseModel):
    input_field: str = Field(..., min_length=1)

class QueryResponse(BaseModel):
    output_field: str

@app.post("/query", response_model=QueryResponse)
async def query(request: QueryRequest):
    result = app.state.program(input_field=request.input_field)
    return QueryResponse(output_field=result.output_field)

@app.get("/health")
async def health():
    return {"status": "ok"}
Adapt
QueryRequest
/
QueryResponse
fields to match the user's inputs/outputs.
python
from contextlib import asynccontextmanager
import dspy
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field

from program import MyProgram

@asynccontextmanager
async def lifespan(app: FastAPI):
    lm = dspy.LM("openai/gpt-4o-mini")
    dspy.configure(lm=lm)
    app.state.program = MyProgram()
    try:
        app.state.program.load("optimized.json")
    except FileNotFoundError:
        pass
    yield

app = FastAPI(title="My AI API", lifespan=lifespan)

class QueryRequest(BaseModel):
    input_field: str = Field(..., min_length=1)

class QueryResponse(BaseModel):
    output_field: str

@app.post("/query", response_model=QueryResponse)
async def query(request: QueryRequest):
    result = app.state.program(input_field=request.input_field)
    return QueryResponse(output_field=result.output_field)

@app.get("/health")
async def health():
    return {"status": "ok"}
根据用户的输入/输出适配
QueryRequest
/
QueryResponse
的字段。

Updated
requirements.txt

更新后的
requirements.txt

dspy>=2.5
fastapi>=0.100
uvicorn[standard]
pydantic-settings>=2.0
dspy>=2.5
fastapi>=0.100
uvicorn[standard]
pydantic-settings>=2.0

.env.example

.env.example

AI_MODEL_NAME=openai/gpt-4o-mini
AI_API_KEY=your-api-key-here
AI_MODEL_NAME=openai/gpt-4o-mini
AI_API_KEY=your-api-key-here

Step 3: Explain next steps

步骤3:说明后续步骤

After generating the project, tell the user:
  1. Fill in
    data.py
    with real training data (20+ examples). Don't have real data yet? Use
    /ai-generating-data
    to generate synthetic training examples.
  2. Run
    evaluate.py
    to see how well the AI works now
  3. Run
    optimize.py
    to automatically improve quality
  4. Run
    main.py
    to use the AI
  5. Serve as API? Use
    /ai-serving-apis
    to put your AI behind FastAPI endpoints
Next:
/ai-improving-accuracy
to measure and improve your AI's quality.
生成项目后,告知用户:
  1. data.py
    中填入真实训练数据
    (20条以上)。还没有真实数据?使用
    /ai-generating-data
    生成合成训练样本。
  2. **运行
    evaluate.py
    **查看当前AI的性能
  3. **运行
    optimize.py
    **自动提升AI性能
  4. **运行
    main.py
    **使用AI功能
  5. **需要作为API服务?**使用
    /ai-serving-apis
    将AI部署在FastAPI端点之后。
后续:使用
/ai-improving-accuracy
衡量并提升AI的准确率。