diff --git a/docs/content/C1 Prompt Engineering for Developer/1. 简介 Introduction.md b/docs/content/C1 Prompt Engineering for Developer/1. 简介 Introduction.md
new file mode 100644
index 0000000..5df6977
--- /dev/null
+++ b/docs/content/C1 Prompt Engineering for Developer/1. 简介 Introduction.md
@@ -0,0 +1,21 @@
+# 第一章 简介
+
+**作者 吴恩达教授**
+
+欢迎来到本课程,我们将为开发人员介绍 ChatGPT 提示词工程(Prompt Engineering)。本课程由 Isa Fulford 教授和我一起授课。Isa 是 OpenAI 的技术团队成员,曾开发过受欢迎的 ChatGPT 检索插件,并且在教授 LLM (Large Language Model, 大语言模型)技术在产品中的应用方面做出了很大贡献。她还参与编写了教授人们使用 Prompt 的 OpenAI cookbook。
+
+互联网上有很多有关提示词(Prompt, 本教程中将保留该术语)的材料,例如《30 prompts everyone has to know》之类的文章。这些文章主要集中在 ChatGPT 的 Web 界面上,许多人在使用它执行特定的、通常是一次性的任务。但是,我认为对于开发人员,LLM 的更强大功能是能通过 API 调用,从而快速构建软件应用程序。我认为这方面还没有得到充分的重视。实际上,我们在 DeepLearning.AI 的姊妹公司 AI Fund 的团队一直在与许多初创公司合作,将这些技术应用于诸多应用程序上。很兴奋能看到 LLM API 能够让开发人员非常快速地构建应用程序。
+
+在本课程中,我们将与您分享一些技巧,来挖掘 LLM 的潜力,也会提供应用上的最佳实践。过程中会涉及大量材料。首先,你会学习到用于软件开发的 Prompt 最佳实践,随后会涉及到几个常用使用例,包括概括、推断、转换与扩展,最后会利用 LLM 构建 chatbot(聊天机器人)。希望这能激发你的想象力,去开拓新应用。
+
+随着 LLM 的发展,其大致可以分为两种类型,后续称为基础 LLM 和指令微调(Instruction Tuned)LLM。基础LLM是基于文本训练数据,训练出预测下一个单词能力的模型。其通常通过在互联网和其他来源的大量数据上训练,来确定紧接着出现的最可能的词。例如,如果你以“从前,有一只独角兽”作为 Prompt ,基础 LLM 可能会继续预测“她与独角兽朋友共同生活在一片神奇森林中”。但是,如果你以“法国的首都是什么”为 Prompt ,则基础 LLM 可能会根据互联网上的文章,将回答预测为“法国最大的城市是什么?法国的人口是多少?”,因为互联网上的文章很可能是有关法国国家的问答题目列表。
+
+而对于指令微调的 LLM ,相关研究和实践正甚嚣尘上,训练它们来遵循指示。因此,如果你问它,“法国的首都是什么?”,它有极大可能输出“法国的首都是巴黎”。指令微调的LLM的训练通常是基于预训练好的LLM的,即模型已经在大量文本数据上进行了训练。然后对其进行进一步训练与微调(finetune),使用的数据包括输入和理想输出(输入是指令、输出是遵循这些指令的良好回答)。然后通常使用一种称为 RLHF(reinforcement learning from human feedback,人类反馈强化学习)的技术进行进一步改进,使系统更能够有帮助地遵循指令。
+
+因为指令微调的 LLM 已经被训练成有益、诚实、无害的,所以与基础 LLM 相比,它们更不可能输出有问题的文本,如有害输出。许多实际使用场景已经转向指令微调的 LLM 。您在互联网上找到的一些最佳实践可能更适用于基础 LLM ,但对于今天的大多数实际应用,我们建议将注意力集中在指令微调的 LLM 上,这些 LLM 更容易使用,而且由于 OpenAI 和其他 LLM 公司的工作,它们变得更加安全,也更加协调。
+
+因此,本课程将重点介绍**针对指令微调 LLM 的最佳实践**,我们也建议您将其用于大多数使用场景。在继续之前,我想感谢 OpenAI 和 DeepLearning.ai 团队为 Isa 和我所提供的材料作出的贡献。我非常感激 OpenAI 的 Andrew Main、Joe Palermo、Boris Power、Ted Sanders 和 Lillian Weng,他们参与了我们的头脑风暴材料的制定和审核,为这个短期课程编制了课程大纲。我也感激 Deep Learning 方面的 Geoff Ladwig、Eddy Shyu 和 Tommy Nelson 的工作。
+
+当您使用指令微调 LLM 时,您可以类比为向另一个人提供指令(假设他很聪明但不知道您任务的具体细节)。因此,当 LLM 无法正常工作时,有时是因为指令不够清晰。例如,如果您想问“请为我写一些关于阿兰·图灵( Alan Turing )的东西”,在此基础上清楚表明您希望文本专注于他的科学工作、个人生活、历史角色或其他方面可能会更有帮助。另外您还可以指定回答的语调, 来更加满足您的需求,可选项包括*专业记者写作*,或者*向朋友写的随笔*等。
+
+如果你将 LLM 视为一名新毕业的大学生,要求他完成这个任务,你甚至可以提前指定他们应该阅读哪些文本片段来写关于 Alan Turing 的文本,这样能够帮助这位新毕业的大学生更好地完成这项任务。下一章你会看到提示词创建的两个原则,一是**清晰明确**,二是**给LLM时间去思考**。
diff --git a/docs/content/C1 Prompt Engineering for Developer/1. 简介.md b/docs/content/C1 Prompt Engineering for Developer/1. 简介.md
deleted file mode 100644
index e69de29..0000000
diff --git a/docs/content/C1 Prompt Engineering for Developer/2. 提示原则 Guidelines.ipynb b/docs/content/C1 Prompt Engineering for Developer/2. 提示原则 Guidelines.ipynb
new file mode 100644
index 0000000..2b2e570
--- /dev/null
+++ b/docs/content/C1 Prompt Engineering for Developer/2. 提示原则 Guidelines.ipynb
@@ -0,0 +1 @@
+{"cells":[{"attachments":{},"cell_type":"markdown","metadata":{},"source":["# 第二章 提示原则 Guidelines"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["\n","您应该通过提供尽可能清晰和具体的指令来表达您希望模型执行的操作。这将引导模型给出正确的输出,并降低您得到无关或不正确响应的可能性。清晰的指令不意味着必须简短,在许多情况下,更长的 Prompt 实际上更清晰,且提供了更多上下文,也就可能产生更详细更相关的输出。"]},{"cell_type":"markdown","metadata":{},"source":["## 一、原则一 编写清晰、具体的指令"]},{"cell_type":"markdown","metadata":{},"source":["### 1.1 使用分隔符清晰地表示输入的不同部分"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["\n","分隔符可以是:```,\"\",<>,:,\\ \\等。\n","\n","您可以使用任何明显的标点符号将特定的文本部分与 Prompt 的其余部分分开。标记的形式不限,只需要让模型明确知道这是一个单独部分。使用分隔符可以有效避免提示词注入( Prompt injection )。提示词注入是指如果允许用户将某些输入添加到(开发者预定义的) Prompt 中,则所提供的指令可能会与开发者想要执行的操作相冲突,从而使 LLM 遵循用户输入的指令,而非执行开发者预期的操作。即,输入里面可能包含其他指令,会覆盖掉您的指令。对此,使用分隔符是一个不错的策略。\n","\n","在以下的例子中,我们给出一段话并要求 GPT 进行总结,在该示例中我们使用 ``` 来作为分隔符。\n"]},{"cell_type":"code","execution_count":11,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["为了获得所需的输出,您应该提供清晰、具体的指示,避免与简短的提示词混淆,并使用更长的提示词来提供更多的清晰度和上下文信息。\n"]}],"source":["from tool import get_completion\n","\n","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":["### 1.2 寻求结构化的输出"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["\n","\n","输出可以是 Json、HTML 等格式。\n","\n","第二个策略是要求生成一个结构化的输出,这可以使模型的输出更容易被我们解析,例如,您可以在 Python 中将其读入字典或列表中。\n","\n","在以下示例中,我们要求 GPT 生成三本书的标题、作者和类别,并要求 GPT 以 Json 的格式返回给我们,为便于解析,我们指定了 Json 的键。"]},{"cell_type":"code","execution_count":15,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["{\n"," \"books\": [\n"," {\n"," \"book_id\": 1,\n"," \"title\": \"迷失的时光\",\n"," \"author\": \"张三\",\n"," \"genre\": \"科幻\"\n"," },\n"," {\n"," \"book_id\": 2,\n"," \"title\": \"幻境之门\",\n"," \"author\": \"李四\",\n"," \"genre\": \"奇幻\"\n"," },\n"," {\n"," \"book_id\": 3,\n"," \"title\": \"虚拟现实\",\n"," \"author\": \"王五\",\n"," \"genre\": \"科幻\"\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":["### 1.3 要求模型检查是否满足条件"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["\n","\n","如果任务包含不一定能满足的假设(条件),我们可以告诉模型先检查这些假设,如果不满足,则会指出并停止执行后续的完整流程。您还可以考虑可能出现的边缘情况及模型的应对,以避免意外的结果或错误发生。\n","\n","在如下示例中,我们将分别给模型两段文本,分别是制作茶的步骤以及一段没有明确步骤的文本。我们将要求模型判断其是否包含一系列指令,如果包含则按照给定格式重新编写指令,不包含则回答“未提供步骤”。"]},{"cell_type":"code","execution_count":16,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Text 1 的总结:\n","第一步 - 把水烧开。\n","第二步 - 拿一个杯子并把茶包放进去。\n","第三步 - 把烧开的水倒在茶包上。\n","第四步 - 等待几分钟,让茶叶浸泡。\n","第五步 - 取出茶包。\n","第六步 - 如果需要,加入糖或牛奶调味。\n","第七步 - 就这样,您可以享受一杯美味的茶了。\n"]}],"source":["# 满足条件的输入(text中提供了步骤)\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":17,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Text 2 的总结:\n","未提供步骤。\n"]}],"source":["# 不满足条件的输入(text中未提供预期指令)\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":["### 1.4 提供少量示例"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["\n","\n","即在要求模型执行实际任务之前,提供给它少量成功执行任务的示例。\n","\n","例如,在以下的示例中,我们告诉模型其任务是以一致的风格回答问题,并先给它一个孩子和祖父之间的对话的例子。孩子说,“请教我何为耐心”,祖父用下述风格的隐喻来回答。由于我们已经告诉模型要以一致的语气回答,因此现在我们问“请教我何为韧性”,由于模型已经有了这个少样本示例( few-shot example ),它将以类似的语气回答下一个任务。"]},{"cell_type":"code","execution_count":22,"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)"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["## 二、原则二 给模型时间去思考\n","\n","如果您发现模型推理过程过于匆忙,导致得出了错误的结论,那么您应该尝试重新构思 Prompt ,要求模型在提供最终答案之前开展**思维链**,或进行一系列相关推理(a chain or series of relevant reasoning)。换句话说,如果您给模型一个在短时间内或用少量文字无法完成的复杂任务,它的输出结果就容易出错。这种情况对人来说也是类似:如果您要求某人完成复杂的数学问题,又不给足够时间计算出答案,他们也可能会犯错误。因此,在这些情况下,您应该指示模型花更多时间思考问题,让它在任务上花费更多计算资源。"]},{"cell_type":"markdown","metadata":{},"source":["### 2.1 指定完成任务所需的步骤"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["\n","\n","接下来我们将通过给定一个复杂任务,给出完成该任务的一系列步骤,来展示这一策略的效果。"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["首先我们描述了杰克和吉尔的故事,并给出提示词执行以下操作:首先,用一句话概括三个反引号限定的文本。第二,将摘要翻译成英语。第三,在英语摘要中列出每个名称。第四,输出包含以下键的 JSON 对象:英语摘要和人名个数。要求输出以换行符分隔。"]},{"cell_type":"code","execution_count":28,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["prompt 1:\n","1-两个兄妹在山上打水时发生意外,但最终平安回家。\n","2-In a charming village, siblings Jack and Jill set off to fetch water from a well on top of a hill. While singing joyfully, they climbed up, but unfortunately, Jack tripped on a stone and rolled down the hill, with Jill following closely behind. Despite some minor injuries, they made it back to their cozy home. Despite the mishap, their adventurous spirit remained undiminished as they continued to explore with delight.\n","3-Jack, Jill\n","4-{\"english_summary\": \"In a charming village, siblings Jack and Jill set off to fetch water from a well on top of a hill. While singing joyfully, they climbed up, but unfortunately, Jack tripped on a stone and rolled down the hill, with Jill following closely behind. Despite some minor injuries, they made it back to their cozy home. Despite the mishap, their adventurous spirit remained undiminished as they continued to explore with delight.\", \"num_names\": 2}\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 对象,其中包含以下键:english_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":[]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["上述输出仍然存在一定问题,例如,键“姓名”会被替换为法语(译注:在英文原版中,要求从英语翻译到法语,对应指令第三步的输出为 'Noms:',为Name的法语,这种行为难以预测,并可能为导出带来困难)\n","\n","因此,我们将Prompt加以改进,该 Prompt 前半部分不变,同时**确切指定了输出的格式**。"]},{"cell_type":"code","execution_count":29,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["\n","prompt 2:\n","Summary: 在一个迷人的村庄里,兄妹杰克和吉尔在山顶井里打水时发生了意外,但他们的冒险精神依然没有减弱,继续充满愉悦地探索。\n","\n","Translation: In a charming village, siblings Jack and Jill set off to fetch water from a well on top of a hill. Unfortunately, Jack tripped on a rock and tumbled down the hill, with Jill following closely behind. Despite some minor injuries, they made it back home safely. Despite the mishap, their adventurous spirit remained strong as they continued to explore joyfully.\n","\n","Names: Jack, Jill\n","\n","JSON Output: {\"English_summary\": \"In a charming village, siblings Jack and Jill set off to fetch water from a well on top of a hill. Unfortunately, Jack tripped on a rock and tumbled down the hill, with Jill following closely behind. Despite some minor injuries, they made it back home safely. Despite the mishap, their adventurous spirit remained strong as they continued to explore joyfully.\", \"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)"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":[" "]},{"cell_type":"markdown","metadata":{},"source":["### 2.2 指导模型在下结论之前找出一个自己的解法"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["\n","\n","明确地指引模型在匆匆做决策之前,要自己思考出一份解决方案。有时这样会得到更好的结果。这与之前所述思想类似,即给模型时间思考。\n","\n","接下来我们会给出一个问题和一份来自学生的解答,要求模型判断解答是否正确:"]},{"cell_type":"code","execution_count":30,"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":[]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["但是注意,学生的解决方案实际上是错误的。(*维护费用项100x应为10x,总费用450x应为360x*)\n","\n","我们可以通过指导模型先自行找出一个解法来解决这个问题。\n","\n","在接下来这个 Prompt 中,我们要求模型先自行解决这个问题,再根据自己的解法与学生的解法进行对比,从而判断学生的解法是否正确。同时,我们给定了输出的格式要求。通过拆分任务、明确步骤,让模型有更多时间思考,有时可以获得更准确的结果。在这个例子中,学生的答案是错误的,但如果我们没有先让模型自己计算,那么可能会被误导以为学生是正确的。"]},{"cell_type":"code","execution_count":40,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["实际解决方案和步骤:\n","\n"," 1. 土地费用:每平方英尺100美元,所以总费用为100x美元。\n"," 2. 太阳能电池板费用:每平方英尺250美元,所以总费用为250x美元。\n"," 3. 维护费用:固定费用为10万美元,额外费用为每平方英尺10美元,所以总费用为10万美元+10x美元。\n"," 4. 总费用:将上述三项费用相加,得到总费用为100x美元+250x美元+10万美元+10x美元=360x+10万美元。\n","\n","学生计算的总费用:450x+10万美元\n","实际计算的总费用:360x+10万美元\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","\n","问题:\n","\n"," 我正在建造一个太阳能发电站,需要帮助计算财务。 \n"," - 土地费用为每平方英尺100美元\n"," - 我可以以每平方英尺250美元的价格购买太阳能电池板\n"," - 我已经谈判好了维护合同,每年需要支付固定的10万美元,并额外支付每平方英尺10美元;\n","\n"," 作为平方英尺数的函数,首年运营的总费用是多少。\n","\n","学生的解决方案:\n","\n"," 设x为发电站的大小,单位为平方英尺。\n"," 费用:\n"," 1. 土地费用:100x美元\n"," 2. 太阳能电池板费用:250x美元\n"," 3. 维护费用:100,000+100x=10万美元+10x美元\n"," 总费用:100x美元+250x美元+10万美元+100x美元=450x+10万美元\n","\n","实际解决方案和步骤:\n","\"\"\"\n","response = get_completion(prompt)\n","print(response)"]},{"cell_type":"markdown","metadata":{},"source":[]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["## 三、局限性"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["**开发大模型相关应用时请务必铭记:**\n","\n","\n","**虚假知识**:模型偶尔会生成一些看似真实实则编造的知识\n","\n","虽然模型在训练过程中接触了大量的知识,但它并没有*完全*记住所见的信息,因此它不甚清楚自己知识的边界。这意味着它可能会尝试回答主题晦涩难懂的问题,并编造听起来合理但实际上并不正确的答案。我们称这些编造的想法为幻觉(Hallucination)。\n","\n","如下示例展示了大模型的幻觉。我们要求告诉我们华为公司生产的 *GT Watch 运动手表* 产品的信息,事实上,这个公司是真实存在的,但产品是编造的,而模型一本正经地提供了它编造的知识,而且迷惑性很强。\n","\n"]},{"cell_type":"code","execution_count":44,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["华为公司生产的GT Watch运动手表是一款智能手表,具有多种功能和特点。以下是相关信息:\n","\n","1. 设计和外观:GT Watch采用圆形表盘设计,具有精致的外观和高质量的材料制造。它有多种颜色和表带选择,可以根据个人喜好进行定制。\n","\n","2. 显示屏:GT Watch配备了1.39英寸的AMOLED显示屏,具有高清分辨率和良好的可视性。用户可以通过触摸屏幕进行操作和导航。\n","\n","3. 运动追踪:GT Watch具有全天候的运动追踪功能,可以监测用户的步数、跑步距离、卡路里消耗和心率等数据。它还支持多种运动模式,如跑步、骑行、游泳等。\n","\n","4. 健康监测:GT Watch可以监测用户的心率、血氧饱和度和睡眠质量等健康指标。它还提供健康建议和提醒,帮助用户保持良好的健康状态。\n","\n","5. 通知和连接:GT Watch可以与用户的手机进行连接,通过蓝牙技术实现通知推送和电话提醒。用户可以在手表上查看短信、电话和社交媒体通知,无需拿出手机。\n","\n","6. 长续航时间:GT Watch具有较长的续航时间,一次充电可以使用数天。它还支持快速充电技术,可以在短时间内充满电。\n","\n","7. 其他功能:GT Watch还具有其他功能,如天气预报、闹钟、计时器、计步器等。它还支持NFC支付和音乐控制等便利功能。\n","\n","总体而言,华为GT Watch是一款功能强大、外观精致的智能运动手表,适合那些注重健康和运动的用户使用。\n"]}],"source":["prompt = f\"\"\"\n","告诉我华为公司生产的GT Watch运动手表的相关信息\n","\"\"\"\n","response = get_completion(prompt)\n","print(response)"]},{"cell_type":"markdown","metadata":{},"source":[]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["由于很容易以假乱真,请读者根据在本系列教程中所学知识,在构建自己的应用程序时尽量避免幻觉情况。幻觉是大模型的一个已知缺陷(注:截至2023年7月),OpenAI也在努力解决该问题。\n","\n","在您希望模型根据文本生成回答时,另一种减少幻觉的策略是先要求模型获取来源于该文本的所有引用信息(任何相关引用,any relevant quotes),然后要求它基于所引用的信息来回答问题,这使得我们能根据答案追溯源文档,通常对减少幻觉非常有帮助。"]},{"attachments":{},"cell_type":"markdown","metadata":{},"source":["**关于反斜杠使用的说明:**\n","\n","在本教程中,我们使用反斜杠 \\ 来使文本适应屏幕大小以提高阅读体验,而没有用换行符 \\n 。GPT-3 并不受换行符(newline characters)的影响,但在您调用其他大模型时,需额外考虑换行符是否会影响模型性能。"]},{"cell_type":"markdown","metadata":{},"source":["## 四、英文原版 Prompt"]},{"cell_type":"markdown","metadata":{},"source":["**1.1 使用分隔符清晰地表示输入的不同部分**"]},{"cell_type":"code","execution_count":45,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["To guide a model towards the desired output and reduce irrelevant or incorrect responses, it is important to provide clear and specific instructions, which can be achieved through longer prompts that offer more clarity and context.\n"]}],"source":["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":"markdown","metadata":{},"source":["**1.2**寻求结构化的输出"]},{"cell_type":"code","execution_count":46,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["{\n"," \"books\": [\n"," {\n"," \"book_id\": 1,\n"," \"title\": \"The Enigma of Elysium\",\n"," \"author\": \"Evelyn Sinclair\",\n"," \"genre\": \"Mystery\"\n"," },\n"," {\n"," \"book_id\": 2,\n"," \"title\": \"Whispers in the Wind\",\n"," \"author\": \"Nathaniel Blackwood\",\n"," \"genre\": \"Fantasy\"\n"," },\n"," {\n"," \"book_id\": 3,\n"," \"title\": \"Echoes of the Past\",\n"," \"author\": \"Amelia Hart\",\n"," \"genre\": \"Romance\"\n"," }\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":"markdown","metadata":{},"source":["**1.3 要求模型检查是否满足条件**"]},{"cell_type":"code","execution_count":56,"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 - If you like, add some sugar or milk to taste.\n","Step 7 - Enjoy your delicious cup of tea.\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":48,"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":"markdown","metadata":{},"source":["**1.4 提供少量示例**(少样本提示词,Few-shot prompting)"]},{"cell_type":"code","execution_count":49,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":[": Resilience is like a mighty oak tree that withstands the strongest storms, bending but never breaking. It is the unwavering determination to rise again after every fall, and the ability to find strength in the face of adversity. Just as a diamond is formed under immense pressure, resilience is forged through challenges and hardships, making us stronger and more resilient in the process.\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":"markdown","metadata":{},"source":["**2.1 指定完成任务所需的步骤**"]},{"cell_type":"code","execution_count":50,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Completion for prompt 1:\n","1 - Jack and Jill, siblings, go on a quest to fetch water from a hilltop well, but encounter misfortune when Jack trips on a stone and tumbles down the hill, with Jill following suit, yet they return home and remain undeterred in their adventurous spirits.\n","\n","2 - Jack et Jill, frère et sœur, partent en quête d'eau d'un puits au sommet d'une colline, mais rencontrent un malheur lorsque Jack trébuche sur une pierre et dévale la colline, suivi par Jill, pourtant ils rentrent chez eux et restent déterminés dans leur esprit d'aventure.\n","\n","3 - Jack, Jill\n","\n","4 - {\n"," \"french_summary\": \"Jack et Jill, frère et sœur, partent en quête d'eau d'un puits au sommet d'une colline, mais rencontrent un malheur lorsque Jack trébuche sur une pierre et dévale la colline, suivi par Jill, pourtant ils rentrent chez eux et restent déterminés dans leur esprit d'aventure.\",\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":51,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["\n","Completion for prompt 2:\n","Summary: Jack and Jill, siblings from a charming village, go on a quest to fetch water from a hilltop well, but encounter misfortune when Jack trips on a stone and tumbles down the hill, with Jill following suit, yet they remain undeterred and continue exploring with delight.\n","\n","Translation: Jack et Jill, frère et sœur d'un charmant village, partent en quête d'eau d'un puits au sommet d'une colline, mais rencontrent un malheur lorsque Jack trébuche sur une pierre et dévale la colline, suivi par Jill, pourtant ils restent déterminés et continuent à explorer avec joie.\n","\n","Names: Jack, Jill\n","\n","Output JSON: \n","{\n"," \"french_summary\": \"Jack et Jill, frère et sœur d'un charmant village, partent en quête d'eau d'un puits au sommet d'une colline, mais rencontrent un malheur lorsque Jack trébuche sur une pierre et dévale la colline, suivi par Jill, pourtant ils restent déterminés et continuent à explorer avec joie.\",\n"," \"num_names\": 2\n","}\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":"markdown","metadata":{},"source":["**2.2 指导模型在下结论之前找出一个自己的解法**"]},{"cell_type":"code","execution_count":52,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["The student's solution is correct. They correctly identified the costs for land, solar panels, and maintenance, and calculated the total cost for the first year of operations as a function of the number of square feet.\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","Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000\n","\"\"\"\n","response = get_completion(prompt)\n","print(response)"]},{"cell_type":"code","execution_count":53,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["To calculate the total cost for the first year of operations, we need to add up the costs of land, solar panels, and maintenance.\n","\n","1. Land cost: $100 / square foot\n","The cost of land is $100 multiplied by the number of square feet.\n","\n","2. Solar panel cost: $250 / square foot\n","The cost of solar panels is $250 multiplied by the number of square feet.\n","\n","3. Maintenance cost: $100,000 + $10 / square foot\n","The maintenance cost is a flat fee of $100,000 per year, plus $10 multiplied by the number of square feet.\n","\n","Total cost: Land cost + Solar panel cost + Maintenance cost\n","\n","So the actual solution is:\n","Total cost = (100 * x) + (250 * x) + (100,000 + (10 * x))\n","\n","Is the student's solution the same as the 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":"markdown","metadata":{},"source":["**3.1 幻觉**"]},{"cell_type":"code","execution_count":54,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["The AeroGlide UltraSlim Smart Toothbrush by Boie is a technologically advanced toothbrush designed to provide a superior brushing experience. Boie is a company known for its innovative oral care products, and the AeroGlide UltraSlim Smart Toothbrush is no exception.\n","\n","One of the standout features of this toothbrush is its ultra-slim design. The brush head is only 2mm thick, making it much thinner than traditional toothbrushes. This slim profile allows for better access to hard-to-reach areas of the mouth, ensuring a thorough and effective clean.\n","\n","The AeroGlide UltraSlim Smart Toothbrush also incorporates smart technology. It connects to a mobile app via Bluetooth, allowing users to track their brushing habits and receive personalized recommendations for improving their oral hygiene routine. The app provides real-time feedback on brushing technique, duration, and coverage, helping users to achieve optimal oral health.\n","\n","The toothbrush features soft, antimicrobial bristles made from a durable thermoplastic elastomer. These bristles are gentle on the gums and teeth, while also being effective at removing plaque and debris. The antimicrobial properties help to keep the brush head clean and hygienic between uses.\n","\n","Another notable feature of the AeroGlide UltraSlim Smart Toothbrush is its long battery life. It can last up to 30 days on a single charge, making it convenient for travel or everyday use without the need for frequent recharging.\n","\n","Overall, the AeroGlide UltraSlim Smart Toothbrush by Boie offers a combination of advanced technology, slim design, and effective cleaning capabilities. It is a great option for those looking to upgrade their oral care routine and achieve a healthier smile.\n"]}],"source":["prompt = f\"\"\"\n","Tell me about AeroGlide UltraSlim Smart Toothbrush by Boie\n","\"\"\"\n","response = get_completion(prompt)\n","print(response)"]}],"metadata":{"kernelspec":{"display_name":"Python 3","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.10.11"},"latex_envs":{"LaTeX_envs_menu_present":true,"autoclose":false,"autocomplete":true,"bibliofile":"biblio.bib","cite_by":"apalike","current_citInitial":1,"eqLabelWithNumbers":true,"eqNumInitial":1,"hotkeys":{"equation":"Ctrl-E","itemize":"Ctrl-I"},"labels_anchors":false,"latex_user_defs":false,"report_style_numbering":false,"user_envs_cfg":false},"toc":{"base_numbering":1,"nav_menu":{},"number_sections":true,"sideBar":true,"skip_h1_title":false,"title_cell":"Table of Contents","title_sidebar":"Contents","toc_cell":false,"toc_position":{},"toc_section_display":true,"toc_window_display":true}},"nbformat":4,"nbformat_minor":4}