feat: finish 4 & 5
This commit is contained in:
@ -0,0 +1,271 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# L4: 处理输入: 思维链推理"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 设置\n",
|
||||
"#### 加载 API key 和相关的 Python 库.\n",
|
||||
"在这门课程中,我们提供了一些代码,帮助你加载OpenAI API key。"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import openai\n",
|
||||
"from dotenv import load_dotenv, find_dotenv\n",
|
||||
"_ = load_dotenv(find_dotenv()) # read local .env file\n",
|
||||
"\n",
|
||||
"openai.api_key = os.environ['OPENAI_API_KEY']"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def get_completion_from_messages(messages, \n",
|
||||
" model=\"gpt-3.5-turbo\", \n",
|
||||
" temperature=0, \n",
|
||||
" max_tokens=500):\n",
|
||||
" response = openai.ChatCompletion.create(\n",
|
||||
" model=model,\n",
|
||||
" messages=messages,\n",
|
||||
" temperature=temperature, \n",
|
||||
" max_tokens=max_tokens, \n",
|
||||
" )\n",
|
||||
" return response.choices[0].message[\"content\"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 思维链提示"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"delimiter = \"####\"\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 RAM,256GB SSD,Intel Core i5处理器\n",
|
||||
"描述:一款适用于日常使用的时尚轻便的超极本。\n",
|
||||
"价格:$799.99\n",
|
||||
"\n",
|
||||
"产品:BlueWave游戏笔记本电脑\n",
|
||||
"类别:计算机和笔记本电脑\n",
|
||||
"品牌:BlueWave\n",
|
||||
"型号:BW-GL200\n",
|
||||
"保修期:2年\n",
|
||||
"评分:4.7\n",
|
||||
"特点:15.6英寸显示屏,16GB RAM,512GB SSD,NVIDIA GeForce RTX 3060\n",
|
||||
"描述:一款高性能的游戏笔记本电脑,提供沉浸式体验。\n",
|
||||
"价格:$1199.99\n",
|
||||
"\n",
|
||||
"产品:PowerLite可转换笔记本电脑\n",
|
||||
"类别:计算机和笔记本电脑\n",
|
||||
"品牌:PowerLite\n",
|
||||
"型号:PL-CV300\n",
|
||||
"保修期:1年\n",
|
||||
"评分:4.3\n",
|
||||
"特点:14英寸触摸屏,8GB RAM,256GB SSD,360度铰链\n",
|
||||
"描述:一款多功能可转换笔记本电脑,具有响应触摸屏。\n",
|
||||
"价格:$699.99\n",
|
||||
"\n",
|
||||
"产品:TechPro台式电脑\n",
|
||||
"类别:计算机和笔记本电脑\n",
|
||||
"品牌:TechPro\n",
|
||||
"型号:TP-DT500\n",
|
||||
"保修期:1年\n",
|
||||
"评分:4.4\n",
|
||||
"特点:Intel Core i7处理器,16GB RAM,1TB HDD,NVIDIA 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 RAM,32GB eMMC,Chrome OS\n",
|
||||
"描述:一款紧凑而价格实惠的Chromebook,适用于日常任务。\n",
|
||||
"价格:$249.99\n",
|
||||
"\n",
|
||||
"步骤 3:{delimiter} 如果消息中包含上述列表中的产品,请列出用户在消息中做出的任何假设,例如笔记本电脑 X 比笔记本电脑 Y 大,或者笔记本电脑 Z 有 2 年保修期。\n",
|
||||
"\n",
|
||||
"步骤 4:{delimiter} 如果用户做出了任何假设,请根据产品信息确定假设是否正确。\n",
|
||||
"\n",
|
||||
"步骤 5:{delimiter} 如果用户有任何错误的假设,请先礼貌地纠正客户的错误假设(如果适用)。只提及或引用可用产品列表中的产品,因为这是商店销售的唯一五款产品。以友好的口吻回答客户。\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": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"步骤 1:#### 确认用户正在询问有关特定产品的问题。\n",
|
||||
"\n",
|
||||
"步骤 2:#### 用户询问 BlueWave Chromebook 和 TechPro 台式电脑之间的价格差异。\n",
|
||||
"\n",
|
||||
"步骤 3:#### 用户假设 BlueWave Chromebook 的价格高于 TechPro 台式电脑。\n",
|
||||
"\n",
|
||||
"步骤 4:#### 用户的假设是正确的。BlueWave Chromebook 的价格为 $249.99,而 TechPro 台式电脑的价格为 $999.99,因此 BlueWave Chromebook 的价格比 TechPro 台式电脑低 $750。\n",
|
||||
"\n",
|
||||
"回复客户:#### BlueWave Chromebook 比 TechPro 台式电脑便宜 $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": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"步骤 1:#### 首先确定用户是否正在询问有关特定产品或产品的问题。产品类别不计入范围。\n",
|
||||
"\n",
|
||||
"步骤 2:#### 如果用户询问特定产品,请确认产品是否在以下列表中。所有可用产品:\n",
|
||||
"\n",
|
||||
"我们很抱歉,我们商店不出售电视机。\n",
|
||||
"\n",
|
||||
"步骤 3:#### 如果消息中包含上述列表中的产品,请列出用户在消息中做出的任何假设,例如笔记本电脑 X 比笔记本电脑 Y 大,或者笔记本电脑 Z 有 2 年保修期。\n",
|
||||
"\n",
|
||||
"N/A\n",
|
||||
"\n",
|
||||
"步骤 4:#### 如果用户做出了任何假设,请根据产品信息确定假设是否正确。\n",
|
||||
"\n",
|
||||
"N/A\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)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 内心独白\n",
|
||||
"- 既然我们要求 LLM 用分隔符分隔推理步骤,那么我们可以隐藏推理思路,让用户看不到这些推理内容。"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"我们很抱歉,我们商店不出售电视机。\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",
|
||||
"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.4"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@ -0,0 +1,915 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# L5 处理输入: Chaining Prompts"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 设置\n",
|
||||
"#### 加载 API key 和相关的 Python 库.\n",
|
||||
"在这门课程中,我们提供了一些代码,帮助你加载OpenAI API key。"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import openai\n",
|
||||
"from dotenv import load_dotenv, find_dotenv\n",
|
||||
"_ = load_dotenv(find_dotenv()) # read local .env file\n",
|
||||
"\n",
|
||||
"openai.api_key = os.environ['OPENAI_API_KEY']"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def get_completion_from_messages(messages, \n",
|
||||
" model=\"gpt-3.5-turbo\", \n",
|
||||
" temperature=0, \n",
|
||||
" max_tokens=500):\n",
|
||||
" response = openai.ChatCompletion.create(\n",
|
||||
" model=model,\n",
|
||||
" messages=messages,\n",
|
||||
" temperature=temperature, \n",
|
||||
" max_tokens=max_tokens, \n",
|
||||
" )\n",
|
||||
" return response.choices[0].message[\"content\"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 实现一个包含多个提示的复杂任务\n",
|
||||
"\n",
|
||||
"### 提取相关产品和类别名称"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[{'category': '智能手机和配件', 'products': ['SmartX ProPhone']}, {'category': '相机和摄像机', 'products': ['FotoSnap DSLR Camera', 'FotoSnap Mirrorless Camera']}, {'category': '电视和家庭影院系统', 'products': ['CineView 4K TV', 'SoundMax Home Theater', 'CineView 8K TV', 'SoundMax Soundbar', 'CineView OLED TV']}]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"delimiter = \"####\"\n",
|
||||
"system_message = f\"\"\"\n",
|
||||
"你将提供服务查询。\n",
|
||||
"服务查询将使用{delimiter}字符分隔。\n",
|
||||
"\n",
|
||||
"仅输出一个Python对象列表,其中每个对象具有以下格式:\n",
|
||||
" 'category': <计算机和笔记本电脑、智能手机和配件、电视和家庭影院系统、游戏机和配件、音频设备、相机和摄像机中的一个>,\n",
|
||||
"或者\n",
|
||||
" 'products': <必须在下面的允许产品列表中找到的产品列表>\n",
|
||||
"\n",
|
||||
"类别和产品必须在客户服务查询中找到。\n",
|
||||
"如果提及了产品,则必须将其与允许产品列表中的正确类别相关联。\n",
|
||||
"如果未找到产品或类别,则输出空列表。\n",
|
||||
"\n",
|
||||
"允许的产品:\n",
|
||||
"\n",
|
||||
"计算机和笔记本电脑类别:\n",
|
||||
"TechPro Ultrabook\n",
|
||||
"BlueWave Gaming Laptop\n",
|
||||
"PowerLite Convertible\n",
|
||||
"TechPro Desktop\n",
|
||||
"BlueWave Chromebook\n",
|
||||
"\n",
|
||||
"智能手机和配件类别:\n",
|
||||
"SmartX ProPhone\n",
|
||||
"MobiTech PowerCase\n",
|
||||
"SmartX MiniPhone\n",
|
||||
"MobiTech Wireless Charger\n",
|
||||
"SmartX EarBuds\n",
|
||||
"\n",
|
||||
"电视和家庭影院系统类别:\n",
|
||||
"CineView 4K TV\n",
|
||||
"SoundMax Home Theater\n",
|
||||
"CineView 8K TV\n",
|
||||
"SoundMax Soundbar\n",
|
||||
"CineView OLED TV\n",
|
||||
"c\n",
|
||||
"游戏机和配件类别:\n",
|
||||
"GameSphere X\n",
|
||||
"ProGamer Controller\n",
|
||||
"GameSphere Y\n",
|
||||
"ProGamer Racing Wheel\n",
|
||||
"GameSphere VR Headset\n",
|
||||
"\n",
|
||||
"音频设备类别:\n",
|
||||
"AudioPhonic Noise-Canceling Headphones\n",
|
||||
"WaveSound Bluetooth Speaker\n",
|
||||
"AudioPhonic True Wireless Earbuds\n",
|
||||
"WaveSound Soundbar\n",
|
||||
"AudioPhonic Turntable\n",
|
||||
"\n",
|
||||
"相机和摄像机类别:\n",
|
||||
"FotoSnap DSLR Camera\n",
|
||||
"ActionCam 4K\n",
|
||||
"FotoSnap Mirrorless Camera\n",
|
||||
"ZoomMaster Camcorder\n",
|
||||
"FotoSnap Instant Camera\n",
|
||||
"\n",
|
||||
"仅输出Python对象列表,不包含其他字符信息。\n",
|
||||
"\"\"\"\n",
|
||||
"user_message_1 = f\"\"\"\n",
|
||||
" 请查询SmartX ProPhone智能手机和FotoSnap相机,包括单反相机。\n",
|
||||
" 另外,请查询关于电视产品的信息。 \"\"\"\n",
|
||||
"messages = [ \n",
|
||||
"{'role':'system', \n",
|
||||
" 'content': system_message}, \n",
|
||||
"{'role':'user', \n",
|
||||
" 'content': f\"{delimiter}{user_message_1}{delimiter}\"}, \n",
|
||||
"] \n",
|
||||
"category_and_product_response_1 = get_completion_from_messages(messages)\n",
|
||||
"print(category_and_product_response_1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"user_message_2 = f\"\"\"我的路由器坏了\"\"\"\n",
|
||||
"messages = [ \n",
|
||||
"{'role':'system',\n",
|
||||
" 'content': system_message}, \n",
|
||||
"{'role':'user',\n",
|
||||
" 'content': f\"{delimiter}{user_message_2}{delimiter}\"}, \n",
|
||||
"] \n",
|
||||
"response = get_completion_from_messages(messages)\n",
|
||||
"print(response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### 召回提取的产品和类别的详细信息"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# product information\n",
|
||||
"products = {\n",
|
||||
" \"TechPro Ultrabook\": {\n",
|
||||
" \"name\": \"TechPro 超极本\",\n",
|
||||
" \"category\": \"电脑和笔记本\",\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 处理器\"],\n",
|
||||
" \"description\": \"一款时尚轻便的超极本,适合日常使用。\",\n",
|
||||
" \"price\": 799.99\n",
|
||||
" },\n",
|
||||
" \"BlueWave Gaming Laptop\": {\n",
|
||||
" \"name\": \"BlueWave 游戏本\",\n",
|
||||
" \"category\": \"电脑和笔记本\",\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\": \"一款高性能的游戏笔记本电脑,提供沉浸式体验。\",\n",
|
||||
" \"price\": 1199.99\n",
|
||||
" },\n",
|
||||
" \"PowerLite Convertible\": {\n",
|
||||
" \"name\": \"PowerLite Convertible\",\n",
|
||||
" \"category\": \"电脑和笔记本\",\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\": \"一款多功能的可转换笔记本电脑,具有灵敏的触摸屏。\",\n",
|
||||
" \"price\": 699.99\n",
|
||||
" },\n",
|
||||
" \"TechPro Desktop\": {\n",
|
||||
" \"name\": \"TechPro Desktop\",\n",
|
||||
" \"category\": \"电脑和笔记本\",\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\": \"一款功能强大的台式电脑,适用于工作和娱乐。\",\n",
|
||||
" \"price\": 999.99\n",
|
||||
" },\n",
|
||||
" \"BlueWave Chromebook\": {\n",
|
||||
" \"name\": \"BlueWave Chromebook\",\n",
|
||||
" \"category\": \"电脑和笔记本\",\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\": \"一款紧凑而价格实惠的Chromebook,适用于日常任务。\",\n",
|
||||
" \"price\": 249.99\n",
|
||||
" },\n",
|
||||
" \"SmartX ProPhone\": {\n",
|
||||
" \"name\": \"SmartX ProPhone\",\n",
|
||||
" \"category\": \"智能手机和配件\",\n",
|
||||
" \"brand\": \"SmartX\",\n",
|
||||
" \"model_number\": \"SX-PP10\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.6,\n",
|
||||
" \"features\": [\"6.1-inch display\", \"128GB storage\", \"12MP dual camera\", \"5G\"],\n",
|
||||
" \"description\": \"一款拥有先进摄像功能的强大智能手机。\",\n",
|
||||
" \"price\": 899.99\n",
|
||||
" },\n",
|
||||
" \"MobiTech PowerCase\": {\n",
|
||||
" \"name\": \"MobiTech PowerCase\",\n",
|
||||
" \"category\": \"专业手机\",\n",
|
||||
" \"brand\": \"MobiTech\",\n",
|
||||
" \"model_number\": \"MT-PC20\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.3,\n",
|
||||
" \"features\": [\"5000mAh battery\", \"Wireless charging\", \"Compatible with SmartX ProPhone\"],\n",
|
||||
" \"description\": \"一款带有内置电池的保护手机壳,可延长使用时间。\",\n",
|
||||
" \"price\": 59.99\n",
|
||||
" },\n",
|
||||
" \"SmartX MiniPhone\": {\n",
|
||||
" \"name\": \"SmartX MiniPhone\",\n",
|
||||
" \"category\": \"专业手机\",\n",
|
||||
" \"brand\": \"SmartX\",\n",
|
||||
" \"model_number\": \"SX-MP5\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.2,\n",
|
||||
" \"features\": [\"4.7-inch display\", \"64GB storage\", \"8MP camera\", \"4G\"],\n",
|
||||
" \"description\": \"一款紧凑而价格实惠的智能手机,适用于基本任务。\",\n",
|
||||
" \"price\": 399.99\n",
|
||||
" },\n",
|
||||
" \"MobiTech Wireless Charger\": {\n",
|
||||
" \"name\": \"MobiTech Wireless Charger\",\n",
|
||||
" \"category\": \"专业手机\",\n",
|
||||
" \"brand\": \"MobiTech\",\n",
|
||||
" \"model_number\": \"MT-WC10\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.5,\n",
|
||||
" \"features\": [\"10W fast charging\", \"Qi-compatible\", \"LED indicator\", \"Compact design\"],\n",
|
||||
" \"description\": \"一款方便的无线充电器,使工作区域整洁无杂物。\",\n",
|
||||
" \"price\": 29.99\n",
|
||||
" },\n",
|
||||
" \"SmartX EarBuds\": {\n",
|
||||
" \"name\": \"SmartX EarBuds\",\n",
|
||||
" \"category\": \"专业手机\",\n",
|
||||
" \"brand\": \"SmartX\",\n",
|
||||
" \"model_number\": \"SX-EB20\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.4,\n",
|
||||
" \"features\": [\"True wireless\", \"Bluetooth 5.0\", \"Touch controls\", \"24-hour battery life\"],\n",
|
||||
" \"description\": \"通过这些舒适的耳塞体验真正的无线自由。\",\n",
|
||||
" \"price\": 99.99\n",
|
||||
" },\n",
|
||||
"\n",
|
||||
" \"CineView 4K TV\": {\n",
|
||||
" \"name\": \"CineView 4K TV\",\n",
|
||||
" \"category\": \"电视和家庭影院系统\",\n",
|
||||
" \"brand\": \"CineView\",\n",
|
||||
" \"model_number\": \"CV-4K55\",\n",
|
||||
" \"warranty\": \"2 years\",\n",
|
||||
" \"rating\": 4.8,\n",
|
||||
" \"features\": [\"55-inch display\", \"4K resolution\", \"HDR\", \"Smart TV\"],\n",
|
||||
" \"description\": \"一款色彩鲜艳、智能功能丰富的惊艳4K电视。\",\n",
|
||||
" \"price\": 599.99\n",
|
||||
" },\n",
|
||||
" \"SoundMax Home Theater\": {\n",
|
||||
" \"name\": \"SoundMax Home Theater\",\n",
|
||||
" \"category\": \"电视和家庭影院系统\",\n",
|
||||
" \"brand\": \"SoundMax\",\n",
|
||||
" \"model_number\": \"SM-HT100\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.4,\n",
|
||||
" \"features\": [\"5.1 channel\", \"1000W output\", \"Wireless subwoofer\", \"Bluetooth\"],\n",
|
||||
" \"description\": \"一款强大的家庭影院系统,提供沉浸式音频体验。\",\n",
|
||||
" \"price\": 399.99\n",
|
||||
" },\n",
|
||||
" \"CineView 8K TV\": {\n",
|
||||
" \"name\": \"CineView 8K TV\",\n",
|
||||
" \"category\": \"电视和家庭影院系统\",\n",
|
||||
" \"brand\": \"CineView\",\n",
|
||||
" \"model_number\": \"CV-8K65\",\n",
|
||||
" \"warranty\": \"2 years\",\n",
|
||||
" \"rating\": 4.9,\n",
|
||||
" \"features\": [\"65-inch display\", \"8K resolution\", \"HDR\", \"Smart TV\"],\n",
|
||||
" \"description\": \"通过这款惊艳的8K电视,体验未来。\",\n",
|
||||
" \"price\": 2999.99\n",
|
||||
" },\n",
|
||||
" \"SoundMax Soundbar\": {\n",
|
||||
" \"name\": \"SoundMax Soundbar\",\n",
|
||||
" \"category\": \"电视和家庭影院系统\",\n",
|
||||
" \"brand\": \"SoundMax\",\n",
|
||||
" \"model_number\": \"SM-SB50\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.3,\n",
|
||||
" \"features\": [\"2.1 channel\", \"300W output\", \"Wireless subwoofer\", \"Bluetooth\"],\n",
|
||||
" \"description\": \"使用这款时尚而功能强大的声音,升级您电视的音频体验。\",\n",
|
||||
" \"price\": 199.99\n",
|
||||
" },\n",
|
||||
" \"CineView OLED TV\": {\n",
|
||||
" \"name\": \"CineView OLED TV\",\n",
|
||||
" \"category\": \"电视和家庭影院系统\",\n",
|
||||
" \"brand\": \"CineView\",\n",
|
||||
" \"model_number\": \"CV-OLED55\",\n",
|
||||
" \"warranty\": \"2 years\",\n",
|
||||
" \"rating\": 4.7,\n",
|
||||
" \"features\": [\"55-inch display\", \"4K resolution\", \"HDR\", \"Smart TV\"],\n",
|
||||
" \"description\": \"通过这款OLED电视,体验真正的五彩斑斓。\",\n",
|
||||
" \"price\": 1499.99\n",
|
||||
" },\n",
|
||||
"\n",
|
||||
" \"GameSphere X\": {\n",
|
||||
" \"name\": \"GameSphere X\",\n",
|
||||
" \"category\": \"游戏机和配件\",\n",
|
||||
" \"brand\": \"GameSphere\",\n",
|
||||
" \"model_number\": \"GS-X\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.9,\n",
|
||||
" \"features\": [\"4K gaming\", \"1TB storage\", \"Backward compatibility\", \"Online multiplayer\"],\n",
|
||||
" \"description\": \"一款下一代游戏机,提供终极游戏体验。\",\n",
|
||||
" \"price\": 499.99\n",
|
||||
" },\n",
|
||||
" \"ProGamer Controller\": {\n",
|
||||
" \"name\": \"ProGamer Controller\",\n",
|
||||
" \"category\": \"游戏机和配件\",\n",
|
||||
" \"brand\": \"ProGamer\",\n",
|
||||
" \"model_number\": \"PG-C100\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.2,\n",
|
||||
" \"features\": [\"Ergonomic design\", \"Customizable buttons\", \"Wireless\", \"Rechargeable battery\"],\n",
|
||||
" \"description\": \"一款高品质的游戏手柄,提供精准和舒适的操作。\",\n",
|
||||
" \"price\": 59.99\n",
|
||||
" },\n",
|
||||
" \"GameSphere Y\": {\n",
|
||||
" \"name\": \"GameSphere Y\",\n",
|
||||
" \"category\": \"游戏机和配件\",\n",
|
||||
" \"brand\": \"GameSphere\",\n",
|
||||
" \"model_number\": \"GS-Y\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.8,\n",
|
||||
" \"features\": [\"4K gaming\", \"500GB storage\", \"Backward compatibility\", \"Online multiplayer\"],\n",
|
||||
" \"description\": \"一款体积紧凑、性能强劲的游戏机。\",\n",
|
||||
" \"price\": 399.99\n",
|
||||
" },\n",
|
||||
" \"ProGamer Racing Wheel\": {\n",
|
||||
" \"name\": \"ProGamer Racing Wheel\",\n",
|
||||
" \"category\": \"游戏机和配件\",\n",
|
||||
" \"brand\": \"ProGamer\",\n",
|
||||
" \"model_number\": \"PG-RW200\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.5,\n",
|
||||
" \"features\": [\"Force feedback\", \"Adjustable pedals\", \"Paddle shifters\", \"Compatible with GameSphere X\"],\n",
|
||||
" \"description\": \"使用这款逼真的赛车方向盘,提升您的赛车游戏体验。\",\n",
|
||||
" \"price\": 249.99\n",
|
||||
" },\n",
|
||||
" \"GameSphere VR Headset\": {\n",
|
||||
" \"name\": \"GameSphere VR Headset\",\n",
|
||||
" \"category\": \"游戏机和配件\",\n",
|
||||
" \"brand\": \"GameSphere\",\n",
|
||||
" \"model_number\": \"GS-VR\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.6,\n",
|
||||
" \"features\": [\"Immersive VR experience\", \"Built-in headphones\", \"Adjustable headband\", \"Compatible with GameSphere X\"],\n",
|
||||
" \"description\": \"通过这款舒适的VR头戴设备,进入虚拟现实的世界。\",\n",
|
||||
" \"price\": 299.99\n",
|
||||
" },\n",
|
||||
"\n",
|
||||
" \"AudioPhonic Noise-Canceling Headphones\": {\n",
|
||||
" \"name\": \"AudioPhonic Noise-Canceling Headphones\",\n",
|
||||
" \"category\": \"音频设备\",\n",
|
||||
" \"brand\": \"AudioPhonic\",\n",
|
||||
" \"model_number\": \"AP-NC100\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.6,\n",
|
||||
" \"features\": [\"Active noise-canceling\", \"Bluetooth\", \"20-hour battery life\", \"Comfortable fit\"],\n",
|
||||
" \"description\": \"通过这款降噪耳机,体验沉浸式的音效。\",\n",
|
||||
" \"price\": 199.99\n",
|
||||
" },\n",
|
||||
" \"WaveSound Bluetooth Speaker\": {\n",
|
||||
" \"name\": \"WaveSound Bluetooth Speaker\",\n",
|
||||
" \"category\": \"音频设备\",\n",
|
||||
" \"brand\": \"WaveSound\",\n",
|
||||
" \"model_number\": \"WS-BS50\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.5,\n",
|
||||
" \"features\": [\"Portable\", \"10-hour battery life\", \"Water-resistant\", \"Built-in microphone\"],\n",
|
||||
" \"description\": \"一款紧凑而多用途的蓝牙音箱,适用于随时随地收听音乐。\",\n",
|
||||
" \"price\": 49.99\n",
|
||||
" },\n",
|
||||
" \"AudioPhonic True Wireless Earbuds\": {\n",
|
||||
" \"name\": \"AudioPhonic True Wireless Earbuds\",\n",
|
||||
" \"category\": \"音频设备\",\n",
|
||||
" \"brand\": \"AudioPhonic\",\n",
|
||||
" \"model_number\": \"AP-TW20\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.4,\n",
|
||||
" \"features\": [\"True wireless\", \"Bluetooth 5.0\", \"Touch controls\", \"18-hour battery life\"],\n",
|
||||
" \"description\": \"通过这款舒适的真无线耳塞,无需线缆即可享受音乐。\",\n",
|
||||
" \"price\": 79.99\n",
|
||||
" },\n",
|
||||
" \"WaveSound Soundbar\": {\n",
|
||||
" \"name\": \"WaveSound Soundbar\",\n",
|
||||
" \"category\": \"音频设备\",\n",
|
||||
" \"brand\": \"WaveSound\",\n",
|
||||
" \"model_number\": \"WS-SB40\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.3,\n",
|
||||
" \"features\": [\"2.0 channel\", \"80W output\", \"Bluetooth\", \"Wall-mountable\"],\n",
|
||||
" \"description\": \"使用这款纤薄而功能强大的声音吧,升级您电视的音频体验。\",\n",
|
||||
" \"price\": 99.99\n",
|
||||
" },\n",
|
||||
" \"AudioPhonic Turntable\": {\n",
|
||||
" \"name\": \"AudioPhonic Turntable\",\n",
|
||||
" \"category\": \"音频设备\",\n",
|
||||
" \"brand\": \"AudioPhonic\",\n",
|
||||
" \"model_number\": \"AP-TT10\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.2,\n",
|
||||
" \"features\": [\"3-speed\", \"Built-in speakers\", \"Bluetooth\", \"USB recording\"],\n",
|
||||
" \"description\": \"通过这款现代化的唱片机,重拾您的黑胶唱片收藏。\",\n",
|
||||
" \"price\": 149.99\n",
|
||||
" },\n",
|
||||
"\n",
|
||||
" \"FotoSnap DSLR Camera\": {\n",
|
||||
" \"name\": \"FotoSnap DSLR Camera\",\n",
|
||||
" \"category\": \"相机和摄像机\",\n",
|
||||
" \"brand\": \"FotoSnap\",\n",
|
||||
" \"model_number\": \"FS-DSLR200\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.7,\n",
|
||||
" \"features\": [\"24.2MP sensor\", \"1080p video\", \"3-inch LCD\", \"Interchangeable lenses\"],\n",
|
||||
" \"description\": \"使用这款多功能的单反相机,捕捉惊艳的照片和视频。\",\n",
|
||||
" \"price\": 599.99\n",
|
||||
" },\n",
|
||||
" \"ActionCam 4K\": {\n",
|
||||
" \"name\": \"ActionCam 4K\",\n",
|
||||
" \"category\": \"相机和摄像机\",\n",
|
||||
" \"brand\": \"ActionCam\",\n",
|
||||
" \"model_number\": \"AC-4K\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.4,\n",
|
||||
" \"features\": [\"4K video\", \"Waterproof\", \"Image stabilization\", \"Wi-Fi\"],\n",
|
||||
" \"description\": \"使用这款坚固而紧凑的4K运动相机,记录您的冒险旅程。\",\n",
|
||||
" \"price\": 299.99\n",
|
||||
" },\n",
|
||||
" \"FotoSnap Mirrorless Camera\": {\n",
|
||||
" \"name\": \"FotoSnap Mirrorless Camera\",\n",
|
||||
" \"category\": \"相机和摄像机\",\n",
|
||||
" \"brand\": \"FotoSnap\",\n",
|
||||
" \"model_number\": \"FS-ML100\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.6,\n",
|
||||
" \"features\": [\"20.1MP sensor\", \"4K video\", \"3-inch touchscreen\", \"Interchangeable lenses\"],\n",
|
||||
" \"description\": \"一款具有先进功能的小巧轻便的无反相机。\",\n",
|
||||
" \"price\": 799.99\n",
|
||||
" },\n",
|
||||
" \"ZoomMaster Camcorder\": {\n",
|
||||
" \"name\": \"ZoomMaster Camcorder\",\n",
|
||||
" \"category\": \"相机和摄像机\",\n",
|
||||
" \"brand\": \"ZoomMaster\",\n",
|
||||
" \"model_number\": \"ZM-CM50\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.3,\n",
|
||||
" \"features\": [\"1080p video\", \"30x optical zoom\", \"3-inch LCD\", \"Image stabilization\"],\n",
|
||||
" \"description\": \"使用这款易于使用的摄像机,捕捉生活的瞬间。\",\n",
|
||||
" \"price\": 249.99\n",
|
||||
" },\n",
|
||||
" \"FotoSnap Instant Camera\": {\n",
|
||||
" \"name\": \"FotoSnap Instant Camera\",\n",
|
||||
" \"category\": \"相机和摄像机\",\n",
|
||||
" \"brand\": \"FotoSnap\",\n",
|
||||
" \"model_number\": \"FS-IC10\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.1,\n",
|
||||
" \"features\": [\"Instant prints\", \"Built-in flash\", \"Selfie mirror\", \"Battery-powered\"],\n",
|
||||
" \"description\": \"使用这款有趣且便携的即时相机,创造瞬间回忆。\",\n",
|
||||
" \"price\": 69.99\n",
|
||||
" }\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def get_product_by_name(name):\n",
|
||||
" return products.get(name, None)\n",
|
||||
"\n",
|
||||
"def get_products_by_category(category):\n",
|
||||
" return [product for product in products.values() if product[\"category\"] == category]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"{'name': 'TechPro 超极本', 'category': '电脑和笔记本', 'brand': 'TechPro', 'model_number': 'TP-UB100', 'warranty': '1 year', 'rating': 4.5, 'features': ['13.3-inch display', '8GB RAM', '256GB SSD', 'Intel Core i5 处理器'], 'description': '一款时尚轻便的超极本,适合日常使用。', 'price': 799.99}\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(get_product_by_name(\"TechPro Ultrabook\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[{'name': 'TechPro 超极本', 'category': '电脑和笔记本', 'brand': 'TechPro', 'model_number': 'TP-UB100', 'warranty': '1 year', 'rating': 4.5, 'features': ['13.3-inch display', '8GB RAM', '256GB SSD', 'Intel Core i5 处理器'], 'description': '一款时尚轻便的超极本,适合日常使用。', 'price': 799.99}, {'name': 'BlueWave 游戏本', 'category': '电脑和笔记本', 'brand': 'BlueWave', 'model_number': 'BW-GL200', 'warranty': '2 years', 'rating': 4.7, 'features': ['15.6-inch display', '16GB RAM', '512GB SSD', 'NVIDIA GeForce RTX 3060'], 'description': '一款高性能的游戏笔记本电脑,提供沉浸式体验。', 'price': 1199.99}, {'name': 'PowerLite Convertible', 'category': '电脑和笔记本', 'brand': 'PowerLite', 'model_number': 'PL-CV300', 'warranty': '1 year', 'rating': 4.3, 'features': ['14-inch touchscreen', '8GB RAM', '256GB SSD', '360-degree hinge'], 'description': '一款多功能的可转换笔记本电脑,具有灵敏的触摸屏。', 'price': 699.99}, {'name': 'TechPro Desktop', 'category': '电脑和笔记本', 'brand': 'TechPro', 'model_number': 'TP-DT500', 'warranty': '1 year', 'rating': 4.4, 'features': ['Intel Core i7 processor', '16GB RAM', '1TB HDD', 'NVIDIA GeForce GTX 1660'], 'description': '一款功能强大的台式电脑,适用于工作和娱乐。', 'price': 999.99}, {'name': 'BlueWave Chromebook', 'category': '电脑和笔记本', 'brand': 'BlueWave', 'model_number': 'BW-CB100', 'warranty': '1 year', 'rating': 4.1, 'features': ['11.6-inch display', '4GB RAM', '32GB eMMC', 'Chrome OS'], 'description': '一款紧凑而价格实惠的Chromebook,适用于日常任务。', 'price': 249.99}]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(get_products_by_category(\"电脑和笔记本\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n",
|
||||
" 请查询SmartX ProPhone智能手机和FotoSnap相机,包括单反相机。\n",
|
||||
" 另外,请查询关于电视产品的信息。 \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(user_message_1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[{'category': '智能手机和配件', 'products': ['SmartX ProPhone']}, {'category': '相机和摄像机', 'products': ['FotoSnap DSLR Camera', 'FotoSnap Mirrorless Camera']}, {'category': '电视和家庭影院系统', 'products': ['CineView 4K TV', 'SoundMax Home Theater', 'CineView 8K TV', 'SoundMax Soundbar', 'CineView OLED TV']}]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(category_and_product_response_1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### 将Python字符串读取为Python字典列表"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json \n",
|
||||
"\n",
|
||||
"def read_string_to_list(input_string):\n",
|
||||
" if input_string is None:\n",
|
||||
" return None\n",
|
||||
"\n",
|
||||
" try:\n",
|
||||
" input_string = input_string.replace(\"'\", \"\\\"\") # Replace single quotes with double quotes for valid JSON\n",
|
||||
" data = json.loads(input_string)\n",
|
||||
" return data\n",
|
||||
" except json.JSONDecodeError:\n",
|
||||
" print(\"Error: Invalid JSON string\")\n",
|
||||
" return None \n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[{'category': '智能手机和配件', 'products': ['SmartX ProPhone']}, {'category': '相机和摄像机', 'products': ['FotoSnap DSLR Camera', 'FotoSnap Mirrorless Camera']}, {'category': '电视和家庭影院系统', 'products': ['CineView 4K TV', 'SoundMax Home Theater', 'CineView 8K TV', 'SoundMax Soundbar', 'CineView OLED TV']}]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"category_and_product_list = read_string_to_list(category_and_product_response_1)\n",
|
||||
"print(category_and_product_list)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 召回相关产品和类别的详细信息"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def generate_output_string(data_list):\n",
|
||||
" output_string = \"\"\n",
|
||||
"\n",
|
||||
" if data_list is None:\n",
|
||||
" return output_string\n",
|
||||
"\n",
|
||||
" for data in data_list:\n",
|
||||
" try:\n",
|
||||
" if \"products\" in data:\n",
|
||||
" products_list = data[\"products\"]\n",
|
||||
" for product_name in products_list:\n",
|
||||
" product = get_product_by_name(product_name)\n",
|
||||
" if product:\n",
|
||||
" output_string += json.dumps(product, indent=4) + \"\\n\"\n",
|
||||
" else:\n",
|
||||
" print(f\"Error: Product '{product_name}' not found\")\n",
|
||||
" elif \"category\" in data:\n",
|
||||
" category_name = data[\"category\"]\n",
|
||||
" category_products = get_products_by_category(category_name)\n",
|
||||
" for product in category_products:\n",
|
||||
" output_string += json.dumps(product, indent=4) + \"\\n\"\n",
|
||||
" else:\n",
|
||||
" print(\"Error: Invalid object format\")\n",
|
||||
" except Exception as e:\n",
|
||||
" print(f\"Error: {e}\")\n",
|
||||
"\n",
|
||||
" return output_string "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"{\n",
|
||||
" \"name\": \"SmartX ProPhone\",\n",
|
||||
" \"category\": \"\\u667a\\u80fd\\u624b\\u673a\\u548c\\u914d\\u4ef6\",\n",
|
||||
" \"brand\": \"SmartX\",\n",
|
||||
" \"model_number\": \"SX-PP10\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.6,\n",
|
||||
" \"features\": [\n",
|
||||
" \"6.1-inch display\",\n",
|
||||
" \"128GB storage\",\n",
|
||||
" \"12MP dual camera\",\n",
|
||||
" \"5G\"\n",
|
||||
" ],\n",
|
||||
" \"description\": \"\\u4e00\\u6b3e\\u62e5\\u6709\\u5148\\u8fdb\\u6444\\u50cf\\u529f\\u80fd\\u7684\\u5f3a\\u5927\\u667a\\u80fd\\u624b\\u673a\\u3002\",\n",
|
||||
" \"price\": 899.99\n",
|
||||
"}\n",
|
||||
"{\n",
|
||||
" \"name\": \"FotoSnap DSLR Camera\",\n",
|
||||
" \"category\": \"\\u76f8\\u673a\\u548c\\u6444\\u50cf\\u673a\",\n",
|
||||
" \"brand\": \"FotoSnap\",\n",
|
||||
" \"model_number\": \"FS-DSLR200\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.7,\n",
|
||||
" \"features\": [\n",
|
||||
" \"24.2MP sensor\",\n",
|
||||
" \"1080p video\",\n",
|
||||
" \"3-inch LCD\",\n",
|
||||
" \"Interchangeable lenses\"\n",
|
||||
" ],\n",
|
||||
" \"description\": \"\\u4f7f\\u7528\\u8fd9\\u6b3e\\u591a\\u529f\\u80fd\\u7684\\u5355\\u53cd\\u76f8\\u673a\\uff0c\\u6355\\u6349\\u60ca\\u8273\\u7684\\u7167\\u7247\\u548c\\u89c6\\u9891\\u3002\",\n",
|
||||
" \"price\": 599.99\n",
|
||||
"}\n",
|
||||
"{\n",
|
||||
" \"name\": \"FotoSnap Mirrorless Camera\",\n",
|
||||
" \"category\": \"\\u76f8\\u673a\\u548c\\u6444\\u50cf\\u673a\",\n",
|
||||
" \"brand\": \"FotoSnap\",\n",
|
||||
" \"model_number\": \"FS-ML100\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.6,\n",
|
||||
" \"features\": [\n",
|
||||
" \"20.1MP sensor\",\n",
|
||||
" \"4K video\",\n",
|
||||
" \"3-inch touchscreen\",\n",
|
||||
" \"Interchangeable lenses\"\n",
|
||||
" ],\n",
|
||||
" \"description\": \"\\u4e00\\u6b3e\\u5177\\u6709\\u5148\\u8fdb\\u529f\\u80fd\\u7684\\u5c0f\\u5de7\\u8f7b\\u4fbf\\u7684\\u65e0\\u53cd\\u76f8\\u673a\\u3002\",\n",
|
||||
" \"price\": 799.99\n",
|
||||
"}\n",
|
||||
"{\n",
|
||||
" \"name\": \"CineView 4K TV\",\n",
|
||||
" \"category\": \"\\u7535\\u89c6\\u548c\\u5bb6\\u5ead\\u5f71\\u9662\\u7cfb\\u7edf\",\n",
|
||||
" \"brand\": \"CineView\",\n",
|
||||
" \"model_number\": \"CV-4K55\",\n",
|
||||
" \"warranty\": \"2 years\",\n",
|
||||
" \"rating\": 4.8,\n",
|
||||
" \"features\": [\n",
|
||||
" \"55-inch display\",\n",
|
||||
" \"4K resolution\",\n",
|
||||
" \"HDR\",\n",
|
||||
" \"Smart TV\"\n",
|
||||
" ],\n",
|
||||
" \"description\": \"\\u4e00\\u6b3e\\u8272\\u5f69\\u9c9c\\u8273\\u3001\\u667a\\u80fd\\u529f\\u80fd\\u4e30\\u5bcc\\u7684\\u60ca\\u82734K\\u7535\\u89c6\\u3002\",\n",
|
||||
" \"price\": 599.99\n",
|
||||
"}\n",
|
||||
"{\n",
|
||||
" \"name\": \"SoundMax Home Theater\",\n",
|
||||
" \"category\": \"\\u7535\\u89c6\\u548c\\u5bb6\\u5ead\\u5f71\\u9662\\u7cfb\\u7edf\",\n",
|
||||
" \"brand\": \"SoundMax\",\n",
|
||||
" \"model_number\": \"SM-HT100\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.4,\n",
|
||||
" \"features\": [\n",
|
||||
" \"5.1 channel\",\n",
|
||||
" \"1000W output\",\n",
|
||||
" \"Wireless subwoofer\",\n",
|
||||
" \"Bluetooth\"\n",
|
||||
" ],\n",
|
||||
" \"description\": \"\\u4e00\\u6b3e\\u5f3a\\u5927\\u7684\\u5bb6\\u5ead\\u5f71\\u9662\\u7cfb\\u7edf\\uff0c\\u63d0\\u4f9b\\u6c89\\u6d78\\u5f0f\\u97f3\\u9891\\u4f53\\u9a8c\\u3002\",\n",
|
||||
" \"price\": 399.99\n",
|
||||
"}\n",
|
||||
"{\n",
|
||||
" \"name\": \"CineView 8K TV\",\n",
|
||||
" \"category\": \"\\u7535\\u89c6\\u548c\\u5bb6\\u5ead\\u5f71\\u9662\\u7cfb\\u7edf\",\n",
|
||||
" \"brand\": \"CineView\",\n",
|
||||
" \"model_number\": \"CV-8K65\",\n",
|
||||
" \"warranty\": \"2 years\",\n",
|
||||
" \"rating\": 4.9,\n",
|
||||
" \"features\": [\n",
|
||||
" \"65-inch display\",\n",
|
||||
" \"8K resolution\",\n",
|
||||
" \"HDR\",\n",
|
||||
" \"Smart TV\"\n",
|
||||
" ],\n",
|
||||
" \"description\": \"\\u901a\\u8fc7\\u8fd9\\u6b3e\\u60ca\\u8273\\u76848K\\u7535\\u89c6\\uff0c\\u4f53\\u9a8c\\u672a\\u6765\\u3002\",\n",
|
||||
" \"price\": 2999.99\n",
|
||||
"}\n",
|
||||
"{\n",
|
||||
" \"name\": \"SoundMax Soundbar\",\n",
|
||||
" \"category\": \"\\u7535\\u89c6\\u548c\\u5bb6\\u5ead\\u5f71\\u9662\\u7cfb\\u7edf\",\n",
|
||||
" \"brand\": \"SoundMax\",\n",
|
||||
" \"model_number\": \"SM-SB50\",\n",
|
||||
" \"warranty\": \"1 year\",\n",
|
||||
" \"rating\": 4.3,\n",
|
||||
" \"features\": [\n",
|
||||
" \"2.1 channel\",\n",
|
||||
" \"300W output\",\n",
|
||||
" \"Wireless subwoofer\",\n",
|
||||
" \"Bluetooth\"\n",
|
||||
" ],\n",
|
||||
" \"description\": \"\\u4f7f\\u7528\\u8fd9\\u6b3e\\u65f6\\u5c1a\\u800c\\u529f\\u80fd\\u5f3a\\u5927\\u7684\\u58f0\\u97f3\\uff0c\\u5347\\u7ea7\\u60a8\\u7535\\u89c6\\u7684\\u97f3\\u9891\\u4f53\\u9a8c\\u3002\",\n",
|
||||
" \"price\": 199.99\n",
|
||||
"}\n",
|
||||
"{\n",
|
||||
" \"name\": \"CineView OLED TV\",\n",
|
||||
" \"category\": \"\\u7535\\u89c6\\u548c\\u5bb6\\u5ead\\u5f71\\u9662\\u7cfb\\u7edf\",\n",
|
||||
" \"brand\": \"CineView\",\n",
|
||||
" \"model_number\": \"CV-OLED55\",\n",
|
||||
" \"warranty\": \"2 years\",\n",
|
||||
" \"rating\": 4.7,\n",
|
||||
" \"features\": [\n",
|
||||
" \"55-inch display\",\n",
|
||||
" \"4K resolution\",\n",
|
||||
" \"HDR\",\n",
|
||||
" \"Smart TV\"\n",
|
||||
" ],\n",
|
||||
" \"description\": \"\\u901a\\u8fc7\\u8fd9\\u6b3eOLED\\u7535\\u89c6\\uff0c\\u4f53\\u9a8c\\u771f\\u6b63\\u7684\\u4e94\\u5f69\\u6591\\u6593\\u3002\",\n",
|
||||
" \"price\": 1499.99\n",
|
||||
"}\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"product_information_for_user_message_1 = generate_output_string(category_and_product_list)\n",
|
||||
"print(product_information_for_user_message_1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### 用户搜索基于产品详细信息生成回答"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 27,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"SmartX ProPhone是一款功能强大的智能手机,拥有6.1英寸的显示屏、128GB的存储空间、12MP的双摄像头和5G网络。FotoSnap相机系列包括单反相机和无反相机,分别拥有不同的像素和视频分辨率,同时支持可更换镜头。电视产品包括CineView 4K TV、CineView 8K TV和CineView OLED TV,分别拥有不同的分辨率和尺寸,同时支持HDR和智能电视功能。此外,我们还提供SoundMax家庭影院和Soundbar音响,以提供更好的音频体验。您有什么关于这些产品的问题吗?\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"system_message = f\"\"\"\n",
|
||||
"您是一家大型电子商店的客服助理。\n",
|
||||
"请以友好和乐于助人的口吻回答问题,并尽量简洁明了。\n",
|
||||
"请确保向用户提出相关的后续问题。\n",
|
||||
"\"\"\"\n",
|
||||
"user_message_1 = f\"\"\"\n",
|
||||
"请介绍一下SmartX ProPhone智能手机和FotoSnap相机,包括单反相机。\n",
|
||||
"另外,介绍关于电视产品的信息。\"\"\"\n",
|
||||
"messages = [ \n",
|
||||
"{'role':'system',\n",
|
||||
" 'content': system_message}, \n",
|
||||
"{'role':'user',\n",
|
||||
" 'content': user_message_1}, \n",
|
||||
"{'role':'assistant',\n",
|
||||
" 'content': f\"\"\"相关产品信息:\\n\\\n",
|
||||
" {product_information_for_user_message_1}\"\"\"}, \n",
|
||||
"]\n",
|
||||
"final_response = get_completion_from_messages(messages)\n",
|
||||
"print(final_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.4"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Reference in New Issue
Block a user