From 45edfa360c76733656ed117e23b2be39697e4589 Mon Sep 17 00:00:00 2001 From: Logan Zou <74288839+nowadays0421@users.noreply.github.com> Date: Fri, 28 Apr 2023 22:41:45 +0800 Subject: [PATCH] upload files --- content/1. 简介.md | 21 + content/2. 提示原则 Guidelines.ipynb | 1162 ++++++++++++++++++++++++++ content/3. 迭代优化 Itrative.ipynb | 837 +++++++++++++++++++ content/7.文本扩展 Expand.ipynb | 441 ++++++++++ 4 files changed, 2461 insertions(+) create mode 100644 content/1. 简介.md create mode 100644 content/2. 提示原则 Guidelines.ipynb create mode 100644 content/3. 迭代优化 Itrative.ipynb create mode 100644 content/7.文本扩展 Expand.ipynb diff --git a/content/1. 简介.md b/content/1. 简介.md new file mode 100644 index 0000000..3856dfa --- /dev/null +++ b/content/1. 简介.md @@ -0,0 +1,21 @@ +# 简介 + +**作者 吴恩达教授** + +欢迎来到本课程,我们将为开发人员介绍 ChatGPT 提示工程。本课程由 Isa Fulford 教授和我一起授课。Isa Fulford 是 OpenAI 的技术团队成员,曾开发过受欢迎的 ChatGPT 检索插件,并且在教授人们如何在产品中使用 LLM 或 LLM 技术方面做出了很大贡献。她还参与编写了教授人们使用 Prompt 的 OpenAI cookbook。 + +互联网上有很多有关提示的材料,例如《30 prompts everyone has to know》之类的文章。这些文章主要集中在 ChatGPT Web 用户界面上,许多人在使用它执行特定的、通常是一次性的任务。但是,我认为 LLM 或大型语言模型作为开发人员的更强大功能是使用 API 调用到 LLM,以快速构建软件应用程序。我认为这方面还没有得到充分的重视。实际上,我们在 DeepLearning.AI 的姊妹公司 AI Fund 的团队一直在与许多初创公司合作,将这些技术应用于许多不同的应用程序上。看到 LLM API 能够让开发人员非常快速地构建应用程序,这真是令人兴奋。 + +在本课程中,我们将与您分享一些可能性以及如何实现它们的最佳实践。 + +随着大型语言模型(LLM)的发展,LLM 大致可以分为两种类型,即基础LLM和指令微调LLM。基础LLM是基于文本训练数据,训练出预测下一个单词能力的模型,其通常是在互联网和其他来源的大量数据上训练的。例如,如果你以“从前有一只独角兽”作为提示,基础LLM可能会继续预测“生活在一个与所有独角兽朋友的神奇森林中”。但是,如果你以“法国的首都是什么”为提示,则基础LLM可能会根据互联网上的文章,将答案预测为“法国最大的城市是什么?法国的人口是多少?”,因为互联网上的文章很可能是有关法国国家的问答题目列表。 + +许多 LLMs 的研究和实践的动力正在指令调整的 LLMs 上。指令调整的 LLMs 已经被训练来遵循指令。因此,如果你问它,“法国的首都是什么?”,它更有可能输出“法国的首都是巴黎”。指令调整的 LLMs 的训练通常是从已经训练好的基本 LLMs 开始,该模型已经在大量文本数据上进行了训练。然后,使用输入是指令、输出是其应该返回的结果的数据集来对其进行微调,要求它遵循这些指令。然后通常使用一种称为 RLHF(reinforcement learning from human feedback,人类反馈强化学习)的技术进行进一步改进,使系统更能够有帮助地遵循指令。 + +因为指令调整的 LLMs 已经被训练成有益、诚实和无害的,所以与基础LLMs相比,它们更不可能输出有问题的文本,如有害输出。许多实际使用场景已经转向指令调整的LLMs。您在互联网上找到的一些最佳实践可能更适用于基础LLMs,但对于今天的大多数实际应用,我们建议将注意力集中在指令调整的LLMs上,这些LLMs更容易使用,而且由于OpenAI和其他LLM公司的工作,它们变得更加安全和更加协调。 + +因此,本课程将重点介绍针对指令调整 LLM 的最佳实践,这是我们建议您用于大多数应用程序的。在继续之前,我想感谢 OpenAI 和 DeepLearning.ai 团队为 Izzy 和我所提供的材料作出的贡献。我非常感激 OpenAI 的 Andrew Main、Joe Palermo、Boris Power、Ted Sanders 和 Lillian Weng,他们参与了我们的头脑风暴材料的制定和审核,为这个短期课程编制了课程大纲。我也感激 Deep Learning 方面的 Geoff Ladwig、Eddy Shyu 和 Tommy Nelson 的工作。 + +当您使用指令调整 LLM 时,请类似于考虑向另一个人提供指令,假设它是一个聪明但不知道您任务的具体细节的人。当 LLM 无法正常工作时,有时是因为指令不够清晰。例如,如果您说“请为我写一些关于阿兰·图灵的东西”,清楚表明您希望文本专注于他的科学工作、个人生活、历史角色或其他方面可能会更有帮助。更多的,您还可以指定文本采取像专业记者写作的语调,或者更像是您向朋友写的随笔。 + +当然,如果你想象一下让一位新毕业的大学生为你完成这个任务,你甚至可以提前指定他们应该阅读哪些文本片段来写关于 Alan Turing的文本,那么这能够帮助这位新毕业的大学生更好地成功完成这项任务。下一章你会看到如何让提示清晰明确,创建提示的一个重要原则,你还会从提示的第二个原则中学到给LLM时间去思考。 \ No newline at end of file diff --git a/content/2. 提示原则 Guidelines.ipynb b/content/2. 提示原则 Guidelines.ipynb new file mode 100644 index 0000000..dab5071 --- /dev/null +++ b/content/2. 提示原则 Guidelines.ipynb @@ -0,0 +1,1162 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 第二章 编写 Prompt 的原则\n", + "\n", + " 本章的主要内容为编写 Prompt 的原则,在本章中,我们将给出两个编写 Prompt 的原则与一些相关的策略,你将练习基于这两个原则来编写有效的 Prompt,从而便捷而有效地使用 LLM。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 一、环境配置" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "本教程使用 OpenAI 所开放的 ChatGPT API,因此你需要首先拥有一个 ChatGPT 的 API_KEY(也可以直接访问官方网址在线测试),然后需要安装 openai 的第三方库" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "首先需要安装所需第三方库:\n", + "\n", + "openai:\n", + "\n", + "```bash\n", + "pip install openai\n", + "```\n", + "\n", + "dotenv:\n", + "\n", + "```bash\n", + "pip install -U python-dotenv\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "# 将自己的 API-KEY 导入系统环境变量\n", + "!export OPENAI_API_KEY='api-key'" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import openai\n", + "import os\n", + "from dotenv import load_dotenv, find_dotenv\n", + "# 导入第三方库\n", + "\n", + "_ = load_dotenv(find_dotenv())\n", + "# 读取系统中的环境变量\n", + "\n", + "openai.api_key = os.getenv('OPENAI_API_KEY')\n", + "# 设置 API_KEY" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "我们将在后续课程中深入探究 OpenAI 提供的 ChatCompletion API 的使用方法,在此处,我们先将它封装成一个函数,你无需知道其内部机理,仅需知道调用该函数输入 Prompt 其将会给出对应的 Completion 即可。" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# 一个封装 OpenAI 接口的函数,参数为 Prompt,返回对应结果\n", + "def get_completion(prompt, model=\"gpt-3.5-turbo\"):\n", + " '''\n", + " prompt: 对应的提示\n", + " model: 调用的模型,默认为 gpt-3.5-turbo(ChatGPT),有内测资格的用户可以选择 gpt-4\n", + " '''\n", + " messages = [{\"role\": \"user\", \"content\": prompt}]\n", + " response = openai.ChatCompletion.create(\n", + " model=model,\n", + " messages=messages,\n", + " temperature=0, # 模型输出的温度系数,控制输出的随机程度\n", + " )\n", + " # 调用 OpenAI 的 ChatCompletion 接口\n", + " return response.choices[0].message[\"content\"]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 二、两个基本原则" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 原则一:编写清晰、具体的指令\n", + "\n", + "你应该通过提供尽可能清晰和具体的指令来表达您希望模型执行的操作。这将引导模型给出正确的输出,并减少你得到无关或不正确响应的可能。编写清晰的指令不意味着简短的指令,因为在许多情况下,更长的提示实际上更清晰且提供了更多上下文,这实际上可能导致更详细更相关的输出。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**策略一:使用分隔符清晰地表示输入的不同部分**,分隔符可以是:```,\"\",<>,\\,<\\tag>等\n", + "\n", + "你可以使用任何明显的标点符号将特定的文本部分与提示的其余部分分开。这可以是任何可以使模型明确知道这是一个单独部分的标记。使用分隔符是一种可以避免提示注入的有用技术。提示注入是指如果用户将某些输入添加到提示中,则可能会向模型提供与您想要执行的操作相冲突的指令,从而使其遵循冲突的指令而不是执行您想要的操作。即,输入里面可能包含其他指令,会覆盖掉你的指令。对此,使用分隔符是一个不错的策略。\n", + "\n", + "以下是一个例子,我们给出一段话并要求 GPT 进行总结,在该示例中我们使用 ``` 来作为分隔符\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Clear and specific instructions should be provided to guide a model towards the desired output, and longer prompts can provide more clarity and context for the model, leading to more detailed and relevant outputs.\n" + ] + } + ], + "source": [ + "# 中文版见下一个 cell\n", + "text = f\"\"\"\n", + "You should express what you want a model to do by \\ \n", + "providing instructions that are as clear and \\ \n", + "specific as you can possibly make them. \\ \n", + "This will guide the model towards the desired output, \\ \n", + "and reduce the chances of receiving irrelevant \\ \n", + "or incorrect responses. Don't confuse writing a \\ \n", + "clear prompt with writing a short prompt. \\ \n", + "In many cases, longer prompts provide more clarity \\ \n", + "and context for the model, which can lead to \\ \n", + "more detailed and relevant outputs.\n", + "\"\"\"\n", + "prompt = f\"\"\"\n", + "Summarize the text delimited by triple backticks \\ \n", + "into a single sentence.\n", + "```{text}```\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "提供清晰具体的指示,避免无关或不正确响应,不要混淆写清晰和写简短,更长的提示可以提供更多清晰度和上下文信息,导致更详细和相关的输出。\n" + ] + } + ], + "source": [ + "text = f\"\"\"\n", + "你应该提供尽可能清晰、具体的指示,以表达你希望模型执行的任务。\\\n", + "这将引导模型朝向所需的输出,并降低收到无关或不正确响应的可能性。\\\n", + "不要将写清晰的提示与写简短的提示混淆。\\\n", + "在许多情况下,更长的提示可以为模型提供更多的清晰度和上下文信息,从而导致更详细和相关的输出。\n", + "\"\"\"\n", + "# 需要总结的文本内容\n", + "prompt = f\"\"\"\n", + "把用三个反引号括起来的文本总结成一句话。\n", + "```{text}```\n", + "\"\"\"\n", + "# 指令内容,使用 ``` 来分隔指令和待总结的内容\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**策略二:要求一个结构化的输出**,可以是 Json、HTML 等格式\n", + "\n", + "第二个策略是要求生成一个结构化的输出,这可以使模型的输出更容易被我们解析,例如,你可以在 Python 中将其读入字典或列表中。。\n", + "\n", + "在以下示例中,我们要求 GPT 生成三本书的标题、作者和类别,并要求 GPT 以 Json 的格式返回给我们,为便于解析,我们指定了 Json 的键。" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[\n", + " {\n", + " \"book_id\": 1,\n", + " \"title\": \"The Lost City of Zorath\",\n", + " \"author\": \"Aria Blackwood\",\n", + " \"genre\": \"Fantasy\"\n", + " },\n", + " {\n", + " \"book_id\": 2,\n", + " \"title\": \"The Last Survivors\",\n", + " \"author\": \"Ethan Stone\",\n", + " \"genre\": \"Science Fiction\"\n", + " },\n", + " {\n", + " \"book_id\": 3,\n", + " \"title\": \"The Secret Life of Bees\",\n", + " \"author\": \"Lila Rose\",\n", + " \"genre\": \"Romance\"\n", + " }\n", + "]\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "Generate a list of three made-up book titles along \\ \n", + "with their authors and genres. \n", + "Provide them in JSON format with the following keys: \n", + "book_id, title, author, genre.\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\n", + " \"books\": [\n", + " {\n", + " \"book_id\": 1,\n", + " \"title\": \"The Shadow of the Wind\",\n", + " \"author\": \"Carlos Ruiz Zafón\",\n", + " \"genre\": \"Mystery\"\n", + " },\n", + " {\n", + " \"book_id\": 2,\n", + " \"title\": \"The Name of the Wind\",\n", + " \"author\": \"Patrick Rothfuss\",\n", + " \"genre\": \"Fantasy\"\n", + " },\n", + " {\n", + " \"book_id\": 3,\n", + " \"title\": \"The Hitchhiker's Guide to the Galaxy\",\n", + " \"author\": \"Douglas Adams\",\n", + " \"genre\": \"Science Fiction\"\n", + " }\n", + " ]\n", + "}\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "请生成包括书名、作者和类别的三本虚构书籍清单,\\\n", + "并以 JSON 格式提供,其中包含以下键:book_id、title、author、genre。\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**策略三:要求模型检查是否满足条件**\n", + "\n", + "如果任务做出的假设不一定满足,我们可以告诉模型先检查这些假设,如果不满足,指示并停止执行。你还可以考虑潜在的边缘情况以及模型应该如何处理它们,以避免意外的错误或结果。\n", + "\n", + "在如下示例中,我们将分别给模型两段文本,分别是制作茶的步骤以及一段没有明确步骤的文本。我们将要求模型判断其是否包含一系列指令,如果包含则按照给定格式重新编写指令,不包含则回答未提供步骤。" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Completion for Text 1:\n", + "Step 1 - Get some water boiling.\n", + "Step 2 - Grab a cup and put a tea bag in it.\n", + "Step 3 - Once the water is hot enough, pour it over the tea bag.\n", + "Step 4 - Let it sit for a bit so the tea can steep.\n", + "Step 5 - After a few minutes, take out the tea bag.\n", + "Step 6 - Add some sugar or milk to taste.\n", + "Step 7 - Enjoy your delicious cup of tea!\n", + "\n", + "\n" + ] + } + ], + "source": [ + "text_1 = f\"\"\"\n", + "Making a cup of tea is easy! First, you need to get some \\ \n", + "water boiling. While that's happening, \\ \n", + "grab a cup and put a tea bag in it. Once the water is \\ \n", + "hot enough, just pour it over the tea bag. \\ \n", + "Let it sit for a bit so the tea can steep. After a \\ \n", + "few minutes, take out the tea bag. If you \\ \n", + "like, you can add some sugar or milk to taste. \\ \n", + "And that's it! You've got yourself a delicious \\ \n", + "cup of tea to enjoy.\n", + "\"\"\"\n", + "prompt = f\"\"\"\n", + "You will be provided with text delimited by triple quotes. \n", + "If it contains a sequence of instructions, \\ \n", + "re-write those instructions in the following format:\n", + "\n", + "Step 1 - ...\n", + "Step 2 - …\n", + "…\n", + "Step N - …\n", + "\n", + "If the text does not contain a sequence of instructions, \\ \n", + "then simply write \\\"No steps provided.\\\"\n", + "\n", + "\\\"\\\"\\\"{text_1}\\\"\\\"\\\"\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(\"Completion for Text 1:\")\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Completion for Text 2:\n", + "No steps provided.\n" + ] + } + ], + "source": [ + "text_2 = f\"\"\"\n", + "The sun is shining brightly today, and the birds are \\\n", + "singing. It's a beautiful day to go for a \\ \n", + "walk in the park. The flowers are blooming, and the \\ \n", + "trees are swaying gently in the breeze. People \\ \n", + "are out and about, enjoying the lovely weather. \\ \n", + "Some are having picnics, while others are playing \\ \n", + "games or simply relaxing on the grass. It's a \\ \n", + "perfect day to spend time outdoors and appreciate the \\ \n", + "beauty of nature.\n", + "\"\"\"\n", + "prompt = f\"\"\"You will be provided with text delimited by triple quotes. \n", + "If it contains a sequence of instructions, \\ \n", + "re-write those instructions in the following format:\n", + "Step 1 - ...\n", + "Step 2 - …\n", + "…\n", + "Step N - …\n", + "\n", + "If the text does not contain a sequence of instructions, \\ \n", + "then simply write \\\"No steps provided.\\\"\n", + "\n", + "\\\"\\\"\\\"{text_2}\\\"\\\"\\\"\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(\"Completion for Text 2:\")\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Text 1 的总结:\n", + "第一步 - 把水烧开。\n", + "第二步 - 拿一个杯子并把茶包放进去。\n", + "第三步 - 把烧开的水倒在茶包上。\n", + "第四步 - 等待几分钟,让茶叶浸泡。\n", + "第五步 - 取出茶包。\n", + "第六步 - 如果你愿意,可以加一些糖或牛奶调味。\n", + "第七步 - 就这样,你可以享受一杯美味的茶了。\n" + ] + } + ], + "source": [ + "# 有步骤的文本\n", + "text_1 = f\"\"\"\n", + "泡一杯茶很容易。首先,需要把水烧开。\\\n", + "在等待期间,拿一个杯子并把茶包放进去。\\\n", + "一旦水足够热,就把它倒在茶包上。\\\n", + "等待一会儿,让茶叶浸泡。几分钟后,取出茶包。\\\n", + "如果你愿意,可以加一些糖或牛奶调味。\\\n", + "就这样,你可以享受一杯美味的茶了。\n", + "\"\"\"\n", + "prompt = f\"\"\"\n", + "您将获得由三个引号括起来的文本。\\\n", + "如果它包含一系列的指令,则需要按照以下格式重新编写这些指令:\n", + "\n", + "第一步 - ...\n", + "第二步 - …\n", + "…\n", + "第N步 - …\n", + "\n", + "如果文本中不包含一系列的指令,则直接写“未提供步骤”。\"\n", + "\\\"\\\"\\\"{text_1}\\\"\\\"\\\"\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(\"Text 1 的总结:\")\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Text 2 的总结:\n", + "未提供步骤。\n" + ] + } + ], + "source": [ + "# 无步骤的文本\n", + "text_2 = f\"\"\"\n", + "今天阳光明媚,鸟儿在歌唱。\\\n", + "这是一个去公园散步的美好日子。\\\n", + "鲜花盛开,树枝在微风中轻轻摇曳。\\\n", + "人们外出享受着这美好的天气,有些人在野餐,有些人在玩游戏或者在草地上放松。\\\n", + "这是一个完美的日子,可以在户外度过并欣赏大自然的美景。\n", + "\"\"\"\n", + "prompt = f\"\"\"\n", + "您将获得由三个引号括起来的文本。\\\n", + "如果它包含一系列的指令,则需要按照以下格式重新编写这些指令:\n", + "\n", + "第一步 - ...\n", + "第二步 - …\n", + "…\n", + "第N步 - …\n", + "\n", + "如果文本中不包含一系列的指令,则直接写“未提供步骤”。\"\n", + "\\\"\\\"\\\"{text_2}\\\"\\\"\\\"\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(\"Text 2 的总结:\")\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**策略四:提供少量示例**\n", + "\n", + "即在要求模型执行实际任务之前,提供给它少量成功执行任务的示例。\n", + "\n", + "例如,在以下的示例中,我们告诉模型其任务是以一致的风格回答问题,并先给它一个孩子和一个祖父之间的对话的例子。孩子说,“教我耐心”,祖父用这些隐喻回答。因此,由于我们已经告诉模型要以一致的语气回答,现在我们说“教我韧性”,由于模型已经有了这个少样本示例,它将以类似的语气回答下一个任务。" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + ": Resilience is like a tree that bends with the wind but never breaks. It is the ability to bounce back from adversity and keep moving forward, even when things get tough. Just like a tree that grows stronger with each storm it weathers, resilience is a quality that can be developed and strengthened over time.\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "Your task is to answer in a consistent style.\n", + "\n", + ": Teach me about patience.\n", + "\n", + ": The river that carves the deepest \\ \n", + "valley flows from a modest spring; the \\ \n", + "grandest symphony originates from a single note; \\ \n", + "the most intricate tapestry begins with a solitary thread.\n", + "\n", + ": Teach me about resilience.\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "<祖父母>: 韧性就像是一棵树,它需要经历风吹雨打、寒冬酷暑,才能成长得更加坚强。在生活中,我们也需要经历各种挫折和困难,才能锻炼出韧性。记住,不要轻易放弃,坚持下去,你会发现自己变得更加坚强。\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "你的任务是以一致的风格回答问题。\n", + "\n", + "<孩子>: 教我耐心。\n", + "\n", + "<祖父母>: 挖出最深峡谷的河流源于一处不起眼的泉眼;最宏伟的交响乐从单一的音符开始;最复杂的挂毯以一根孤独的线开始编织。\n", + "\n", + "<孩子>: 教我韧性。\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 原则二:给模型时间去思考\n", + "\n", + "如果模型匆忙地得出了错误的结论,您应该尝试重新构思查询,请求模型在提供最终答案之前进行一系列相关的推理。换句话说,如果您给模型一个在短时间或用少量文字无法完成的任务,它可能会猜测错误。这种情况对人来说也是一样的。如果您让某人在没有时间计算出答案的情况下完成复杂的数学问题,他们也可能会犯错误。因此,在这些情况下,您可以指示模型花更多时间思考问题,这意味着它在任务上花费了更多的计算资源。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**策略一:指定完成任务所需的步骤**\n", + "\n", + "接下来我们将通过给定一个复杂任务,给出完成该任务的一系列步骤,来展示这一策略的效果" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "首先我们描述了杰克和吉尔的故事,并给出一个指令。该指令是执行以下操作。首先,用一句话概括三个反引号限定的文本。第二,将摘要翻译成法语。第三,在法语摘要中列出每个名称。第四,输出包含以下键的 JSON 对象:法语摘要和名称数。然后我们要用换行符分隔答案。" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Completion for prompt 1:\n", + "Two siblings, Jack and Jill, go on a quest to fetch water from a well on a hilltop, but misfortune strikes and they both tumble down the hill, returning home slightly battered but with their adventurous spirits undimmed.\n", + "\n", + "Deux frères et sœurs, Jack et Jill, partent en quête d'eau d'un puits sur une colline, mais un malheur frappe et ils tombent tous les deux de la colline, rentrant chez eux légèrement meurtris mais avec leurs esprits aventureux intacts. \n", + "Noms: Jack, Jill.\n", + "\n", + "{\n", + " \"french_summary\": \"Deux frères et sœurs, Jack et Jill, partent en quête d'eau d'un puits sur une colline, mais un malheur frappe et ils tombent tous les deux de la colline, rentrant chez eux légèrement meurtris mais avec leurs esprits aventureux intacts.\",\n", + " \"num_names\": 2\n", + "}\n" + ] + } + ], + "source": [ + "text = f\"\"\"\n", + "In a charming village, siblings Jack and Jill set out on \\ \n", + "a quest to fetch water from a hilltop \\ \n", + "well. As they climbed, singing joyfully, misfortune \\ \n", + "struck—Jack tripped on a stone and tumbled \\ \n", + "down the hill, with Jill following suit. \\ \n", + "Though slightly battered, the pair returned home to \\ \n", + "comforting embraces. Despite the mishap, \\ \n", + "their adventurous spirits remained undimmed, and they \\ \n", + "continued exploring with delight.\n", + "\"\"\"\n", + "# example 1\n", + "prompt_1 = f\"\"\"\n", + "Perform the following actions: \n", + "1 - Summarize the following text delimited by triple \\\n", + "backticks with 1 sentence.\n", + "2 - Translate the summary into French.\n", + "3 - List each name in the French summary.\n", + "4 - Output a json object that contains the following \\\n", + "keys: french_summary, num_names.\n", + "\n", + "Separate your answers with line breaks.\n", + "\n", + "Text:\n", + "```{text}```\n", + "\"\"\"\n", + "response = get_completion(prompt_1)\n", + "print(\"Completion for prompt 1:\")\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "prompt 1:\n", + "1-兄妹在山顶井里打水时发生意外,但仍然保持冒险精神。\n", + "2-Dans un charmant village, les frère et sœur Jack et Jill partent chercher de l'eau dans un puits au sommet de la montagne. Malheureusement, Jack trébuche sur une pierre et tombe de la montagne, suivi de près par Jill. Bien qu'ils soient légèrement blessés, ils retournent chez eux chaleureusement. Malgré cet accident, leur esprit d'aventure ne diminue pas et ils continuent à explorer joyeusement.\n", + "3-Jack, Jill\n", + "4-{\n", + " \"French_summary\": \"Dans un charmant village, les frère et sœur Jack et Jill partent chercher de l'eau dans un puits au sommet de la montagne. Malheureusement, Jack trébuche sur une pierre et tombe de la montagne, suivi de près par Jill. Bien qu'ils soient légèrement blessés, ils retournent chez eux chaleureusement. Malgré cet accident, leur esprit d'aventure ne diminue pas et ils continuent à explorer joyeusement.\",\n", + " \"num_names\": 2\n", + "}\n" + ] + } + ], + "source": [ + "text = f\"\"\"\n", + "在一个迷人的村庄里,兄妹杰克和吉尔出发去一个山顶井里打水。\\\n", + "他们一边唱着欢乐的歌,一边往上爬,\\\n", + "然而不幸降临——杰克绊了一块石头,从山上滚了下来,吉尔紧随其后。\\\n", + "虽然略有些摔伤,但他们还是回到了温馨的家中。\\\n", + "尽管出了这样的意外,他们的冒险精神依然没有减弱,继续充满愉悦地探索。\n", + "\"\"\"\n", + "# example 1\n", + "prompt_1 = f\"\"\"\n", + "执行以下操作:\n", + "1-用一句话概括下面用三个反引号括起来的文本。\n", + "2-将摘要翻译成法语。\n", + "3-在法语摘要中列出每个人名。\n", + "4-输出一个 JSON 对象,其中包含以下键:French_summary,num_names。\n", + "\n", + "请用换行符分隔您的答案。\n", + "\n", + "Text:\n", + "```{text}```\n", + "\"\"\"\n", + "response = get_completion(prompt_1)\n", + "print(\"prompt 1:\")\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "上述输出仍然存在一定问题,例如,键“姓名”会被替换为法语,因此,我们给出一个更好的 Prompt,该 Prompt 指定了输出的格式" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Completion for prompt 2:\n", + "Summary: 兄妹杰克和吉尔在山顶井里打水时发生意外,但他们仍然保持冒险精神继续探索。\n", + "Translation: Jack and Jill, deux frères et sœurs, ont eu un accident en allant chercher de l'eau dans un puits de montagne, mais ils ont continué à explorer avec un esprit d'aventure.\n", + "Names: Jack, Jill\n", + "Output JSON: {\"french_summary\": \"Jack and Jill, deux frères et sœurs, ont eu un accident en allant chercher de l'eau dans un puits de montagne, mais ils ont continué à explorer avec un esprit d'aventure.\", \"num_names\": 2}\n" + ] + } + ], + "source": [ + "prompt_2 = f\"\"\"\n", + "Your task is to perform the following actions: \n", + "1 - Summarize the following text delimited by <> with 1 sentence.\n", + "2 - Translate the summary into French.\n", + "3 - List each name in the French summary.\n", + "4 - Output a json object that contains the \n", + "following keys: french_summary, num_names.\n", + "\n", + "Use the following format:\n", + "Text: \n", + "Summary: \n", + "Translation: \n", + "Names: \n", + "Output JSON: \n", + "\n", + "Text: <{text}>\n", + "\"\"\"\n", + "response = get_completion(prompt_2)\n", + "print(\"\\nCompletion for prompt 2:\")\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "prompt 2:\n", + "摘要:兄妹杰克和吉尔在迷人的村庄里冒险,不幸摔伤后回到家中,但仍然充满冒险精神。\n", + "翻译:In a charming village, siblings Jack and Jill set out to fetch water from a mountaintop well. While climbing and singing, Jack trips on a stone and tumbles down the mountain, with Jill following closely behind. Despite some bruises, they make it back home safely. Their adventurous spirit remains undiminished as they continue to explore with joy.\n", + "名称:Jack,Jill\n", + "输出 JSON:{\"English_summary\": \"In a charming village, siblings Jack and Jill set out to fetch water from a mountaintop well. While climbing and singing, Jack trips on a stone and tumbles down the mountain, with Jill following closely behind. Despite some bruises, they make it back home safely. Their adventurous spirit remains undiminished as they continue to explore with joy.\", \"num_names\": 2}\n" + ] + } + ], + "source": [ + "prompt_2 = f\"\"\"\n", + "1-用一句话概括下面用<>括起来的文本。\n", + "2-将摘要翻译成英语。\n", + "3-在英语摘要中列出每个名称。\n", + "4-输出一个 JSON 对象,其中包含以下键:English_summary,num_names。\n", + "\n", + "请使用以下格式:\n", + "文本:<要总结的文本>\n", + "摘要:<摘要>\n", + "翻译:<摘要的翻译>\n", + "名称:<英语摘要中的名称列表>\n", + "输出 JSON:<带有 English_summary 和 num_names 的 JSON>\n", + "\n", + "Text: <{text}>\n", + "\"\"\"\n", + "response = get_completion(prompt_2)\n", + "print(\"\\nprompt 2:\")\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**策略二:指导模型在下结论之前找出一个自己的解法**\n", + "\n", + "有时候,在明确指导模型在做决策之前要思考解决方案时,我们会得到更好的结果。\n", + "\n", + "接下来我们会给出一个问题和一个学生的解答,要求模型判断解答是否正确" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The student's solution is correct.\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "Determine if the student's solution is correct or not.\n", + "\n", + "Question:\n", + "I'm building a solar power installation and I need \\\n", + " help working out the financials. \n", + "- Land costs $100 / square foot\n", + "- I can buy solar panels for $250 / square foot\n", + "- I negotiated a contract for maintenance that will cost \\ \n", + "me a flat $100k per year, and an additional $10 / square \\\n", + "foot\n", + "What is the total cost for the first year of operations \n", + "as a function of the number of square feet.\n", + "\n", + "Student's Solution:\n", + "Let x be the size of the installation in square feet.\n", + "Costs:\n", + "1. Land cost: 100x\n", + "2. Solar panel cost: 250x\n", + "3. Maintenance cost: 100,000 + 100x\n", + "学生的解决方案:\n", + "设x为发电站的大小,单位为平方英尺。\n", + "费用:\n", + "Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "学生的解决方案是正确的。\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "判断学生的解决方案是否正确。\n", + "\n", + "问题:\n", + "我正在建造一个太阳能发电站,需要帮助计算财务。\n", + "\n", + " 土地费用为 100美元/平方英尺\n", + " 我可以以 250美元/平方英尺的价格购买太阳能电池板\n", + " 我已经谈判好了维护合同,每年需要支付固定的10万美元,并额外支付每平方英尺10美元\n", + " 作为平方英尺数的函数,首年运营的总费用是多少。\n", + "\n", + "学生的解决方案:\n", + "设x为发电站的大小,单位为平方英尺。\n", + "费用:\n", + "\n", + " 土地费用:100x\n", + " 太阳能电池板费用:250x\n", + " 维护费用:100,000美元+100x\n", + " 总费用:100x+250x+100,000美元+100x=450x+100,000美元\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "但是注意,学生的解决方案实际上是错误的。\n", + "\n", + "我们可以通过指导模型先自行找出一个解法来解决这个问题。\n", + "\n", + "在接下来这个 Prompt 中,我们要求模型先自行解决这个问题,再根据自己的解法与学生的解法进行对比,从而判断学生的解法是否正确。同时,我们给定了输出的格式要求。通过明确步骤,让模型有更多时间思考,有时可以获得更准确的结果。在这个例子中,学生的答案是错误的,但如果我们没有先让模型自己计算,那么可能会被误导以为学生是正确的。" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Let x be the size of the installation in square feet.\n", + "\n", + "Costs:\n", + "1. Land cost: 100x\n", + "2. Solar panel cost: 250x\n", + "3. Maintenance cost: 100,000 + 10x\n", + "\n", + "Total cost: 100x + 250x + 100,000 + 10x = 360x + 100,000\n", + "\n", + "Is the student's solution the same as actual solution just calculated:\n", + "No\n", + "\n", + "Student grade:\n", + "Incorrect\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "Your task is to determine if the student's solution \\\n", + "is correct or not.\n", + "To solve the problem do the following:\n", + "- First, work out your own solution to the problem. \n", + "- Then compare your solution to the student's solution \\ \n", + "and evaluate if the student's solution is correct or not. \n", + "Don't decide if the student's solution is correct until \n", + "you have done the problem yourself.\n", + "\n", + "Use the following format:\n", + "Question:\n", + "```\n", + "question here\n", + "```\n", + "Student's solution:\n", + "```\n", + "student's solution here\n", + "```\n", + "Actual solution:\n", + "```\n", + "steps to work out the solution and your solution here\n", + "```\n", + "Is the student's solution the same as actual solution \\\n", + "just calculated:\n", + "```\n", + "yes or no\n", + "```\n", + "Student grade:\n", + "```\n", + "correct or incorrect\n", + "```\n", + "\n", + "Question:\n", + "```\n", + "I'm building a solar power installation and I need help \\\n", + "working out the financials. \n", + "- Land costs $100 / square foot\n", + "- I can buy solar panels for $250 / square foot\n", + "- I negotiated a contract for maintenance that will cost \\\n", + "me a flat $100k per year, and an additional $10 / square \\\n", + "foot\n", + "What is the total cost for the first year of operations \\\n", + "as a function of the number of square feet.\n", + "``` \n", + "Student's solution:\n", + "```\n", + "Let x be the size of the installation in square feet.\n", + "Costs:\n", + "1. Land cost: 100x\n", + "2. Solar panel cost: 250x\n", + "3. Maintenance cost: 100,000 + 100x\n", + "Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000\n", + "```\n", + "Actual solution:\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "正确的解决方案和步骤:\n", + " 1. 计算土地费用:100美元/平方英尺 * x平方英尺 = 100x美元\n", + " 2. 计算太阳能电池板费用:250美元/平方英尺 * x平方英尺 = 250x美元\n", + " 3. 计算维护费用:10万美元 + 10美元/平方英尺 * x平方英尺 = 10万美元 + 10x美元\n", + " 4. 计算总费用:100x美元 + 250x美元 + 10万美元 + 10x美元 = 360x + 10万美元\n", + "\n", + "学生的解决方案和实际解决方案是否相同:否\n", + "\n", + "学生的成绩:不正确\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "请判断学生的解决方案是否正确,请通过如下步骤解决这个问题:\n", + "\n", + "步骤:\n", + "\n", + " 首先,自己解决问题。\n", + " 然后将你的解决方案与学生的解决方案进行比较,并评估学生的解决方案是否正确。在自己完成问题之前,请勿决定学生的解决方案是否正确。\n", + "\n", + "使用以下格式:\n", + "\n", + " 问题:问题文本\n", + " 学生的解决方案:学生的解决方案文本\n", + " 实际解决方案和步骤:实际解决方案和步骤文本\n", + " 学生的解决方案和实际解决方案是否相同:是或否\n", + " 学生的成绩:正确或不正确\n", + "\n", + "问题:\n", + "\n", + " 我正在建造一个太阳能发电站,需要帮助计算财务。 \n", + " - 土地费用为每平方英尺100美元\n", + " - 我可以以每平方英尺250美元的价格购买太阳能电池板\n", + " - 我已经谈判好了维护合同,每年需要支付固定的10万美元,并额外支付每平方英尺10美元\n", + " 作为平方英尺数的函数,首年运营的总费用是多少。\n", + "\n", + "学生的解决方案:\n", + "\n", + " 设x为发电站的大小,单位为平方英尺。\n", + " 费用:\n", + " 1. 土地费用:100x\n", + " 2. 太阳能电池板费用:250x\n", + " 3. 维护费用:100,000+100x\n", + " 总费用:100x+250x+100,000+100x=450x+100,000\n", + "\n", + "实际解决方案和步骤:\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 三、局限性" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**虚假知识**:模型偶尔会生成一些看似真实实则编造的知识\n", + "\n", + "如果模型在训练过程中接触了大量的知识,它并没有完全记住所见的信息,因此它并不很清楚自己知识的边界。这意味着它可能会尝试回答有关晦涩主题的问题,并编造听起来合理但实际上并不正确的答案。我们称这些编造的想法为幻觉。\n", + "\n", + "例如在如下示例中,我们要求告诉我们 Boie 公司生产的 AeroGlide UltraSlim Smart Toothbrush 产品的信息,事实上,这个公司是真实存在的,但产品是编造的,模型则会一本正经地告诉我们编造的知识。\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The AeroGlide UltraSlim Smart Toothbrush by Boie is a high-tech toothbrush that uses advanced sonic technology to provide a deep and thorough clean. It features a slim and sleek design that makes it easy to hold and maneuver, and it comes with a range of smart features that help you optimize your brushing routine.\n", + "\n", + "One of the key features of the AeroGlide UltraSlim Smart Toothbrush is its advanced sonic technology, which uses high-frequency vibrations to break up plaque and bacteria on your teeth and gums. This technology is highly effective at removing even the toughest stains and buildup, leaving your teeth feeling clean and fresh.\n", + "\n", + "In addition to its sonic technology, the AeroGlide UltraSlim Smart Toothbrush also comes with a range of smart features that help you optimize your brushing routine. These include a built-in timer that ensures you brush for the recommended two minutes, as well as a pressure sensor that alerts you if you're brushing too hard.\n", + "\n", + "Overall, the AeroGlide UltraSlim Smart Toothbrush by Boie is a highly advanced and effective toothbrush that is perfect for anyone looking to take their oral hygiene to the next level. With its advanced sonic technology and smart features, it provides a deep and thorough clean that leaves your teeth feeling fresh and healthy.\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "Tell me about AeroGlide UltraSlim Smart Toothbrush by Boie\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Boie公司生产的AeroGlide UltraSlim Smart Toothbrush是一款智能牙刷,具有以下特点:\n", + "\n", + "1. 超薄设计:刷头仅有0.8毫米的厚度,可以更容易地进入口腔深处,清洁更彻底。\n", + "\n", + "2. 智能感应:牙刷配备了智能感应技术,可以自动识别刷头的位置和方向,确保每个部位都得到充分的清洁。\n", + "\n", + "3. 高效清洁:牙刷采用了高速振动技术,每分钟可达到40000次,可以有效去除牙菌斑和污渍。\n", + "\n", + "4. 轻松携带:牙刷采用了便携式设计,可以轻松放入口袋或旅行包中,随时随地进行口腔清洁。\n", + "\n", + "5. 环保材料:牙刷采用了环保材料制造,不含有害物质,对环境友好。\n", + "\n", + "总之,Boie公司生产的AeroGlide UltraSlim Smart Toothbrush是一款高效、智能、环保的牙刷,可以帮助用户轻松保持口腔健康。\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "告诉我 Boie 公司生产的 AeroGlide UltraSlim Smart Toothbrush 的相关信息\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "模型会输出看上去非常真实的编造知识,这有时会很危险。因此,请确保使用我们在本笔记本中介绍的一些技巧,以尝试在构建自己的应用程序时避免这种情况。这是模型已知的一个弱点,也是我们正在积极努力解决的问题。在你希望模型根据文本生成答案的情况下,另一种减少幻觉的策略是先要求模型找到文本中的任何相关引用,然后要求它使用这些引用来回答问题,这种追溯源文档的方法通常对减少幻觉非常有帮助。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**说明:在本教程中,我们使用 \\ 来使文本适应屏幕大小以提高阅读体验,GPT 并不受 \\ 的影响,但在你调用其他大模型时,需额外考虑 \\ 是否会影响模型性能**" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/content/3. 迭代优化 Itrative.ipynb b/content/3. 迭代优化 Itrative.ipynb new file mode 100644 index 0000000..8b197a8 --- /dev/null +++ b/content/3. 迭代优化 Itrative.ipynb @@ -0,0 +1,837 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 迭代式提示开发\n", + "\n", + "当使用 LLM 构建应用程序时,我从来没有在第一次尝试中就成功使用最终应用程序中所需的 Prompt。但这并不重要,只要您有一个好的迭代过程来不断改进您的 Prompt,那么你就能够得到一个适合任务的 Prompt。我认为在提示方面,第一次成功的几率可能会高一些,但正如上所说,第一个提示是否有效并不重要。最重要的是为您的应用程序找到有效提示的过程。\n", + "\n", + "因此,在本章中,我们将以从产品说明书中生成营销文案这一示例,展示一些框架,以提示你思考如何迭代地分析和完善你的 Prompt。\n", + "\n", + "如果您之前与我一起上过机器学习课程,您可能见过我使用的一张图表,说明了机器学习开发的流程。通常是先有一个想法,然后再实现它:编写代码,获取数据,训练模型,这会给您一个实验结果。然后您可以查看输出结果,进行错误分析,找出它在哪里起作用或不起作用,甚至可以更改您想要解决的问题的确切思路或方法,然后更改实现并运行另一个实验等等,反复迭代,以获得有效的机器学习模型。在编写 Prompt 以使用 LLM 开发应用程序时,这个过程可能非常相似,您有一个关于要完成的任务的想法,可以尝试编写第一个 Prompt,满足上一章说过的两个原则:清晰明确,并且给系统足够的时间思考。然后您可以运行它并查看结果。如果第一次效果不好,那么迭代的过程就是找出为什么指令不够清晰或为什么没有给算法足够的时间思考,以便改进想法、改进提示等等,循环多次,直到找到适合您的应用程序的 Prompt。\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 环境配置\n", + "\n", + "同上一章,我们首先需要配置使用 OpenAI API 的环境" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import openai\n", + "import os\n", + "from dotenv import load_dotenv, find_dotenv\n", + "# 导入第三方库\n", + "\n", + "_ = load_dotenv(find_dotenv())\n", + "# 读取系统中的环境变量\n", + "\n", + "openai.api_key = os.getenv('OPENAI_API_KEY')\n", + "# 设置 API_KEY" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "# 一个封装 OpenAI 接口的函数,参数为 Prompt,返回对应结果\n", + "def get_completion(prompt, model=\"gpt-3.5-turbo\"):\n", + " '''\n", + " prompt: 对应的提示\n", + " model: 调用的模型,默认为 gpt-3.5-turbo(ChatGPT),有内测资格的用户可以选择 gpt-4\n", + " '''\n", + " messages = [{\"role\": \"user\", \"content\": prompt}]\n", + " response = openai.ChatCompletion.create(\n", + " model=model,\n", + " messages=messages,\n", + " temperature=0, # 模型输出的温度系数,控制输出的随机程度\n", + " )\n", + " # 调用 OpenAI 的 ChatCompletion 接口\n", + " return response.choices[0].message[\"content\"]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 任务——从产品说明书生成一份营销产品描述" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "这里有一个椅子的产品说明书,描述说它是一个中世纪灵感家族的一部分,讨论了构造、尺寸、椅子选项、材料等等,产地是意大利。假设您想要使用这份说明书帮助营销团队为在线零售网站撰写营销式描述" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# 示例:产品说明书\n", + "fact_sheet_chair = \"\"\"\n", + "OVERVIEW\n", + "- Part of a beautiful family of mid-century inspired office furniture, \n", + "including filing cabinets, desks, bookcases, meeting tables, and more.\n", + "- Several options of shell color and base finishes.\n", + "- Available with plastic back and front upholstery (SWC-100) \n", + "or full upholstery (SWC-110) in 10 fabric and 6 leather options.\n", + "- Base finish options are: stainless steel, matte black, \n", + "gloss white, or chrome.\n", + "- Chair is available with or without armrests.\n", + "- Suitable for home or business settings.\n", + "- Qualified for contract use.\n", + "\n", + "CONSTRUCTION\n", + "- 5-wheel plastic coated aluminum base.\n", + "- Pneumatic chair adjust for easy raise/lower action.\n", + "\n", + "DIMENSIONS\n", + "- WIDTH 53 CM | 20.87”\n", + "- DEPTH 51 CM | 20.08”\n", + "- HEIGHT 80 CM | 31.50”\n", + "- SEAT HEIGHT 44 CM | 17.32”\n", + "- SEAT DEPTH 41 CM | 16.14”\n", + "\n", + "OPTIONS\n", + "- Soft or hard-floor caster options.\n", + "- Two choices of seat foam densities: \n", + "medium (1.8 lb/ft3) or high (2.8 lb/ft3)\n", + "- Armless or 8 position PU armrests \n", + "\n", + "MATERIALS\n", + "SHELL BASE GLIDER\n", + "- Cast Aluminum with modified nylon PA6/PA66 coating.\n", + "- Shell thickness: 10 mm.\n", + "SEAT\n", + "- HD36 foam\n", + "\n", + "COUNTRY OF ORIGIN\n", + "- Italy\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Introducing our stunning mid-century inspired office chair, the perfect addition to any home or business setting. Part of a beautiful family of office furniture, including filing cabinets, desks, bookcases, meeting tables, and more, this chair is available in several options of shell color and base finishes to suit your style. Choose from plastic back and front upholstery (SWC-100) or full upholstery (SWC-110) in 10 fabric and 6 leather options.\n", + "\n", + "The chair is constructed with a 5-wheel plastic coated aluminum base and features a pneumatic chair adjust for easy raise/lower action. It is available with or without armrests and is qualified for contract use. The base finish options are stainless steel, matte black, gloss white, or chrome.\n", + "\n", + "Measuring at a width of 53 cm, depth of 51 cm, and height of 80 cm, with a seat height of 44 cm and seat depth of 41 cm, this chair is designed for ultimate comfort. You can also choose between soft or hard-floor caster options and two choices of seat foam densities: medium (1.8 lb/ft3) or high (2.8 lb/ft3). The armrests are available in either an armless or 8 position PU option.\n", + "\n", + "The materials used in the construction of this chair are of the highest quality. The shell base glider is made of cast aluminum with modified nylon PA6/PA66 coating and has a shell thickness of 10 mm. The seat is made of HD36 foam, ensuring maximum comfort and durability.\n", + "\n", + "This chair is made in Italy and is the perfect combination of style and functionality. Upgrade your workspace with our mid-century inspired office chair today!\n" + ] + } + ], + "source": [ + "# 提示:基于说明书生成营销描述\n", + "prompt = f\"\"\"\n", + "Your task is to help a marketing team create a \n", + "description for a retail website of a product based \n", + "on a technical fact sheet.\n", + "\n", + "Write a product description based on the information \n", + "provided in the technical specifications delimited by \n", + "triple backticks.\n", + "\n", + "Technical specifications: ```{fact_sheet_chair}```\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "# 示例:产品说明书\n", + "fact_sheet_chair = \"\"\"\n", + "概述\n", + "\n", + " 美丽的中世纪风格办公家具系列的一部分,包括文件柜、办公桌、书柜、会议桌等。\n", + " 多种外壳颜色和底座涂层可选。\n", + " 可选塑料前后靠背装饰(SWC-100)或10种面料和6种皮革的全面装饰(SWC-110)。\n", + " 底座涂层选项为:不锈钢、哑光黑色、光泽白色或铬。\n", + " 椅子可带或不带扶手。\n", + " 适用于家庭或商业场所。\n", + " 符合合同使用资格。\n", + "\n", + "结构\n", + "\n", + " 五个轮子的塑料涂层铝底座。\n", + " 气动椅子调节,方便升降。\n", + "\n", + "尺寸\n", + "\n", + " 宽度53厘米|20.87英寸\n", + " 深度51厘米|20.08英寸\n", + " 高度80厘米|31.50英寸\n", + " 座椅高度44厘米|17.32英寸\n", + " 座椅深度41厘米|16.14英寸\n", + "\n", + "选项\n", + "\n", + " 软地板或硬地板滚轮选项。\n", + " 两种座椅泡沫密度可选:中等(1.8磅/立方英尺)或高(2.8磅/立方英尺)。\n", + " 无扶手或8个位置PU扶手。\n", + "\n", + "材料\n", + "外壳底座滑动件\n", + "\n", + " 改性尼龙PA6/PA66涂层的铸铝。\n", + " 外壳厚度:10毫米。\n", + " 座椅\n", + " HD36泡沫\n", + "\n", + "原产国\n", + "\n", + " 意大利\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "产品描述:\n", + "\n", + "我们自豪地推出美丽的中世纪风格办公家具系列,其中包括文件柜、办公桌、书柜、会议桌等。我们的产品采用多种外壳颜色和底座涂层,以满足您的个性化需求。您可以选择塑料前后靠背装饰(SWC-100)或10种面料和6种皮革的全面装饰(SWC-110),以使您的办公室更加舒适和时尚。\n", + "\n", + "我们的底座涂层选项包括不锈钢、哑光黑色、光泽白色或铬,以满足您的不同需求。椅子可带或不带扶手,适用于家庭或商业场所。我们的产品符合合同使用资格,为您提供更加可靠的保障。\n", + "\n", + "我们的产品采用五个轮子的塑料涂层铝底座,气动椅子调节,方便升降。尺寸为宽度53厘米|20.87英寸,深度51厘米|20.08英寸,高度80厘米|31.50英寸,座椅高度44厘米|17.32英寸,座椅深度41厘米|16.14英寸,为您提供舒适的使用体验。\n", + "\n", + "我们的产品还提供软地板或硬地板滚轮选项,两种座椅泡沫密度可选:中等(1.8磅/立方英尺)或高(2.8磅/立方英尺),以及无扶手或8个位置PU扶手,以满足您的不同需求。\n", + "\n", + "我们的产品采用改性尼龙PA6/PA66涂层的铸铝外壳底座滑动件,外壳厚度为10毫米,座椅采用HD36泡沫,为您提供更加舒适的使用体验。我们的产品原产国为意大利,为您提供更加优质的品质保证。\n" + ] + } + ], + "source": [ + "# 提示:基于说明书创建营销描述\n", + "prompt = f\"\"\"\n", + "你的任务是帮助营销团队基于技术说明书创建一个产品的营销描述。\n", + "\n", + "根据```标记的技术说明书中提供的信息,编写一个产品描述。\n", + "\n", + "技术说明: {fact_sheet_chair}\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 问题一:生成文本太长\n", + "\n", + "它似乎很好地写了一个描述,介绍了一个惊人的中世纪灵感办公椅,很好地完成了要求,即从技术说明书开始编写产品描述。但是当我看到这个时,我会觉得这个太长了。\n", + "\n", + "所以我有了一个想法。我写了一个提示,得到了结果。但是我对它不是很满意,因为它太长了,所以我会澄清我的提示,并说最多使用50个字。\n", + "\n", + "因此,我通过要求它限制生成文本长度来解决这一问题" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Introducing our beautiful medieval-style office furniture collection, including filing cabinets, desks, bookcases, and conference tables. Choose from a variety of shell colors and base coatings, with optional plastic or fabric/leather decoration. The chair features a plastic-coated aluminum base with five wheels and pneumatic height adjustment. Perfect for home or commercial use. Made in Italy.\n" + ] + } + ], + "source": [ + "# 优化后的 Prompt,要求生成描述不多于 50 词\n", + "prompt = f\"\"\"\n", + "Your task is to help a marketing team create a \n", + "description for a retail website of a product based \n", + "on a technical fact sheet.\n", + "\n", + "Write a product description based on the information \n", + "provided in the technical specifications delimited by \n", + "triple backticks.\n", + "\n", + "Use at most 50 words.\n", + "\n", + "Technical specifications: ```{fact_sheet_chair}```\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "取出回答并根据空格拆分,答案为54个字,较好地完成了我的要求" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "54\n" + ] + } + ], + "source": [ + "lst = response.split()\n", + "print(len(lst))" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "中世纪风格办公家具系列,包括文件柜、办公桌、书柜、会议桌等。多种颜色和涂层可选,可带或不带扶手。底座涂层选项为不锈钢、哑光黑色、光泽白色或铬。适用于家庭或商业场所,符合合同使用资格。意大利制造。\n" + ] + } + ], + "source": [ + "# 优化后的 Prompt,要求生成描述不多于 50 词\n", + "prompt = f\"\"\"\n", + "您的任务是帮助营销团队基于技术说明书创建一个产品的零售网站描述。\n", + "\n", + "根据```标记的技术说明书中提供的信息,编写一个产品描述。\n", + "\n", + "使用最多50个词。\n", + "\n", + "技术规格:```{fact_sheet_chair}```\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "97" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 由于中文需要分词,此处直接计算整体长度\n", + "len(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "LLM在遵循非常精确的字数限制方面表现得还可以,但并不那么出色。有时它会输出60或65个单词的内容,但这还算是合理的。这原因是 LLM 解释文本使用一种叫做分词器的东西,但它们往往在计算字符方面表现一般般。有很多不同的方法来尝试控制你得到的输出的长度。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 问题二:文本关注在错误的细节上\n", + "\n", + "我们会发现的第二个问题是,这个网站并不是直接向消费者销售,它实际上旨在向家具零售商销售家具,他们会更关心椅子的技术细节和材料。在这种情况下,你可以修改这个提示,让它更精确地描述椅子的技术细节。\n", + "\n", + "解决方法:要求它专注于与目标受众相关的方面。" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Introducing our beautiful medieval-style office furniture collection, including file cabinets, desks, bookcases, and conference tables. Available in multiple shell colors and base coatings, with optional plastic or fabric/leather upholstery. Features a plastic-coated aluminum base with five wheels and pneumatic chair adjustment. Suitable for home or commercial use and made with high-quality materials, including cast aluminum with a modified nylon coating and HD36 foam. Made in Italy.\n" + ] + } + ], + "source": [ + "# 优化后的 Prompt,说明面向对象,应具有什么性质且侧重于什么方面\n", + "prompt = f\"\"\"\n", + "Your task is to help a marketing team create a \n", + "description for a retail website of a product based \n", + "on a technical fact sheet.\n", + "\n", + "Write a product description based on the information \n", + "provided in the technical specifications delimited by \n", + "triple backticks.\n", + "\n", + "The description is intended for furniture retailers, \n", + "so should be technical in nature and focus on the \n", + "materials the product is constructed from.\n", + "\n", + "Use at most 50 words.\n", + "\n", + "Technical specifications: ```{fact_sheet_chair}```\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "这款中世纪风格办公家具系列包括文件柜、办公桌、书柜和会议桌等,适用于家庭或商业场所。可选多种外壳颜色和底座涂层,底座涂层选项为不锈钢、哑光黑色、光泽白色或铬。椅子可带或不带扶手,可选软地板或硬地板滚轮,两种座椅泡沫密度可选。外壳底座滑动件采用改性尼龙PA6/PA66涂层的铸铝,座椅采用HD36泡沫。原产国为意大利。\n" + ] + } + ], + "source": [ + "# 优化后的 Prompt,说明面向对象,应具有什么性质且侧重于什么方面\n", + "prompt = f\"\"\"\n", + "您的任务是帮助营销团队基于技术说明书创建一个产品的零售网站描述。\n", + "\n", + "根据```标记的技术说明书中提供的信息,编写一个产品描述。\n", + "\n", + "该描述面向家具零售商,因此应具有技术性质,并侧重于产品的材料构造。\n", + "\n", + "使用最多50个单词。\n", + "\n", + "技术规格: ```{fact_sheet_chair}```\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "我可能进一步想要在描述的结尾包括产品ID。因此,我可以进一步改进这个提示,要求在描述的结尾,包括在技术说明中的每个7个字符产品ID。" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Introducing our beautiful medieval-style office furniture collection, featuring file cabinets, desks, bookshelves, and conference tables. Available in multiple shell colors and base coatings, with optional plastic or fabric/leather decorations. The chair comes with or without armrests and has a plastic-coated aluminum base with five wheels and pneumatic height adjustment. Suitable for home or commercial use. Made in Italy.\n", + "\n", + "Product IDs: SWC-100, SWC-110\n" + ] + } + ], + "source": [ + "# 更进一步,要求在描述末尾包含 7个字符的产品ID\n", + "prompt = f\"\"\"\n", + "Your task is to help a marketing team create a \n", + "description for a retail website of a product based \n", + "on a technical fact sheet.\n", + "\n", + "Write a product description based on the information \n", + "provided in the technical specifications delimited by \n", + "triple backticks.\n", + "\n", + "The description is intended for furniture retailers, \n", + "so should be technical in nature and focus on the \n", + "materials the product is constructed from.\n", + "\n", + "At the end of the description, include every 7-character \n", + "Product ID in the technical specification.\n", + "\n", + "Use at most 50 words.\n", + "\n", + "Technical specifications: ```{fact_sheet_chair}```\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "这款中世纪风格的办公家具系列包括文件柜、办公桌、书柜和会议桌等,适用于家庭或商业场所。可选多种外壳颜色和底座涂层,底座涂层选项为不锈钢、哑光黑色、光泽白色或铬。椅子可带或不带扶手,可选塑料前后靠背装饰或10种面料和6种皮革的全面装饰。座椅采用HD36泡沫,可选中等或高密度,座椅高度44厘米,深度41厘米。外壳底座滑动件采用改性尼龙PA6/PA66涂层的铸铝,外壳厚度为10毫米。原产国为意大利。产品ID:SWC-100/SWC-110。\n" + ] + } + ], + "source": [ + "# 更进一步\n", + "prompt = f\"\"\"\n", + "您的任务是帮助营销团队基于技术说明书创建一个产品的零售网站描述。\n", + "\n", + "根据```标记的技术说明书中提供的信息,编写一个产品描述。\n", + "\n", + "该描述面向家具零售商,因此应具有技术性质,并侧重于产品的材料构造。\n", + "\n", + "在描述末尾,包括技术规格中每个7个字符的产品ID。\n", + "\n", + "使用最多50个单词。\n", + "\n", + "技术规格: ```{fact_sheet_chair}```\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 问题三:需要一个表格形式的描述\n", + "\n", + "以上是许多开发人员通常会经历的迭代提示开发的简短示例。我的建议是,像上一章中所演示的那样,Prompt 应该保持清晰和明确,并在必要时给模型一些思考时间。在这些要求的基础上,通常值得首先尝试编写 Prompt ,看看会发生什么,然后从那里开始迭代地完善 Prompt,以逐渐接近所需的结果。因此,许多成功的提示都是通过这种迭代过程得出的。我将向您展示一个更复杂的提示示例,可能会让您对ChatGPT的能力有更深入的了解。\n", + "\n", + "这里我添加了一些额外的说明,要求它抽取信息并组织成表格,并指定表格的列、表名和格式,还要求它将所有内容格式化为可以在网页使用的 HTML。" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "
\n", + "

