LLM系列 | 13: 如何链接多个Prompt处理复杂任务?以智能客服为例

简介

溪云初起日沉阁,山雨欲来风满楼。
在这里插入图片描述

小伙伴们好,我是微信公众号《小窗幽记机器学习》的小编:卖老班章的小女孩。紧接前面几篇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
}

可以看出各个类别都是falseflagged结果也为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的方式,使其更易于管理,并减少错误的可能性。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/47289.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

chatgpt赋能python:Python调用蓝牙:从入门到实战

Python调用蓝牙&#xff1a;从入门到实战 如果你想要通过Python调用蓝牙&#xff0c;以此来读取和操作周围的设备&#xff0c;那么你已经来到了正确的地方。在本篇文章中&#xff0c;我们将探讨如何使用Python编程语言来与周围的蓝牙设备进行通信&#xff0c;为你的应用程序增…

chatgpt赋能python:如何使用Python操作蓝牙——一篇详细的教程

如何使用Python操作蓝牙——一篇详细的教程 蓝牙技术已经成为了我们生活中不可或缺的一部分&#xff0c;它可用于连接各种设备&#xff0c;例如耳机、音箱、手表&#xff0c;甚至是汽车。而Python作为一种广泛应用于各种领域的高级编程语言&#xff0c;也可以用来操作蓝牙设备…

做什么生意是最稳定的呢?

只要做生意就会有风险&#xff0c;钱存在银行最没有风险。 风险的大小取决于你承受能力&#xff0c;如果你只有几万元&#xff0c;那么摆地摊进货对于你来说都是大风险。如果你有几千万上亿&#xff0c;炒个十万的期货都对你来说无关紧要。 房产投资风险最小 投资也可以算是…

教你怎么鉴别发霉大米?

珍宝岛大米是黑龙江省鸡西虎林市的名优土特产&#xff0c;是鸡西市的三大地理标志保护产品之一。黑龙江省虎林市的虎林镇、杨岗镇、虎头镇、迎春镇、宝东镇、东方红镇、伟光乡、新乐乡、忠诚乡、阿北乡、珍宝岛乡等12个乡镇是经过我国农业部门实地考察过亲自验证的名优产地&…

设计模式——命令模式

在软件开发系统中&#xff0c;“方法的请求者”与“方法的实现者”之间经常存在紧密的耦合关系&#xff0c;这不利于软件功能的扩展与维护。例如&#xff0c;想对方法进行“撤销、重做、记录”等处理都很不方便&#xff0c;因此“如何将方法的请求者与实现者解耦&#xff1f;”…

java毕业设计——基于java+JavaBean+jsp的网上零食销售系统设计与实现(毕业论文+程序源码)——网上零食销售系统

基于javaJavaBeanjsp的网上零食销售系统设计与实现&#xff08;毕业论文程序源码&#xff09; 大家好&#xff0c;今天给大家介绍基于javaJavaBeanjsp的网上零食销售系统设计与实现&#xff0c;文章末尾附有本毕业设计的论文和源码下载地址哦。需要下载开题报告PPT模板及论文答…

2021年中国电饭锅(电饭煲)行业现状及前景分析:个性化消费推动市场向高端、智能化发展[图]

一、电饭锅&#xff08;电饭煲&#xff09;产业链 电饭煲又称作电锅&#xff0c;电饭锅。是利用转变为热能的&#xff0c;具有对进行、、炖、煲、煨等多种操作功能&#xff0c;使用方便、安全可靠。它不但能够把食物做熟&#xff0c;而且能够保温&#xff0c;使用起来清洁卫生&…

设计模式 -- 命令模式(14)

目录 前言定义优点与缺点优点缺点 结构与实现实现需求代码 前言 在现实生活中&#xff0c;命令模式的例子也很多。比如看电视时&#xff0c;我们只需要轻轻一按遥控器就能完成频道的切换&#xff0c;这就是命令模式&#xff0c;将换台请求和换台处理完全解耦了。电视机遥控器&…

我所理解的生活!

写于:2013-7 无码的睁大眼睛看&#xff0c;有码的眯起眼睛看&#xff0c;没有情节的直接删除 ----------------------这就是我所理解的生活&#xff01; 我也许能够一直打着寻找自我的幌子&#xff0c;继续在这个社会招摇撞骗。凭良心说我喜欢千奇百怪的结果&#xff0c;于是…

