简介
溪云初起日沉阁,山雨欲来风满楼。
小伙伴们好,我是微信公众号《小窗幽记机器学习》的小编:卖老班章的小女孩。紧接前面几篇ChatGPT Prompt工程和应用系列文章:
- 04:ChatGPT Prompt编写指南
- 05:如何优化ChatGPT Prompt?
- 06:ChatGPT Prompt实践:文本摘要&推断&转换
- 07:ChatGPT Prompt实践:以智能客服邮件为例
- 08:ChatGPT Prompt实践:如何用ChatGPT构建点餐机器人?
- 09:基于ChatGPT构建智能客服系统(query分类&安全审核&防注入)
- 10:如何编写思维链Prompt?以智能客服为例
- 11:LangChain危矣?亲测ChatGPT函数调用功能:以天气问答为例
- 12:实测OpenAI函数调用功能:以数据库问答为例
今天这篇小作文是吴恩达《Building Systems with the ChatGPT API》课程的第2篇笔记。延续前文思维链Prompt的编写,今天这篇小作文依然以智能客服场景为例,介绍如何通过将复杂任务拆分为一系列简单的子任务来链接多个Prompts。如果需要IPython Notebook版代码,请在微信公众号:小窗幽记机器学习上添加小编微信。
示例1: 抽取query中相关产品和类别
前文已经展示如何对用户的查询(query)进行分类,得到类别结果比如该query是涉"账户问题"还是"产品问题"。然后根据不同类别,执行不同业务逻辑。
delimiter = "####"
system_message = f"""
你将获得客户服务咨询。客户服务咨询将使用{delimiter}字符进行分隔。\
输出一个Python对象列表,其中每个对象具有以下格式:\
'category': <计算机和笔记本、智能手机和配件、电视和家庭影院系统、游戏机和配件、音频设备、相机和摄像机之一>,
或者
'products': <必须是在下面的允许产品列表中找到的产品列表>类别和产品必须在客户服务咨询中找到。\
如果提到了某个产品,则该产品必须与允许产品列表中的正确类别相关联。
如果未找到任何产品或类别,则输出一个空列表。允许的产品:计算机和笔记本类别:
TechPro超极本
BlueWave游戏笔记本电脑
PowerLite可转换本
TechPro台式机
BlueWave Chromebook智能手机和配件类别:
SmartX ProPhone
MobiTech PowerCase
SmartX MiniPhone
MobiTech无线充电器
SmartX耳机电视和家庭影院系统类别:
CineView 4K电视
SoundMax家庭影院
CineView 8K电视
SoundMax音响
CineView OLED电视游戏机和配件类别:
GameSphere X
ProGamer手柄
GameSphere Y
ProGamer方向盘
GameSphere VR头盔音频设备类别:
AudioPhonic降噪耳机
WaveSound蓝牙音箱
AudioPhonic真无线耳机
WaveSound音响
AudioPhonic唱片机相机和摄像机类别:
FotoSnap单反相机
ActionCam 4K
FotoSnap无反相机
ZoomMaster摄像机
FotoSnap即时相机只输出对象列表,不包含其他内容。
"""user_message_1 = f"""
我想了解下smartx pro phone 和 \
fotosnap单反相机。 \
此外,还有关于你们的tvs"""messages = [
{'role':'system', 'content': system_message},
{'role':'user', 'content': f"{delimiter}{user_message_1}{delimiter}"},
]
category_and_product_response_1 = get_completion_from_messages(messages)
print(category_and_product_response_1)
ChatGPT 回复如下:
[{'category': '智能手机和配件'}, {'products': ['SmartX ProPhone']}, {'category': '相机和摄像机'}, {'products': ['FotoSnap单反相机']}, {'category': '电视和家庭影院系统'}, {'products': ['CineView 4K电视', 'CineView 8K电视', 'CineView OLED电视']}]
示例2: 咨询不在清单内的产品
user_message_2 = f"""我的路由器坏了"""
messages = [
{'role':'system','content': system_message},
{'role':'user','content': f"{delimiter}{user_message_2}{delimiter}"},
]
response = get_completion_from_messages(messages)
print(response)
由于路由器不在产品清单里面,所以返回结果为空,符合预期。
[ ]
示例3: 用户表述某个产品坏了
user_message_2 = f"""我的电视坏了"""
messages = [
{'role':'system','content': system_message},
{'role':'user','content': f"{delimiter}{user_message_2}{delimiter}"},
]
response = get_completion_from_messages(messages)
print(response)
ChatGPT 回复结果如下:
[{'category': '电视和家庭影院系统'}, {'products': []}]
示例4: 返回产品详情
返回相关产品和类别的详情
进一步增加产品的详情,能够通过产品名和类型找到对应产品的详情。
# product information
products = {"TechPro超级本": {"name": "TechPro 超级本","category": "计算机和笔记本电脑","brand": "TechPro","model_number": "TP-UB100","warranty": "1年","rating": 4.5,"features": ["13.3英寸显示屏", "8GB RAM", "256GB SSD", "英特尔酷睿i5处理器"],"description": "一款时尚轻薄的超级本,适用于日常使用。","price": 799.99},"BlueWave游戏笔记本电脑": {"name": "BlueWave 游戏笔记本电脑","category": "计算机和笔记本电脑","brand": "BlueWave","model_number": "BW-GL200","warranty": "2年","rating": 4.7,"features": ["15.6英寸显示屏", "16GB RAM", "512GB SSD", "NVIDIA GeForce RTX 3060"],"description": "一款高性能游戏笔记本电脑,提供沉浸式体验。","price": 1199.99},"PowerLite可转换本": {"name": "PowerLite 可转换本","category": "计算机和笔记本电脑","brand": "PowerLite","model_number": "PL-CV300","warranty": "1年","rating": 4.3,"features": ["14英寸触摸屏", "8GB RAM", "256GB SSD", "360度铰链"],"description": "一款多功能可转换本,配备响应灵敏的触摸屏。","price": 699.99},"TechPro台式机": {"name": "TechPro 台式机","category": "计算机和笔记本电脑","brand": "TechPro","model_number": "TP-DT500","warranty": "1年","rating": 4.4,"features": ["英特尔酷睿i7处理器", "16GB RAM", "1TB HDD", "NVIDIA GeForce GTX 1660"],"description": "一款功能强大的台式电脑,适用于工作和娱乐。","price": 999.99},"BlueWave Chromebook": {"name": "BlueWave Chromebook","category": "计算机和笔记本电脑","brand": "BlueWave","model_number": "BW-CB100","warranty": "1年","rating": 4.1,"features": ["11.6英寸显示屏", "4GB RAM", "32GB eMMC", "Chrome OS"],"description": "一款紧凑实惠的 Chromebook,适用于日常任务。","price": 249.99},"SmartX ProPhone": {"name": "SmartX ProPhone","category": "智能手机和配件","brand": "SmartX","model_number": "SX-PP10","warranty": "1年","rating": 4.6,"features": ["6.1英寸显示屏", "128GB存储空间", "12MP双摄像头", "5G"],"description": "一款功能强大的智能手机,具有先进的摄像功能。","price": 899.99},"MobiTech PowerCase": {"name": "MobiTech PowerCase","category": "智能手机和配件","brand": "MobiTech","model_number": "MT-PC20","warranty": "1年","rating": 4.3,"features": ["5000mAh电池", "无线充电", "适用于SmartX ProPhone"],"description": "一款带有内置电池的保护壳,可延长使用时间。","price": 59.99},"SmartX MiniPhone": {"name": "SmartX MiniPhone","category": "智能手机和配件","brand": "SmartX","model_number": "SX-MP5","warranty": "1年","rating": 4.2,"features": ["4.7英寸显示屏", "64GB存储空间", "8MP摄像头", "4G"],"description": "一款小巧实惠的智能手机,适合基本任务。","price": 399.99},"MobiTech无线充电器": {"name": "MobiTech Wireless Charger","category": "智能手机和配件","brand": "MobiTech","model_number": "MT-WC10","warranty": "1年","rating": 4.5,"features": ["10W快速充电", "Qi兼容", "LED指示灯", "紧凑设计"],"description": "一款方便的无线充电器,适用于整洁的工作空间。","price": 29.99},"SmartX耳机": {"name": "SmartX EarBuds","category": "智能手机和配件","brand": "SmartX","model_number": "SX-EB20","warranty": "1年","rating": 4.4,"features": ["真无线", "蓝牙5.0", "触摸控制", "24小时电池续航"],"description": "通过这款舒适的耳塞体验真正的无线自由。","price": 99.99},"CineView 4K电视": {"name": "CineView 4K TV","category": "电视和家庭影院系统","brand": "CineView","model_number": "CV-4K55","warranty": "2年","rating": 4.8,"features": ["55英寸显示屏", "4K分辨率", "HDR", "智能电视"],"description": "一款具有鲜艳色彩和智能功能的惊艳4K电视。","price": 599.99},"SoundMax家庭影院": {"name": "SoundMax Home Theater","category": "电视和家庭影院系统","brand": "SoundMax","model_number": "SM-HT100","warranty": "1年","rating": 4.4,"features": ["5.1声道", "1000W输出", "无线低音炮", "蓝牙"],"description": "一套功能强大的家庭影院系统,提供沉浸式的音频体验。","price": 399.99},"CineView 8K电视": {"name": "CineView 8K TV","category": "电视和家庭影院系统","brand": "CineView","model_number": "CV-8K65","warranty": "2年","rating": 4.9,"features": ["65英寸显示屏", "8K分辨率", "HDR", "智能电视"],"description": "通过这款令人惊叹的8K电视体验电视的未来。","price": 2999.99},"SoundMax音响": {"name": "SoundMax Soundbar","category": "电视和家庭影院系统","brand": "SoundMax","model_number": "SM-SB50","warranty": "1年","rating": 4.3,"features": ["2.1声道", "300W输出", "无线低音炮", "蓝牙"],"description": "通过这款时尚而强大的声音条升级您的电视音效。","price": 199.99},"CineView OLED电视": {"name": "CineView OLED TV","category": "电视和家庭影院系统","brand": "CineView","model_number": "CV-OLED55","warranty": "2年","rating": 4.7,"features": ["55英寸显示屏", "4K分辨率", "HDR", "智能电视"],"description": "通过这款OLED电视体验真正的纯黑和鲜艳色彩。","price": 1499.99},"GameSphere X": {"name": "GameSphere X","category": "游戏主机和配件","brand": "GameSphere","model_number": "GS-X","warranty": "1年","rating": 4.9,"features": ["4K游戏", "1TB存储空间", "向后兼容", "在线多人游戏"],"description": "下一代游戏主机,带来极致的游戏体验。","price": 499.99},"ProGamer手柄": {"name": "ProGamer Controller","category": "游戏主机和配件","brand": "ProGamer","model_number": "PG-C100","warranty": "1年","rating": 4.2,"features": ["符合人体工学设计", "可自定义按钮", "无线", "可充电电池"],"description": "高质量的游戏手柄,提供精准和舒适的操控。","price": 59.99},"GameSphere Y": {"name": "GameSphere Y","category": "游戏主机和配件","brand": "GameSphere","model_number": "GS-Y","warranty": "1年","rating": 4.8,"features": ["4K游戏", "500GB存储空间", "向后兼容", "在线多人游戏"],"description": "一款体积紧凑、性能强大的游戏主机。","price": 399.99},"ProGamer方向盘": {"name": "ProGamer Racing Wheel","category": "游戏主机和配件","brand": "ProGamer","model_number": "PG-RW200","warranty": "1年","rating": 4.5,"features": ["力反馈", "可调节踏板", "桨片换挡器", "适用于GameSphere X"],"description": "通过这款逼真的赛车方向盘提升您的赛车游戏体验。","price": 249.99},"GameSphere VR头盔": {"name": "GameSphere VR Headset","category": "游戏主机和配件","brand": "GameSphere","model_number": "GS-VR","warranty": "1年","rating": 4.6,"features": ["沉浸式虚拟现实体验", "内置耳机", "可调节头带", "适用于GameSphere X"],"description": "通过这款舒适的VR头盔进入虚拟现实世界。","price": 299.99},"AudioPhonic降噪耳机": {"name": "AudioPhonic Noise-Canceling Headphones","category": "音频设备","brand": "AudioPhonic","model_number": "AP-NC100","warranty": "1年","rating": 4.6,"features": ["主动降噪", "蓝牙", "20小时电池续航", "舒适佩戴"],"description": "通过这款降噪耳机享受沉浸式音效。","price": 199.99},"WaveSound蓝牙音响": {"name": "WaveSound Bluetooth Speaker","category": "音频设备","brand": "WaveSound","model_number": "WS-BS50","warranty": "1年","rating": 4.5,"features": ["便携式", "10小时电池续航", "防水", "内置麦克风"],"description": "一款便携多功能的蓝牙音箱,随时随地享受音乐。","price": 49.99},"AudioPhonic真无线耳机": {"name": "AudioPhonic True Wireless Earbuds","category": "音频设备","brand": "AudioPhonic","model_number": "AP-TW20","warranty": "1年","rating": 4.4,"features": ["真无线", "蓝牙5.0", "触控操作", "18小时电池续航"],"description": "使用这款舒适的真无线耳机享受无线音乐。","price": 79.99},"WaveSound音响": {"name": "WaveSound Soundbar","category": "音频设备","brand": "WaveSound","model_number": "WS-SB40","warranty": "1年","rating": 4.3,"features": ["2.0声道", "80W输出", "蓝牙", "可壁挂"],"description": "使用这款纤薄而强大的声音条升级您的电视音效。","price": 99.99},"AudioPhonic唱片机": {"name": "AudioPhonic Turntable","category": "音频设备","brand": "AudioPhonic","model_number": "AP-TT10","warranty": "1年","rating": 4.2,"features": ["3种速度", "内置扬声器", "蓝牙", "USB录音"],"description": "通过这款现代化的唱片播放机重新发现您的黑胶唱片收藏。","price": 149.99},"FotoSnap单反相机": {"name": "FotoSnap DSLR Camera","category": "相机和摄像机","brand": "FotoSnap","model_number": "FS-DSLR200","warranty": "1年","rating": 4.7,"features": ["2420万像素传感器", "1080p视频", "3英寸液晶屏", "可换镜头"],"description": "使用这款多功能单反相机捕捉令人惊叹的照片和视频。","price": 599.99},"ActionCam 4K": {"name": "ActionCam 4K","category": "相机和摄像机","brand": "ActionCam","model_number": "AC-4K","warranty": "1年","rating": 4.4,"features": ["4K视频", "防水", "图像稳定", "Wi-Fi"],"description": "使用这款坚固而紧凑的4K运动相机记录您的冒险之旅。","price": 299.99},"FotoSnap 无反相机": {"name": "FotoSnap Mirrorless Camera","category": "相机和摄像机","brand": "FotoSnap","model_number": "FS-ML100","warranty": "1年","rating": 4.6,"features": ["2010万像素传感器", "4K视频", "3英寸触摸屏", "可换镜头"],"description": "一款小巧轻便的无反相机,具备先进的功能。","price": 799.99},"ZoomMaster 摄像机": {"name": "ZoomMaster Camcorder","category": "相机和摄像机","brand": "ZoomMaster","model_number": "ZM-CM50","warranty": "1年","rating": 4.3,"features": ["1080p视频", "30倍光学变焦", "3英寸液晶屏", "图像稳定"],"description": "使用这款易于使用的摄像机捕捉生活中的瞬间。","price": 249.99},"FotoSnap 即时相机": {"name": "FotoSnap Instant Camera","category": "相机和摄像机","brand": "FotoSnap","model_number": "FS-IC10","warranty": "1年","rating": 4.1,"features": ["即时打印", "内置闪光灯", "自拍镜", "电池供电"],"description": "使用这款有趣便携的即时相机创造即时的回忆。","price": 69.99}
}def get_product_by_name(name):return products.get(name, None)def get_products_by_category(category):return [product for product in products.values() if product["category"] == category]import json def read_string_to_list(input_string):if input_string is None:return Nonetry:input_string = input_string.replace("'", "\"") # Replace single quotes with double quotes for valid JSONdata = json.loads(input_string)return dataexcept json.JSONDecodeError:print("Error: Invalid JSON string")return None
尝试查找目标产品"TechPro超级本":
print(get_product_by_name("TechPro超级本"))
返回的TechPro超级本
详情如下:
{'name': 'TechPro 超级本', 'category': '计算机和笔记本电脑', 'brand': 'TechPro', 'model_number': 'TP-UB100', 'warranty': '1年', 'rating': 4.5, 'features': ['13.3英寸显示屏', '8GB RAM', '256GB SSD', '英特尔酷睿i5处理器'], 'description': '一款时尚轻薄的超级本,适用于日常使用。', 'price': 799.99}
尝试查看目标类型计算机和笔记本电脑
的产品:
print(get_products_by_category("计算机和笔记本电脑"))
返回类型为计算机和笔记本电脑
的全部产品:
[{'name': 'TechPro 超级本', 'category': '计算机和笔记本电脑', 'brand': 'TechPro', 'model_number': 'TP-UB100', 'warranty': '1年', 'rating': 4.5, 'features': ['13.3英寸显示屏', '8GB RAM', '256GB SSD', '英特尔酷睿i5处理器'], 'description': '一款时尚轻薄的超级本,适用于日常使用。', 'price': 799.99}, {'name': 'BlueWave 游戏笔记本电脑', 'category': '计算机和笔记本电脑', 'brand': 'BlueWave', 'model_number': 'BW-GL200', 'warranty': '2年', 'rating': 4.7, 'features': ['15.6英寸显示屏', '16GB RAM', '512GB SSD', 'NVIDIA GeForce RTX 3060'], 'description': '一款高性能游戏笔记本电脑,提供沉浸式体验。', 'price': 1199.99}, {'name': 'PowerLite 可转换本', 'category': '计算机和笔记本电脑', 'brand': 'PowerLite', 'model_number': 'PL-CV300', 'warranty': '1年', 'rating': 4.3, 'features': ['14英寸触摸屏', '8GB RAM', '256GB SSD', '360度铰链'], 'description': '一款多功能可转换本,配备响应灵敏的触摸屏。', 'price': 699.99}, {'name': 'TechPro 台式机', 'category': '计算机和笔记本电脑', 'brand': 'TechPro', 'model_number': 'TP-DT500', 'warranty': '1年', 'rating': 4.4, 'features': ['英特尔酷睿i7处理器', '16GB RAM', '1TB HDD', 'NVIDIA GeForce GTX 1660'], 'description': '一款功能强大的台式电脑,适用于工作和娱乐。', 'price': 999.99}, {'name': 'BlueWave Chromebook', 'category': '计算机和笔记本电脑', 'brand': 'BlueWave', 'model_number': 'BW-CB100', 'warranty': '1年', 'rating': 4.1, 'features': ['11.6英寸显示屏', '4GB RAM', '32GB eMMC', 'Chrome OS'], 'description': '一款紧凑实惠的 Chromebook,适用于日常任务。', 'price': 249.99}]
尝试将前面的category_and_product_response_1
转为dict:
category_and_product_list = read_string_to_list(category_and_product_response_1)
print(category_and_product_list)
结果如下:
[{'category': '智能手机和配件'}, {'products': ['SmartX ProPhone']}, {'category': '相机和摄像机'}, {'products': ['FotoSnap单反相机']}, {'category': '电视和家庭影院系统'}, {'products': ['CineView 4K电视', 'CineView 8K电视', 'CineView OLED电视']}]
下面尝试获取category_and_product_list
对应的产品详情:
def generate_output_string_cn(data_list):output_string = ""if data_list is None:return output_stringfor data in data_list:try:if "products" in data:products_list = data["products"]for product_name in products_list:product = get_product_by_name(product_name)if product:output_string += json.dumps(product, ensure_ascii=False, indent=4) + "\n"else:print(f"Error: Product '{product_name}' not found")elif "category" in data:category_name = data["category"]category_products = get_products_by_category(category_name)for product in category_products:output_string += json.dumps(product, ensure_ascii=False, indent=4) + "\n"else:print("Error: Invalid object format")except Exception as e:print(f"Error: {e}")return output_string
具体如下:
product_information_for_user_message_1 = generate_output_string_cn(category_and_product_list)
print(product_information_for_user_message_1)
输出结果如下:
{"name": "SmartX ProPhone","category": "智能手机和配件","brand": "SmartX","model_number": "SX-PP10","warranty": "1年","rating": 4.6,"features": ["6.1英寸显示屏","128GB存储空间","12MP双摄像头","5G"],"description": "一款功能强大的智能手机,具有先进的摄像功能。","price": 899.99
}
{"name": "MobiTech PowerCase","category": "智能手机和配件","brand": "MobiTech","model_number": "MT-PC20","warranty": "1年","rating": 4.3,"features": ["5000mAh电池","无线充电","适用于SmartX ProPhone"],"description": "一款带有内置电池的保护壳,可延长使用时间。","price": 59.99
}
{"name": "SmartX MiniPhone","category": "智能手机和配件","brand": "SmartX","model_number": "SX-MP5","warranty": "1年","rating": 4.2,"features": ["4.7英寸显示屏","64GB存储空间","8MP摄像头","4G"],"description": "一款小巧实惠的智能手机,适合基本任务。","price": 399.99
}
{"name": "MobiTech Wireless Charger","category": "智能手机和配件","brand": "MobiTech","model_number": "MT-WC10","warranty": "1年","rating": 4.5,"features": ["10W快速充电","Qi兼容","LED指示灯","紧凑设计"],"description": "一款方便的无线充电器,适用于整洁的工作空间。","price": 29.99
}
{"name": "SmartX EarBuds","category": "智能手机和配件","brand": "SmartX","model_number": "SX-EB20","warranty": "1年","rating": 4.4,"features": ["真无线","蓝牙5.0","触摸控制","24小时电池续航"],"description": "通过这款舒适的耳塞体验真正的无线自由。","price": 99.99
}
{"name": "SmartX ProPhone","category": "智能手机和配件","brand": "SmartX","model_number": "SX-PP10","warranty": "1年","rating": 4.6,"features": ["6.1英寸显示屏","128GB存储空间","12MP双摄像头","5G"],"description": "一款功能强大的智能手机,具有先进的摄像功能。","price": 899.99
}
{"name": "FotoSnap DSLR Camera","category": "相机和摄像机","brand": "FotoSnap","model_number": "FS-DSLR200","warranty": "1年","rating": 4.7,"features": ["2420万像素传感器","1080p视频","3英寸液晶屏","可换镜头"],"description": "使用这款多功能单反相机捕捉令人惊叹的照片和视频。","price": 599.99
}
{"name": "ActionCam 4K","category": "相机和摄像机","brand": "ActionCam","model_number": "AC-4K","warranty": "1年","rating": 4.4,"features": ["4K视频","防水","图像稳定","Wi-Fi"],"description": "使用这款坚固而紧凑的4K运动相机记录您的冒险之旅。","price": 299.99
}
{"name": "FotoSnap Mirrorless Camera","category": "相机和摄像机","brand": "FotoSnap","model_number": "FS-ML100","warranty": "1年","rating": 4.6,"features": ["2010万像素传感器","4K视频","3英寸触摸屏","可换镜头"],"description": "一款小巧轻便的无反相机,具备先进的功能。","price": 799.99
}
{"name": "ZoomMaster Camcorder","category": "相机和摄像机","brand": "ZoomMaster","model_number": "ZM-CM50","warranty": "1年","rating": 4.3,"features": ["1080p视频","30倍光学变焦","3英寸液晶屏","图像稳定"],"description": "使用这款易于使用的摄像机捕捉生活中的瞬间。","price": 249.99
}
{"name": "FotoSnap Instant Camera","category": "相机和摄像机","brand": "FotoSnap","model_number": "FS-IC10","warranty": "1年","rating": 4.1,"features": ["即时打印","内置闪光灯","自拍镜","电池供电"],"description": "使用这款有趣便携的即时相机创造即时的回忆。","price": 69.99
}
{"name": "FotoSnap DSLR Camera","category": "相机和摄像机","brand": "FotoSnap","model_number": "FS-DSLR200","warranty": "1年","rating": 4.7,"features": ["2420万像素传感器","1080p视频","3英寸液晶屏","可换镜头"],"description": "使用这款多功能单反相机捕捉令人惊叹的照片和视频。","price": 599.99
}
{"name": "CineView 4K TV","category": "电视和家庭影院系统","brand": "CineView","model_number": "CV-4K55","warranty": "2年","rating": 4.8,"features": ["55英寸显示屏","4K分辨率","HDR","智能电视"],"description": "一款具有鲜艳色彩和智能功能的惊艳4K电视。","price": 599.99
}
{"name": "SoundMax Home Theater","category": "电视和家庭影院系统","brand": "SoundMax","model_number": "SM-HT100","warranty": "1年","rating": 4.4,"features": ["5.1声道","1000W输出","无线低音炮","蓝牙"],"description": "一套功能强大的家庭影院系统,提供沉浸式的音频体验。","price": 399.99
}
{"name": "CineView 8K TV","category": "电视和家庭影院系统","brand": "CineView","model_number": "CV-8K65","warranty": "2年","rating": 4.9,"features": ["65英寸显示屏","8K分辨率","HDR","智能电视"],"description": "通过这款令人惊叹的8K电视体验电视的未来。","price": 2999.99
}
{"name": "SoundMax Soundbar","category": "电视和家庭影院系统","brand": "SoundMax","model_number": "SM-SB50","warranty": "1年","rating": 4.3,"features": ["2.1声道","300W输出","无线低音炮","蓝牙"],"description": "通过这款时尚而强大的声音条升级您的电视音效。","price": 199.99
}
{"name": "CineView OLED TV","category": "电视和家庭影院系统","brand": "CineView","model_number": "CV-OLED55","warranty": "2年","rating": 4.7,"features": ["55英寸显示屏","4K分辨率","HDR","智能电视"],"description": "通过这款OLED电视体验真正的纯黑和鲜艳色彩。","price": 1499.99
}
{"name": "CineView 4K TV","category": "电视和家庭影院系统","brand": "CineView","model_number": "CV-4K55","warranty": "2年","rating": 4.8,"features": ["55英寸显示屏","4K分辨率","HDR","智能电视"],"description": "一款具有鲜艳色彩和智能功能的惊艳4K电视。","price": 599.99
}
{"name": "CineView 8K TV","category": "电视和家庭影院系统","brand": "CineView","model_number": "CV-8K65","warranty": "2年","rating": 4.9,"features": ["65英寸显示屏","8K分辨率","HDR","智能电视"],"description": "通过这款令人惊叹的8K电视体验电视的未来。","price": 2999.99
}
{"name": "CineView OLED TV","category": "电视和家庭影院系统","brand": "CineView","model_number": "CV-OLED55","warranty": "2年","rating": 4.7,"features": ["55英寸显示屏","4K分辨率","HDR","智能电视"],"description": "通过这款OLED电视体验真正的纯黑和鲜艳色彩。","price": 1499.99
}
基于产品详情进行答复
如果想要智能客服能够在知晓用户咨询的具体产品,那么可以针对目标产品的详情给出进一步的介绍。
system_message = f"""
你是一名大型电子商店的客户服务助理,\
你将以友善和乐于助人的语气回答客户的问题,并提供简明扼要的答案。\
请确保向用户提出相关的后续问题。
"""user_message_1 = f"""
介绍下 smartx ProPhone 和 \
fotosnap单反相机。 \
此外,还有你们的tvs"""# 试试下面的
# user_message_1 = f"""
# 介绍下 smartx pro phone 和 \
# fotosnap单反相机。 \
# 此外,还有你们的tvs"""messages = [
{'role':'system','content': system_message},
{'role':'user','content': user_message_1},
{'role':'assistant','content': f"""相关产品信息如下:\n\{product_information_for_user_message_1}"""},
]
final_response = get_completion_from_messages(messages)
print(final_response)
输出结果如下:
我们有SmartX ProPhone智能手机和FotoSnap单反相机。SmartX ProPhone具有6.1英寸显示屏,128GB存储空间,12MP双摄像头和5G网络。FotoSnap单反相机具有2420万像素传感器,1080p视频,3英寸液晶屏和可换镜头。此外,我们还有CineView 4K电视,CineView 8K电视和CineView OLED电视,以及SoundMax家庭影院系统和SoundMax声音条。您需要了解更多关于这些产品的信息吗?
示例5: 输出检查
安全检查
在构建一个完整应用服务的时候不仅需要对用户输入进行安全审核,还需要对模型生成的结果进行内容审核:
final_response_to_customer = f"""
SmartX ProPhone具有6.1英寸的显示屏,128GB存储空间,\
12MP双摄像头和5G功能。FotoSnap DSLR相机具有24.2MP传感器,\
1080p视频,3英寸液晶屏和可更换镜头。我们还有各种电视,\
包括CineView 4K电视,拥有55英寸显示屏、4K分辨率、HDR和智能电视功能。\
我们还有SoundMax家庭影院音响系统,配备5.1声道,\
1000W输出功率,无线低音炮和蓝牙功能。\
您对这些产品或我们提供的其他产品有任何具体问题吗?
"""
response = openai.Moderation.create(input=final_response_to_customer
)
moderation_output = response["results"][0]
print(moderation_output)
输出结果如下:
{"categories": {"hate": false,"hate/threatening": false,"self-harm": false,"sexual": false,"sexual/minors": false,"violence": false,"violence/graphic": false},"category_scores": {"hate": 3.8089063e-06,"hate/threatening": 1.9733807e-08,"self-harm": 4.7657116e-07,"sexual": 7.7758545e-05,"sexual/minors": 2.952843e-07,"violence": 5.5471032e-06,"violence/graphic": 1.5545219e-06},"flagged": false
}
可以看出各个类别都是false
,flagged
结果也为false
。
预期检查
回复符合预期的情况:
system_message = f"""
你是一个助手,负责评估客服代理的回复是否足够回答客户问题,\
并验证助手引用的产品信息是否准确无误。\
产品信息、用户和客服代理的消息将由3个反引号```括起来。
请以单个字母形式(不带标点)回答:
Y - 如果输出足够回答问题,并且正确使用了产品信息
N - 否则只输出一个字母。
"""
customer_message = f"""
介绍下 smartx pro phone 和 \
fotosnap单反相机。\
此外,也讲讲你们的tvs"""product_information = """
{"name": "SmartX ProPhone","category": "智能手机和配件","brand": "SmartX","model_number": "SX-PP10","warranty": "1年","rating": 4.6,"features": ["6.1英寸显示屏","128GB存储空间","12MP双摄像头","5G"],"description": "一款功能强大、具有先进相机功能的智能手机。","price": 899.99}{"name": "FotoSnap DSLR相机","category": "相机和摄像机","brand": "FotoSnap","model_number": "FS-DSLR200","warranty": "1年","rating": 4.7,"features": ["24.2MP传感器","1080p视频","3英寸液晶屏","可更换镜头"],"description": "通过这款多功能的单反相机捕捉令人惊叹的照片和视频。","price": 599.99}{"name": "CineView 4K电视","category": "电视和家庭影院系统","brand": "CineView","model_number": "CV-4K55","warranty": "2年","rating": 4.8,"features": ["55英寸显示屏","4K分辨率","HDR","智能电视"],"description": "一款拥有鲜艳色彩和智能功能的惊艳4K电视。","price": 599.99}{"name": "SoundMax家庭影院","category": "电视和家庭影院系统","brand": "SoundMax","model_number": "SM-HT100","warranty": "1年","rating": 4.4,"features": ["5.1声道","1000W输出","无线低音炮","蓝牙"],"description": "一款强大的家庭影院系统,带来沉浸式音频体验。","price": 399.99}{"name": "CineView 8K电视","category": "电视和家庭影院系统","brand": "CineView","model_number": "CV-8K65","warranty": "2年","rating": 4.9,"features": ["65英寸显示屏","8K分辨率","HDR","智能电视"],"description": "通过这款惊人的8K电视体验电视的未来。","price": 2999.99}{"name": "SoundMax音响","category": "电视和家庭影院系统","brand": "SoundMax","model_number": "SM-SB50","warranty": "1年","rating": 4.3,"features": ["2.1声道","300W输出","无线低音炮","蓝牙"],"description": "通过这款时尚而强大的音响升级您的电视音频效果。","price": 199.99}{"name": "CineView OLED电视","category": "电视和家庭影院系统","brand": "CineView","model_number": "CV-OLED55","warranty": "2年","rating": 4.7,"features": ["55英寸显示屏","4K分辨率","HDR","智能电视"],"description": "通过这款OLED电视体验真正的黑色和鲜艳色彩。","price": 1499.99}
"""q_a_pair = f"""
Customer message: ```{customer_message}```
Product information: ```{product_information}```
Agent response: ```{final_response_to_customer}```助手的回复是否正确?
该回复是否充分?输出 Y 或者 N
"""
messages = [{'role': 'system', 'content': system_message},{'role': 'user', 'content': q_a_pair}
]response = get_completion_from_messages(messages, max_tokens=1)
print(response)
输出结果如下:
Y
回复不符合预期的情况:
another_response = "生活就像一盒巧克力"
q_a_pair = f"""
Customer message: ```{customer_message}```
Product information: ```{product_information}```
Agent response: ```{another_response}```助手的回复是否正确?输出 Y 或者 N
"""
messages = [{'role': 'system', 'content': system_message},{'role': 'user', 'content': q_a_pair}
]response = get_completion_from_messages(messages)
print(response)
输出结果如下:
N
总结
本文以更加复杂的智能客服场景为例,通过构建链接多个Prompt实现复杂的客服问答服务。在实际工作过程,对于复杂任务需要将其拆分为一系列简单的子任务来链接多个Prompt。这种链接多个Prompt的方式与此前介绍的思维链不同。思维链Prompt是使用一个长而复杂的指令,实质上只有一条Prompt,对任务的处理是一次完成。而链接多个Prompt是将一个任务拆解成多个模块,每个子任务单独用一个prompt完成。面对复杂的任务,通过分解成多个子任务,链接多个Prompt的方式,使其更易于管理,并减少错误的可能性。