自学内容网 自学内容网

大模型提示学习与思维链——《动手学大模型》实践教程第二章

1 前言

从一些大模型相关的论文里学了一些理论知识,但是还是欠缺实践经验,本系列博文是在学习上交大张倬胜老师的开源项目的基础上写的相关总结,旨在提升自己的大模型实践能力。开源项目地址:dive-into-llms
备注:本项目大部分资源都需要科学上网才能获取。

免责声明
本系列博文所有技巧仅供参考,不保证百分百正确。若有任何问题,欢迎联系博主。
本系列博文所涉及的资源均来自互联网,如侵犯了您的版权请联系我删除,谢谢。

2 大模型提示学习与思维链

1
2
3
4
5
6
7
8
9

3 实践代码

导读: 该部分介绍大模型的API调用与推理指南

教程目标:

  1. 熟悉大语言模型的使用方式
  2. 掌握零样本和少样本提示工程
  3. 了解思维链推理技术

3.1 获得大模型调用权限

通义千问:https://help.aliyun.com/zh/dashscope/developer-reference/quick-start

智谱AI: https://open.bigmodel.cn/

Openai:https://platform.openai.com/playground

其他:文心一言、百川等

*基本流程:开通服务获得API-KEY(获赠计算额度),使用API Key调用服务

3.2 调用方式(以通义千问为例)

使用流程:https://help.aliyun.com/zh/model-studio/getting-started/first-api-call-to-qwen

注册阿里云 → 实名认证 → 开通百炼 → 获取API KEY → 安装SDK → 调用API

3.2.1 通过GUI界面调用(适合案例测试)

进入模型体验中心测试:https://bailian.console.aliyun.com/home#/home
prompt

3.2.2 通过命令行调用(适合开发、规模化实验)

快速入门:https://help.aliyun.com/zh/dashscope/create-a-chat-foundation-model

  1. 普通调用:
import random
from http import HTTPStatus
# 建议dashscope SDK 的版本 >= 1.14.0
from dashscope import Generation


def call_with_messages():
    messages = [{'role': 'system', 'content': 'You are a helpful assistant.'},
                {'role': 'user', 'content': '用萝卜、土豆、茄子做饭,给我个菜谱。'}]
    response = Generation.call(model="qwen-turbo",
                               messages=messages,
                               # 设置随机数种子seed,如果没有设置,则随机数种子默认为1234
                               seed=random.randint(1, 10000),
                               temperature=0.8,
                               top_p=0.8,
                               top_k=50,
                               # 将输出设置为"message"格式
                               result_format='message')
    if response.status_code == HTTPStatus.OK:
        print(response)
    else:
        print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
            response.request_id, response.status_code,
            response.code, response.message
        ))


if __name__ == '__main__':
    call_with_messages()
  1. 流式调用:
from http import HTTPStatus
from dashscope import Generation


def call_with_stream():
    messages = [
        {'role':'system','content':'you are a helpful assistant'},
        {'role': 'user','content': '用萝卜、土豆、茄子做饭,给我个菜谱。'}
        ]
    responses = Generation.call(
        model="qwen-turbo",
        messages=messages,
        # 设置输出为'message'格式
        result_format='message',
        # 设置输出方式为流式输出
        stream=True,
        # 增量式流式输出
        incremental_output=True
        )
    for response in responses:
        if response.status_code == HTTPStatus.OK:
            print(response)
        else:
            print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
                response.request_id, response.status_code,
                response.code, response.message
            ))

if __name__ == '__main__':
    call_with_stream()
  1. 在屏幕上显示美观的格式
from http import HTTPStatus
from dashscope import Generation


