提示工程prompt優(yōu)化框架DSPy

對于LLM大模型應(yīng)用來說,提示工程prompting engineering 是基本的蔑歌,也是很重要的工具,對于模型智力和推理能力有著至關(guān)重要的影響揽碘。

從手工開發(fā)和維護prompt次屠,一些框架開始創(chuàng)造性和自動的生成和優(yōu)化prompt,例如React和Auto GPT等雳刺。斯坦福的項目 DSPy就是一個prompt優(yōu)化的框架劫灶。

DSPy利用數(shù)據(jù)集迭代優(yōu)化prompt,類似機器學(xué)習(xí)的過程掖桦,工作流程一般是:

1-收集訓(xùn)練數(shù)據(jù)
2-定義一個 program
3-選擇評估方式和優(yōu)化器
4-Compile本昏!

import dspy
from dspy.datasets.gsm8k import GSM8K, gsm8k_metric
from dspy.teleprompt import BootstrapFewShot
from dspy.evaluate import Evaluate

# Set up the LM
turbo = dspy.OpenAI(model='gpt-3.5-turbo-instruct', max_tokens=250,
                    api_key='your_OPENAI_API_KEY')
dspy.settings.configure(lm=turbo)

# 載入訓(xùn)練數(shù)據(jù)
gsm8k = GSM8K()
gsm8k_trainset, gsm8k_devset = gsm8k.train[:10], gsm8k.dev[:10]

# 定義 program
class CoT(dspy.Module):
    def __init__(self):
        super().__init__()
        self.prog = dspy.ChainOfThought("question -> answer")

    def forward(self, question):
        return self.prog(question=question)

# 定義優(yōu)化器
config = dict(max_bootstrapped_demos=4, max_labeled_demos=4)
teleprompter = BootstrapFewShot(metric=gsm8k_metric, **config)

# Compile!
optimized_cot = teleprompter.compile(CoT(), trainset=gsm8k_trainset)

# 效果評估
evaluate = Evaluate(devset=gsm8k_devset, metric=gsm8k_metric, num_threads=4, display_progress=True, display_table=0)
evaluate(optimized_cot)

turbo.inspect_history(n=1)

生成的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: Sarah buys 20 pencils on Monday. Then she buys 18 more pencils on Tuesday. On Wednesday she buys triple the number of pencils she did on Tuesday. How many pencils does she have?
Reasoning: Let's think step by step in order to determine the total number of pencils. On Monday, Sarah bought 20 pencils. On Tuesday, she bought another 18, bringing the total to 38 pencils. Then on Wednesday, she bought triple the number of pencils she did on Tuesday, which would be 3 x 18 = 54 pencils. So the total number of pencils is 38 + 54 = 92.
Answer: 92

---

Question: Rookie police officers have to buy duty shoes at the full price of $85, but officers who have served at least a year get a 20% discount. Officers who have served at least three years get an additional 25% off the discounted price. How much does an officer who has served at least three years have to pay for shoes?
Reasoning: Let's think step by step in order to solve this problem. We can start by calculating the discount that an officer who has served at least three years can get. This would be 25% of the discounted price. Then, we can find the original discounted price, which is 85 - (85 * 20%) = 85 - 17 = 68. Finally, we can calculate the final price by applying the additional 25% discount on the discounted price of 68, resulting in a discount of 17 and a final price of 51.
Answer: 51

---

Question: The average score on last week's Spanish test was 90. Marco scored 10% less than the average test score and Margaret received 5 more points than Marco. What score did Margaret receive on her test?
Reasoning: Let's think step by step in order to produce the answer. First, we know that Marco scored 10% less than the average, which means he scored 90 - (90 * 0.1) = 90 * 0.9 = 81. Then, we know that Margaret scored 5 more points than Marco, which means she scored 81 + 5 = 86.
Answer: 86

---

Question: A third of the contestants at a singing competition are female, and the rest are male. If there are 18 contestants in total, how many of them are male?
Reasoning: Let's think step by step in order to find the number of male contestants. We know the total number of contestants is 18 and that a third of them are female. This means that there are 2/3 of the contestants are male. In order to find the number of male contestants, we can multiply 18 by 2 and divide by 3, giving us a total of 12 male contestants.
Answer: 12

---

Question: Trey is raising money for a new bike that costs $112. He plans to spend the next two weeks selling bracelets for $1 each. On average, how many bracelets does he need to sell each day?
Reasoning: Let's think step by step in order to find the average number of bracelets Trey needs to sell each day. We know that Trey needs to raise $112 and each bracelet costs $1. This means he needs to sell 112 bracelets in total. He has two weeks to sell them, which means he needs to sell 112/14 = 8 bracelets per day on average.
Answer: 8
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末枪汪,一起剝皮案震驚了整個濱河市涌穆,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌雀久,老刑警劉巖宿稀,帶你破解...
    沈念sama閱讀 206,126評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異赖捌,居然都是意外死亡祝沸,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評論 2 382
  • 文/潘曉璐 我一進店門越庇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來罩锐,“玉大人,你說我怎么就攤上這事卤唉∩螅” “怎么了?”我有些...
    開封第一講書人閱讀 152,445評論 0 341
  • 文/不壞的土叔 我叫張陵搬味,是天一觀的道長境氢。 經(jīng)常有香客問我,道長碰纬,這世上最難降的妖魔是什么萍聊? 我笑而不...
    開封第一講書人閱讀 55,185評論 1 278
  • 正文 為了忘掉前任,我火速辦了婚禮悦析,結(jié)果婚禮上寿桨,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好亭螟,可當(dāng)我...
    茶點故事閱讀 64,178評論 5 371
  • 文/花漫 我一把揭開白布挡鞍。 她就那樣靜靜地躺著,像睡著了一般预烙。 火紅的嫁衣襯著肌膚如雪墨微。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 48,970評論 1 284
  • 那天扁掸,我揣著相機與錄音翘县,去河邊找鬼。 笑死谴分,一個胖子當(dāng)著我的面吹牛锈麸,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播牺蹄,決...
    沈念sama閱讀 38,276評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼忘伞,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了沙兰?” 一聲冷哼從身側(cè)響起氓奈,我...
    開封第一講書人閱讀 36,927評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎僧凰,沒想到半個月后探颈,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,400評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡训措,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,883評論 2 323
  • 正文 我和宋清朗相戀三年伪节,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片绩鸣。...
    茶點故事閱讀 37,997評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡怀大,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出呀闻,到底是詐尸還是另有隱情化借,我是刑警寧澤,帶...
    沈念sama閱讀 33,646評論 4 322
  • 正文 年R本政府宣布捡多,位于F島的核電站蓖康,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏垒手。R本人自食惡果不足惜蒜焊,卻給世界環(huán)境...
    茶點故事閱讀 39,213評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望科贬。 院中可真熱鬧泳梆,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至套硼,卻和暖如春卡辰,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背邪意。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評論 1 260
  • 我被黑心中介騙來泰國打工看政, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人抄罕。 一個月前我還...
    沈念sama閱讀 45,423評論 2 352
  • 正文 我出身青樓,卻偏偏與公主長得像于颖,于是被迫代替她去往敵國和親呆贿。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,722評論 2 345

推薦閱讀更多精彩內(nèi)容