DSPy框架

动机

通过声明式,而非手工调优提示词

官方文档

https://github.com/stanfordnlp/dspy

示例测试

基本问答

示例1

import dspy
from openai import OpenAI, api_key

API_KEY = "YOUR_API_KEY"
API_BASE_URL = "https://api.chatanywhere.tech/v1"

lm = dspy.LM('openai/gpt-4o-mini', api_key=API_KEY, api_base=API_BASE_URL)
dspy.configure(lm=lm)
qa = dspy.Predict('question: str -> response: str')

def get_response(question):
    print("question:", question)
    res = qa(question=question).response
    return res

print("--Answer:", get_response("艾尔登法环最差的结局是什么?"))
print(dspy.inspect_history(n=1))
print("--Answer:", get_response("What's the worst ending of Elden Ring?"))
print(dspy.inspect_history(n=1))

dspy.inspect_history(n=1)可以查看最近1次交互

思维链

import dspy
from openai import OpenAI, api_key

API_KEY = "YOUR_API_KEY"
API_BASE_URL = "https://api.chatanywhere.tech/v1"
lm = dspy.LM('openai/gpt-4o-mini', api_key=API_KEY, api_base=API_BASE_URL)
dspy.configure(lm=lm)
cot = dspy.ChainOfThought('question: str -> response: str')

def get_response(question):
    print("question:", question)
    res = cot(question=question).response
    return res

print("--Answer:", get_response("In what order would you recommend the new player to explore the map in the game Elden Ring?"))
dspy.inspect_history(n=1)

输出:

