【大模型应用开发极简入门】提示工程一:1. 通过context、task、role文本结构设计有效的提示词、 2. OpenAI的提示词任务示例

本文主要是介绍【大模型应用开发极简入门】提示工程一:1. 通过context、task、role文本结构设计有效的提示词、 2. OpenAI的提示词任务示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 一. chat_completion函数
  • 二. 设计有效的提示词
    • 1.上下文
      • 1.1. 更多细节的上下文
      • 1.2. 让GPT改进上下文
    • 2.任务
      • 2.1. 提供足够的任务信息
      • 2.2. OpenAI的任务示例
        • 语法纠正
        • 总结
        • TL;DR概要
        • Python转自然语言
        • 计算时间复杂度
        • 修复Python bug
        • 产生python函数
    • 3.角色

了解LLM和OpenAI API的基础知识,之后我们就可以了解一些更高阶的知识,比如提示工程、零样本学习、少样本学习到为特定任务微调模型,接下来我们将要了解提供开发LLM驱动型应用程序所需的一切知识。

一. chat_completion函数

先来回顾一下chat_completion函数

def chat_completion(prompt, model="gpt-4", temperature=0):res = openai.ChatCompletion.create(model=model,messages=[{"role": "user", "content": prompt}],temperature=temperature,)print(res["choices"][0]["message"]["content"])

 
model和temperature是两个可选特征,分别被默认设置为gpt-4和0。

 

为了说明提示工程的原理,我们使用示例文本“As Descartes said, I think therefore”(正如笛卡儿所说,我思故)。如果将此文本输入GPT-4,那么模型自然会通过迭代式地添加最可能出现的标记来补全句子:

chat_completion("As Descartes said, I think therefore")

模型的输出消息如下所示:

I am. This famous philosophical statement, also known as "Cogito, ergo
sum," emphasizes the existence of the self through the act of thinking
or doubting. Descartes used this statement as a foundational principle
in his philosophy, arguing that one's own existence is the most certain
and indubitable fact that can be known.

 

需要注意的是:

  • 提示工程可能会影响OpenAI API的使用成本。
    该成本与你发送给OpenAI并从其接收的标记数成正比。如所述,我们强烈建议使用max_tokens参数,以避免费用超出预期。
  • 另外在openai库的方法中使用不同的参数,因为如果使用temperature、top_p和max_tokens等参数,那么即使使用相同的提示词,你也可能得到截然不同的结果。

 

二. 设计有效的提示词

很多任务可以通过提示词来完成,包括摘要、文本分类、情感分析和问题回答。在所有这些任务中,我们通常需要在提示词中定义三大要素:角色、上下文和任务,如下图。

在这里插入图片描述

这三大要素并非都是必需的,它们的顺序也不是固定的。不过,如果能够构造好提示词的结构并明确定义这三大要素,你就能够得到不错的结果。

请注意,即使使用了这三大要素,对于复杂的任务,你也可能需要采用更高级的技巧,如零样本学习、少样本学习和微调。

 

1.上下文

提示词的第一大要素是上下文。在为LLM编写输入文本时,必须尽可能详细地描述上下文。为了说明这一点,假设我们想使用GPT-4创建一个应用程序,用于推荐午餐的主菜。

1.1. 更多细节的上下文

接下来,我们将比较在两个上下文中获得的结果。第一个上下文提供较少的细节,第二个上下文则提供较多的细节。假设使用以下较短的提示词:

