prompt自主生成框架 - DSPy

DSPy是斯坦福大学搞的一个prompt自动生成框架,核心理念是

"Prompt不应该由人来写,应该由模型自己优化。"

目前已有很多项目组已经不在手写propmt,而是用DSPy自动生成。

这里尝试在本地环境运行DSPy,示例其prmopt的生成过程。

所用示例和代码,参考和修改自网络资料。

1 DSPy

1.1 DSPy说明

DSPy只需要定义任务逻辑,如"输入是问题,输出是答案",然后准备高质量的数据集。

DSPy会自动在后台把这个Prompt"编译"出来。自动尝试各种Few-Shot的组合,调整指令,直到在测试集上效果达到最优。

1.2 DSPy安装

DSPy安装命令如下所示。

pip install dspy -i https://pypi.tuna.tsinghua.edu.cn/simple

pip install dspy-ai -i https://pypi.tuna.tsinghua.edu.cn/simple

1.3 LLM安装

这里采用ollama大模型,并假设ollama模型gemma3n:e2b已安装。具体过程参考

https://blog.csdn.net/liliang199/article/details/152125310

2 DSPy测试

2.1 DSPy连接LLM

DSPy通过dspy.LM连接大模型,具体为通过model的名称区分。

比如"openai/gpt-4o", "ollama/gemma3n:e2b"

并通过api_base和token_key定义LLM的基础url和令牌。

这里通过ollma/gemma3n:e2b示例llm的调用过程,示例如下。

复制代码
import dspy


# Local Ollama
lm = dspy.LM(model="ollama/gemma3n:e2b", api_base="http://localhost:11434")

# Configure globally
dspy.configure(lm=lm)
responses = await lm.acall("What is the capital of USA?")
print(responses)

输出如下

"The capital of the USA is \*\*Washington, D.C.\*\* \\n\\nIt's a federal district, not a state, and serves as the seat of the U.S. government.\\n\\n\\n\\n"

如果是其他服务商提供了LLM,并且兼容openai接口,可以通过如是示例配置。

在模型名称前需要跟"openai",示例如下

"openai/deepseek-r1"

openai兼容llm的配置示例

复制代码
import os
os.environ['OPENAI_API_KEY'] = "sk-xxxxxxxxxx"
os.environ['OPENAI_BASE_URL'] = "https://llm_provider_base_url/v1"
model_name = "openai/deepseek-r1"


import dspy

# OpenAI
lm = dspy.LM(model=model_name, temperature=0.7, max_tokens=1000)

# Configure globally
dspy.configure(lm=lm)

2.2 数学计算prompt示例

1)24点计算

任务示例如下,输入为

point_list="1,3,6,0"

需要通过加、减、乘、除、括号等基本运算符,将这4个数字组合成一个将表达式。

要求表达式的值为24,表达式只能包含这4个数字,并且这4个数必须出现在表达式中。

复制代码
point_list="1,3,6,0"

PROMPT_TEMPLATE = """
        Use the following four natural numbers {point_list} to form a four-rule operation expression. This expression calculates a value equal to 24, where only the four natural numbers and add, subtract, multiply, divide symbols and parentheses. The expression can only have four natural numbers given in the expression, and all four natural numbers must be included.
        Please simply return an expression that meets the above requirements, don't include '= 24'.
    """
question = PROMPT_TEMPLATE.format(point_list=f"[{point_list}]")
module = dspy.ChainOfThought("question -> answer")
result = module(question=question)
print(f"Hello111 ---{result}---")

输出如下所示。

DSPy经过一些列尝试,成功将1,3,6,0组合成一个表达式, answer='(6 + 0) * (3 + 1)。

经过LLM测试和实际计算,表达式结果为24,满足要求。

Hello111 ---Prediction(

reasoning="We need to find an expression using the numbers 1, 3, 6, and 0 with +, -, *, / and parentheses that equals 24. We must use all four numbers. Let's try different combinations.\n\nOne possible approach is to multiply some numbers and then combine them with other operations.\n- 6 * 3 * 1 = 18. We need to get to 24.\n- 6 * 3 = 18. We need to get to 24.\n- 18 * 1 = 18. We need to get to 24.\n- 18 + 6 = 24. This works!\n\nAnother approach:\n- 6 * 1 = 6. We need to get to 24.\n- 6 * 3 = 18. We need to get to 24.\n- 18 + 0 = 18. We need to get to 24.\n- 18 * 0 = 0. We need to get to 24.\n\nLet's try another combination:\n- 6 * 3 * 0 = 0. We need to get to 24.\n- 6 * 3 = 18. We need to get to 24.\n- 18 / 0 is undefined.\n\nLet's try:\n(6 - 3) * 1 * 0 = 3 * 1 * 0 = 0.\n\nLet's try:\n(6 + 3) * 1 * 0 = 9 * 1 * 0 = 0.\n\nLet's try:\n(6 * 3) * 1 * 0 = 18 * 1 * 0 = 0.\n\nLet's try:\n(6 + 0) * (3 - 1) = 6 * 2 = 12.\n\nLet's try:\n(6 + 0) * 3 * 1 = 6 * 3 * 1 = 18.\n\nLet's try:\n(6 + 0) * (3 + 1) = 6 * 4 = 24.",

answer='(6 + 0) * (3 + 1)'

)---