[[ ## reasoning ## ]]
In Elden Ring, the map is vast and filled with various regions that present different challenges and enemies. For a new player, it's essential to explore in a way that gradually increases their understanding of the game mechanics, combat, and lore. A recommended order would be to start in the starting area, Limgrave, which serves as a tutorial zone. From there, players can move to the Weeping Peninsula, which is relatively easier and offers valuable resources. After that, exploring the Stormveil Castle will introduce players to more complex gameplay elements. Following Stormveil, players can venture into the Liurnia of the Lakes, which provides a mix of exploration and combat challenges. Finally, players can tackle the Caelid region, which is more difficult and requires a better understanding of the game. This progression allows players to build their skills and gear before facing tougher enemies.

[[ ## response ## ]]
I recommend that new players explore the map in the following order: 

1. **Limgrave** - Start here to get familiar with the game mechanics and combat.
2. **Weeping Peninsula** - This area is relatively easier and offers valuable resources.
3. **Stormveil Castle** - A step up in difficulty that introduces more complex gameplay elements.
4. **Liurnia of the Lakes** - A mix of exploration and combat challenges that will test your skills.
5. **Caelid** - This region is more difficult and should be approached after gaining experience and better gear.

This order will help you gradually build your skills and prepare for tougher challenges ahead.

[[ ## completed ## ]]

输出:

Response:
[[ ## reasoning ## ]]
鲍鱼是一种海鲜,清洗时需要特别注意,以去除表面的沙子和杂质。首先,鲍鱼的外壳上可能会有泥沙和海藻,因此需要用刷子轻轻刷洗外壳。其次,鲍鱼的肉质部分也需要清洗,以去除可能的沙粒和腥味。最后,清洗时要小心,不要损伤鲍鱼的肉质。
[[ ## response ## ]]
清洗鲍鱼的步骤如下:
1. 用刷子轻轻刷洗鲍鱼的外壳,去除泥沙和海藻。
2. 将鲍鱼放入清水中,轻轻搓洗肉质部分,确保去除沙粒和杂质。
3. 如果有必要,可以用少量盐水浸泡几分钟,以进一步去腥。
4. 最后,用清水冲洗干净,确保没有残留的盐和杂质。
[[ ## completed ## ]]

附带参数

import dspy
from pprint import pprint
from setting import API_KEY, API_BASE_URL
lm = dspy.LM('openai/gpt-4o-mini', api_key=API_KEY, api_base=API_BASE_URL)
dspy.configure(lm=lm)
cot = dspy.ChainOfThought('question: str -> answer: str', n=10)
response = cot(question="击败满月女王蕾娜拉之后应该去哪里?")
pprint(response.completions.answer)

句子分类

# @Author: weirdgiser
# @Time: 2024/11/13
# @Function: 示例:分类
import dspy
from typing import Literal
from setting import API_KEY, API_BASE_URL
lm = dspy.LM('openai/gpt-4o-mini', api_key=API_KEY, api_base=API_BASE_URL)
dspy.configure(lm=lm)
class Labeling(dspy.Signature):
    """Classify."""

    sentence: str = dspy.InputField()
    label: Literal['技术创新','新品发布', '投资收购', '战略合作', '消费者活动', '产能变化', '经营指标'] = dspy.OutputField()

sentences = ["双十一大促销", "华为三折叠引爆全场", '小米15pro发布会即将开始', '苹果收购特斯拉', '比亚迪财报亮眼', '特斯拉产能扩大', '小米营收增长']
classify = dspy.Predict(Labeling)
for sentence in sentences:
    res = classify(sentence=sentence)
    print(res.label, sentence)

输出:

底层prompt及响应:


System message:

Your input fields are:
1. `sentence` (str)

Your output fields are:
1. `label` (Literal[技术创新, 新品发布, 投资收购, 战略合作, 消费者活动, 产能变化, 经营指标])

All interactions will be structured in the following way, with the appropriate values filled in.

[[ ## sentence ## ]]
{sentence}

[[ ## label ## ]]
{label}        # note: the value you produce must be one of: 技术创新; 新品发布; 投资收购; 战略合作; 消费者活动; 产能变化; 经营指标

[[ ## completed ## ]]

In adhering to this structure, your objective is: 
        Classify.


User message:

[[ ## sentence ## ]]
小米发布新款手机,售价不到1000元

Respond with the corresponding output fields, starting with the field `[[ ## label ## ]]` (must be formatted as a valid Python Literal[技术创新, 新品发布, 投资收购, 战略合作, 消费者活动, 产能变化, 经营指标]), and then ending with the marker for `[[ ## completed ## ]]`.


Response:

[[ ## label ## ]]
新品发布

[[ ## completed ## ]]

RAG:检索增强生成

(暂略)

小结

定义任务和优化目标,准备一些示例输入

  1. 定义任务

    预期的输入/输出行为:基于私有化数据的聊天机器人?从文本中抽取信息?翻译系统?总结信息并附带引用?

    质量和预算。

  2. 定义工作流程

  3. 给出示例场景

  4. 定义数据

    正式声明训练数据和验证数据,用于验证和优化。

  5. 定义指标

    定义好的输出、坏的输出的特征。如果无法定义指标,将难以评估和提升。

  6. 收集初步评估结果

  7. 使用优化器编译

  8. 迭代

模块

  • Predict

  • ChainOfThought

  • ChainOfThoughtWithHint

  • ReAct

  • MultiChainComparison

  • ProgramOfThought

  • Retrieve

DSPy模块的特性

  • 抽象了LLM提示技巧(如思维链和ReAct)

  • 具有可学习的参数

  • 多个模块可以组成更大的模块。与PyTorch框架中的NN模块类似,只是应用在LM编程领域。

官方文档:https://dspy.ai/building-blocks/3-modules

模型支持

相关Issues

源码分析

相关依赖

相关文章

DSPy相关

告别提示词工程, 斯坦福重新定义LLM开发范式,DSPy框架已获18.4K Star!

论文: DSPY: COMPILING DECLARATIVE LANGUAGE MODEL CALLS INTO SELF-IMPROVING PIPELINES

API Key相关

GitHub - chatanywhere/GPT_API_free: Free ChatGPT API Key,免费ChatGPT API,支持GPT4 API(免费),ChatGPT国内可用免费转发API,直连无需代理。可以搭配ChatBox等软件/插件使用,极大降低接口使用成本。国内即可无限制畅快聊天。

CoolCats
CoolCats
理学学士

我的研究兴趣是时空数据分析、知识图谱、自然语言处理与服务端开发