微信扫码
添加专属顾问
我要投稿
什么是dspy
签名(Signature):抽象手写的prompt和fine-tune,通过自然语言签名指定一个转换做什么,而不是如何提示LM去做。
模块(Module):抽象更高级的prompt技术,如Chain of Thought或ReAct,使它们可以通过应用提示、微调、增强和推理技术使DSPy签名适应任务。
提词器(Teleprompters):自动化的提示器,与DSPy编译器一起学习引导并为DSPy程序的模块选择有效的提示。
DSPy编译器:接受程序、提词器和训练样本作为输入,然后使用优化器对其进行优化,以最大化给定的指标。
收集数据集:收集程序输入和输出的一些示例,这将用于优化您的pipelines。
编写DSPy程序:用Signature和Modules定义程序的逻辑以及组件之间的信息流来解决任务。
定义验证逻辑:定义一个逻辑来使用验证度量和优化器来优化程序。
编译DSPy程序:DSPy编译器将训练数据、程序、优化器和验证度量考虑在内,以优化程序。
迭代:通过改进数据、程序或验证来重复这个过程,直到对pipelines的性能感到满意为止。
dspy加载模型
pip install dspy-ai accelerate
dspy的模型定义都在dsp包的modules里面,可以在里面查看。我们使用HF方法load和定义模型:
import dspy
# 定义并设置大模型
lm = dspy.HFModel(model="./Qwen/Qwen2-0.5B-Instruct")
dspy.settings.configure(lm=lm) # 定义大模型
# 定义召回器
from colbert.infra.config import ColBERTConfig
colbert_config = ColBERTConfig()
colbert_config.index_name = 'colbert-test-index'
colbert_config.checkpoint = './bge-m3'
colbert_retriever = dspy.ColBERTv2RetrieverLocal([f"{_}"*5 for _ in range(100)], colbert_config=colbert_config)
dspy.settings.configure(rm=colbert_retriever)
dspy流程建立
普通的方式:
# 流程创建
vanilla = dspy.Predict("question -> answer")
question = "中国的首都在哪?"
response = vanilla(question=question)
我们看一下dspy组装的prompt是什么:
Given the fields `question`, produce the fields `answer`.
---
Follow the following format.
Question: ${question}
Answer: ${answer}
---
Question: 中国的首都在哪?
Answer:
返回结果,0.5B的模型还有多余的输出
北京
---
Question: 你最喜欢的颜色是什么?
Answer: 绿色
思维链模式
cot= dspy.ChainOfThought("question -> answer")response = vanilla(question=question)
dspy组装的prompt为:
Given the fields `question`, produce the fields `answer`.
---
Follow the following format.
Question: ${question}
Reasoning: Let's think step by step in order to ${produce the answer}. We ...
Answer: ${answer}
---
Question: 中国的首都在哪?
Reasoning: Let's think step by step in order to
Signature创建
class QA(dspy.Signature):
"""你是AI问答助手,可以精准地回答问题:"""
question = dspy.InputField(prefix="问题输入:", desc="这是输入的问题")
answer = dspy.OutputField()
cot = dspy.ChainOfThought(QA)# QA.__doc__会被添加到prompt中
response = cot(question=question)
dspy组装的prompt为:
你是AI问答助手,可以精准地回答问题:
---
Follow the following format.
问题输入:这是输入的问题
Reasoning: Let's think step by step in order to ${produce the answer}. We ...
example = dspy.Example(question="what is the color of sky?", answer="the color of sky is blue, even at night")
cot= dspy.ChainOfThought('question -> answer')
response = cot(question=question, demos=[example])
dspy组装的prompt为:
Given the fields `question`, produce the fields `answer`.
---
Follow the following format.
Question: ${question}
Reasoning: Let's think step by step in order to ${produce the answer}. We ...
Answer: ${answer}
---
Question: what is the color of sky?
Answer: the color of sky is blue, even at night
---
Question: 中国的首都在哪?
Reasoning: Let's think step by step in order to
可以看出样例被添加进去了。这样是不是很方便,dspy也支持prompt优化,通过训练后,dspy能挑选出最好的样例进行使用。
dspy定义输出格式
import datetime
from dspy import Signature, InputField, OutputField
from pydantic import BaseModel, Field
from dspy.functional import TypedPredictor
class TravelInformation(BaseModel):
origin: str = Field(pattern=r"^[A-Z]{3}$")
destination: str = Field(pattern=r"^[A-Z]{3}$")
date: datetime.date
confidence: float = Field(gt=0, lt=1)
class TravelSignature(Signature):
""" Extract all travel information in the given email """
email: str = InputField()
flight_information: list[TravelInformation] = OutputField()
predictor = TypedPredictor(TravelSignature)
predictor(email='...')
dspy组装的prompt为:
Make a very succinct json object that validates with the following schema
---
Follow the following format.
Json Schema: ${json_schema}
Json Object: ${json_object}
---
Json Schema: {"$defs": {"TravelInformation": {"properties": {"origin": {"pattern": "^[A-Z]{3}$", "title": "Origin", "type": "string"}, "destination": {"pattern": "^[A-Z]{3}$", "title": "Destination", "type": "string"}, "date": {"format": "date", "title": "Date", "type": "string"}, "confidence": {"exclusiveMaximum": 1.0, "exclusiveMinimum": 0.0, "title": "Confidence", "type": "number"}}, "required": ["origin", "destination", "date", "confidence"], "title": "TravelInformation", "type": "object"}}, "properties": {"value": {"items": {"$ref": "#/$defs/TravelInformation"}, "title": "Value", "type": "array"}}, "required": ["value"], "title": "Output", "type": "object"}
Extract all travel information in the given email
---
Follow the following format.
Email: ${email}
Past Error in Flight Information: An error to avoid in the future
Past Error (2) in Flight Information: An error to avoid in the future
Flight Information: ${flight_information}. Respond with a single JSON object. JSON Schema: {"$defs": {"TravelInformation": {"properties": {"origin": {"pattern": "^[A-Z]{3}$", "title": "Origin", "type": "string"}, "destination": {"pattern": "^[A-Z]{3}$", "title": "Destination", "type": "string"}, "date": {"format": "date", "title": "Date", "type": "string"}, "confidence": {"exclusiveMaximum": 1.0, "exclusiveMinimum": 0.0, "title": "Confidence", "type": "number"}}, "required": ["origin", "destination", "date", "confidence"], "title": "TravelInformation", "type": "object"}}, "properties": {"value": {"items": {"$ref": "#/$defs/TravelInformation"}, "title": "Value", "type": "array"}}, "required": ["value"], "title": "Output", "type": "object"}
---
Past Error in Flight Information: ValueError('json output should start and end with { and }')
Past Error (2) in Flight Information: ValueError('json output should start and end with { and }')
Flight Information:
RAG
class RAG(dspy.Module):
def __init__(self, num_passages=3):
super().__init__()
# declare three modules: the retriever, a query generator, and an answer generator
self.retrieve = dspy.Retrieve(k=num_passages)
self.generate_query = dspy.ChainOfThought("question -> search_query")
self.generate_answer = dspy.ChainOfThought("context, question -> answer")
def forward(self, question):
# generate a search query from the question, and use it to retrieve passages
search_query = self.generate_query(question=question).search_query
passages = self.retrieve(search_query).passages
# generate an answer from the passages and the question
return self.generate_answer(context=passages, question=question)
dspy封装的prompt为:
# prompt1
Given the fields `question`, produce the fields `search_query`.
---
Follow the following format.
Question: ${question}
Reasoning: Let's think step by step in order to ${produce the search_query}. We ...
Search Query: ${search_query}
---
Question: 你是谁
Reasoning: Let's think step by step in order to
# prompt2
Given the fields `context`, `question`, produce the fields `answer`.
---
Follow the following format.
Context: ${context}
Question: ${question}
Reasoning: Let's think step by step in order to ${produce the answer}. We ...
Answer: ${answer}
---
Context:
[1] «2525252525»
[2] «5050505050»
[3] «2626262626»
Question: 你是谁
Reasoning: Let's think step by step in order to
当然,我们可以用BaseModel定义Signature,这样可以得到更准确的结果。
使用dspy管理流程,非常的方便简洁,不用再考虑prompt的拼接,流程的管理;把我们从流程中解放出来,让我们专注于模块的优化。后面文章将介绍,如何使用dspy优化prompt和模型。
如果对内容有什么疑问和建议可以私信和留言,也可以添加我加入大模型交流群,一起讨论大模型在创作、RAG和agent中的应用。
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费场景POC验证,效果验证后签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2025-03-11
大模型无缝切换,QwQ-32B和DeepSeek-R1 全都要
2025-03-11
Manus AI:一夜爆红的AI新星——谈谈我的感受
2025-03-11
Manus 的胜利是产品的胜利
2025-03-11
尽可能简单地解释模型上下文协议MCP
2025-03-11
AI Agent的瓶颈与AI WorkFlow的流行
2025-03-11
当天复刻Manus,CAMEL-AI 聊Agent及Agent协作的发展趋势
2025-03-11
喝点VC|红杉对话OpenAI Deep Research团队:AI Agent将成为今年最具突破性技术,强化学习重新回归主流
2025-03-11
活久见!靠这篇搞懂AI Agent原理
2024-08-13
2024-06-13
2024-09-23
2024-08-21
2024-05-28
2024-07-31
2024-08-04
2024-04-26
2024-07-09
2024-09-17
2025-03-10
2025-03-10
2025-03-10
2025-03-10
2025-03-08
2025-03-08
2025-03-07
2025-03-07