Introducing our beautiful collection of medieval-style office furniture, including file cabinets, desks, bookcases, and conference tables. Choose from a variety of shell colors and base coatings. You can opt for plastic front and backrest decoration (SWC-100) or full decoration with 10 fabrics and 6 leathers (SWC-110). Base coating options include stainless steel, matte black, glossy white, or chrome. The chair is available with or without armrests and is suitable for both home and commercial settings. It is contract eligible.

\n", + "

The structure features a plastic-coated aluminum base with five wheels. The chair is pneumatically adjustable for easy height adjustment.

\n", + "

Product IDs: SWC-100, SWC-110

\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Product Dimensions
Width20.87 inches
Depth20.08 inches
Height31.50 inches
Seat Height17.32 inches
Seat Depth16.14 inches
\n", + "

Options include soft or hard floor casters. You can choose from two seat foam densities: medium (1.8 pounds/cubic foot) or high (2.8 pounds/cubic foot). The chair is available with or without 8-position PU armrests.

\n", + "

Materials:

\n", + "
    \n", + "
  • Shell, base, and sliding parts: cast aluminum coated with modified nylon PA6/PA66. Shell thickness: 10mm.
  • \n", + "
  • Seat: HD36 foam
  • \n", + "
\n", + "

Made in Italy.