2)掷骰子计算

创建了一个 dspy.ChainOfThought 类型的问答模块 math,然后向 math 模块提出了一个问题:

掷出两个骰子,总和等于2的概率是多少?

示例代码如下

复制代码
math = dspy.ChainOfThought("question -> answer: float")
response = math(question="Two dice are tossed. What is the probability that the sum equals two?")
print(response)

机器上的运行结果如下。

Prediction(

reasoning='Let S be the sample space of the outcomes when two dice are tossed. Each die has 6 faces, so the total number of outcomes is 6 \\\\times 6 = 36.\nWe want to find the probability that the sum of the two dice is equal to 2. Let A be the event that the sum of the two dice is 2. The only outcome that satisfies this condition is (1, 1).\nThe number of outcomes in event A is 1.\nThe total number of outcomes in the sample space S is 36.\nThe probability of event A is the ratio of the number of outcomes in A to the total number of outcomes in S.\nP(A) = \\\\frac{\\\\text{Number of outcomes in A}}{\\\\text{Total number of outcomes in S}} = \\\\frac{1}{36}.',

answer=0.02777777777777777

)

模型给醋的理由是

当两个骰子被掷出时,有36种可能的结果,因为每个骰子都有6个面。得到2之和的唯一方法是:两个骰子都显示一个1 (1+1)。因此,概率是1/36。

3)情感预测

以下是情感预测的示例代码。

复制代码
sentence = "it's a charming and often affecting journey."  # example from the SST-2 dataset.

# 1) Declare with a signature.
classify = dspy.Predict('sentence -> sentiment: bool')

# 2) Call with input argument(s). 
response = classify(sentence=sentence)

# 3) Access the output.
print(response.sentiment)

输出如下

True

reference


Programming ---not prompting---LMs

https://dspy.ai/

dspy.LM

https://dspy.ai/api/models/LM/

DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines

https://arxiv.org/abs/2310.03714

初识DSPy框架 - 关于DSPy的简单介绍

https://zhuanlan.zhihu.com/p/14379055281

大模型Agent的核心还是prompt?

https://www.zhihu.com/question/628670548

DSPy: Programming---not prompting---Foundation Models

https://github.com/stanfordnlp/dspy

docker ollama部署轻量级嵌入模型 - EmbeddingGemma

https://blog.csdn.net/liliang199/article/details/152125310

gemma 3n-低内存和计算需求架构的探索

https://blog.csdn.net/liliang199/article/details/155109021

13 DSPy贴身实战

https://uaxe.github.io/geektime-docs/AI-%E5%A4%A7%E6%95%B0%E6%8D%AE/LLM%E8%87%AA%E4%B8%BB%E6%99%BA%E8%83%BD%E4%BD%93%E5%BA%94%E7%94%A8%E5%AE%9E%E6%88%98%E8%AF%BE/13-DSPy%E8%B4%B4%E8%BA%AB%E5%AE%9E%E6%88%98/

相关推荐
ai大模型-探索者17 小时前
意图识别三选一(BERT微调、LLM+Prompt、Jev)
人工智能·prompt·bert·jev
%KT%17 小时前
Prompt Engineering、Context Engineering、Harness Engineering
prompt
forest_092 天前
《大模型原理通俗讲》系列 00|开篇 大模型到底是怎么回事
人工智能·python·prompt·agi
2601_962885722 天前
把行情 DataFrame 喂给 ChatGPT/Claude 分析:prompt 工作流
chatgpt·prompt
A-刘晨阳2 天前
同一句需求优化前后差多少?Prompt Optimizer先整理提示词,再用同一个模型直接对比结果
运维·prompt·cpolar
“AI国潮设计-小江”2 天前
【AIGC实战】Python+SDXL打造“潮汕英歌舞IP”万能立牌插件,批量生成“发财元宝蛋糕”视觉营销方案(附源码)
开发语言·人工智能·python·prompt·aigc
刘胡子大叔3 天前
用 system_prompt 控制智能体说话风格
人工智能·prompt
forest_094 天前
用Python复刻ClaudeDuet:主动式Agent四件套,定时晨报、后台巡检、危险动作审批(附完整可运行代码)
人工智能·python·架构·prompt·agi
“AI国潮设计-小江”4 天前
AIGC实战:Python+SDXL批量生成英歌舞IP立牌,解锁猪猪蛋糕“反差萌”流量密码(附完整API源码)
开发语言·人工智能·python·prompt·aigc
“AI国潮设计-小江”4 天前
【AIGC实战】Python+SDXL批量生成“潮汕英歌舞不倒翁”IP | 角色一致性与多场景融合方案,附完整代码
开发语言·人工智能·python·prompt·aigc