• 全国 [切换]
  • 二维码
    早勤网

    手机WAP版

    手机也能找商机,信息同步6大终端平台!

    微信小程序

    微信公众号

    当前位置: 首页 » 行业新闻 » 热点新闻 » 正文

    提示工程prompt优化框架DSPy

    放大字体  缩小字体 发布日期:2021-02-13 00:08:53   浏览次数:810  发布人:2845****  IP:124.223.189***  评论:0
    导读

    对于LLM大模型应用来说,提示工程prompting engineering 是基本的,也是很重要的工具,对于模型智力和推理能力有着至关重要的影响。 从手工开发和维护prompt,一些框架开始创造性和自动的生成和优化prompt,例如React和Auto GPT等。斯坦福的项目 DSPy就是一个prompt优化的框架。 DSPy利用数据集迭代优化prompt,类似机器学习的过程,工作流程一般是:

    对于LLM大模型应用来说,提示工程prompting engineering 是基本的,也是很重要的工具,对于模型智力和推理能力有着至关重要的影响。

    从手工开发和维护prompt,一些框架开始创造性和自动的生成和优化prompt,例如React和Auto GPT等。斯坦福的项目 DSPy就是一个prompt优化的框架。

    DSPy利用数据集迭代优化prompt,类似机器学习的过程,工作流程一般是:

    1-收集训练数据
    2-定义一个 program
    3-选择评估方式和优化器
    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) # 载入训练数据 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) # 定义优化器 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

    不推荐电脑端访问(用户少)
    请微信扫一扫,查看完整信息(记得微信收藏)
     
    (文/匿名(若涉版权问题请联系我们核实发布者) / 非法信息举报 / 删稿)
    打赏
    免责声明
    • 
    本文为昵称为 2845**** 发布的作品,本文仅代表发布者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,发布者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们154208694@qq.com删除,我们积极做(权利人与发布者之间的调停者)中立处理。郑重说明:不 违规举报 视为放弃权利,本站不承担任何责任!
    有个别老鼠屎以营利为目的遇到侵权情况但不联系本站或自己发布违规信息然后直接向本站索取高额赔偿等情况,本站一概以诈骗报警处理,曾经有1例诈骗分子已经绳之以法,本站本着公平公正的原则,若遇 违规举报 我们100%在3个工作日内处理!
    0 条相关评论
     

    (c)2008-现在 zaoqin.com All Rights Reserved.