\n", + "
\n" + ] + } + ], + "source": [ + "# 要求它抽取信息并组织成表格,并指定表格的列、表名和格式\n", + "prompt = f\"\"\"\n", + "Your task is to help a marketing team create a \n", + "description for a retail website of a product based \n", + "on a technical fact sheet.\n", + "\n", + "Write a product description based on the information \n", + "provided in the technical specifications delimited by \n", + "triple backticks.\n", + "\n", + "The description is intended for furniture retailers, \n", + "so should be technical in nature and focus on the \n", + "materials the product is constructed from.\n", + "\n", + "At the end of the description, include every 7-character \n", + "Product ID in the technical specification.\n", + "\n", + "After the description, include a table that gives the \n", + "product's dimensions. The table should have two columns.\n", + "In the first column include the name of the dimension. \n", + "In the second column include the measurements in inches only.\n", + "\n", + "Give the table the title 'Product Dimensions'.\n", + "\n", + "Format everything as HTML that can be used in a website. \n", + "Place the description in a
element.\n", + "\n", + "Technical specifications: ```{fact_sheet_chair}```\n", + "\"\"\"\n", + "\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "

Introducing our beautiful collection of medieval-style office furniture, including file cabinets, desks, bookcases, and conference tables. Choose from a variety of shell colors and base coatings. You can opt for plastic front and backrest decoration (SWC-100) or full decoration with 10 fabrics and 6 leathers (SWC-110). Base coating options include stainless steel, matte black, glossy white, or chrome. The chair is available with or without armrests and is suitable for both home and commercial settings. It is contract eligible.