def call_with_stream():
    messages = [
        {'role':'system','content':'you are a helpful assistant'},
        {'role': 'user','content': '用萝卜、土豆、茄子做饭,给我个菜谱。'}
        ]
    responses = Generation.call(
        model="qwen-turbo",
        messages=messages,
        # 设置输出为'message'格式
        result_format='message',
        # 设置输出方式为流式输出
        stream=True,
        # 增量式流式输出
        incremental_output=True
        )
    for response in responses:
        if response.status_code == HTTPStatus.OK:
            print(response.output.choices[0].message.content,end="",flush=True)
        else:
            print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
                response.request_id, response.status_code,
                response.code, response.message
            ))

if __name__ == '__main__':
    call_with_stream()

3.3 提示学习(Prompting)

  • 零样本提示:给出目标指令提示
  • 少样本提示:提供任务范例提示
机器翻译情感分析
零样本Translate English to Frenchcheese =>Given a review, extract the aspect term(s) and determine their corresponding sentiment polarity. Review: I can say that I am fully satisfied with the performance that the computer has supplied.
参考输出Le fromage[[performance, positive]]
少样本Translate English to Frenchsea otter => loutre de merpepprimint => menthe poivréeplush giraffe => girafe peluchecheese =>Given a review, extract the aspect term(s) and determine their corresponding sentiment polarity. Here are some examples:
Review: It runs perfectly.
Label: [[runs, positive]]
Review: The service is awful.
Label: [[service, negative]]
Review: lots of extra space but the keyboard is ridiculously small.
Label: [[space, positive], [keyboard, negative]]
Review: I can say that I am fully satisfied with the performance that the computer has supplied .
参考输出Le fromage[[performance, positive]]

3.4 思维链提示

基本思路:模拟人类的思考过程,将多步骤推理问题分解成一系列中间步骤,进而实现问题分解和逐步求解

自然语言思维链(CoT)程序思维链(PoT)
零样本Q: There were 10 friends playing a video game online when 7 players quit. If each player left had 8 lives, how many lives did they have total?
A: Let’s think step by step.
Question: Jordan wanted to surprise her mom with a homemade birthday cake. From reading the instructions, she knew it would take 20 minutes to make the cake batter and 30 minutes to bake the cake. The cake would require 2 hours to cool and an additional 10 minutes to frost the cake. If she plans to make the cake all on the same day, what is the latest time of day that Jordan can start making the cake to be ready to serve it at 5:00 pm?
Answer this question by implementing a solver() function.
def solver():
# Let’s write a Python program step by step, and then return the answer
# Firstly, we need define the following variable:
参考输出There were 10 friends playing a video game online. This means that, at the start, there were 10 x 8 = 80 lives in total. Then, 7 players quit. This means that 7 x 8 = 56 lives were lost. Therefore, the total number of lives remaining is 80 - 56 = 24. The answer is 24.
minutes_to_make_batter = 20
minutes_to_bake_cake = 30
minutes_to_cool_cake = 2 * 60
minutes_to_frost_cake = 10
total_minutes = minutes_to_make_batter + minutes_to_bake_cake + minutes_to_cool_cake + minutes_to_frost_cake
total_hours = total_minutes / 60
ans = 5 - total_hours
少样本Q: There are 15 trees in the grove. Grove workers will plant trees in the grove today. After they are done, there will be 21 trees. How many trees did the grove workers plant today?
A: There are 15 trees originally. Then there were 21 trees after some more were planted. So there must have been 21 - 15 = 6. The answer is 6.
Q: If there are 3 cars in the parking lot and 2 more cars arrive, how many cars are in the parking lot?
A: There are originally 3 cars. 2 more cars arrive. 3 + 2 = 5. The answer is 5.
Q: Leah had 32 chocolates and her sister had 42. If they ate 35, how many pieces do they have left in total?
A: Originally, Leah had 32 chocolates. Her sister had 42. So in total they had 32 + 42 = 74. After eating 35, they had 74 - 35 = 39. The answer is 39.
Q: Jason had 20 lollipops. He gave Denny some lollipops. Now Jason has 12 lollipops. How many lollipops did Jason give to Denny?
A: Jason started with 20 lollipops. Then he had 12 after giving some to Denny. So he gave Denny 20 - 12 = 8. The answer is 8.
Q: There were 10 friends playing a video game online when 7 players quit. If each player left had 8 lives, how many lives did they have total?
A:
Question: Janet’s ducks lay 16 eggs per day. She eats three for breakfast every morning and bakes muffins for her friends every day with four. She sells the remainder at the farmers’ market daily for $2 per fresh duck egg. How much in dollars does she make every day at the farmers’ market?
Python code, return ans
total_eggs = 16
eaten_eggs = 3
baked_eggs = 4
sold_eggs = total_eggs - eaten_eggs - baked_eggs
dollars_per_egg = 2
ans = sold_eggs * dollars_per_egg

