12.2 提示词优化与调优工具
12.2.1 工具生态概览
工具类型
代表工具
核心功能
12.2.2 提示词开发框架
LangChain
from langchain_core.prompts import (
ChatPromptTemplate,
FewShotPromptTemplate,
PromptTemplate,
)
# 基础模板
simple_template = PromptTemplate(
input_variables=["topic", "audience"],
template="为{audience}写一篇关于{topic}的科普文章,长度约 500 字。"
)
# 生成提示词
prompt = simple_template.format(topic="量子计算", audience="高中生")
# Chat 模板
chat_template = ChatPromptTemplate.from_messages([
("system", "你是一位专业的科普作家,擅长用通俗语言解释复杂概念。"),
("human", "请解释{topic}的基本原理。"),
])
# 少样本模板
examples = [
{"input": "什么是 AI", "output": "人工智能是让计算机模拟人类智能的技术..."},
{"input": "什么是区块链", "output": "区块链是一种分布式账本技术..."},
]
few_shot_template = FewShotPromptTemplate(
examples=examples,
example_prompt=PromptTemplate(
input_variables=["input", "output"],
template="问:{input}\n 答:{output}"
),
prefix="你是一位科技知识专家。以下是一些回答示例:",
suffix="问:{query}\n 答:",
input_variables=["query"],
)
print(prompt)
print(chat_template.format(topic="量子计算"))
print(few_shot_template.format(query="什么是量子纠缠?"))LlamaIndex
12.2.3 版本控制与监控工具
PromptLayer
Weights & Biases:W&B 平台
12.2.4 交互式调试工具
OpenAI Playground
Anthropic Console
12.2.5 自动化测试工具
Promptfoo
DSPy
12.2.6 优化工作流最佳实践
工作流详解
动手试试
最后更新于