\n", + "

The structure features a plastic-coated aluminum base with five wheels. The chair is pneumatically adjustable for easy height adjustment.

\n", + "

Product IDs: SWC-100, SWC-110

\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Product Dimensions
Width20.87 inches
Depth20.08 inches
Height31.50 inches
Seat Height17.32 inches
Seat Depth16.14 inches
\n", + "

Options include soft or hard floor casters. You can choose from two seat foam densities: medium (1.8 pounds/cubic foot) or high (2.8 pounds/cubic foot). The chair is available with or without 8-position PU armrests.

\n", + "

Materials:

\n", + "
    \n", + "
  • Shell, base, and sliding parts: cast aluminum coated with modified nylon PA6/PA66. Shell thickness: 10mm.
  • \n", + "
  • Seat: HD36 foam
  • \n", + "
\n", + "

Made in Italy.

\n", + "
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# 表格是以 HTML 格式呈现的,加载出来\n", + "from IPython.display import display, HTML\n", + "\n", + "display(HTML(response))" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "
\n", + "

中世纪风格办公家具系列椅子

\n", + "

这款椅子是中世纪风格办公家具系列的一部分,适用于家庭或商业场所。它有多种外壳颜色和底座涂层可选,包括不锈钢、哑光黑色、光泽白色或铬。您可以选择带或不带扶手的椅子,以及软地板或硬地板滚轮选项。此外,您可以选择两种座椅泡沫密度:中等(1.8磅/立方英尺)或高(2.8磅/立方英尺)。

