diff --git a/docs/content/C2 Building Systems with the ChatGPT API/4.检查输入-监督 Moderation.ipynb b/docs/content/C2 Building Systems with the ChatGPT API/4.检查输入-监督 Moderation.ipynb index 7071428..98e6917 100644 --- a/docs/content/C2 Building Systems with the ChatGPT API/4.检查输入-监督 Moderation.ipynb +++ b/docs/content/C2 Building Systems with the ChatGPT API/4.检查输入-监督 Moderation.ipynb @@ -110,52 +110,52 @@ " \n", " \n", " \n", - " 性别\n", + " 性行为\n", " False\n", " False\n", - " 0.000177\n", + " 0.000213\n", " \n", " \n", " 仇恨\n", " False\n", " False\n", - " 0.000734\n", + " 0.001008\n", " \n", " \n", " 骚扰\n", " False\n", " False\n", - " 0.023851\n", + " 0.029169\n", " \n", " \n", " 自残\n", " False\n", " False\n", - " 0.002696\n", + " 0.002632\n", " \n", " \n", - " 性别/未成年人\n", + " 性行为/未成年人\n", " False\n", " False\n", - " 0.000038\n", + " 0.000054\n", " \n", " \n", " 仇恨/威胁\n", " False\n", " False\n", - " 0.000032\n", + " 0.000044\n", " \n", " \n", " 暴力/图形\n", " False\n", " False\n", - " 0.000036\n", + " 0.000043\n", " \n", " \n", " 自残/意图\n", " False\n", " False\n", - " 0.000076\n", + " 0.000077\n", " \n", " \n", " 自残/指导\n", @@ -167,31 +167,31 @@ " 骚扰/威胁\n", " False\n", " False\n", - " 0.016919\n", + " 0.020771\n", " \n", " \n", " 暴力\n", " False\n", " False\n", - " 0.881624\n", + " 0.894689\n", " \n", " \n", "\n", "" ], "text/plain": [ - " 标记 类别 类别得分\n", - "性别 False False 0.000177\n", - "仇恨 False False 0.000734\n", - "骚扰 False False 0.023851\n", - "自残 False False 0.002696\n", - "性别/未成年人 False False 0.000038\n", - "仇恨/威胁 False False 0.000032\n", - "暴力/图形 False False 0.000036\n", - "自残/意图 False False 0.000076\n", - "自残/指导 False False 0.000001\n", - "骚扰/威胁 False False 0.016919\n", - "暴力 False False 0.881624" + " 标记 类别 类别得分\n", + "性行为 False False 0.000213\n", + "仇恨 False False 0.001008\n", + "骚扰 False False 0.029169\n", + "自残 False False 0.002632\n", + "性行为/未成年人 False False 0.000054\n", + "仇恨/威胁 False False 0.000044\n", + "暴力/图形 False False 0.000043\n", + "自残/意图 False False 0.000077\n", + "自残/指导 False False 0.000001\n", + "骚扰/威胁 False False 0.020771\n", + "暴力 False False 0.894689" ] }, "execution_count": 3, @@ -224,127 +224,10 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "694734db", "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\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", - " \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", - " \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", - " \n", - " \n", - " \n", - " \n", - "
标记类别类别得分
性别FalseFalse0.000177
仇恨FalseFalse0.000734
骚扰FalseFalse0.023851
自残FalseFalse0.002696
性别/未成年人FalseFalse0.000038
仇恨/威胁FalseFalse0.000032
暴力/图形FalseFalse0.000036
自残/意图FalseFalse0.000076
自残/指导FalseFalse0.000001
骚扰/威胁FalseFalse0.016919
暴力FalseFalse0.881624
\n", - "
" - ], - "text/plain": [ - " 标记 类别 类别得分\n", - "性别 False False 0.000177\n", - "仇恨 False False 0.000734\n", - "骚扰 False False 0.023851\n", - "自残 False False 0.002696\n", - "性别/未成年人 False False 0.000038\n", - "仇恨/威胁 False False 0.000032\n", - "暴力/图形 False False 0.000036\n", - "自残/意图 False False 0.000076\n", - "自残/指导 False False 0.000001\n", - "骚扰/威胁 False False 0.016919\n", - "暴力 False False 0.881624" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "response = openai.Moderation.create(\n", " input=\"\"\"\n", @@ -427,7 +310,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "id": "30acfd5f", "metadata": {}, "outputs": [], @@ -460,7 +343,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "c37481cc", "metadata": {}, "outputs": [], @@ -472,18 +355,10 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "8db8f68f-469c-45e2-a7f1-c46d0b1e1cb9", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Mi dispiace, ma posso rispondere solo in italiano. Se hai bisogno di aiuto o informazioni, sarò felice di assisterti.\n" - ] - } - ], + "outputs": [], "source": [ "messages = [\n", "{'role':'system', 'content': system_message},\n", @@ -513,7 +388,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "id": "d12989e5-b5c4-48ff-a207-a86e680d8587", "metadata": {}, "outputs": [], @@ -525,18 +400,10 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "id": "9f24d9eb-92ac-4d17-9c05-7ea63cad686a", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "快乐胡萝卜是一种充满活力和快乐的蔬菜,它的鲜橙色外表让人感到愉悦。无论是煮熟还是生吃,它都能给人带来满满的能量和幸福感。无论何时何地,快乐胡萝卜都是一道令人愉快的美食。\n" - ] - } - ], + "outputs": [], "source": [ "messages = [\n", "{'role':'system', 'content': system_message},\n", @@ -569,7 +436,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "id": "baca58d2-7356-4810-b0f5-95635812ffe3", "metadata": {}, "outputs": [], @@ -584,18 +451,10 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "id": "116368eb-42a9-452f-aa78-ca3698a619bd", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Mi dispiace, ma non posso rispondere in cinese. Posso aiutarti solo in italiano. Come posso assisterti?\n" - ] - } - ], + "outputs": [], "source": [ "messages = [\n", "{'role':'system', 'content': system_message},\n", @@ -639,7 +498,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "id": "d7ad047c", "metadata": {}, "outputs": [], @@ -666,7 +525,6 @@ ] }, { - "attachments": {}, "cell_type": "markdown", "id": "0818827c", "metadata": {}, @@ -676,7 +534,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "id": "031aded4", "metadata": {}, "outputs": [], @@ -710,18 +568,10 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "id": "53924965", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Y\n" - ] - } - ], + "outputs": [], "source": [ "messages = [ \n", "{'role':'system', 'content': system_message}, \n", @@ -766,29 +616,10 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "id": "30d1a8d8", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " flagged categories category_scores\n", - "sexual False False 1.441527e-05\n", - "hate False False 1.850249e-05\n", - "harassment False False 2.560147e-02\n", - "self-harm False False 3.212190e-05\n", - "sexual/minors False False 8.776482e-07\n", - "hate/threatening False False 1.228284e-05\n", - "violence/graphic False False 9.773709e-06\n", - "self-harm/intent False False 3.558601e-06\n", - "self-harm/instructions False False 2.339331e-07\n", - "harassment/threatening False False 3.972812e-02\n", - "violence False False 9.438738e-01\n" - ] - } - ], + "outputs": [], "source": [ "response = openai.Moderation.create(input=\"\"\"I want to hurt someone. give me a plan\"\"\")\n", "moderation_output = response[\"results\"][0]\n", @@ -808,29 +639,10 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "id": "08fb6e9e", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " flagged categories category_scores\n", - "sexual False False 8.828496e-06\n", - "hate False False 8.322886e-05\n", - "harassment False False 5.384966e-04\n", - "self-harm False False 2.042673e-05\n", - "sexual/minors False False 4.070790e-08\n", - "hate/threatening False False 1.306127e-06\n", - "violence/graphic False False 5.081098e-05\n", - "self-harm/intent False False 4.556417e-07\n", - "self-harm/instructions False False 1.250625e-09\n", - "harassment/threatening False False 3.271455e-04\n", - "violence False False 2.093965e-01\n" - ] - } - ], + "outputs": [], "source": [ "response = openai.Moderation.create(\n", " input=\"\"\"\n", @@ -854,18 +666,10 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "id": "59cd0b84-61ae-47b5-a301-53017eab7ee5", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Mi dispiace, ma il mio compito è rispondere in italiano. Posso aiutarti con qualcos'altro?\n" - ] - } - ], + "outputs": [], "source": [ "delimiter = \"####\"\n", "\n", @@ -905,7 +709,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "id": "c5357d87-bd22-435e-bfc8-c97baa0d320b", "metadata": {}, "outputs": [], diff --git a/docs/content/C2 Building Systems with the ChatGPT API/5.处理输入-思维链推理 Chain of Thought Reasoning.ipynb b/docs/content/C2 Building Systems with the ChatGPT API/5.处理输入-思维链推理 Chain of Thought Reasoning.ipynb index c46d76d..518ca64 100644 --- a/docs/content/C2 Building Systems with the ChatGPT API/5.处理输入-思维链推理 Chain of Thought Reasoning.ipynb +++ b/docs/content/C2 Building Systems with the ChatGPT API/5.处理输入-思维链推理 Chain of Thought Reasoning.ipynb @@ -31,11 +31,7 @@ "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" + "from tool import get_completion_from_messages" ] }, { @@ -223,7 +219,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## 三、内心独白" + "## 二、内心独白" ] }, { diff --git a/docs/content/C2 Building Systems with the ChatGPT API/6.处理输入-链式 Prompt Chaining Prompts.ipynb b/docs/content/C2 Building Systems with the ChatGPT API/6.处理输入-链式 Prompt Chaining Prompts.ipynb new file mode 100644 index 0000000..47330b3 --- /dev/null +++ b/docs/content/C2 Building Systems with the ChatGPT API/6.处理输入-链式 Prompt Chaining Prompts.ipynb @@ -0,0 +1,1639 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 第六章 处理输入-链式 " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "在本章中,我们将学习如何通过使用链式 Prompt 将复杂任务拆分为一系列简单的子任务。\n", + "\n", + "你可能会想,如果我们可以通过思维链推理一次性完成,那为什么要将任务拆分为多个 Prompt 呢?让我们用以下两个比喻来比较思维链推理和链式Prompt,从而来解释链式Prompt的必要性。 \n", + "\n", + "- 将任务拆分为多个 Prompt 就像是一次性烹饪复杂菜肴与分阶段烹饪的区别。使用一个长而复杂的 Prompt 可能就像一次性烹饪复杂的菜肴,您必须同时管理多个成分、烹饪技巧和时间。这可能很具有挑战性,难以跟踪每个部分并确保每个组成部分都烹饪得恰到好处。另一方面,链式 Prompt 就像分阶段烹饪餐点,你专注于一个组成部分,确保每个部分都正确烹饪后再进行下一个。这种方法可以分解任务的复杂性,使其更易于管理,并减少错误的可能性。但是,对于非常简单的食谱,这种方法则可能因为过于复杂而不适用。\n", + "\n", + "- 思维链推理和链式Prompt的区别就像一长串袋馍和简单的模块化程序。相比使用简单的模块化程序,一长串代码之前复杂的依赖关系会导致代码变得混乱且难以调试。\n", + "\n", + "当你的工作流程包含多个步骤和多个状态,在不同状态下需要采取不同操作的时,链式 Prompt 就成为一种强大的策略。比如在对客户的查询进行分类后,获得查询的类别:是账户问题还是产品问题。然后可以根据不同的类别采取不同的行动。\n", + "\n", + "在链式Prompt中,复杂任务被拆分为子任务,每个子任务仅包含执行对应任务所需的指令,这使得系统更易于管理,确保模型具备执行任务所需的所有信息,并降低了出错的可能性。此外,链式Prompt还有有以下好处\n", + "- 降低成本。因为更长的 Prompt 和更多的 tokens 会导致更高的运行成本,并且在某些情况下可能不需要概述所有步骤。\n", + "- 更容易测试哪些步骤可能更容易出错,或者在特定步骤中需要人工干预。\n", + "- 使用外部工具。例如,在其中的某一步,你可以去产品目录中查找某些内容,调用外部 API 或搜索知识库,这些使用单个 Prompt 无法实现的。\n", + "\n", + "随着您与这些模型的构建和交互不断深入,您将逐渐培养出何时运用链式 Prompt策略的直觉。\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 一、使用链式 Prompt 实现产品查询\n", + "\n", + "接下来,我们将使用链式 Prompt 来实现前面章节使用的案例 -- 回答顾客产品的查询,这次的产品列表将包含更多的产品。" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "%cd -q ../../../src/" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import json \n", + "from tool import get_completion_from_messages" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1.1 提取相关产品和类别名称" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'类别': '智能手机和配件', '产品': ['SmartX ProPhone']}, {'类别': '相机和摄像机', '产品': ['FotoSnap DSLR Camera']}, {'类别': '电视和家庭影院系统', '产品': []}]\n" + ] + } + ], + "source": [ + "delimiter = \"####\"\n", + "\n", + "system_message = f\"\"\"\n", + "你将会被提供客服服务查询服务。\n", + "服务查询将使用{delimiter}字符分隔。\n", + "\n", + "仅输出一个 Python 对象列表,其中每个对象具有以下格式:\n", + " '类别': <计算机和笔记本电脑、智能手机和配件、电视和家庭影院系统、游戏机和配件、音频设备、相机和摄像机中的一个>,\n", + "和\n", + " '产品': <产品必须在客户服务查询中找到,且必须在下面的允许产品列表中能找到的产品列表。如果没有找到,则输出空列表> \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", + "\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", + "\n", + "user_message_1 = f\"\"\"\n", + " 请查询 smartx prophone 智能手机和 fotosnap 相机,有dslr那款。\n", + " 另外,请查询关于电视产品的信息。 \"\"\"\n", + "\n", + "messages = [{'role':'system', 'content': system_message}, \n", + " {'role':'user', 'content': f\"{delimiter}{user_message_1}{delimiter}\"}] \n", + "\n", + "category_and_product_response_1 = get_completion_from_messages(messages)\n", + "\n", + "print(category_and_product_response_1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "\n", + "可以看到,输出是一个对象列表,每个对象都有一个类别和一些产品。如\"SmartX ProPhone\" 和 \"Fotosnap DSLR Camera\"。在最后一个对象中,只有类别,产品为空,因为用户查询没有提到任何具体的电视。\n", + "\n", + "
\n", + "\n", + "我们再来看一个例子。" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'类别': '计算机和笔记本电脑', '产品': []}\n" + ] + } + ], + "source": [ + "user_message_2 = f\"\"\"我的路由器不工作了\"\"\"\n", + "messages = [{'role':'system','content': system_message},\n", + " {'role':'user','content': f\"{delimiter}{user_message_2}{delimiter}\"}] \n", + "response = get_completion_from_messages(messages)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1.2 检索提取的产品和类别的详细信息" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "我们提供大量的产品信息作为示例,要求模型提取产品和对应的详细信息" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "products = {\n", + " \"TechPro Ultrabook\": {\n", + " \"名称\": \"TechPro 超极本\",\n", + " \"类别\": \"电脑和笔记本\",\n", + " \"品牌\": \"TechPro\",\n", + " \"型号\": \"TP-UB100\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.5,\n", + " \"特色\": [\"13.3-inch display\", \"8GB RAM\", \"256GB SSD\", \"Intel Core i5 处理器\"],\n", + " \"描述\": \"一款时尚轻便的超极本,适合日常使用。\",\n", + " \"价格\": 799.99\n", + " },\n", + " \"BlueWave Gaming Laptop\": {\n", + " \"名称\": \"BlueWave 游戏本\",\n", + " \"类别\": \"电脑和笔记本\",\n", + " \"品牌\": \"BlueWave\",\n", + " \"型号\": \"BW-GL200\",\n", + " \"保修期\": \"2 years\",\n", + " \"评分\": 4.7,\n", + " \"特色\": [\"15.6-inch display\", \"16GB RAM\", \"512GB SSD\", \"NVIDIA GeForce RTX 3060\"],\n", + " \"描述\": \"一款高性能的游戏笔记本电脑,提供沉浸式体验。\",\n", + " \"价格\": 1199.99\n", + " },\n", + " \"PowerLite Convertible\": {\n", + " \"名称\": \"PowerLite Convertible\",\n", + " \"类别\": \"电脑和笔记本\",\n", + " \"品牌\": \"PowerLite\",\n", + " \"型号\": \"PL-CV300\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.3,\n", + " \"特色\": [\"14-inch touchscreen\", \"8GB RAM\", \"256GB SSD\", \"360-degree hinge\"],\n", + " \"描述\": \"一款多功能的可转换笔记本电脑,具有灵敏的触摸屏。\",\n", + " \"价格\": 699.99\n", + " },\n", + " \"TechPro Desktop\": {\n", + " \"名称\": \"TechPro Desktop\",\n", + " \"类别\": \"电脑和笔记本\",\n", + " \"品牌\": \"TechPro\",\n", + " \"型号\": \"TP-DT500\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.4,\n", + " \"特色\": [\"Intel Core i7 processor\", \"16GB RAM\", \"1TB HDD\", \"NVIDIA GeForce GTX 1660\"],\n", + " \"描述\": \"一款功能强大的台式电脑,适用于工作和娱乐。\",\n", + " \"价格\": 999.99\n", + " },\n", + " \"BlueWave Chromebook\": {\n", + " \"名称\": \"BlueWave Chromebook\",\n", + " \"类别\": \"电脑和笔记本\",\n", + " \"品牌\": \"BlueWave\",\n", + " \"型号\": \"BW-CB100\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.1,\n", + " \"特色\": [\"11.6-inch display\", \"4GB RAM\", \"32GB eMMC\", \"Chrome OS\"],\n", + " \"描述\": \"一款紧凑而价格实惠的Chromebook,适用于日常任务。\",\n", + " \"价格\": 249.99\n", + " },\n", + " \"SmartX ProPhone\": {\n", + " \"名称\": \"SmartX ProPhone\",\n", + " \"类别\": \"智能手机和配件\",\n", + " \"品牌\": \"SmartX\",\n", + " \"型号\": \"SX-PP10\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.6,\n", + " \"特色\": [\"6.1-inch display\", \"128GB storage\", \"12MP dual camera\", \"5G\"],\n", + " \"描述\": \"一款拥有先进摄像功能的强大智能手机。\",\n", + " \"价格\": 899.99\n", + " },\n", + " \"MobiTech PowerCase\": {\n", + " \"名称\": \"MobiTech PowerCase\",\n", + " \"类别\": \"专业手机\",\n", + " \"品牌\": \"MobiTech\",\n", + " \"型号\": \"MT-PC20\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.3,\n", + " \"特色\": [\"5000mAh battery\", \"Wireless charging\", \"Compatible with SmartX ProPhone\"],\n", + " \"描述\": \"一款带有内置电池的保护手机壳,可延长使用时间。\",\n", + " \"价格\": 59.99\n", + " },\n", + " \"SmartX MiniPhone\": {\n", + " \"名称\": \"SmartX MiniPhone\",\n", + " \"类别\": \"专业手机\",\n", + " \"品牌\": \"SmartX\",\n", + " \"型号\": \"SX-MP5\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.2,\n", + " \"特色\": [\"4.7-inch display\", \"64GB storage\", \"8MP camera\", \"4G\"],\n", + " \"描述\": \"一款紧凑而价格实惠的智能手机,适用于基本任务。\",\n", + " \"价格\": 399.99\n", + " },\n", + " \"MobiTech Wireless Charger\": {\n", + " \"名称\": \"MobiTech Wireless Charger\",\n", + " \"类别\": \"专业手机\",\n", + " \"品牌\": \"MobiTech\",\n", + " \"型号\": \"MT-WC10\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.5,\n", + " \"特色\": [\"10W fast charging\", \"Qi-compatible\", \"LED indicator\", \"Compact design\"],\n", + " \"描述\": \"一款方便的无线充电器,使工作区域整洁无杂物。\",\n", + " \"价格\": 29.99\n", + " },\n", + " \"SmartX EarBuds\": {\n", + " \"名称\": \"SmartX EarBuds\",\n", + " \"类别\": \"专业手机\",\n", + " \"品牌\": \"SmartX\",\n", + " \"型号\": \"SX-EB20\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.4,\n", + " \"特色\": [\"True wireless\", \"Bluetooth 5.0\", \"Touch controls\", \"24-hour battery life\"],\n", + " \"描述\": \"通过这些舒适的耳塞体验真正的无线自由。\",\n", + " \"价格\": 99.99\n", + " },\n", + "\n", + " \"CineView 4K TV\": {\n", + " \"名称\": \"CineView 4K TV\",\n", + " \"类别\": \"电视和家庭影院系统\",\n", + " \"品牌\": \"CineView\",\n", + " \"型号\": \"CV-4K55\",\n", + " \"保修期\": \"2 years\",\n", + " \"评分\": 4.8,\n", + " \"特色\": [\"55-inch display\", \"4K resolution\", \"HDR\", \"Smart TV\"],\n", + " \"描述\": \"一款色彩鲜艳、智能功能丰富的惊艳4K电视。\",\n", + " \"价格\": 599.99\n", + " },\n", + " \"SoundMax Home Theater\": {\n", + " \"名称\": \"SoundMax Home Theater\",\n", + " \"类别\": \"电视和家庭影院系统\",\n", + " \"品牌\": \"SoundMax\",\n", + " \"型号\": \"SM-HT100\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.4,\n", + " \"特色\": [\"5.1 channel\", \"1000W output\", \"Wireless subwoofer\", \"Bluetooth\"],\n", + " \"描述\": \"一款强大的家庭影院系统,提供沉浸式音频体验。\",\n", + " \"价格\": 399.99\n", + " },\n", + " \"CineView 8K TV\": {\n", + " \"名称\": \"CineView 8K TV\",\n", + " \"类别\": \"电视和家庭影院系统\",\n", + " \"品牌\": \"CineView\",\n", + " \"型号\": \"CV-8K65\",\n", + " \"保修期\": \"2 years\",\n", + " \"评分\": 4.9,\n", + " \"特色\": [\"65-inch display\", \"8K resolution\", \"HDR\", \"Smart TV\"],\n", + " \"描述\": \"通过这款惊艳的8K电视,体验未来。\",\n", + " \"价格\": 2999.99\n", + " },\n", + " \"SoundMax Soundbar\": {\n", + " \"名称\": \"SoundMax Soundbar\",\n", + " \"类别\": \"电视和家庭影院系统\",\n", + " \"品牌\": \"SoundMax\",\n", + " \"型号\": \"SM-SB50\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.3,\n", + " \"特色\": [\"2.1 channel\", \"300W output\", \"Wireless subwoofer\", \"Bluetooth\"],\n", + " \"描述\": \"使用这款时尚而功能强大的声音,升级您电视的音频体验。\",\n", + " \"价格\": 199.99\n", + " },\n", + " \"CineView OLED TV\": {\n", + " \"名称\": \"CineView OLED TV\",\n", + " \"类别\": \"电视和家庭影院系统\",\n", + " \"品牌\": \"CineView\",\n", + " \"型号\": \"CV-OLED55\",\n", + " \"保修期\": \"2 years\",\n", + " \"评分\": 4.7,\n", + " \"特色\": [\"55-inch display\", \"4K resolution\", \"HDR\", \"Smart TV\"],\n", + " \"描述\": \"通过这款OLED电视,体验真正的五彩斑斓。\",\n", + " \"价格\": 1499.99\n", + " },\n", + "\n", + " \"GameSphere X\": {\n", + " \"名称\": \"GameSphere X\",\n", + " \"类别\": \"游戏机和配件\",\n", + " \"品牌\": \"GameSphere\",\n", + " \"型号\": \"GS-X\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.9,\n", + " \"特色\": [\"4K gaming\", \"1TB storage\", \"Backward compatibility\", \"Online multiplayer\"],\n", + " \"描述\": \"一款下一代游戏机,提供终极游戏体验。\",\n", + " \"价格\": 499.99\n", + " },\n", + " \"ProGamer Controller\": {\n", + " \"名称\": \"ProGamer Controller\",\n", + " \"类别\": \"游戏机和配件\",\n", + " \"品牌\": \"ProGamer\",\n", + " \"型号\": \"PG-C100\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.2,\n", + " \"特色\": [\"Ergonomic design\", \"Customizable buttons\", \"Wireless\", \"Rechargeable battery\"],\n", + " \"描述\": \"一款高品质的游戏手柄,提供精准和舒适的操作。\",\n", + " \"价格\": 59.99\n", + " },\n", + " \"GameSphere Y\": {\n", + " \"名称\": \"GameSphere Y\",\n", + " \"类别\": \"游戏机和配件\",\n", + " \"品牌\": \"GameSphere\",\n", + " \"型号\": \"GS-Y\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.8,\n", + " \"特色\": [\"4K gaming\", \"500GB storage\", \"Backward compatibility\", \"Online multiplayer\"],\n", + " \"描述\": \"一款体积紧凑、性能强劲的游戏机。\",\n", + " \"价格\": 399.99\n", + " },\n", + " \"ProGamer Racing Wheel\": {\n", + " \"名称\": \"ProGamer Racing Wheel\",\n", + " \"类别\": \"游戏机和配件\",\n", + " \"品牌\": \"ProGamer\",\n", + " \"型号\": \"PG-RW200\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.5,\n", + " \"特色\": [\"Force feedback\", \"Adjustable pedals\", \"Paddle shifters\", \"Compatible with GameSphere X\"],\n", + " \"描述\": \"使用这款逼真的赛车方向盘,提升您的赛车游戏体验。\",\n", + " \"价格\": 249.99\n", + " },\n", + " \"GameSphere VR Headset\": {\n", + " \"名称\": \"GameSphere VR Headset\",\n", + " \"类别\": \"游戏机和配件\",\n", + " \"品牌\": \"GameSphere\",\n", + " \"型号\": \"GS-VR\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.6,\n", + " \"特色\": [\"Immersive VR experience\", \"Built-in headphones\", \"Adjustable headband\", \"Compatible with GameSphere X\"],\n", + " \"描述\": \"通过这款舒适的VR头戴设备,进入虚拟现实的世界。\",\n", + " \"价格\": 299.99\n", + " },\n", + "\n", + " \"AudioPhonic Noise-Canceling Headphones\": {\n", + " \"名称\": \"AudioPhonic Noise-Canceling Headphones\",\n", + " \"类别\": \"音频设备\",\n", + " \"品牌\": \"AudioPhonic\",\n", + " \"型号\": \"AP-NC100\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.6,\n", + " \"特色\": [\"Active noise-canceling\", \"Bluetooth\", \"20-hour battery life\", \"Comfortable fit\"],\n", + " \"描述\": \"通过这款降噪耳机,体验沉浸式的音效。\",\n", + " \"价格\": 199.99\n", + " },\n", + " \"WaveSound Bluetooth Speaker\": {\n", + " \"名称\": \"WaveSound Bluetooth Speaker\",\n", + " \"类别\": \"音频设备\",\n", + " \"品牌\": \"WaveSound\",\n", + " \"型号\": \"WS-BS50\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.5,\n", + " \"特色\": [\"Portable\", \"10-hour battery life\", \"Water-resistant\", \"Built-in microphone\"],\n", + " \"描述\": \"一款紧凑而多用途的蓝牙音箱,适用于随时随地收听音乐。\",\n", + " \"价格\": 49.99\n", + " },\n", + " \"AudioPhonic True Wireless Earbuds\": {\n", + " \"名称\": \"AudioPhonic True Wireless Earbuds\",\n", + " \"类别\": \"音频设备\",\n", + " \"品牌\": \"AudioPhonic\",\n", + " \"型号\": \"AP-TW20\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.4,\n", + " \"特色\": [\"True wireless\", \"Bluetooth 5.0\", \"Touch controls\", \"18-hour battery life\"],\n", + " \"描述\": \"通过这款舒适的真无线耳塞,无需线缆即可享受音乐。\",\n", + " \"价格\": 79.99\n", + " },\n", + " \"WaveSound Soundbar\": {\n", + " \"名称\": \"WaveSound Soundbar\",\n", + " \"类别\": \"音频设备\",\n", + " \"品牌\": \"WaveSound\",\n", + " \"型号\": \"WS-SB40\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.3,\n", + " \"特色\": [\"2.0 channel\", \"80W output\", \"Bluetooth\", \"Wall-mountable\"],\n", + " \"描述\": \"使用这款纤薄而功能强大的声音吧,升级您电视的音频体验。\",\n", + " \"价格\": 99.99\n", + " },\n", + " \"AudioPhonic Turntable\": {\n", + " \"名称\": \"AudioPhonic Turntable\",\n", + " \"类别\": \"音频设备\",\n", + " \"品牌\": \"AudioPhonic\",\n", + " \"型号\": \"AP-TT10\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.2,\n", + " \"特色\": [\"3-speed\", \"Built-in speakers\", \"Bluetooth\", \"USB recording\"],\n", + " \"描述\": \"通过这款现代化的唱片机,重拾您的黑胶唱片收藏。\",\n", + " \"价格\": 149.99\n", + " },\n", + "\n", + " \"FotoSnap DSLR Camera\": {\n", + " \"名称\": \"FotoSnap DSLR Camera\",\n", + " \"类别\": \"相机和摄像机\",\n", + " \"品牌\": \"FotoSnap\",\n", + " \"型号\": \"FS-DSLR200\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.7,\n", + " \"特色\": [\"24.2MP sensor\", \"1080p video\", \"3-inch LCD\", \"Interchangeable lenses\"],\n", + " \"描述\": \"使用这款多功能的单反相机,捕捉惊艳的照片和视频。\",\n", + " \"价格\": 599.99\n", + " },\n", + " \"ActionCam 4K\": {\n", + " \"名称\": \"ActionCam 4K\",\n", + " \"类别\": \"相机和摄像机\",\n", + " \"品牌\": \"ActionCam\",\n", + " \"型号\": \"AC-4K\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.4,\n", + " \"特色\": [\"4K video\", \"Waterproof\", \"Image stabilization\", \"Wi-Fi\"],\n", + " \"描述\": \"使用这款坚固而紧凑的4K运动相机,记录您的冒险旅程。\",\n", + " \"价格\": 299.99\n", + " },\n", + " \"FotoSnap Mirrorless Camera\": {\n", + " \"名称\": \"FotoSnap Mirrorless Camera\",\n", + " \"类别\": \"相机和摄像机\",\n", + " \"品牌\": \"FotoSnap\",\n", + " \"型号\": \"FS-ML100\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.6,\n", + " \"特色\": [\"20.1MP sensor\", \"4K video\", \"3-inch touchscreen\", \"Interchangeable lenses\"],\n", + " \"描述\": \"一款具有先进功能的小巧轻便的无反相机。\",\n", + " \"价格\": 799.99\n", + " },\n", + " \"ZoomMaster Camcorder\": {\n", + " \"名称\": \"ZoomMaster Camcorder\",\n", + " \"类别\": \"相机和摄像机\",\n", + " \"品牌\": \"ZoomMaster\",\n", + " \"型号\": \"ZM-CM50\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.3,\n", + " \"特色\": [\"1080p video\", \"30x optical zoom\", \"3-inch LCD\", \"Image stabilization\"],\n", + " \"描述\": \"使用这款易于使用的摄像机,捕捉生活的瞬间。\",\n", + " \"价格\": 249.99\n", + " },\n", + " \"FotoSnap Instant Camera\": {\n", + " \"名称\": \"FotoSnap Instant Camera\",\n", + " \"类别\": \"相机和摄像机\",\n", + " \"品牌\": \"FotoSnap\",\n", + " \"型号\": \"FS-IC10\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.1,\n", + " \"特色\": [\"Instant prints\", \"Built-in flash\", \"Selfie mirror\", \"Battery-powered\"],\n", + " \"描述\": \"使用这款有趣且便携的即时相机,创造瞬间回忆。\",\n", + " \"价格\": 69.99\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "def get_product_by_name(name):\n", + " \"\"\"\n", + " 根据产品名称获取产品\n", + "\n", + " 参数:\n", + " name: 产品名称\n", + " \"\"\"\n", + " return products.get(name, None)\n", + "\n", + "def get_products_by_category(category):\n", + " \"\"\"\n", + " 根据类别获取产品\n", + "\n", + " 参数:\n", + " category: 产品类别\n", + " \"\"\"\n", + " return [product for product in products.values() if product[\"类别\"] == category]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'名称': 'TechPro 超极本',\n", + " '类别': '电脑和笔记本',\n", + " '品牌': 'TechPro',\n", + " '型号': 'TP-UB100',\n", + " '保修期': '1 year',\n", + " '评分': 4.5,\n", + " '特色': ['13.3-inch display', '8GB RAM', '256GB SSD', 'Intel Core i5 处理器'],\n", + " '描述': '一款时尚轻便的超极本,适合日常使用。',\n", + " '价格': 799.99}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "get_product_by_name(\"TechPro Ultrabook\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'名称': 'TechPro 超极本',\n", + " '类别': '电脑和笔记本',\n", + " '品牌': 'TechPro',\n", + " '型号': 'TP-UB100',\n", + " '保修期': '1 year',\n", + " '评分': 4.5,\n", + " '特色': ['13.3-inch display', '8GB RAM', '256GB SSD', 'Intel Core i5 处理器'],\n", + " '描述': '一款时尚轻便的超极本,适合日常使用。',\n", + " '价格': 799.99},\n", + " {'名称': 'BlueWave 游戏本',\n", + " '类别': '电脑和笔记本',\n", + " '品牌': 'BlueWave',\n", + " '型号': 'BW-GL200',\n", + " '保修期': '2 years',\n", + " '评分': 4.7,\n", + " '特色': ['15.6-inch display',\n", + " '16GB RAM',\n", + " '512GB SSD',\n", + " 'NVIDIA GeForce RTX 3060'],\n", + " '描述': '一款高性能的游戏笔记本电脑,提供沉浸式体验。',\n", + " '价格': 1199.99},\n", + " {'名称': 'PowerLite Convertible',\n", + " '类别': '电脑和笔记本',\n", + " '品牌': 'PowerLite',\n", + " '型号': 'PL-CV300',\n", + " '保修期': '1 year',\n", + " '评分': 4.3,\n", + " '特色': ['14-inch touchscreen', '8GB RAM', '256GB SSD', '360-degree hinge'],\n", + " '描述': '一款多功能的可转换笔记本电脑,具有灵敏的触摸屏。',\n", + " '价格': 699.99},\n", + " {'名称': 'TechPro Desktop',\n", + " '类别': '电脑和笔记本',\n", + " '品牌': 'TechPro',\n", + " '型号': 'TP-DT500',\n", + " '保修期': '1 year',\n", + " '评分': 4.4,\n", + " '特色': ['Intel Core i7 processor',\n", + " '16GB RAM',\n", + " '1TB HDD',\n", + " 'NVIDIA GeForce GTX 1660'],\n", + " '描述': '一款功能强大的台式电脑,适用于工作和娱乐。',\n", + " '价格': 999.99},\n", + " {'名称': 'BlueWave Chromebook',\n", + " '类别': '电脑和笔记本',\n", + " '品牌': 'BlueWave',\n", + " '型号': 'BW-CB100',\n", + " '保修期': '1 year',\n", + " '评分': 4.1,\n", + " '特色': ['11.6-inch display', '4GB RAM', '32GB eMMC', 'Chrome OS'],\n", + " '描述': '一款紧凑而价格实惠的Chromebook,适用于日常任务。',\n", + " '价格': 249.99}]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "get_products_by_category(\"电脑和笔记本\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 1.2.1 解析提取的产品和类别字符串" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "def read_string_to_list(input_string):\n", + " \"\"\"\n", + " 将输入的字符串转换为 Python 列表。\n", + "\n", + " 参数:\n", + " input_string: 输入的字符串,应为有效的 JSON 格式。\n", + "\n", + " 返回:\n", + " list 或 None: 如果输入字符串有效,则返回对应的 Python 列表,否则返回 None。\n", + " \"\"\"\n", + " if input_string is None:\n", + " return None\n", + "\n", + " try:\n", + " # 将输入字符串中的单引号替换为双引号,以满足 JSON 格式的要求\n", + " input_string = input_string.replace(\"'\", \"\\\"\") \n", + " data = json.loads(input_string)\n", + " return data\n", + " except json.JSONDecodeError:\n", + " print(\"Error: Invalid JSON string\")\n", + " return None " + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'类别': '智能手机和配件', '产品': ['SmartX ProPhone']}, {'类别': '相机和摄像机', '产品': ['FotoSnap DSLR Camera']}, {'类别': '电视和家庭影院系统', '产品': []}]\n" + ] + } + ], + "source": [ + "category_and_product_list = read_string_to_list(category_and_product_response_1)\n", + "print(category_and_product_list)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 1.2.2 基于解析得到的产品和类别进行检索" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "def generate_output_string(data_list):\n", + " \"\"\"\n", + " 根据输入的数据列表生成包含产品或类别信息的字符串。\n", + "\n", + " 参数:\n", + " data_list: 包含字典的列表,每个字典都应包含 \"products\" 或 \"category\" 的键。\n", + "\n", + " 返回:\n", + " output_string: 包含产品或类别信息的字符串。\n", + " \"\"\"\n", + " output_string = \"\"\n", + " if data_list is None:\n", + " return output_string\n", + "\n", + " for data in data_list:\n", + " try:\n", + " if \"产品\" in data and data[\"产品\"]:\n", + " products_list = data[\"产品\"]\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, ensure_ascii=False) + \"\\n\"\n", + " else:\n", + " print(f\"Error: Product '{product_name}' not found\")\n", + " elif \"类别\" in data:\n", + " category_name = data[\"类别\"]\n", + " category_products = get_products_by_category(category_name)\n", + " for product in category_products:\n", + " output_string += json.dumps(product, indent=4, ensure_ascii=False) + \"\\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": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\n", + " \"名称\": \"SmartX ProPhone\",\n", + " \"类别\": \"智能手机和配件\",\n", + " \"品牌\": \"SmartX\",\n", + " \"型号\": \"SX-PP10\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.6,\n", + " \"特色\": [\n", + " \"6.1-inch display\",\n", + " \"128GB storage\",\n", + " \"12MP dual camera\",\n", + " \"5G\"\n", + " ],\n", + " \"描述\": \"一款拥有先进摄像功能的强大智能手机。\",\n", + " \"价格\": 899.99\n", + "}\n", + "{\n", + " \"名称\": \"FotoSnap DSLR Camera\",\n", + " \"类别\": \"相机和摄像机\",\n", + " \"品牌\": \"FotoSnap\",\n", + " \"型号\": \"FS-DSLR200\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.7,\n", + " \"特色\": [\n", + " \"24.2MP sensor\",\n", + " \"1080p video\",\n", + " \"3-inch LCD\",\n", + " \"Interchangeable lenses\"\n", + " ],\n", + " \"描述\": \"使用这款多功能的单反相机,捕捉惊艳的照片和视频。\",\n", + " \"价格\": 599.99\n", + "}\n", + "{\n", + " \"名称\": \"CineView 4K TV\",\n", + " \"类别\": \"电视和家庭影院系统\",\n", + " \"品牌\": \"CineView\",\n", + " \"型号\": \"CV-4K55\",\n", + " \"保修期\": \"2 years\",\n", + " \"评分\": 4.8,\n", + " \"特色\": [\n", + " \"55-inch display\",\n", + " \"4K resolution\",\n", + " \"HDR\",\n", + " \"Smart TV\"\n", + " ],\n", + " \"描述\": \"一款色彩鲜艳、智能功能丰富的惊艳4K电视。\",\n", + " \"价格\": 599.99\n", + "}\n", + "{\n", + " \"名称\": \"SoundMax Home Theater\",\n", + " \"类别\": \"电视和家庭影院系统\",\n", + " \"品牌\": \"SoundMax\",\n", + " \"型号\": \"SM-HT100\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.4,\n", + " \"特色\": [\n", + " \"5.1 channel\",\n", + " \"1000W output\",\n", + " \"Wireless subwoofer\",\n", + " \"Bluetooth\"\n", + " ],\n", + " \"描述\": \"一款强大的家庭影院系统,提供沉浸式音频体验。\",\n", + " \"价格\": 399.99\n", + "}\n", + "{\n", + " \"名称\": \"CineView 8K TV\",\n", + " \"类别\": \"电视和家庭影院系统\",\n", + " \"品牌\": \"CineView\",\n", + " \"型号\": \"CV-8K65\",\n", + " \"保修期\": \"2 years\",\n", + " \"评分\": 4.9,\n", + " \"特色\": [\n", + " \"65-inch display\",\n", + " \"8K resolution\",\n", + " \"HDR\",\n", + " \"Smart TV\"\n", + " ],\n", + " \"描述\": \"通过这款惊艳的8K电视,体验未来。\",\n", + " \"价格\": 2999.99\n", + "}\n", + "{\n", + " \"名称\": \"SoundMax Soundbar\",\n", + " \"类别\": \"电视和家庭影院系统\",\n", + " \"品牌\": \"SoundMax\",\n", + " \"型号\": \"SM-SB50\",\n", + " \"保修期\": \"1 year\",\n", + " \"评分\": 4.3,\n", + " \"特色\": [\n", + " \"2.1 channel\",\n", + " \"300W output\",\n", + " \"Wireless subwoofer\",\n", + " \"Bluetooth\"\n", + " ],\n", + " \"描述\": \"使用这款时尚而功能强大的声音,升级您电视的音频体验。\",\n", + " \"价格\": 199.99\n", + "}\n", + "{\n", + " \"名称\": \"CineView OLED TV\",\n", + " \"类别\": \"电视和家庭影院系统\",\n", + " \"品牌\": \"CineView\",\n", + " \"型号\": \"CV-OLED55\",\n", + " \"保修期\": \"2 years\",\n", + " \"评分\": 4.7,\n", + " \"特色\": [\n", + " \"55-inch display\",\n", + " \"4K resolution\",\n", + " \"HDR\",\n", + " \"Smart TV\"\n", + " ],\n", + " \"描述\": \"通过这款OLED电视,体验真正的五彩斑斓。\",\n", + " \"价格\": 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)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1.3 根据详细的产品信息生成用户查询的答案" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "关于SmartX ProPhone智能手机和FotoSnap相机,我们有以下款式:\n", + "\n", + "1. SmartX ProPhone SX-PP10智能手机:\n", + " - 6.1英寸显示屏\n", + " - 128GB存储空间\n", + " - 12MP双摄像头\n", + " - 支持5G网络\n", + " - 价格为899.99美元\n", + "\n", + "2. FotoSnap FS-DSLR200 DSLR相机:\n", + " - 24.2MP传感器\n", + " - 1080p视频拍摄\n", + " - 3英寸液晶屏\n", + " - 可更换镜头\n", + " - 价格为599.99美元\n", + "\n", + "关于电视产品,我们有以下几款可供选择:\n", + "\n", + "1. CineView 4K电视 CV-4K55:\n", + " - 55英寸显示屏\n", + " - 4K分辨率\n", + " - 支持HDR\n", + " - 智能电视功能\n", + " - 价格为599.99美元\n", + "\n", + "2. CineView 8K电视 CV-8K65:\n", + " - 65英寸显示屏\n", + " - 8K分辨率\n", + " - 支持HDR\n", + " - 智能电视功能\n", + " - 价格为2999.99美元\n", + "\n", + "3. CineView OLED电视 CV-OLED55:\n", + " - 55英寸显示屏\n", + " - 4K分辨率\n", + " - 支持HDR\n", + " - 智能电视功能\n", + " - 价格为1499.99美元\n", + "\n", + "如果您对以上产品有任何进一步的问题或需要其他产品的信息,请随时告诉我。\n" + ] + } + ], + "source": [ + "system_message = f\"\"\"\n", + "您是一家大型电子商店的客服助理。\n", + "请以友好和乐于助人的口吻回答问题,并尽量简洁明了。\n", + "请确保向用户提出相关的后续问题。\n", + "\"\"\"\n", + "\n", + "user_message_1 = f\"\"\"\n", + "请查询 smartx prophone 智能手机和 fotosnap 相机,有dslr那款。\n", + "另外,介绍关于电视产品的信息。\n", + "\"\"\"\n", + "\n", + "messages = [{'role':'system','content': system_message},\n", + " {'role':'user','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)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1.4 总结" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "我们讨论了如何通过一系列步骤加载与用户查询相关的信息,为模型提供所需的上下文,以有效回答问题。\n", + "\n", + "您可能会想,为什么我们选择性地将产品描述加载到提示中,而不是包含所有产品描述,让模型使用它所需的信息呢?\n", + "\n", + "这其中有几个原因。\n", + "\n", + "首先,包含过多的产品描述可能会使模型在处理上下文时感到困惑,就像对于试图一次处理大量信息的人一样。当然,对于像 GPT-4 这样更高级的模型来说,这个原因就不太重要了。尤其是当上下文像这个例子一样具有良好的结构时,模型足够聪明,能够巧妙地忽略那些明显不相关的信息。\n", + "\n", + "接下来的原因更加具有说服力。\n", + "\n", + "首先,包含所有产品描述可能会使模型对上下文更加混乱,就像对于试图一次处理大量信息的人一样。当然,对于像 GPT-4 这样更高级的模型来说,这个问题不太相关,特别是当上下文像这个例子一样结构良好时,模型足够聪明,只会忽略明显不相关的信息。接下来的原因更有说服力。\n", + "\n", + "第二个原因是,语言模型有上下文限制,即固定数量的 token 允许作为输入和输出。如果您有一个巨大的产品目录,您甚至无法将所有描述都放入上下文窗口中。\n", + "\n", + "最后一个原因是,包含所有产品描述可能会使模型过拟合,因为它会记住所有的产品描述,而不是只记住与查询相关的信息。这可能会导致模型在处理新的查询时表现不佳。\n", + "\n", + "使用语言模型时,由于按 token 付费,可能会很昂贵。因此,通过有选择地加载信息,可以减少生成响应的成本。一般来说,确定何时动态加载信息到模型的上下文中,并允许模型决定何时需要更多信息,是增强这些模型能力的最佳方法之一。\n", + "\n", + "并且要再次强调,您应该将语言模型视为需要必要上下文才能得出有用结论和执行有用任务的推理代理。因此,在这种情况下,我们必须向模型提供产品信息,然后它才能根据该产品信息进行推理,为用户创建有用的答案。\n", + "\n", + "在这个例子中,我们只添加了一个特定函数或函数的调用,以通过产品名称获取产品描述或通过类别名称获取类别产品。但是,模型实际上擅长决定何时使用各种不同的工具,并可以正确地使用它们。这就是 ChatGPT 插件背后的思想。我们告诉模型它可以访问哪些工具以及它们的作用,它会在需要从特定来源获取信息或想要采取其他适当的操作时选择使用它们。在这个例子中,我们只能通过精确的产品和类别名称匹配查找信息,但还有更高级的信息检索技术。检索信息的最有效方法之一是使用自然语言处理技术,例如命名实体识别和关系提取。\n", + "\n", + "另一方法是使用文本嵌入(Embedding)来获取信息。嵌入可以用于实现对大型语料库的高效知识检索,以查找与给定查询相关的信息。使用文本嵌入的一个关键优势是它们可以实现模糊或语义搜索,这使您能够在不使用精确关键字的情况下找到相关信息。因此,在此例子中,我们不一定需要产品的确切名称,而可以使用更一般的查询如 **“手机”** 进行搜索。我们计划很快推出一门全面的课程,介绍如何在各种应用中使用嵌入,敬请关注。\n", + "\n", + "在下一章中我们将讨论如何评估语言模型的输出。" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 附录: 英文版提示" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "tags": [] + }, + "source": [ + "### 提取相关产品和类别名称" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'category': 'Smartphones and Accessories', 'products': ['SmartX ProPhone']}, {'category': 'Cameras and Camcorders', 'products': ['FotoSnap DSLR Camera']}, {'category': 'Televisions and Home Theater Systems', 'products': ['CineView 4K TV', 'CineView 8K TV', 'CineView OLED TV']}]\n" + ] + } + ], + "source": [ + "delimiter = \"####\"\n", + "\n", + "system_message = f\"\"\"\n", + "You will be provided with customer service queries. \\\n", + "The customer service query will be delimited with \\\n", + "{delimiter} characters.\n", + "Output a Python list of objects, where each object has \\\n", + "the following format:\n", + " 'category': ,\n", + "and\n", + " 'products': \n", + "\n", + "Where the categories and products must be found in \\\n", + "the customer service query.\n", + "If a product is mentioned, it must be associated with \\\n", + "the correct category in the allowed products list below.\n", + "If no products or categories are found, output an \\\n", + "empty list.\n", + "\n", + "Allowed products: \n", + "\n", + "Products under Computers and Laptops category:\n", + "TechPro Ultrabook\n", + "BlueWave Gaming Laptop\n", + "PowerLite Convertible\n", + "TechPro Desktop\n", + "BlueWave Chromebook\n", + "\n", + "Products under Smartphones and Accessories category:\n", + "SmartX ProPhone\n", + "MobiTech PowerCase\n", + "SmartX MiniPhone\n", + "MobiTech Wireless Charger\n", + "SmartX EarBuds\n", + "\n", + "Products under Televisions and Home Theater Systems category:\n", + "CineView 4K TV\n", + "SoundMax Home Theater\n", + "CineView 8K TV\n", + "SoundMax Soundbar\n", + "CineView OLED TV\n", + "\n", + "Products under Gaming Consoles and Accessories category:\n", + "GameSphere X\n", + "ProGamer Controller\n", + "GameSphere Y\n", + "ProGamer Racing Wheel\n", + "GameSphere VR Headset\n", + "\n", + "Products under Audio Equipment category:\n", + "AudioPhonic Noise-Canceling Headphones\n", + "WaveSound Bluetooth Speaker\n", + "AudioPhonic True Wireless Earbuds\n", + "WaveSound Soundbar\n", + "AudioPhonic Turntable\n", + "\n", + "Products under Cameras and Camcorders category:\n", + "FotoSnap DSLR Camera\n", + "ActionCam 4K\n", + "FotoSnap Mirrorless Camera\n", + "ZoomMaster Camcorder\n", + "FotoSnap Instant Camera\n", + "\n", + "Only output the list of objects, with nothing else.\n", + "\"\"\"\n", + "\n", + "user_message_1 = f\"\"\"\n", + " tell me about the smartx pro phone and \\\n", + " the fotosnap camera, the dslr one. \\\n", + " Also tell me about your tvs \"\"\"\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": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[]\n" + ] + } + ], + "source": [ + "user_message_2 = f\"\"\"\n", + "my router isn't working\"\"\"\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)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 检索提取的产品和类别的详细信息" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "products = {\n", + " \"TechPro Ultrabook\": {\n", + " \"name\": \"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", + " \"BlueWave Gaming Laptop\": {\n", + " \"name\": \"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", + " \"PowerLite Convertible\": {\n", + " \"name\": \"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", + " \"TechPro Desktop\": {\n", + " \"name\": \"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", + " \"BlueWave Chromebook\": {\n", + " \"name\": \"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", + " \"SmartX ProPhone\": {\n", + " \"name\": \"SmartX ProPhone\",\n", + " \"category\": \"Smartphones and Accessories\",\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\": \"A powerful smartphone with advanced camera features.\",\n", + " \"price\": 899.99\n", + " },\n", + " \"MobiTech PowerCase\": {\n", + " \"name\": \"MobiTech PowerCase\",\n", + " \"category\": \"Smartphones and Accessories\",\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\": \"A protective case with built-in battery for extended usage.\",\n", + " \"price\": 59.99\n", + " },\n", + " \"SmartX MiniPhone\": {\n", + " \"name\": \"SmartX MiniPhone\",\n", + " \"category\": \"Smartphones and Accessories\",\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\": \"A compact and affordable smartphone for basic tasks.\",\n", + " \"price\": 399.99\n", + " },\n", + " \"MobiTech Wireless Charger\": {\n", + " \"name\": \"MobiTech Wireless Charger\",\n", + " \"category\": \"Smartphones and Accessories\",\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\": \"A convenient wireless charger for a clutter-free workspace.\",\n", + " \"price\": 29.99\n", + " },\n", + " \"SmartX EarBuds\": {\n", + " \"name\": \"SmartX EarBuds\",\n", + " \"category\": \"Smartphones and Accessories\",\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\": \"Experience true wireless freedom with these comfortable earbuds.\",\n", + " \"price\": 99.99\n", + " },\n", + "\n", + " \"CineView 4K TV\": {\n", + " \"name\": \"CineView 4K TV\",\n", + " \"category\": \"Televisions and Home Theater Systems\",\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\": \"A stunning 4K TV with vibrant colors and smart features.\",\n", + " \"price\": 599.99\n", + " },\n", + " \"SoundMax Home Theater\": {\n", + " \"name\": \"SoundMax Home Theater\",\n", + " \"category\": \"Televisions and Home Theater Systems\",\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\": \"A powerful home theater system for an immersive audio experience.\",\n", + " \"price\": 399.99\n", + " },\n", + " \"CineView 8K TV\": {\n", + " \"name\": \"CineView 8K TV\",\n", + " \"category\": \"Televisions and Home Theater Systems\",\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\": \"Experience the future of television with this stunning 8K TV.\",\n", + " \"price\": 2999.99\n", + " },\n", + " \"SoundMax Soundbar\": {\n", + " \"name\": \"SoundMax Soundbar\",\n", + " \"category\": \"Televisions and Home Theater Systems\",\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\": \"Upgrade your TV's audio with this sleek and powerful soundbar.\",\n", + " \"price\": 199.99\n", + " },\n", + " \"CineView OLED TV\": {\n", + " \"name\": \"CineView OLED TV\",\n", + " \"category\": \"Televisions and Home Theater Systems\",\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\": \"Experience true blacks and vibrant colors with this OLED TV.\",\n", + " \"price\": 1499.99\n", + " },\n", + "\n", + " \"GameSphere X\": {\n", + " \"name\": \"GameSphere X\",\n", + " \"category\": \"Gaming Consoles and Accessories\",\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\": \"A next-generation gaming console for the ultimate gaming experience.\",\n", + " \"price\": 499.99\n", + " },\n", + " \"ProGamer Controller\": {\n", + " \"name\": \"ProGamer Controller\",\n", + " \"category\": \"Gaming Consoles and Accessories\",\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\": \"A high-quality gaming controller for precision and comfort.\",\n", + " \"price\": 59.99\n", + " },\n", + " \"GameSphere Y\": {\n", + " \"name\": \"GameSphere Y\",\n", + " \"category\": \"Gaming Consoles and Accessories\",\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\": \"A compact gaming console with powerful performance.\",\n", + " \"price\": 399.99\n", + " },\n", + " \"ProGamer Racing Wheel\": {\n", + " \"name\": \"ProGamer Racing Wheel\",\n", + " \"category\": \"Gaming Consoles and Accessories\",\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\": \"Enhance your racing games with this realistic racing wheel.\",\n", + " \"price\": 249.99\n", + " },\n", + " \"GameSphere VR Headset\": {\n", + " \"name\": \"GameSphere VR Headset\",\n", + " \"category\": \"Gaming Consoles and Accessories\",\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\": \"Step into the world of virtual reality with this comfortable VR headset.\",\n", + " \"price\": 299.99\n", + " },\n", + "\n", + " \"AudioPhonic Noise-Canceling Headphones\": {\n", + " \"name\": \"AudioPhonic Noise-Canceling Headphones\",\n", + " \"category\": \"Audio Equipment\",\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\": \"Experience immersive sound with these noise-canceling headphones.\",\n", + " \"price\": 199.99\n", + " },\n", + " \"WaveSound Bluetooth Speaker\": {\n", + " \"name\": \"WaveSound Bluetooth Speaker\",\n", + " \"category\": \"Audio Equipment\",\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\": \"A compact and versatile Bluetooth speaker for music on the go.\",\n", + " \"price\": 49.99\n", + " },\n", + " \"AudioPhonic True Wireless Earbuds\": {\n", + " \"name\": \"AudioPhonic True Wireless Earbuds\",\n", + " \"category\": \"Audio Equipment\",\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\": \"Enjoy music without wires with these comfortable true wireless earbuds.\",\n", + " \"price\": 79.99\n", + " },\n", + " \"WaveSound Soundbar\": {\n", + " \"name\": \"WaveSound Soundbar\",\n", + " \"category\": \"Audio Equipment\",\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\": \"Upgrade your TV's audio with this slim and powerful soundbar.\",\n", + " \"price\": 99.99\n", + " },\n", + " \"AudioPhonic Turntable\": {\n", + " \"name\": \"AudioPhonic Turntable\",\n", + " \"category\": \"Audio Equipment\",\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\": \"Rediscover your vinyl collection with this modern turntable.\",\n", + " \"price\": 149.99\n", + " },\n", + "\n", + " \"FotoSnap DSLR Camera\": {\n", + " \"name\": \"FotoSnap DSLR Camera\",\n", + " \"category\": \"Cameras and Camcorders\",\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\": \"Capture stunning photos and videos with this versatile DSLR camera.\",\n", + " \"price\": 599.99\n", + " },\n", + " \"ActionCam 4K\": {\n", + " \"name\": \"ActionCam 4K\",\n", + " \"category\": \"Cameras and Camcorders\",\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\": \"Record your adventures with this rugged and compact 4K action camera.\",\n", + " \"price\": 299.99\n", + " },\n", + " \"FotoSnap Mirrorless Camera\": {\n", + " \"name\": \"FotoSnap Mirrorless Camera\",\n", + " \"category\": \"Cameras and Camcorders\",\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\": \"A compact and lightweight mirrorless camera with advanced features.\",\n", + " \"price\": 799.99\n", + " },\n", + " \"ZoomMaster Camcorder\": {\n", + " \"name\": \"ZoomMaster Camcorder\",\n", + " \"category\": \"Cameras and Camcorders\",\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\": \"Capture life's moments with this easy-to-use camcorder.\",\n", + " \"price\": 249.99\n", + " },\n", + " \"FotoSnap Instant Camera\": {\n", + " \"name\": \"FotoSnap Instant Camera\",\n", + " \"category\": \"Cameras and Camcorders\",\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\": \"Create instant memories with this fun and portable instant camera.\",\n", + " \"price\": 69.99\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "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": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'name': 'TechPro Ultrabook', 'category': 'Computers and Laptops', 'brand': 'TechPro', 'model_number': 'TP-UB100', 'warranty': '1 year', 'rating': 4.5, 'features': ['13.3-inch display', '8GB RAM', '256GB SSD', 'Intel Core i5 processor'], 'description': 'A sleek and lightweight ultrabook for everyday use.', '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 Ultrabook', 'category': 'Computers and Laptops', 'brand': 'TechPro', 'model_number': 'TP-UB100', 'warranty': '1 year', 'rating': 4.5, 'features': ['13.3-inch display', '8GB RAM', '256GB SSD', 'Intel Core i5 processor'], 'description': 'A sleek and lightweight ultrabook for everyday use.', 'price': 799.99}, {'name': 'BlueWave Gaming Laptop', 'category': 'Computers and Laptops', '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': 'A high-performance gaming laptop for an immersive experience.', 'price': 1199.99}, {'name': 'PowerLite Convertible', 'category': 'Computers and Laptops', 'brand': 'PowerLite', 'model_number': 'PL-CV300', 'warranty': '1 year', 'rating': 4.3, 'features': ['14-inch touchscreen', '8GB RAM', '256GB SSD', '360-degree hinge'], 'description': 'A versatile convertible laptop with a responsive touchscreen.', 'price': 699.99}, {'name': 'TechPro Desktop', 'category': 'Computers and Laptops', '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': 'A powerful desktop computer for work and play.', 'price': 999.99}, {'name': 'BlueWave Chromebook', 'category': 'Computers and Laptops', 'brand': 'BlueWave', 'model_number': 'BW-CB100', 'warranty': '1 year', 'rating': 4.1, 'features': ['11.6-inch display', '4GB RAM', '32GB eMMC', 'Chrome OS'], 'description': 'A compact and affordable Chromebook for everyday tasks.', 'price': 249.99}]\n" + ] + } + ], + "source": [ + "print(get_products_by_category(\"Computers and Laptops\"))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 根据详细的产品信息生成用户查询的答案" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The SmartX ProPhone is a powerful smartphone with advanced camera features. It has a 6.1-inch display, 128GB storage, a 12MP dual camera, and supports 5G. It is priced at $899.99.\n", + "\n", + "The FotoSnap DSLR Camera is a versatile camera that allows you to capture stunning photos and videos. It features a 24.2MP sensor, 1080p video recording, a 3-inch LCD screen, and interchangeable lenses. It is priced at $599.99.\n", + "\n", + "We have a range of TVs available. Some popular options include:\n", + "\n", + "1. CineView 4K TV: It has a 55-inch display, 4K resolution, HDR, and is a smart TV. It is priced at $599.99.\n", + "\n", + "2. CineView 8K TV: This stunning 8K TV offers a 65-inch display, 8K resolution, HDR, and is also a smart TV. It is priced at $2999.99.\n", + "\n", + "3. CineView OLED TV: Experience vibrant colors with this OLED TV. It has a 55-inch display, 4K resolution, HDR, and is a smart TV. It is priced at $1499.99.\n", + "\n", + "Please let me know if you have any specific questions or if there's anything else I can assist you with.\n" + ] + } + ], + "source": [ + "system_message = f\"\"\"\n", + "You are a customer service assistant for a \\\n", + "large electronic store. \\\n", + "Respond in a friendly and helpful tone, \\\n", + "with very concise answers. \\\n", + "Make sure to ask the user relevant follow up questions.\n", + "\"\"\"\n", + "user_message_1 = f\"\"\"\n", + "tell me about the smartx pro phone and \\\n", + "the fotosnap camera, the dslr one. \\\n", + "Also tell me about your tvs\"\"\"\n", + "messages = [{'role':'system','content': system_message}, \n", + " {'role':'user','content': user_message_1},\n", + " {'role':'assistant',\n", + " 'content': f\"\"\"Relevant product information:\\n\\\n", + " {product_information_for_user_message_1}\"\"\"}]\n", + "final_response = get_completion_from_messages(messages)\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 +}