chat_completion("Give me a suggestion for the main course for
today's lunch.")

我们得到以下输出消息:

Grilled chicken with roasted vegetables and quinoa.

现在,在提示词中加入更多上下文信息:

prompt = """
Context: I do 2 hours of sport a day. I am vegetarian, and I don't
like green vegetables. I am conscientious about eating healthily.
Task: Give me a suggestion for a main course for today's lunch."""
chat_completion(prompt)

我们得到以下输出消息:


For today's lunch, you can try a Chickpea and Sweet Potato Curry
served with Quinoa. This dish is packed with protein, fiber, and
essential nutrients, while also being vegetarian and not relying
on green vegetables. The curry can be made with chickpeas, sweet
potatoes, tomatoes, onions, garlic, and a blend of spices like
turmeric, cumin, and coriander. Cook the quinoa separately and
serve it alongside the curry for a delicious and healthy meal.

在第二个例子中,由于提示词含有更多上下文,因此模型给出的句子更详细:它推荐了一道富含蛋白质的健康素食菜肴。

 

1.2. 让GPT改进上下文

为了找到可改进之处,你可以请GPT-4提些意见。

在下面的示例中,我们修改提示词,不再要求模型回答问题,而是指示它以增强补全效果为目标来向我们提出关于上下文的问题。使用以下提示词:

prompt = """
Context: I do 2 hours of sport a day. I am vegetarian and I don't
like green vegetables. I am very careful to eat healthily.
Task: Give me a suggestion for a main course for today's lunch? Do
not perform the requested task! Instead, can you ask me questions
about the context so that when I answer, you can perform the
requested task more efficiently?
"""
chat_completion(prompt)

GPT-4向我们提出以下问题:


Sure, I have a few questions to help me suggest a suitable main
course for you:
1. What type of sport do you do, and do you have any specific nutritional requirements related to it?
2. Are there any other vegetables or ingredients that you particularlyenjoy or dislike?
3. Do you prefer a hot or cold meal for lunch?
4. Are you open to trying new recipes or do you prefer sticking tofamiliar dishes?

模型提出的问题通常与提示词相关。根据这些问题,我们可以在提示词中更新上下文。

总之,我们建议你在构建提示词时经常使用这个技巧。

 

2.任务

任务定义了你对GPT-4模型的用法,并且应该明确且具体。你应该提供足够的任务信息,并在提示词中使用合适的短语来引导模型给出你所期望的结果。

2.1. 提供足够的任务信息

在前面的例子中,任务是让模型为午餐推荐一道主菜。我们可以为该任务添加细节,以说明我们想要的内容。在本例中,我们要求模型添加一张表格,列出每种食材所需的克数

prompt = """
Context: I do 2 hours of sport a day. I am vegetarian, and I don't
like green vegetables. I am conscientious about eating healthily.
Task: Give me a suggestion for a main course for today's lunch. With
this suggestion, I also want a table with two columns where each row
contains an ingredient from the main course. The first column of the
table is the name of the ingredient. The second column of the table
is the number of grams of that ingredient needed for one person. Do
not give the recipe for preparing the main course.
"""
chat_completion(prompt)

模型给出以下输出结果:

Suggestion for today's lunch: Stuffed Bell Peppers with Quinoa and
Black Beans
| Ingredient          | Grams per person |
|---------------------|------------------|
| Bell peppers        | 200g             |
| Quinoa              | 60g              |
| Black beans         | 75g              |
| Onion               | 50g              |
| Garlic              | 5g               |
| Olive oil           | 10g              |
| Tomato sauce        | 50g              |
| Cumin               | 1g               |
| Chili powder        | 1g               |
| Salt                | 1g               |
| Pepper              | 1g               |
| Shredded cheese     | 30g              |

 

2.2. OpenAI的任务示例

OpenAI API示例页面列出了48个任务示例。这些示例展示了GPT模型可以执行的任务,其中每个示例都配有相关的提示词和演示。见官网: Prompt examples

虽然这些示例使用了GPT-3模型和Completion端点,但对于ChatCompletion端点来说,原理是相同的,并且这些示例很好地说明了如何给OpenAI模型指派任务。

语法纠正

纠正句子中的语病,并将其修改为标准的英语句子。

from openai import OpenAI
client = OpenAI()response = client.chat.completions.create(model="gpt-3.5-turbo",messages=[{"role": "system","content": "You will be provided with statements, and your task is to convert them to standard English."},{"role": "user","content": "She no went to the market."}],temperature=0.7,max_tokens=64,top_p=1
)

 

总结

给二年级学生概括一下

from openai import OpenAI
client = OpenAI()response = client.chat.completions.create(model="gpt-3.5-turbo",messages=[{"role": "system","content": "Summarize content you are provided with for a second-grade student."},{"role": "user","content": "Jupiter is the fifth planet from the Sun and the largest in the Solar System. It is a gas giant with a mass one-thousandth that of the Sun, but two-and-a-half times that of all the other planets in the Solar System combined. Jupiter is one of the brightest objects visible to the naked eye in the night sky, and has been known to ancient civilizations since before recorded history. It is named after the Roman god Jupiter.[19] When viewed from Earth, Jupiter can be bright enough for its reflected light to cast visible shadows,[20] and is on average the third-brightest natural object in the night sky after the Moon and Venus."}],temperature=0.7,max_tokens=64,top_p=1
)

 

TL;DR概要

TL;DR是“too long; didn’t read”的首字母缩写,意为“太长了,没读”。有人发现,只需在文本末尾添加Tl;dr,即可请求模型对文本进行总结。

提示词示例如下。

A neutron star [...] atomic nuclei. Tl;dr

 

Python转自然语言

用自然语言解释一段Python代码。

from openai import OpenAI
client = OpenAI()response = client.chat.completions.create(model="gpt-4",messages=[{"role": "system","content": "You will be provided with a piece of code, and your task is to explain it in a concise way."},{"role": "user","content": "class Log:\n        def __init__(self, path):\n            dirname = os.path.dirname(path)\n            os.makedirs(dirname, exist_ok=True)\n            f = open(path, \"a+\")\n    \n            # Check that the file is newline-terminated\n            size = os.path.getsize(path)\n            if size > 0:\n                f.seek(size - 1)\n                end = f.read(1)\n                if end != \"\\n\":\n                    f.write(\"\\n\")\n            self.f = f\n            self.path = path\n    \n        def log(self, event):\n            event[\"_event_id\"] = str(uuid.uuid4())\n            json.dump(event, self.f)\n            self.f.write(\"\\n\")\n    \n        def state(self):\n            state = {\"complete\": set(), \"last\": None}\n            for line in open(self.path):\n                event = json.loads(line)\n                if event[\"type\"] == \"submit\" and event[\"success\"]:\n                    state[\"complete\"].add(event[\"id\"])\n                    state[\"last\"] = event\n            return state"}],temperature=0.7,max_tokens=64,top_p=1
)

 

计算时间复杂度

计算一个函数的时间复杂度。

from openai import OpenAI
client = OpenAI()response = client.chat.completions.create(model="gpt-3.5-turbo",messages=[{"role": "system","content": "You will be provided with Python code, and your task is to calculate its time complexity."},{"role": "user","content": "def foo(n, k):\n        accum = 0\n        for i in range(n):\n            for l in range(k):\n                accum += i\n        return accum"}],temperature=0.7,max_tokens=64,top_p=1
)

 

修复Python bug

修复含有bug的Python代码。

from openai import OpenAI
client = OpenAI()response = client.chat.completions.create(model="gpt-4",messages=[{"role": "system","content": "You will be provided with a piece of Python code, and your task is to find and fix bugs in it."},{"role": "user","content": "import Random\n    a = random.randint(1,12)\n    b = random.randint(1,12)\n    for i in range(10):\n        question = \"What is \"+a+\" x \"+b+\"? \"\n        answer = input(question)\n        if answer = a*b\n            print (Well done!)\n        else:\n            print(\"No.\")"}],temperature=0.7,max_tokens=64,top_p=1
)

SQL请求
构建简单的SQL查询。提示词示例如下。

Create a SQL request to find all users who live in California and
have over 1000 credits.

 

产生python函数
from openai import OpenAI
client = OpenAI()response = client.chat.completions.create(model="gpt-4",messages=[{"role": "user","content": "Write a Python function that takes as input a file path to an image, loads the image into memory as a numpy array, then crops the rows and columns around the perimeter if they are darker than a threshold value. Use the mean value of rows and columns to decide if they should be marked for deletion."}],temperature=0.7,max_tokens=64,top_p=1
)

 

3.角色

在撰写提示词时,影响模型的最后一种方式是赋予其一个角色。当然,角色和上下文可以独立使用,但同时使用可以加强对模型输出的控制

在以下示例中,我们赋予模型运动营养专家的角色:

prompt = """
# Role: You are a nutritionist designing healthy diets for high-performance athletes. You take into account the nutrition needed for a good recovery.
# Context: I do 2 hours of sport a day. I am vegetarian, and I don't like green vegetables. I am conscientious about eating healthily.
# Task: Based on your expertise defined in your role, give me a suggestion for a main course for today's lunch. With this suggestion, I also want a table with two columns where each row in the table contains an ingredient from the main course. The first column of the table is the name of the ingredient. The second column of the table is the number of grams of that ingredient needed for one person. Do not give the recipe for preparing the main course.
"""
chat_completion(prompt)

 
我们得到以下结果:

Main Course Suggestion: Quinoa and Chickpea Stuffed Bell Peppers
Ingredient Table:
| Ingredient          | Grams per Person |
|---------------------|------------------|
| Quinoa              | 60               |
| Chickpeas           | 100              |
| Bell Peppers        | 200              |
| Olive Oil           | 10               |
| Onion               | 50               |
| Garlic              | 5                |
| Tomato              | 100              |
| Feta Cheese         | 30               |
| Kalamata Olives     | 20               |
| Fresh Basil         | 5                |
| Salt                | 2                |
| Black Pepper        | 1                |

 
提示词可用于调整像GPT模型这样的LLM的概率分布集。它们可以被视为模型指南,引导模型生成特定类型的结果。

 

注意,上述提到的上下文、任务、角色的提示词格式,只是一种方法,你完全可以创建不明确定义这些要素的提示词。不要受限于“上下文−任务−角色”框架,而应将其视为帮助你有效设计提示词的工具。

 

这篇关于【大模型应用开发极简入门】提示工程一:1. 通过context、task、role文本结构设计有效的提示词、 2. OpenAI的提示词任务示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1056076

相关文章

MybatisX快速生成增删改查的方法示例

《MybatisX快速生成增删改查的方法示例》MybatisX是基于IDEA的MyBatis/MyBatis-Plus开发插件,本文主要介绍了MybatisX快速生成增删改查的方法示例,文中通过示例代... 目录1 安装2 基本功能2.1 XML跳转2.2 代码生成2.2.1 生成.xml中的sql语句头2

JavaScript实战:智能密码生成器开发指南

本文通过JavaScript实战开发智能密码生成器,详解如何运用crypto.getRandomValues实现加密级随机密码生成,包含多字符组合、安全强度可视化、易混淆字符排除等企业级功能。学习密码强度检测算法与信息熵计算原理,获取可直接嵌入项目的完整代码,提升Web应用的安全开发能力 目录

MySQL数据库实现批量表分区完整示例

《MySQL数据库实现批量表分区完整示例》通俗地讲表分区是将一大表,根据条件分割成若干个小表,:本文主要介绍MySQL数据库实现批量表分区的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考... 目录一、表分区条件二、常规表和分区表的区别三、表分区的创建四、将既有表转换分区表脚本五、批量转换表为分区

PyTorch中cdist和sum函数使用示例详解

《PyTorch中cdist和sum函数使用示例详解》torch.cdist是PyTorch中用于计算**两个张量之间的成对距离(pairwisedistance)**的函数,常用于点云处理、图神经网... 目录基本语法输出示例1. 简单的 2D 欧几里得距离2. 批量形式(3D Tensor)3. 使用不

Python模拟串口通信的示例详解

《Python模拟串口通信的示例详解》pySerial是Python中用于操作串口的第三方模块,它支持Windows、Linux、OSX、BSD等多个平台,下面我们就来看看Python如何使用pySe... 目录1.win 下载虚www.chinasem.cn拟串口2、确定串口号3、配置串口4、串口通信示例5

电脑系统Hosts文件原理和应用分享

《电脑系统Hosts文件原理和应用分享》Hosts是一个没有扩展名的系统文件,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Hosts文件中寻找对应的IP地址,一旦找到,系统会立即打开对应... Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与其对应

Kali Linux安装实现教程(亲测有效)

《KaliLinux安装实现教程(亲测有效)》:本文主要介绍KaliLinux安装实现教程(亲测有效),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、下载二、安装总结一、下载1、点http://www.chinasem.cn击链接 Get Kali | Kal

电脑提示Winmm.dll缺失怎么办? Winmm.dll文件丢失的多种修复技巧

《电脑提示Winmm.dll缺失怎么办?Winmm.dll文件丢失的多种修复技巧》有时电脑会出现无法启动程序,因为计算机中丢失winmm.dll的情况,其实,winmm.dll丢失是一个比较常见的问... 在大部分情况下出现我们运行或安装软件,游戏出现提示丢失某些DLL文件或OCX文件的原因可能是原始安装包

C#使用MQTTnet实现服务端与客户端的通讯的示例

《C#使用MQTTnet实现服务端与客户端的通讯的示例》本文主要介绍了C#使用MQTTnet实现服务端与客户端的通讯的示例,包括协议特性、连接管理、QoS机制和安全策略,具有一定的参考价值,感兴趣的可... 目录一、MQTT 协议简介二、MQTT 协议核心特性三、MQTTNET 库的核心功能四、服务端(BR

CSS 样式表的四种应用方式及css注释的应用小结

《CSS样式表的四种应用方式及css注释的应用小结》:本文主要介绍了CSS样式表的四种应用方式及css注释的应用小结,本文通过实例代码给大家介绍的非常详细,详细内容请阅读本文,希望能对你有所帮助... 一、外部 css(推荐方式)定义:将 CSS 代码保存为独立的 .css 文件,通过 <link> 标签