\n", + "

椅子的外壳底座滑动件是改性尼龙PA6/PA66涂层的铸铝,外壳厚度为10毫米。座椅采用HD36泡沫,底座是五个轮子的塑料涂层铝底座,可以进行气动椅子调节,方便升降。此外,椅子符合合同使用资格,是您理想的选择。

\n", + "

产品ID:SWC-100

\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
产品尺寸
宽度20.87英寸
深度20.08英寸
高度31.50英寸
座椅高度17.32英寸
座椅深度16.14英寸
\n" + ] + } + ], + "source": [ + "# 要求它抽取信息并组织成表格,并指定表格的列、表名和格式\n", + "prompt = f\"\"\"\n", + "您的任务是帮助营销团队基于技术说明书创建一个产品的零售网站描述。\n", + "\n", + "根据```标记的技术说明书中提供的信息,编写一个产品描述。\n", + "\n", + "该描述面向家具零售商,因此应具有技术性质,并侧重于产品的材料构造。\n", + "\n", + "在描述末尾,包括技术规格中每个7个字符的产品ID。\n", + "\n", + "在描述之后,包括一个表格,提供产品的尺寸。表格应该有两列。第一列包括尺寸的名称。第二列只包括英寸的测量值。\n", + "\n", + "给表格命名为“产品尺寸”。\n", + "\n", + "将所有内容格式化为可用于网站的HTML格式。将描述放在
元素中。\n", + "\n", + "技术规格:```{fact_sheet_chair}```\n", + "\"\"\"\n", + "\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "

