前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >探索大模型能力--prompt工程

探索大模型能力--prompt工程

原创
作者头像
languageX
修改2024-01-23 14:15:50
1K0
修改2024-01-23 14:15:50
举报
文章被收录于专栏:大语言模型大语言模型

之前的文章中我们结合prompt-engineering课程体验了下混元大模型,今天再介绍大家一些写好prompt的思路以及prompt资料。

1 prompt工程是什么

1.1 什么是Prompt?

LLM大语言模型终究也只是一个工具,我们不可能每个人都去训一个大模型,但是我们可以思考如何利用好大模型,让他提升我们的工作效率。就像计算器工具一样,要你算10的10倍,你是按10次“10+”,还是直接“10*10”,如何利用工具决定了你几点下班。。

提示词(prompt)就是你给大模型下达的指令输入,它包含以下任意要素:

指令:想要模型执行的特定任务或者指令。

上下文:包含外部信息或额外的上下文信息,引导语言模型更好地响应。

输入数据:用户输入的内容或问题

输出指示:指定输出的类型或格式。

1.2 什么是prompt工程?

提示工程就是开发和优化提示词(Prompt),一种在大模型中使用的技巧,通过提供清晰、简洁的指令或问题,充分发挥大模型的能力,让模型更好地理解我们的需求,从而得到更好的模型输出。

给大模型提示语的时候,你就是产品经理:

你随便提需求,模型也就随口一答;

你提出详细的需求,给出充分的需求背景,给足输出数据,定义好输出格式,好的模型就像一个好的程序员,会给你一个满意的答复的~

2 prompt工程基本原则

我们再回顾下prompt工程课程中的两大基本原则:

2.1 给出清晰,详细的指令

策略1:使用分割符清晰的知识输出的不同部分,比如"",<>,<\tag>等分隔符

策略2:指定一个结构化的输出,比如json,html等格式

策略3:要求模型自检测是否满足条件

策略4:提供少量示例

2.2 给模型思考的时间

我们不一定要一步得到答案,可以让模型进行一系列的推理,给他推理思考的时间,再最终给出答案。

策略1:指定完成任务需要的步骤

告诉模型完成这个指令,需要执行几步,每步的详细步骤

策略2:明确模型再给出结论之前推理出自己的解决方案

让模型自己推理,写出每一步的步骤,给足够的时间(token-钱)边思考边执行。

3 prompt工程迭代思维

迭代思想无处不在,提示工程和软件工程,算法模型一样都是一个迭代的过程。。

4 prompt工程方法

prompt的原则和方法在吴恩达教授的课程中讲解的很详细 ,这里我们再介绍一些高阶方法。

4.1 思维链

思维链的概念是在Google的论文Chain-of-Thought Prompting Elicits Reasoning in Large Language Models中被首次提出。思维链是一系列的短句子,模仿了人类在回答问题时的思考和可能的推理过程。

作者在文中也指出作为一种促进语言模型推理的方法,思维链提示具有几个吸引人的特性:

首先,思维链允许模型将多步问题进行拆分,分解为多个中间步骤。

其次,思维链使得模型的回答过程具有较好的可解释性,同时这一特性为定位推理错误,并修改错误提供了可能。

第三,链式思维推理可用于数学单词问题、常识推理和符号操作等任务,并且理论上适用于任何可以通过语言解决的任务。

最后,只要将思维序列链的例子包含到少样本提示的范例中,就可以很容易地在足够大的现成语言模型中引出思维链推理。

下图一次介绍了少样本提示(a),少样本思维链(b),零样本提示(c),零样本思维链(d)

4.2 思维树

Tree of Thoughts (ToT)是思维链的进一步拓展,主要想解决 LM 推理过程存在如下两个问题:

不会探索不同的可能选择分支

无法在节点进行前后向的探索

ToT 将问题建模为树状搜索过程,包括四个步骤:问题分解、想法生成,状态评价以及搜索算法的选择。

4.3 用prompt写prompt

以上方法都太麻烦,还有一些懒人方法。利用ChatGPT插件或者自己写一个prompt来帮你写prompt,用魔法打败魔法。

比如https://github.com/minghaochen/universal-prompt/blob/master/universal-prompt.txt 给出一个优化prompt的模版。