我用Python爬取美食网站3032个菜谱并分析,没有我不会做的菜!

这是巨变的中国&#xff0c;人和食物&#xff0c;比任何时候走的更快。近日&#xff0c;J哥为了寻味中国&#xff0c;奔走于某五线城市的大街小巷&#xff0c;结果除了累&#xff0c;啥也没寻到。 于是&#xff0c;J哥默默打开了各大美食网站&#xff0c;如豆果美食、下厨房、…

一年时间,拿到了人生中的第一个20万

目录 一、2021年度博客之星评选第二名二、博客新星导师三、哪吒社区四、粉丝群五、付费专栏六、Java学习路线总结&#xff0c;搬砖工逆袭Java架构师七、关于读书八、你好2023 一、2021年度博客之星评选第二名 2022年&#xff0c;是哪吒收获的一年&#xff0c;收获了人生中的第…

深圳潮汕肠粉店

转载于:https://www.cnblogs.com/wujiafeng/p/3416493.html

做个合格的吃货~利用Python爬取美食网站3032个菜谱并分析

导语&#xff1a; 这两天气温急剧下降&#xff0c;天冷的小编实在是不想出门&#xff0c;到了饭点~诶&#xff01;兴冲冲点开某团美滋滋点了个外卖!然后就是等了快两小时&#xff08;天气恶劣铁汁萌也要理解下外卖员下雪之后路面也滑咱也不要催单&#xff09;终于等到外卖结果…

作为潮汕人你了解肠粉么 附带家常肠粉做法

随着时代的变化年轻人们走出家乡在外奔波&#xff0c;但却忘不了家乡的味道&#xff0c;思思念念让我们来说一说家乡“惠来”的那些味道吧&#xff01; 肠 粉 关于肠粉的起源&#xff0c; 众说纷纭&#xff0c;没有统一的说法&#xff0c;有人说“最早是抗日战争时期由泮塘荷仙…

Alien Skin X7PS调色滤镜插件下载及PS调色滤镜教程

Alien Skin Exposure X7 PS是一款专业的图像编辑和后期处理软件&#xff0c;功能非常强大&#xff0c;但对新手不是很友好&#xff0c;比较难上手。今天就为大家介绍一款实用的调色滤镜插件&#xff0c;上手方便&#xff0c;操作简单&#xff0c;处理后的照片质量也很高&#x…

PS2023磨皮滤镜插件Portraiture人像美容

在PS后期修图中&#xff0c;有一个非常关键的步骤&#xff0c;那就是磨皮。磨皮的方法有很多种&#xff0c;但是其中最方便快捷的达到大片级效果的方法莫过于使用插件。 但是目前市面上PS磨皮插件众多&#xff0c;该怎么选择呢&#xff1f;经过多次磨皮效果对比&#xff0c;小…

不会PS图片怎么批量调色

现在很多实体制造业都转向线上&#xff0c;转向线上的第一步就是有自己的线上展示平台和微商城。小企业不能在一开始就能搭建好团队&#xff0c;那么非专业人士进行这种线下转线上&#xff0c;会有很多操作上问题。其中有个问题就是&#xff0c;不会PS图片怎么批量调色&#xf…

PS滤镜怎么调色,可以用ps滤镜插件调色

**品牌型号&#xff1a;**联想 E431 **系统&#xff1a;**Windows 10 19042 64位家庭版 **软件版本&#xff1a;**Alien Skin Exposure X7 在使用PS修图时&#xff0c;很多时候都会给照片添加滤镜。下面我就为大家介绍PS滤镜调色的方法和位置。 一、PS滤镜怎么调色 首先下…

JixiPix Hand Tint Pro for Mac(图片调色修图软件)

JixiPix Hand Tint是一款功能强大的图像处理软件&#xff0c;可以为用户带来丰富的图像效果和过滤器等服务&#xff0c;同时降低了图片处理的效率&#xff0c;没有PS操作复杂&#xff0c;小白新手也可以流畅使用。Hand Tint Pro还内置了丰富的特效和滤镜&#xff0c;只需要导入…

PS调色精灵ps插件

链接&#xff1a;https://pan.baidu.com/s/1fxoQSp2hDqwf5HXfhSXntA 提取码&#xff1a;ot42