中世纪风格办公家具系列椅子

\n", + "

这款椅子是中世纪风格办公家具系列的一部分,适用于家庭或商业场所。它有多种外壳颜色和底座涂层可选,包括不锈钢、哑光黑色、光泽白色或铬。您可以选择带或不带扶手的椅子,以及软地板或硬地板滚轮选项。此外,您可以选择两种座椅泡沫密度:中等(1.8磅/立方英尺)或高(2.8磅/立方英尺)。

\n", + "

椅子的外壳底座滑动件是改性尼龙PA6/PA66涂层的铸铝,外壳厚度为10毫米。座椅采用HD36泡沫,底座是五个轮子的塑料涂层铝底座,可以进行气动椅子调节,方便升降。此外,椅子符合合同使用资格,是您理想的选择。

\n", + "

产品ID:SWC-100

\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
产品尺寸
宽度20.87英寸
深度20.08英寸
高度31.50英寸
座椅高度17.32英寸
座椅深度16.14英寸
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# 表格是以 HTML 格式呈现的,加载出来\n", + "from IPython.display import display, HTML\n", + "\n", + "display(HTML(response))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "本章的主要内容是 LLM 在开发应用程序中的迭代式提示开发过程。开发者需要先尝试编写提示,然后通过迭代逐步完善它,直至得到需要的结果。关键在于拥有一种有效的开发提示的过程,而不是知道完美的提示。对于一些更复杂的应用程序,可以对多个样本进行迭代开发提示并进行评估。最后,可以在更成熟的应用程序中测试多个提示在多个样本上的平均或最差性能。在使用 Jupyter 代码笔记本示例时,请尝试不同的变化并查看结果。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/content/7.文本扩展 Expand.ipynb b/content/7.文本扩展 Expand.ipynb new file mode 100644 index 0000000..a871431 --- /dev/null +++ b/content/7.文本扩展 Expand.ipynb @@ -0,0 +1,441 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 第七章 扩展\n", + "\n", + "扩展是将短文本,例如一组说明或主题列表,输入到大型语言模型中,让模型生成更长的文本,例如基于某个主题的电子邮件或论文。这样做有一些很好的用途,例如将大型语言模型用作头脑风暴的伙伴。但这种做法也存在一些问题,例如某人可能会使用它来生成大量垃圾邮件。因此,当你使用大型语言模型的这些功能时,请仅以负责任的方式和有益于人们的方式使用它们。\n", + "\n", + "在本章中,你将学会如何基于 OpenAI API 生成适用于每个客户评价的客户服务电子邮件。我们还将使用模型的另一个输入参数称为温度,这种参数允许您在模型响应中变化探索的程度和多样性。\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 一、环境配置\n", + "\n", + "同以上几章,你需要类似的代码来配置一个可以使用 OpenAI API 的环境" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 将自己的 API-KEY 导入系统环境变量\n", + "!export OPENAI_API_KEY='api-key'" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import openai\n", + "import os\n", + "from dotenv import load_dotenv, find_dotenv\n", + "# 导入第三方库\n", + "\n", + "_ = load_dotenv(find_dotenv())\n", + "# 读取系统中的环境变量\n", + "\n", + "openai.api_key = os.getenv('OPENAI_API_KEY')\n", + "# 设置 API_KEY" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "# 一个封装 OpenAI 接口的函数,参数为 Prompt,返回对应结果\n", + "def get_completion(prompt, model=\"gpt-3.5-turbo\", temperature=0):\n", + " '''\n", + " prompt: 对应的提示\n", + " model: 调用的模型,默认为 gpt-3.5-turbo(ChatGPT),有内测资格的用户可以选择 gpt-4\n", + " temperature: 温度系数\n", + " '''\n", + " messages = [{\"role\": \"user\", \"content\": prompt}]\n", + " response = openai.ChatCompletion.create(\n", + " model=model,\n", + " messages=messages,\n", + " temperature=temperature, # 模型输出的温度系数,控制输出的随机程度\n", + " )\n", + " # 调用 OpenAI 的 ChatCompletion 接口\n", + " return response.choices[0].message[\"content\"]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 二、定制客户邮件" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "我们将根据客户评价和情感撰写自定义电子邮件响应。因此,我们将给定客户评价和情感,并生成自定义响应即使用 LLM 根据客户评价和评论情感生成定制电子邮件。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "我们首先给出一个示例,包括一个评论及对应的情感" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "# given the sentiment from the lesson on \"inferring\",\n", + "# and the original customer message, customize the email\n", + "sentiment = \"negative\"\n", + "\n", + "# review for a blender\n", + "review = f\"\"\"\n", + "So, they still had the 17 piece system on seasonal \\\n", + "sale for around $49 in the month of November, about \\\n", + "half off, but for some reason (call it price gouging) \\\n", + "around the second week of December the prices all went \\\n", + "up to about anywhere from between $70-$89 for the same \\\n", + "system. And the 11 piece system went up around $10 or \\\n", + "so in price also from the earlier sale price of $29. \\\n", + "So it looks okay, but if you look at the base, the part \\\n", + "where the blade locks into place doesn’t look as good \\\n", + "as in previous editions from a few years ago, but I \\\n", + "plan to be very gentle with it (example, I crush \\\n", + "very hard items like beans, ice, rice, etc. in the \\ \n", + "blender first then pulverize them in the serving size \\\n", + "I want in the blender then switch to the whipping \\\n", + "blade for a finer flour, and use the cross cutting blade \\\n", + "first when making smoothies, then use the flat blade \\\n", + "if I need them finer/less pulpy). Special tip when making \\\n", + "smoothies, finely cut and freeze the fruits and \\\n", + "vegetables (if using spinach-lightly stew soften the \\ \n", + "spinach then freeze until ready for use-and if making \\\n", + "sorbet, use a small to medium sized food processor) \\ \n", + "that you plan to use that way you can avoid adding so \\\n", + "much ice if at all-when making your smoothie. \\\n", + "After about a year, the motor was making a funny noise. \\\n", + "I called customer service but the warranty expired \\\n", + "already, so I had to buy another one. FYI: The overall \\\n", + "quality has gone done in these types of products, so \\\n", + "they are kind of counting on brand recognition and \\\n", + "consumer loyalty to maintain sales. Got it in about \\\n", + "two days.\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "# 我们可以在推理那章学习到如何对一个评论判断其情感倾向\n", + "sentiment = \"negative\"\n", + "\n", + "# 一个产品的评价\n", + "review = f\"\"\"\n", + "他们在11月份的季节性销售期间以约49美元的价格出售17件套装,折扣约为一半。\\\n", + "但由于某些原因(可能是价格欺诈),到了12月第二周,同样的套装价格全都涨到了70美元到89美元不等。\\\n", + "11件套装的价格也上涨了大约10美元左右。\\\n", + "虽然外观看起来还可以,但基座上锁定刀片的部分看起来不如几年前的早期版本那么好。\\\n", + "不过我打算非常温柔地使用它,例如,\\\n", + "我会先在搅拌机中将像豆子、冰、米饭等硬物研磨,然后再制成所需的份量,\\\n", + "切换到打蛋器制作更细的面粉,或者在制作冰沙时先使用交叉切割刀片,然后使用平面刀片制作更细/不粘的效果。\\\n", + "制作冰沙时,特别提示:\\\n", + "将水果和蔬菜切碎并冷冻(如果使用菠菜,则轻轻煮软菠菜,然后冷冻直到使用;\\\n", + "如果制作果酱,则使用小到中号的食品处理器),这样可以避免在制作冰沙时添加太多冰块。\\\n", + "大约一年后,电机发出奇怪的噪音,我打电话给客服,但保修已经过期了,所以我不得不再买一个。\\\n", + "总的来说,这些产品的总体质量已经下降,因此它们依靠品牌认可和消费者忠诚度来维持销售。\\\n", + "货物在两天内到达。\n", + "\"\"\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "我们已经使用推断课程中学到的提取了情感,这是一个关于搅拌机的客户评价,现在我们将根据情感定制回复。\n", + "\n", + "这里的指令是:假设你是一个客户服务AI助手,你的任务是为客户发送电子邮件回复,根据通过三个反引号分隔的客户电子邮件,生成一封回复以感谢客户的评价。" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Dear Valued Customer,\n", + "\n", + "Thank you for taking the time to leave a review about our product. We are sorry to hear that you experienced an increase in price and that the quality of the product did not meet your expectations. We apologize for any inconvenience this may have caused you.\n", + "\n", + "We would like to assure you that we take all feedback seriously and we will be sure to pass your comments along to our team. If you have any further concerns, please do not hesitate to reach out to our customer service team for assistance.\n", + "\n", + "Thank you again for your review and for choosing our product. We hope to have the opportunity to serve you better in the future.\n", + "\n", + "Best regards,\n", + "\n", + "AI customer agent\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "You are a customer service AI assistant.\n", + "Your task is to send an email reply to a valued customer.\n", + "Given the customer email delimited by ```, \\\n", + "Generate a reply to thank the customer for their review.\n", + "If the sentiment is positive or neutral, thank them for \\\n", + "their review.\n", + "If the sentiment is negative, apologize and suggest that \\\n", + "they can reach out to customer service. \n", + "Make sure to use specific details from the review.\n", + "Write in a concise and professional tone.\n", + "Sign the email as `AI customer agent`.\n", + "Customer review: ```{review}```\n", + "Review sentiment: {sentiment}\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "尊敬的客户,\n", + "\n", + "非常感谢您对我们产品的评价。我们非常抱歉您在购买过程中遇到了价格上涨的问题。我们一直致力于为客户提供最优惠的价格,但由于市场波动,价格可能会有所变化。我们深表歉意,如果您需要任何帮助,请随时联系我们的客户服务团队。\n", + "\n", + "我们非常感谢您对我们产品的详细评价和使用技巧。我们将会把您的反馈传达给我们的产品团队,以便改进我们的产品质量和性能。\n", + "\n", + "再次感谢您对我们的支持和反馈。如果您需要任何帮助或有任何疑问,请随时联系我们的客户服务团队。\n", + "\n", + "祝您一切顺利!\n", + "\n", + "AI客户代理\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "你是一位客户服务的AI助手。\n", + "你的任务是给一位重要客户发送邮件回复。\n", + "根据客户通过“```”分隔的评价,生成回复以感谢客户的评价。提醒模型使用评价中的具体细节\n", + "用简明而专业的语气写信。\n", + "作为“AI客户代理”签署电子邮件。\n", + "客户评论:\n", + "```{review}```\n", + "评论情感:{sentiment}\n", + "\"\"\"\n", + "response = get_completion(prompt)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 三、使用温度系数\n", + "\n", + "接下来,我们将使用语言模型的一个称为“温度”的参数,它将允许我们改变模型响应的多样性。您可以将温度视为模型探索或随机性的程度。\n", + "\n", + "例如,在一个特定的短语中,“我的最爱食品”最有可能的下一个词是“比萨”,其次最有可能的是“寿司”和“塔可”。因此,在温度为零时,模型将总是选择最有可能的下一个词,而在较高的温度下,它还将选择其中一个不太可能的词,在更高的温度下,它甚至可能选择塔可,而这种可能性仅为五分之一。您可以想象,随着模型继续生成更多单词的最终响应,“我的最爱食品是比萨”将会与第一个响应“我的最爱食品是塔可”产生差异。因此,随着模型的继续,这两个响应将变得越来越不同。\n", + "\n", + "一般来说,在构建需要可预测响应的应用程序时,我建议使用温度为零。在所有课程中,我们一直设置温度为零,如果您正在尝试构建一个可靠和可预测的系统,我认为您应该选择这个温度。如果您尝试以更具创意的方式使用模型,可能需要更广泛地输出不同的结果,那么您可能需要使用更高的温度。" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "# given the sentiment from the lesson on \"inferring\",\n", + "# and the original customer message, customize the email\n", + "sentiment = \"negative\"\n", + "\n", + "# review for a blender\n", + "review = f\"\"\"\n", + "So, they still had the 17 piece system on seasonal \\\n", + "sale for around $49 in the month of November, about \\\n", + "half off, but for some reason (call it price gouging) \\\n", + "around the second week of December the prices all went \\\n", + "up to about anywhere from between $70-$89 for the same \\\n", + "system. And the 11 piece system went up around $10 or \\\n", + "so in price also from the earlier sale price of $29. \\\n", + "So it looks okay, but if you look at the base, the part \\\n", + "where the blade locks into place doesn’t look as good \\\n", + "as in previous editions from a few years ago, but I \\\n", + "plan to be very gentle with it (example, I crush \\\n", + "very hard items like beans, ice, rice, etc. in the \\ \n", + "blender first then pulverize them in the serving size \\\n", + "I want in the blender then switch to the whipping \\\n", + "blade for a finer flour, and use the cross cutting blade \\\n", + "first when making smoothies, then use the flat blade \\\n", + "if I need them finer/less pulpy). Special tip when making \\\n", + "smoothies, finely cut and freeze the fruits and \\\n", + "vegetables (if using spinach-lightly stew soften the \\ \n", + "spinach then freeze until ready for use-and if making \\\n", + "sorbet, use a small to medium sized food processor) \\ \n", + "that you plan to use that way you can avoid adding so \\\n", + "much ice if at all-when making your smoothie. \\\n", + "After about a year, the motor was making a funny noise. \\\n", + "I called customer service but the warranty expired \\\n", + "already, so I had to buy another one. FYI: The overall \\\n", + "quality has gone done in these types of products, so \\\n", + "they are kind of counting on brand recognition and \\\n", + "consumer loyalty to maintain sales. Got it in about \\\n", + "two days.\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Dear valued customer,\n", + "\n", + "Thank you for taking the time to share your review with us. We are sorry to hear that you were disappointed with the prices of our products and the quality of our blender. We apologize for any inconvenience this may have caused you.\n", + "\n", + "We value your feedback and would like to make things right for you. Please feel free to contact our customer service team so we can assist you with any concerns or issues you may have. We are committed to providing you with the best possible service and products.\n", + "\n", + "Thank you again for your review and for being a loyal customer. We hope to have the opportunity to serve you better in the future.\n", + "\n", + "Sincerely,\n", + "AI customer agent\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "You are a customer service AI assistant.\n", + "Your task is to send an email reply to a valued customer.\n", + "Given the customer email delimited by ```, \\\n", + "Generate a reply to thank the customer for their review.\n", + "If the sentiment is positive or neutral, thank them for \\\n", + "their review.\n", + "If the sentiment is negative, apologize and suggest that \\\n", + "they can reach out to customer service. \n", + "Make sure to use specific details from the review.\n", + "Write in a concise and professional tone.\n", + "Sign the email as `AI customer agent`.\n", + "Customer review: ```{review}```\n", + "Review sentiment: {sentiment}\n", + "\"\"\"\n", + "response = get_completion(prompt, temperature=0.7)\n", + "print(response)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "尊敬的客户,\n", + "\n", + "非常感谢您对我们产品的评价。我们由衷地为您在购买过程中遇到的问题表示抱歉。我们确实在12月份的第二周调整了价格,但这是由于市场因素所致,并非价格欺诈。我们深刻意识到您对产品质量的担忧,我们将尽一切努力改进产品,以提供更好的体验。\n", + "\n", + "我们非常感激您对我们产品的使用经验和制作技巧的分享。您的建议和反馈对我们非常重要,我们将以此为基础,进一步改进我们的产品。\n", + "\n", + "如果您有任何疑问或需要进一步帮助,请随时联系我们的客户服务部门。我们将尽快回复您并提供帮助。\n", + "\n", + "最后,请再次感谢您对我们产品的评价和选择。我们期待着未来与您的合作。\n", + "\n", + "此致\n", + "\n", + "敬礼\n", + "\n", + "AI客户代理\n" + ] + } + ], + "source": [ + "prompt = f\"\"\"\n", + "你是一名客户服务的AI助手。\n", + "你的任务是给一位重要的客户发送邮件回复。\n", + "根据通过“```”分隔的客户电子邮件生成回复,以感谢客户的评价。\n", + "如果情感是积极的或中性的,感谢他们的评价。\n", + "如果情感是消极的,道歉并建议他们联系客户服务。\n", + "请确保使用评论中的具体细节。\n", + "以简明和专业的语气写信。\n", + "以“AI客户代理”的名义签署电子邮件。\n", + "客户评价:```{review}```\n", + "评论情感:{sentiment}\n", + "\"\"\"\n", + "response = get_completion(prompt, temperature=0.7)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "在温度为零时,每次执行相同的提示时,您应该期望获得相同的完成。而使用温度为0.7,则每次都会获得不同的输出。\n", + "\n", + "所以,您可以看到它与我们之前收到的电子邮件不同。让我们再次执行它,以显示我们将再次获得不同的电子邮件。\n", + "\n", + "因此,我建议您自己尝试温度,以查看输出如何变化。总之,在更高的温度下,模型的输出更加随机。您几乎可以将其视为在更高的温度下,助手更易分心,但也许更有创造力。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}