更新Building Systems with the ChatGPT API第五章

This commit is contained in:
joyenjoye
2023-07-22 18:36:10 +08:00
parent f80878aa0e
commit f3788aafbe

View File

@ -0,0 +1,501 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 第五章 处理输入-思维链推理"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"在本章中,我们将学习处理输入,通过一系列步骤生成有用的输出。\n",
"\n",
"模型在回答特定问题之前需要进行详细地推理否者可能会因为过于匆忙得出结论而在推理过程中出错。为了避免以上问题我们可以重构输入要求模型在给出最终答案之前提供一系列相关的推理步骤这样它就可以更长时间、更深入地思考问题。这种要求模型逐步推理问题的策略为思维链推理Chain of Thought Reasoning。"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%cd -q ../../../src/"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import openai\n",
"import pandas as pd\n",
"from io import StringIO\n",
"from tool import get_completion, get_completion_from_messages"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 一、思维链提示设计\n",
"\n",
"思维链提示设计Chain of Thought Prompting是通过设计系统消息要求模型在得出结论之前一步一步推理答案。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.1 系统消息设计"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"delimiter = \"====\"\n",
"\n",
"system_message = f\"\"\"\n",
"请按照以下步骤回答客户的提问。客户的提问将以{delimiter}分隔。\n",
"\n",
"步骤 1:{delimiter}首先确定用户是否正在询问有关特定产品或产品的问题。产品类别不计入范围。\n",
"\n",
"步骤 2:{delimiter}如果用户询问特定产品,请确认产品是否在以下列表中。所有可用产品:\n",
"\n",
"产品TechPro 超极本\n",
"类别:计算机和笔记本电脑\n",
"品牌TechPro\n",
"型号TP-UB100\n",
"保修期1 年\n",
"评分4.5\n",
"特点13.3 英寸显示屏8GB RAM256GB SSDIntel Core i5 处理器\n",
"描述:一款适用于日常使用的时尚轻便的超极本。\n",
"价格:$799.99\n",
"\n",
"产品BlueWave 游戏笔记本电脑\n",
"类别:计算机和笔记本电脑\n",
"品牌BlueWave\n",
"型号BW-GL200\n",
"保修期2 年\n",
"评分4.7\n",
"特点15.6 英寸显示屏16GB RAM512GB SSDNVIDIA GeForce RTX 3060\n",
"描述:一款高性能的游戏笔记本电脑,提供沉浸式体验。\n",
"价格:$1199.99\n",
"\n",
"产品PowerLite 可转换笔记本电脑\n",
"类别:计算机和笔记本电脑\n",
"品牌PowerLite\n",
"型号PL-CV300\n",
"保修期1年\n",
"评分4.3\n",
"特点14 英寸触摸屏8GB RAM256GB SSD360 度铰链\n",
"描述:一款多功能可转换笔记本电脑,具有响应触摸屏。\n",
"价格:$699.99\n",
"\n",
"产品TechPro 台式电脑\n",
"类别:计算机和笔记本电脑\n",
"品牌TechPro\n",
"型号TP-DT500\n",
"保修期1年\n",
"评分4.4\n",
"特点Intel Core i7 处理器16GB RAM1TB HDDNVIDIA GeForce GTX 1660\n",
"描述:一款功能强大的台式电脑,适用于工作和娱乐。\n",
"价格:$999.99\n",
"\n",
"产品BlueWave Chromebook\n",
"类别:计算机和笔记本电脑\n",
"品牌BlueWave\n",
"型号BW-CB100\n",
"保修期1 年\n",
"评分4.1\n",
"特点11.6 英寸显示屏4GB RAM32GB eMMCChrome OS\n",
"描述:一款紧凑而价格实惠的 Chromebook适用于日常任务。\n",
"价格:$249.99\n",
"\n",
"步骤 3:{delimiter} 如果消息中包含上述列表中的产品,请列出用户在消息中做出的任何假设,\\\n",
"例如笔记本电脑 X 比笔记本电脑 Y 大,或者笔记本电脑 Z 有 2 年保修期。\n",
"\n",
"步骤 4:{delimiter} 如果用户做出了任何假设,请根据产品信息确定假设是否正确。\n",
"\n",
"步骤 5:{delimiter} 如果用户有任何错误的假设,请先礼貌地纠正客户的错误假设(如果适用)。\\\n",
"只提及或引用可用产品列表中的产品,因为这是商店销售的唯一五款产品。以友好的口吻回答客户。\n",
"\n",
"使用以下格式回答问题:\n",
"步骤 1: {delimiter} <步骤 1 的推理>\n",
"步骤 2: {delimiter} <步骤 2 的推理>\n",
"步骤 3: {delimiter} <步骤 3 的推理>\n",
"步骤 4: {delimiter} <步骤 4 的推理>\n",
"回复客户: {delimiter} <回复客户的内容>\n",
"\n",
"请确保每个步骤上面的回答中中使用 {delimiter} 对步骤和步骤的推理进行分隔。\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.2 用户消息测试"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.2.1 更贵的电脑"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"步骤 1: 用户询问了关于产品价格的问题。\n",
"步骤 2: 用户提到了两个产品其中一个是BlueWave Chromebook另一个是TechPro 台式电脑。\n",
"步骤 3: 用户假设BlueWave Chromebook比TechPro 台式电脑贵。\n",
"步骤 4: 根据产品信息,我们可以确定用户的假设是错误的。\n",
"回复客户: BlueWave Chromebook 的价格是 $249.99,而 TechPro 台式电脑的价格是 $999.99。因此TechPro 台式电脑比 BlueWave Chromebook 贵 $750。\n"
]
}
],
"source": [
"user_message = f\"\"\"BlueWave Chromebook 比 TechPro 台式电脑贵多少?\"\"\"\n",
"\n",
"messages = [ \n",
"{'role':'system', \n",
" 'content': system_message}, \n",
"{'role':'user', \n",
" 'content': f\"{delimiter}{user_message}{delimiter}\"}, \n",
"] \n",
"\n",
"response = get_completion_from_messages(messages)\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.2.2 你有电视么?"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"步骤 1: 我们需要确定用户是否正在询问有关特定产品或产品的问题。产品类别不计入范围。\n",
"\n",
"步骤 2: 在可用产品列表中,没有提到任何电视机产品。\n",
"\n",
"回复客户: 很抱歉,我们目前没有可用的电视机产品。我们的产品范围主要包括计算机和笔记本电脑。如果您对其他产品有任何需求或疑问,请随时告诉我们。\n"
]
}
],
"source": [
"user_message = f\"\"\"你有电视机么\"\"\"\n",
"messages = [ \n",
"{'role':'system', \n",
" 'content': system_message}, \n",
"{'role':'user', \n",
" 'content': f\"{delimiter}{user_message}{delimiter}\"}, \n",
"] \n",
"response = get_completion_from_messages(messages)\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 三、内心独白"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"在实际应用中我们并不想要将推理的过程呈现给用户。比如在辅导类应用程序中我们希望学生能够思考得出自己的答案。呈现关于学生解决方案的推理过程可能会将答案泄露。内心独白Inner Monologue本质就是隐藏模型推理过程可以用来一定程度上解决这个问题。具体而言通过让模型将部分需要隐藏的输出以结构化的方式储存以便后续解析。接下来在将结果呈现给用户之前结构化的结果被解析只有部分结果被输出并呈现给用户。"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"很抱歉,我们目前没有可用的电视机产品。我们的产品范围主要包括计算机和笔记本电脑。如果您对其他产品有任何需求或疑问,请随时告诉我们。\n"
]
}
],
"source": [
"try:\n",
" if delimiter in response:\n",
" final_response = response.split(delimiter)[-1].strip()\n",
" else:\n",
" final_response = response.split(\":\")[-1].strip()\n",
"except Exception as e:\n",
" final_response = \"对不起,我现在有点问题,请尝试问另外一个问题\"\n",
" \n",
"print(final_response)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"在下一章中,我们将学习一种处理复杂任务的新策略,即将复杂任务分解为一系列更简单的子任务,而不是试图在一个 Prompt 中完成整个任务。\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 附录: 英文版提示"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 思维链提示设计"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"delimiter = \"####\"\n",
"system_message = f\"\"\"\n",
"Follow these steps to answer the customer queries.\n",
"The customer query will be delimited with four hashtags,\\\n",
"i.e. {delimiter}. \n",
"\n",
"Step 1:{delimiter} First decide whether the user is \\\n",
"asking a question about a specific product or products. \\\n",
"Product cateogry doesn't count. \n",
"\n",
"Step 2:{delimiter} If the user is asking about \\\n",
"specific products, identify whether \\\n",
"the products are in the following list.\n",
"All available products: \n",
"1. Product: TechPro Ultrabook\n",
" Category: Computers and Laptops\n",
" Brand: TechPro\n",
" Model Number: TP-UB100\n",
" Warranty: 1 year\n",
" Rating: 4.5\n",
" Features: 13.3-inch display, 8GB RAM, 256GB SSD, Intel Core i5 processor\n",
" Description: A sleek and lightweight ultrabook for everyday use.\n",
" Price: $799.99\n",
"\n",
"2. Product: BlueWave Gaming Laptop\n",
" Category: Computers and Laptops\n",
" Brand: BlueWave\n",
" Model Number: BW-GL200\n",
" Warranty: 2 years\n",
" Rating: 4.7\n",
" Features: 15.6-inch display, 16GB RAM, 512GB SSD, NVIDIA GeForce RTX 3060\n",
" Description: A high-performance gaming laptop for an immersive experience.\n",
" Price: $1199.99\n",
"\n",
"3. Product: PowerLite Convertible\n",
" Category: Computers and Laptops\n",
" Brand: PowerLite\n",
" Model Number: PL-CV300\n",
" Warranty: 1 year\n",
" Rating: 4.3\n",
" Features: 14-inch touchscreen, 8GB RAM, 256GB SSD, 360-degree hinge\n",
" Description: A versatile convertible laptop with a responsive touchscreen.\n",
" Price: $699.99\n",
"\n",
"4. Product: TechPro Desktop\n",
" Category: Computers and Laptops\n",
" Brand: TechPro\n",
" Model Number: TP-DT500\n",
" Warranty: 1 year\n",
" Rating: 4.4\n",
" Features: Intel Core i7 processor, 16GB RAM, 1TB HDD, NVIDIA GeForce GTX 1660\n",
" Description: A powerful desktop computer for work and play.\n",
" Price: $999.99\n",
"\n",
"5. Product: BlueWave Chromebook\n",
" Category: Computers and Laptops\n",
" Brand: BlueWave\n",
" Model Number: BW-CB100\n",
" Warranty: 1 year\n",
" Rating: 4.1\n",
" Features: 11.6-inch display, 4GB RAM, 32GB eMMC, Chrome OS\n",
" Description: A compact and affordable Chromebook for everyday tasks.\n",
" Price: $249.99\n",
"\n",
"Step 3:{delimiter} If the message contains products \\\n",
"in the list above, list any assumptions that the \\\n",
"user is making in their \\\n",
"message e.g. that Laptop X is bigger than \\\n",
"Laptop Y, or that Laptop Z has a 2 year warranty.\n",
"\n",
"Step 4:{delimiter}: If the user made any assumptions, \\\n",
"figure out whether the assumption is true based on your \\\n",
"product information. \n",
"\n",
"Step 5:{delimiter}: First, politely correct the \\\n",
"customer's incorrect assumptions if applicable. \\\n",
"Only mention or reference products in the list of \\\n",
"5 available products, as these are the only 5 \\\n",
"products that the store sells. \\\n",
"Answer the customer in a friendly tone.\n",
"\n",
"Use the following format:\n",
"Step 1:{delimiter} <step 1 reasoning>\n",
"Step 2:{delimiter} <step 2 reasoning>\n",
"Step 3:{delimiter} <step 3 reasoning>\n",
"Step 4:{delimiter} <step 4 reasoning>\n",
"Response to user:{delimiter} <response to customer>\n",
"\n",
"Make sure to include {delimiter} to separate every step.\n",
"\"\"\"\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Step 1:#### The user is asking about the price difference between the BlueWave Chromebook and the TechPro Desktop.\n",
"\n",
"Step 2:#### Both the BlueWave Chromebook and the TechPro Desktop are available products.\n",
"\n",
"Step 3:#### The user assumes that the BlueWave Chromebook is more expensive than the TechPro Desktop.\n",
"\n",
"Step 4:#### Based on the product information, the price of the BlueWave Chromebook is $249.99, and the price of the TechPro Desktop is $999.99. Therefore, the TechPro Desktop is actually more expensive than the BlueWave Chromebook.\n",
"\n",
"Response to user:#### The BlueWave Chromebook is actually less expensive than the TechPro Desktop. The BlueWave Chromebook is priced at $249.99, while the TechPro Desktop is priced at $999.99.\n"
]
}
],
"source": [
"user_message = f\"\"\"\n",
"by how much is the BlueWave Chromebook more expensive \\\n",
"than the TechPro Desktop\"\"\"\n",
"\n",
"messages = [ \n",
"{'role':'system', \n",
" 'content': system_message}, \n",
"{'role':'user', \n",
" 'content': f\"{delimiter}{user_message}{delimiter}\"}, \n",
"] \n",
"\n",
"response = get_completion_from_messages(messages)\n",
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Step 1:#### The user is asking if the store sells TVs, which is a question about a specific product category.\n",
"\n",
"Step 2:#### TVs are not included in the list of available products. The store only sells computers and laptops.\n",
"\n",
"Response to user:#### I'm sorry, but we currently do not sell TVs. Our store specializes in computers and laptops. If you have any questions or need assistance with our available products, feel free to ask.\n"
]
}
],
"source": [
"user_message = f\"\"\"\n",
"do you sell tvs\"\"\"\n",
"messages = [ \n",
"{'role':'system', \n",
" 'content': system_message}, \n",
"{'role':'user', \n",
" 'content': f\"{delimiter}{user_message}{delimiter}\"}, \n",
"] \n",
"response = get_completion_from_messages(messages)\n",
"print(response)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 内心独白"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I'm sorry, but we currently do not sell TVs. Our store specializes in computers and laptops. If you have any questions or need assistance with our available products, feel free to ask.\n"
]
}
],
"source": [
"try:\n",
" final_response = response.split(delimiter)[-1].strip()\n",
"except Exception as e:\n",
" final_response = \"Sorry, I'm having trouble right now, please try asking another question.\"\n",
" \n",
"print(final_response)"
]
}
],
"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.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}