Question: A robe takes 2 bolts of blue fiber and half that much white fiber. How many bolts in total does it take?
Python code, return ans
bolts_of_blue_fiber = 2
bolts_of_white_fiber = num_of_blue_fiber / 2
ans = bolts_of_blue_fiber + bolts_of_white_fiber

Question: Josh decides to try flipping a house. He buys a house for $80,000 and then puts in $50,000 in repairs. This increased the value of the house by 150%. How much profit did he make?
Python code, return ans
cost_of_original_house = 80000
increase_rate = 150 / 100
value_of_house = (1 + increase_rate) * cost_of_original_housecost_of_repair = 50000
ans = value_of_house - cost_of_repair - cost_of_original_house

Question: Every day, Wendi feeds each of her chickens three cups of mixed chicken feed, containing seeds, mealworms and vegetables to help keep them healthy. She gives the chickens their feed in three separate meals. In the morning, she gives her flock of chickens 15 cups of feed. In the afternoon, she gives her chickens another 25 cups of feed. How many cups of feed does she need to give her chickens in the final meal of the day if the size of Wendi’s flock is 20 chickens?
Python code, return ans
numb_of_chickens = 20
cups_for_each_chicken = 3
cups_for_all_chicken = num_of_chickens * cups_for_each_chickencups_in_the_morning = 15
cups_in_the_afternoon = 25
ans = cups_for_all_chicken - cups_in_the_morning - cups_in_the_afternoon

Question: Jordan wanted to surprise her mom with a homemade birthday cake. From reading the instructions, she knew it would take 20 minutes to make the cake batter and 30 minutes to bake the cake. The cake would require 2 hours to cool and an additional 10 minutes to frost the cake. If she plans to make the cake all on the same day, what is the latest time of day that Jordan can start making the cake to be ready to serve it at 5:00 pm?
Python code, return ans
参考输出There were 10 friends playing a video game online. This means that, at the start, there were 10 x 8 = 80 lives in total. Then, 7 players quit. This means that 7 x 8 = 56 lives were lost. Therefore, the total number of lives remaining is 80 - 56 = 24. The answer is 24.
minutes_to_make_batter = 20
minutes_to_bake_cake = 30
minutes_to_cool_cake = 2 * 60
minutes_to_frost_cake = 10
total_minutes = minutes_to_make_batter + minutes_to_bake_cake + minutes_to_cool_cake + minutes_to_frost_cake
total_hours = total_minutes / 60
ans = 5 - total_hours

3.5 观察&思考:

  1. 错误范例的影响:把少样本学习中的例子改成错误的答案,结果会发生变化吗?
    https://github.com/sunlab-osu/Understanding-CoT(ACL 2023)

错误范例的影响

  1. 自洽性提升推理结果:设置temperature大于0(如0.7),保持同样的输入,多次采样,生成多个推理路径和答案,最终选择答案出现最多的作为最终答案输出。
    https://openreview.net/pdf?id=1PL1NIMMrw(ICLR 2023)

自洽性提升推理结果


原文地址:https://blog.csdn.net/sinat_16020825/article/details/143838406

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!