ai-kickoff
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseStart 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 # DependenciesStep 1: Gather requirements
步骤1:收集需求
Ask the user:
- What should the AI do? (sort content, answer questions, extract data, take actions, or describe it)
- What goes in and what comes out? (e.g., "customer email in, category out" or "question in, answer out")
- Do you have example data? (if yes, what format — CSV, JSON, database?)
- Which AI provider? (default: OpenAI — DSPy works with any provider)
询问用户:
- AI的功能是什么?(内容分类、问答、数据提取、执行操作,或其他描述)
- 输入和输出分别是什么?(例如:"输入客户邮件,输出分类结果"或"输入问题,输出答案")
- 是否有示例数据?(如果有,格式是什么——CSV、JSON、数据库?)
- 选择哪个AI提供商?(默认:OpenAI —— DSPy兼容所有提供商)
Step 2: Generate the project
步骤2:生成项目
requirements.txt
requirements.txtrequirements.txt
requirements.txtdspy>=2.5Add if loading from HuggingFace. Add provider-specific packages if needed.
datasetsdspy>=2.5如果从HuggingFace加载数据,需添加包。如有需要,添加对应AI提供商的专属包。
datasetsdata.py
data.pydata.py
data.pyCreate 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.pyprogram.py
program.pyCreate the DSPy module. Choose the right module based on the task:
- Simple input/output:
dspy.Predict - Needs reasoning: (most tasks)
dspy.ChainOfThought - 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.pymetrics.py
metrics.pypython
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_fieldpython
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_fieldevaluate.py
evaluate.pyevaluate.py
evaluate.pypython
import dspy
from dspy.evaluate import Evaluate
from program import MyProgram
from metrics import metric
from data import load_datapython
import dspy
from dspy.evaluate import Evaluate
from program import MyProgram
from metrics import metric
from data import load_dataConfigure 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}")
undefinedprogram = MyProgram()
evaluator = Evaluate(devset=devset, metric=metric, num_threads=4, display_progress=True)
score = evaluator(program)
print(f"Score: {score}")
undefinedoptimize.py
optimize.pyoptimize.py
optimize.pypython
import dspy
from program import MyProgram
from metrics import metric
from data import load_datapython
import dspy
from program import MyProgram
from metrics import metric
from data import load_dataConfigure 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")
undefinedoptimized.save("optimized.json")
undefinedmain.py
main.pymain.py
main.pypython
import dspy
from program import MyProgrampython
import dspy
from program import MyProgramConfigure 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)
undefinedresult = program(input_field="test input")
print(result)
undefinedStep 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.exampleserver.py
server.pyserver.py
server.pypython
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 / fields to match the user's inputs/outputs.
QueryRequestQueryResponsepython
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"}根据用户的输入/输出适配/的字段。
QueryRequestQueryResponseUpdated requirements.txt
requirements.txt更新后的requirements.txt
requirements.txtdspy>=2.5
fastapi>=0.100
uvicorn[standard]
pydantic-settings>=2.0dspy>=2.5
fastapi>=0.100
uvicorn[standard]
pydantic-settings>=2.0.env.example
.env.example.env.example
.env.exampleAI_MODEL_NAME=openai/gpt-4o-mini
AI_API_KEY=your-api-key-hereAI_MODEL_NAME=openai/gpt-4o-mini
AI_API_KEY=your-api-key-hereStep 3: Explain next steps
步骤3:说明后续步骤
After generating the project, tell the user:
-
Fill inwith real training data (20+ examples). Don't have real data yet? Use
data.pyto generate synthetic training examples./ai-generating-data -
Runto see how well the AI works now
evaluate.py -
Runto automatically improve quality
optimize.py -
Runto use the AI
main.py -
Serve as API? Useto put your AI behind FastAPI endpoints
/ai-serving-apis
Next: to measure and improve your AI's quality.
/ai-improving-accuracy生成项目后,告知用户:
-
在中填入真实训练数据(20条以上)。还没有真实数据?使用
data.py生成合成训练样本。/ai-generating-data -
**运行**查看当前AI的性能
evaluate.py -
**运行**自动提升AI性能
optimize.py -
**运行**使用AI功能
main.py -
**需要作为API服务?**使用将AI部署在FastAPI端点之后。
/ai-serving-apis
后续:使用衡量并提升AI的准确率。
/ai-improving-accuracy