代码语言:javascript
复制
message = [
            {"content": """
            I want you to act as an AI prompt engineer. You are expert at writing ChatGPT Prompts to get the best results.

            To create efficient prompts that yield high-quality responses, consider the following principles and strategies: \
            1. Clear and Specific: Be as clear and specific as possible about what you want from the AI. If you want a certain type of response, outline that in your prompt. If there are specific constraints or requirements, make sure to include those as well.
            2. Open-ended vs. Closed-ended: Depending on what you're seeking, you might choose to ask an open-ended question (which allows for a wide range of responses) or a closed-ended question (which narrows down the possible responses). Both have their uses, so choose according to your needs.
            3. Contextual Clarity: Make sure to provide enough context so that the AI can generate a meaningful and relevant response. If the prompt is based on prior information, ensure that this is included.
            4. Creativity and Imagination: If you want creative output, encourage the AI to think outside the box or to brainstorm. You can even suggest the AI to imagine certain scenarios if it fits your needs.

            There is a well-written prompt delimited by <> for your reference: <Your task is to be my brainstorming partner and provide creative ideas and suggestions for a given topic or problem. Your response should include original, unique, and relevant ideas that could help solve the problem or further explore the topic in an interesting way. Please note that your response should also take into account any specific requirements or constraints of the task.>

            Your task is to write an effective ChatGPT Prompt based on given keywords or to modify the given prompts. Answer in the same language as me.
            """,
            "role": "system"}
        ]
message.append(
            {"content": "Please help me to write an effective ChatGPT Prompt based on the following keywords or prompt: {}".format(prompt),
             "role": "user"}
        )

在调用ChatGPT接口时,使用如上message,就可以优化你的prompt.

代码语言:javascript
复制
Before:
translate the following English content into Chinese, 并润色
After:
<Please translate the following English text into Chinese, ensuring not only accuracy in translation but also enhancing the prose to flow naturally and elegantly in Chinese. The translation should read as if originally written by a native speaker, with attention to cultural nuances and idiomatic expressions.>

5 实践

上面说了这么多概念和理论方法,还有没有更好的方法,快速让我知道怎么写好prompt?

有!用别人的经验!

前公司有个同事做过一个分享,大佬说过一句话:要想写好prompt最好的方法就是知道他的训练数据是什么样子的。可惜,很少大模型完全开源他们的数据集。

但是很多大模型都会开放一个平台给大家体验,大家也都会将好的用例分享出来。

比如智普清言,我们看看程序员灵感

这里面就是一些优秀prompt,这些经验我们就可以直接拿来主义。

大致可以总结出模版:先给模型一个角色定位(你是一名***),再给出具体的需求(请写一个***需求的代码)。

上一个示例:

代码语言:javascript
复制
你是一位擅长编写各种程序的资深工程师,我需要你根据以下需求,为我生成一段Python代码:
代码目的:设计一个双十一自动抢购脚本
代码功能:在双十一当天自动登录购物网站,定时抢购预设商品
代码要求:使用面向对象编程方式,使用selenium库进行网页操作
代码语言:javascript
复制
为了实现这个双十一自动抢购脚本,我们可以进行以下步骤:
1. 首先,需要安装selenium库以及对应浏览器的驱动。
2. 其次,需要登录目标购物网站。
3. 然后,需要定位到目标商品的抢购按钮。
4. 最后,在双十一当天设定一个定时器来点击抢购按钮。
以下是一个简单的示例代码:
```python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
class DoubleElevenShopper:
    def __init__(self, website, username, password, item_link):
        self.website = website
        self.username = username
        self.password = password
        self.item_link = item_link
        self.driver = webdriver.Chrome()  # 使用Chrome浏览器
    def login(self):
        self.driver.get(self.website)
        username_field = self.driver.find_element_by_name("username")  # 这里需要根据目标网站的登录表单进行调整
        password_field = self.driver.find_element_by_name("password")
        username_field.send_keys(self.username)
        password_field.send_keys(self.password)
        password_field.send_keys(Keys.RETURN)
        time.sleep(5)  # 等待登录完成
    def buy_item(self):
        self.driver.get(self.item_link)
        buy_button = self.driver.find_element_by_id("buy_button")  # 这里需要根据目标商品的购买按钮进行调整
        buy_button.click()
    def run(self):
        self.login()
        self.buy_item()
        self.driver.quit()
if __name__ == "__main__":
    shopper = DoubleElevenShopper(
        website="https://www.example.com",
        username="your_username",
        password="your_password",
        item_link="https://www.example.com/item"
    )
    shopper.run()
```
注意:
1. 需要安装Chrome浏览器和ChromeDriver。
2. 登录和购买按钮的元素定位需要根据具体的购物网站进行调整。
3. 此代码为简单示例,实际使用可能需要处理更多的异常情况。
安装selenium库和ChromeDriver:
```
pip install selenium
```
下载ChromeDriver并将其路径添加到环境变量中。

最后奉上最全prompt指南:https://github.com/dair-ai/Prompt-Engineering-Guide

参考:

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

https://aijishu.com/a/1060000000421219

我正在参与2023腾讯技术创作特训营第三期有奖征文,组队打卡瓜分大奖!

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 prompt工程是什么
    • 1.1 什么是Prompt?
      • 1.2 什么是prompt工程?
      • 2 prompt工程基本原则
        • 2.1 给出清晰,详细的指令
          • 2.2 给模型思考的时间
          • 3 prompt工程迭代思维
          • 4 prompt工程方法
            • 4.1 思维链
              • 4.2 思维树
                • 4.3 用prompt写prompt
                • 5 实践
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档