跳到主要内容
基于 ChatGPT 构建电商客服问答系统 | 极客日志
Python AI 算法
基于 ChatGPT 构建电商客服问答系统 综述由AI生成 基于 ChatGPT 构建电商客服问答系统的完整流程。内容包括大语言模型基础知识、Tokens 概念、链式处理方法,以及使用 Python 和 OpenAI API 实现的具体代码。通过封装聊天函数、文件处理函数和链式提问函数,实现了用户输入审核、商品信息检索、答案生成及合规性检查。最后利用 Panel 库搭建了可视化交互界面,展示了从数据准备到系统部署的全链路开发实践。
星星泡饭 发布于 2025/2/6 更新于 2026/6/12 25 浏览以电商客服助手为例,通过链式调用语言模型,结合多个 Prompt 实现复杂的问答与推理功能。
一、基础知识了解
1.1 语言模型
大语言模型(LLM)是通过预测下一个词的监督学习方式进行训练的。具体来说,首先准备一个包含数百亿甚至更多词的大规模文本数据集。然后,可以从这些文本中提取句子或句子片段作为模型输入。模型会根据当前输入 Context 预测下一个词的概率分布。通过不断比较模型预测和实际的下一个词,并更新模型参数最小化两者差异,语言模型逐步掌握了语言的规律,学会了预测下一个词。就这样不断训练,使得模型参数收敛,预测能力提高。
可以分为基础语言模型 和指令调优语言模型 。基础语言模型没有明确的目标导向,对于具体的问题会给出开放性的回答;而指令微调的语言模型则进行了专门的训练,更加适合任务导向的对话应用。
1.2 Tokens
在 LLM 中,模型处理的都是 token。例如在处理一个句子的时侯,会先使用分词器将其拆分成一个个 token,而不是原始的单词。
例如,对于'Learning new things is fun!'这句话,每个单词都被转换为一个 token,而对于较少使用的单词,如'Prompting as powerful developer tool',单词'prompting'会被拆分为三个 token,即"prom"、"pt"和"ing"。
对于英文输入,一个 token 一般对应 4 个字符或者四分之三个单词;对于中文输入,一个 token 一般对应一个或半个词。
模型是有着 token 限制的,指的是输入的 prompt 和输出的 completion 的 token 之和。
1.3 提问方式
如下 messages 是一种和模型交互的信息格式:
要说明"role"和"content"两部分,role 代表传递的内容代表的身份,可能是系统消息 system、用户消息 user;content 就是代表传递的具体内容。
1.4 链式处理
让模型处理的问题虽然看上去只有一句话,但实际可能非常复杂,如'请给我介绍一下 iphone 15 的相关信息',对于模型来说,它理解并处理这个问题的步骤是:
先理解要查询的内容
然后去检索详细的信息
在生成查询的答案
其实在设计的时候,可以把程序设计成多个 Prompt。这样的好处有分解了复杂度,拆分 prompt 来降低计算成本,更容易测试和调试等。
二、准备
2.1 环境准备
安装 Anaconda、openai,在 OpenAI 官网注册并获取 API Key。
由于我的 API 额度已经过期,所以我用的第三方 API,和官网的使用只有 BASE_URL 的区别,其他都一样。
2.2 思路规划
完成一个系统的搭建的思路如下:
如果要加上可视化的页面,就加上交互操作即可。
2.3 代码准备
2.3.1 产品信息文件
在这里我主要是用的他们课程里面的产品信息,具体如下,其中包含产品名称、分类等字段,保存为 products.json 文件:
{
"TechPro Ultrabook" : {
"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
}
,
"BlueWave Gaming Laptop"
:
{
"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
}
,
"PowerLite Convertible"
:
{
"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
}
,
"TechPro Desktop"
:
{
"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
}
,
"BlueWave Chromebook"
:
{
"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
}
,
"SmartX ProPhone"
:
{
"name"
:
"SmartX ProPhone"
,
"category"
:
"Smartphones and Accessories"
,
"brand"
:
"SmartX"
,
"model_number"
:
"SX-PP10"
,
"warranty"
:
"1 year"
,
"rating"
:
4.6
,
"features"
:
[
"6.1-inch display"
,
"128GB storage"
,
"12MP dual camera"
,
"5G"
]
,
"description"
:
"A powerful smartphone with advanced camera features."
,
"price"
:
899.99
}
,
"MobiTech PowerCase"
:
{
"name"
:
"MobiTech PowerCase"
,
"category"
:
"Smartphones and Accessories"
,
"brand"
:
"MobiTech"
,
"model_number"
:
"MT-PC20"
,
"warranty"
:
"1 year"
,
"rating"
:
4.3
,
"features"
:
[
"5000mAh battery"
,
"Wireless charging"
,
"Compatible with SmartX ProPhone"
]
,
"description"
:
"A protective case with built-in battery for extended usage."
,
"price"
:
59.99
}
,
"SmartX MiniPhone"
:
{
"name"
:
"SmartX MiniPhone"
,
"category"
:
"Smartphones and Accessories"
,
"brand"
:
"SmartX"
,
"model_number"
:
"SX-MP5"
,
"warranty"
:
"1 year"
,
"rating"
:
4.2
,
"features"
:
[
"4.7-inch display"
,
"64GB storage"
,
"8MP camera"
,
"4G"
]
,
"description"
:
"A compact and affordable smartphone for basic tasks."
,
"price"
:
399.99
}
,
"MobiTech Wireless Charger"
:
{
"name"
:
"MobiTech Wireless Charger"
,
"category"
:
"Smartphones and Accessories"
,
"brand"
:
"MobiTech"
,
"model_number"
:
"MT-WC10"
,
"warranty"
:
"1 year"
,
"rating"
:
4.5
,
"features"
:
[
"10W fast charging"
,
"Qi-compatible"
,
"LED indicator"
,
"Compact design"
]
,
"description"
:
"A convenient wireless charger for a clutter-free workspace."
,
"price"
:
29.99
}
,
"SmartX EarBuds"
:
{
"name"
:
"SmartX EarBuds"
,
"category"
:
"Smartphones and Accessories"
,
"brand"
:
"SmartX"
,
"model_number"
:
"SX-EB20"
,
"warranty"
:
"1 year"
,
"rating"
:
4.4
,
"features"
:
[
"True wireless"
,
"Bluetooth 5.0"
,
"Touch controls"
,
"24-hour battery life"
]
,
"description"
:
"Experience true wireless freedom with these comfortable earbuds."
,
"price"
:
99.99
}
,
"CineView 4K TV"
:
{
"name"
:
"CineView 4K TV"
,
"category"
:
"Televisions and Home Theater Systems"
,
"brand"
:
"CineView"
,
"model_number"
:
"CV-4K55"
,
"warranty"
:
"2 years"
,
"rating"
:
4.8
,
"features"
:
[
"55-inch display"
,
"4K resolution"
,
"HDR"
,
"Smart TV"
]
,
"description"
:
"A stunning 4K TV with vibrant colors and smart features."
,
"price"
:
599.99
}
,
"SoundMax Home Theater"
:
{
"name"
:
"SoundMax Home Theater"
,
"category"
:
"Televisions and Home Theater Systems"
,
"brand"
:
"SoundMax"
,
"model_number"
:
"SM-HT100"
,
"warranty"
:
"1 year"
,
"rating"
:
4.4
,
"features"
:
[
"5.1 channel"
,
"1000W output"
,
"Wireless subwoofer"
,
"Bluetooth"
]
,
"description"
:
"A powerful home theater system for an immersive audio experience."
,
"price"
:
399.99
}
,
"CineView 8K TV"
:
{
"name"
:
"CineView 8K TV"
,
"category"
:
"Televisions and Home Theater Systems"
,
"brand"
:
"CineView"
,
"model_number"
:
"CV-8K65"
,
"warranty"
:
"2 years"
,
"rating"
:
4.9
,
"features"
:
[
"65-inch display"
,
"8K resolution"
,
"HDR"
,
"Smart TV"
]
,
"description"
:
"Experience the future of television with this stunning 8K TV."
,
"price"
:
2999.99
}
,
"SoundMax Soundbar"
:
{
"name"
:
"SoundMax Soundbar"
,
"category"
:
"Televisions and Home Theater Systems"
,
"brand"
:
"SoundMax"
,
"model_number"
:
"SM-SB50"
,
"warranty"
:
"1 year"
,
"rating"
:
4.3
,
"features"
:
[
"2.1 channel"
,
"300W output"
,
"Wireless subwoofer"
,
"Bluetooth"
]
,
"description"
:
"Upgrade your TV's audio with this sleek and powerful soundbar."
,
"price"
:
199.99
}
,
"CineView OLED TV"
:
{
"name"
:
"CineView OLED TV"
,
"category"
:
"Televisions and Home Theater Systems"
,
"brand"
:
"CineView"
,
"model_number"
:
"CV-OLED55"
,
"warranty"
:
"2 years"
,
"rating"
:
4.7
,
"features"
:
[
"55-inch display"
,
"4K resolution"
,
"HDR"
,
"Smart TV"
]
,
"description"
:
"Experience true blacks and vibrant colors with this OLED TV."
,
"price"
:
1499.99
}
,
"GameSphere X"
:
{
"name"
:
"GameSphere X"
,
"category"
:
"Gaming Consoles and Accessories"
,
"brand"
:
"GameSphere"
,
"model_number"
:
"GS-X"
,
"warranty"
:
"1 year"
,
"rating"
:
4.9
,
"features"
:
[
"4K gaming"
,
"1TB storage"
,
"Backward compatibility"
,
"Online multiplayer"
]
,
"description"
:
"A next-generation gaming console for the ultimate gaming experience."
,
"price"
:
499.99
}
,
"ProGamer Controller"
:
{
"name"
:
"ProGamer Controller"
,
"category"
:
"Gaming Consoles and Accessories"
,
"brand"
:
"ProGamer"
,
"model_number"
:
"PG-C100"
,
"warranty"
:
"1 year"
,
"rating"
:
4.2
,
"features"
:
[
"Ergonomic design"
,
"Customizable buttons"
,
"Wireless"
,
"Rechargeable battery"
]
,
"description"
:
"A high-quality gaming controller for precision and comfort."
,
"price"
:
59.99
}
,
"GameSphere Y"
:
{
"name"
:
"GameSphere Y"
,
"category"
:
"Gaming Consoles and Accessories"
,
"brand"
:
"GameSphere"
,
"model_number"
:
"GS-Y"
,
"warranty"
:
"1 year"
,
"rating"
:
4.8
,
"features"
:
[
"4K gaming"
,
"500GB storage"
,
"Backward compatibility"
,
"Online multiplayer"
]
,
"description"
:
"A compact gaming console with powerful performance."
,
"price"
:
399.99
}
,
"ProGamer Racing Wheel"
:
{
"name"
:
"ProGamer Racing Wheel"
,
"category"
:
"Gaming Consoles and Accessories"
,
"brand"
:
"ProGamer"
,
"model_number"
:
"PG-RW200"
,
"warranty"
:
"1 year"
,
"rating"
:
4.5
,
"features"
:
[
"Force feedback"
,
"Adjustable pedals"
,
"Paddle shifters"
,
"Compatible with GameSphere X"
]
,
"description"
:
"Enhance your racing games with this realistic racing wheel."
,
"price"
:
249.99
}
,
"GameSphere VR Headset"
:
{
"name"
:
"GameSphere VR Headset"
,
"category"
:
"Gaming Consoles and Accessories"
,
"brand"
:
"GameSphere"
,
"model_number"
:
"GS-VR"
,
"warranty"
:
"1 year"
,
"rating"
:
4.6
,
"features"
:
[
"Immersive VR experience"
,
"Built-in headphones"
,
"Adjustable headband"
,
"Compatible with GameSphere X"
]
,
"description"
:
"Step into the world of virtual reality with this comfortable VR headset."
,
"price"
:
299.99
}
,
"AudioPhonic Noise-Canceling Headphones"
:
{
"name"
:
"AudioPhonic Noise-Canceling Headphones"
,
"category"
:
"Audio Equipment"
,
"brand"
:
"AudioPhonic"
,
"model_number"
:
"AP-NC100"
,
"warranty"
:
"1 year"
,
"rating"
:
4.6
,
"features"
:
[
"Active noise-canceling"
,
"Bluetooth"
,
"20-hour battery life"
,
"Comfortable fit"
]
,
"description"
:
"Experience immersive sound with these noise-canceling headphones."
,
"price"
:
199.99
}
,
"WaveSound Bluetooth Speaker"
:
{
"name"
:
"WaveSound Bluetooth Speaker"
,
"category"
:
"Audio Equipment"
,
"brand"
:
"WaveSound"
,
"model_number"
:
"WS-BS50"
,
"warranty"
:
"1 year"
,
"rating"
:
4.5
,
"features"
:
[
"Portable"
,
"10-hour battery life"
,
"Water-resistant"
,
"Built-in microphone"
]
,
"description"
:
"A compact and versatile Bluetooth speaker for music on the go."
,
"price"
:
49.99
}
,
"AudioPhonic True Wireless Earbuds"
:
{
"name"
:
"AudioPhonic True Wireless Earbuds"
,
"category"
:
"Audio Equipment"
,
"brand"
:
"AudioPhonic"
,
"model_number"
:
"AP-TW20"
,
"warranty"
:
"1 year"
,
"rating"
:
4.4
,
"features"
:
[
"True wireless"
,
"Bluetooth 5.0"
,
"Touch controls"
,
"18-hour battery life"
]
,
"description"
:
"Enjoy music without wires with these comfortable true wireless earbuds."
,
"price"
:
79.99
}
,
"WaveSound Soundbar"
:
{
"name"
:
"WaveSound Soundbar"
,
"category"
:
"Audio Equipment"
,
"brand"
:
"WaveSound"
,
"model_number"
:
"WS-SB40"
,
"warranty"
:
"1 year"
,
"rating"
:
4.3
,
"features"
:
[
"2.0 channel"
,
"80W output"
,
"Bluetooth"
,
"Wall-mountable"
]
,
"description"
:
"Upgrade your TV's audio with this slim and powerful soundbar."
,
"price"
:
99.99
}
,
"AudioPhonic Turntable"
:
{
"name"
:
"AudioPhonic Turntable"
,
"category"
:
"Audio Equipment"
,
"brand"
:
"AudioPhonic"
,
"model_number"
:
"AP-TT10"
,
"warranty"
:
"1 year"
,
"rating"
:
4.2
,
"features"
:
[
"3-speed"
,
"Built-in speakers"
,
"Bluetooth"
,
"USB recording"
]
,
"description"
:
"Rediscover your vinyl collection with this modern turntable."
,
"price"
:
149.99
}
,
"FotoSnap DSLR Camera"
:
{
"name"
:
"FotoSnap DSLR Camera"
,
"category"
:
"Cameras and Camcorders"
,
"brand"
:
"FotoSnap"
,
"model_number"
:
"FS-DSLR200"
,
"warranty"
:
"1 year"
,
"rating"
:
4.7
,
"features"
:
[
"24.2MP sensor"
,
"1080p video"
,
"3-inch LCD"
,
"Interchangeable lenses"
]
,
"description"
:
"Capture stunning photos and videos with this versatile DSLR camera."
,
"price"
:
599.99
}
,
"ActionCam 4K"
:
{
"name"
:
"ActionCam 4K"
,
"category"
:
"Cameras and Camcorders"
,
"brand"
:
"ActionCam"
,
"model_number"
:
"AC-4K"
,
"warranty"
:
"1 year"
,
"rating"
:
4.4
,
"features"
:
[
"4K video"
,
"Waterproof"
,
"Image stabilization"
,
"Wi-Fi"
]
,
"description"
:
"Record your adventures with this rugged and compact 4K action camera."
,
"price"
:
299.99
}
,
"FotoSnap Mirrorless Camera"
:
{
"name"
:
"FotoSnap Mirrorless Camera"
,
"category"
:
"Cameras and Camcorders"
,
"brand"
:
"FotoSnap"
,
"model_number"
:
"FS-ML100"
,
"warranty"
:
"1 year"
,
"rating"
:
4.6
,
"features"
:
[
"20.1MP sensor"
,
"4K video"
,
"3-inch touchscreen"
,
"Interchangeable lenses"
]
,
"description"
:
"A compact and lightweight mirrorless camera with advanced features."
,
"price"
:
799.99
}
,
"ZoomMaster Camcorder"
:
{
"name"
:
"ZoomMaster Camcorder"
,
"category"
:
"Cameras and Camcorders"
,
"brand"
:
"ZoomMaster"
,
"model_number"
:
"ZM-CM50"
,
"warranty"
:
"1 year"
,
"rating"
:
4.3
,
"features"
:
[
"1080p video"
,
"30x optical zoom"
,
"3-inch LCD"
,
"Image stabilization"
]
,
"description"
:
"Capture life's moments with this easy-to-use camcorder."
,
"price"
:
249.99
}
,
"FotoSnap Instant Camera"
:
{
"name"
:
"FotoSnap Instant Camera"
,
"category"
:
"Cameras and Camcorders"
,
"brand"
:
"FotoSnap"
,
"model_number"
:
"FS-IC10"
,
"warranty"
:
"1 year"
,
"rating"
:
4.1
,
"features"
:
[
"Instant prints"
,
"Built-in flash"
,
"Selfie mirror"
,
"Battery-powered"
]
,
"description"
:
"Create instant memories with this fun and portable instant camera."
,
"price"
:
69.99
}
}
2.3.2 调用 API 因为要和 ChatGPT 聊天,所以在定义这个函数的时候需要用到创建 chat completion,在最新版的示例如下:
三、具体代码
3.1 封装聊天函数 主要是通过调用 OpenAI API 来实现和模型的对话,并返回模型的对话,封装为 get_completion_from_messages,并保存在 tool.py 文件中。
from openai import OpenAI
API_SECRET_KEY = "sk-..."
BASE_URL = "https://flag.smarttrot.com/v1/"
client = OpenAI(api_key=API_SECRET_KEY, base_url=BASE_URL)
'''
封装一个支持更多参数的自定义访问 OpenAI GPT3.5 的函数
参数:
messages: 这是一个消息列表,每个消息都是一个字典,包含 role(角色)和 content(内容)。角色可以是'system'、'user'或 'assistant',内容是角色的消息。
model: 调用的模型,默认为 gpt-3.5-turbo(ChatGPT),有内测资格的用户可以选择 gpt-4
temperature: 这决定模型输出的随机程度,默认为 0,表示输出将非常确定。增加温度会使输出更随
机。
max_tokens: 这决定模型输出的最大的 token 数。
'''
def get_completion_from_messages (messages,
model='gpt-3.5-turbo' ,
temperature=0 ,
max_tokens=500 ):
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
)
return response.choices[0 ].message.content
3.2 封装文件处理函数 主要是封装一系列用于处理文件的函数,如获取产品、获取分类等,保存为 utils_zh.py 文件。
import json
from tool import get_completion_from_messages
from collections import defaultdict
with open ("products.json" , "r" ) as file:
products = json.load(file)
delimiter = "####"
system_message = f"""
您将获得客户服务查询。
客户服务查询将使用{delimiter} 字符作为分隔符。
请仅输出一个可解析的 Python 列表,列表每一个元素是一个 JSON 对象,每个对象具有以下格式:
'category': <包括以下几个类别:Computers and Laptops、Smartphones and Accessories、
Televisions and Home Theater Systems、Gaming Consoles and Accessories、Audio
Equipment、Cameras and Camcorders>,
以及
'products': <必须是下面的允许产品列表中找到的产品列表>
类别和产品必须在客户服务查询中找到。
如果提到了某个产品,它必须与允许产品列表中的正确类别关联。
如果未找到任何产品或类别,则输出一个空列表。
除了列表外,不要输出其他任何信息!
允许的产品:
Computers and Laptops category:
TechPro Ultrabook
BlueWave Gaming Laptop
PowerLite Convertible
TechPro Desktop
BlueWave Chromebook
Smartphones and Accessories category:
SmartX ProPhone
MobiTech PowerCase
SmartX MiniPhone
MobiTech Wireless Charger
SmartX EarBuds
Televisions and Home Theater Systems category:
CineView 4K TV
SoundMax Home Theater
CineView 8K TV
SoundMax Soundbar
CineView OLED TV
Gaming Consoles and Accessories category:
GameSphere X
ProGamer Controller
GameSphere Y
ProGamer Racing Wheel
GameSphere VR Headset
Audio Equipment category:
AudioPhonic Noise-Canceling Headphones
WaveSound Bluetooth Speaker
AudioPhonic True Wireless Earbuds
WaveSound Soundbar
AudioPhonic Turntable
Cameras and Camcorders category:
FotoSnap DSLR Camera
ActionCam 4K
FotoSnap Mirrorless Camera
ZoomMaster Camcorder
FotoSnap Instant Camera
只输出对象列表,不包含其他内容。
"""
def get_product_by_name (name ):
"""
根据产品名称获取产品
参数:
name: 产品名称
"""
return products.get(name, None )
def get_products_by_category (category ):
"""
根据类别获取产品
参数:
category: 产品类别
"""
return [product for product in products.values() if product["category" ] == category]
def read_string_to_list (input_string ):
"""
将输入的字符串转换为 Python 列表。
参数:
input_string: 输入的字符串,应为有效的 JSON 格式。
返回:
list 或 None: 如果输入字符串有效,则返回对应的 Python 列表,否则返回 None。
"""
if input_string is None :
return None
try :
input_string = input_string.replace("'" , '"' )
data = json.loads(input_string)
return data
except json.JSONDecodeError:
print ("Error: Invalid JSON string" )
return None
def generate_output_string (data_list ):
"""
根据输入的数据列表生成包含产品或类别信息的字符串。
参数:
data_list: 包含字典的列表,每个字典都应包含 "products" 或 "category" 的键。
返回:
output_string: 包含产品或类别信息的字符串。
"""
output_string = ""
if data_list is None :
return output_string
for data in data_list:
try :
if "products" in data and data["products" ]:
products_list = data["products" ]
for product_name in products_list:
product = get_product_by_name(product_name)
if product:
output_string += json.dumps(product, indent=4 , ensure_ascii=False ) + "\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, indent=4 , ensure_ascii=False ) + "\n"
else :
print ("Error: Invalid object format" )
except Exception as e:
print (f"Error: {e} " )
return output_string
def find_category_and_product_only (user_input, products_and_category ):
delimiter = "####"
system_message = f"""
您将获得客户服务查询。
客户服务查询将使用{delimiter} 字符作为分隔符。
请仅输出一个可解析的 Python 列表,列表每一个元素是一个 JSON 对象,每个对象具有以下格式:
'category': <包括以下几个类别:Computers and Laptops、Smartphones and Accessories、
Televisions and Home Theater Systems、Gaming Consoles and Accessories、Audio
Equipment、Cameras and Camcorders>,
以及
'products': <必须是下面的允许产品列表中找到的产品列表>
类别和产品必须在客户服务查询中找到。
如果提到了某个产品,它必须与允许产品列表中的正确类别关联。
如果未找到任何产品或类别,则输出一个空列表。
除了列表外,不要输出其他任何信息!
允许的产品:
Computers and Laptops category:
TechPro Ultrabook
BlueWave Gaming Laptop
PowerLite Convertible
TechPro Desktop
BlueWave Chromebook
Smartphones and Accessories category:
SmartX ProPhone
MobiTech PowerCase
SmartX MiniPhone
MobiTech Wireless Charger
SmartX EarBuds
Televisions and Home Theater Systems category:
CineView 4K TV
SoundMax Home Theater
CineView 8K TV
SoundMax Soundbar
CineView OLED TV
Gaming Consoles and Accessories category:
GameSphere X
ProGamer Controller
GameSphere Y
ProGamer Racing Wheel
GameSphere VR Headset
Audio Equipment category:
AudioPhonic Noise-Canceling Headphones
WaveSound Bluetooth Speaker
AudioPhonic True Wireless Earbuds
WaveSound Soundbar
AudioPhonic Turntable
Cameras and Camcorders category:
FotoSnap DSLR Camera
ActionCam 4K
FotoSnap Mirrorless Camera
ZoomMaster Camcorder
FotoSnap Instant Camera
只输出对象列表,不包含其他内容。
"""
messages = [
{'role' :'system' , 'content' : system_message},
{'role' :'user' , 'content' : f"{delimiter} {user_input} {delimiter} " },
]
return get_completion_from_messages(messages)
def get_products_and_category ():
"""
Used in L5
"""
products = get_products()
products_by_category = defaultdict(list )
for product_name, product_info in products.items():
category = product_info.get('category' )
if category:
products_by_category[category].append(product_info.get('name' ))
return dict (products_by_category)
products_file = 'products.json'
def get_products ():
with open (products_file, 'r' ) as file:
products = json.load(file)
return products
3.3 封装链式提问函数 主要是完成之前的整个提问、审核流程思路,保存为 qa.py 文件。
from openai import OpenAI
import utils_zh
from tool import get_completion_from_messages
API_SECRET_KEY = "sk-..."
BASE_URL = "https://flag.smarttrot.com/v1/"
client = OpenAI(api_key=API_SECRET_KEY, base_url=BASE_URL)
def process_user_message_ch (user_input, all_messages, debug=True ):
"""
对用户信息进行预处理
参数:
user_input : 用户输入
all_messages : 历史信息
debug : 是否开启 DEBUG 模式,默认开启
"""
delimiter = "```"
response = client.moderations.create(
input =user_input
)
moderation_output = response.results[0 ]
if moderation_output.flagged:
print ("第一步:输入被 Moderation API 检测到敏感信息,直接返回提示语" )
return "你的输入被检测到包含敏感信息,请重新输入!"
if debug: print ("第一步:输入通过 Moderation API 检测" )
category_and_product_response = utils_zh.find_category_and_product_only(user_input, utils_zh.get_products_and_category())
category_and_product_list = utils_zh.read_string_to_list(category_and_product_response)
if debug: print ("第二步:抽取出商品和对应的目录" )
product_information = utils_zh.generate_output_string(category_and_product_list)
if debug: print ("第三步:查找抽取出的商品信息" )
system_message = f"""
您是一家大型电子商店的客户服务助理。\
请以友好和乐于助人的语气回答问题,并提供简洁明了的答案。\
请确保向用户提出相关的后续问题。
"""
messages = [
{"role" : "system" , "content" : system_message},
{"role" : "user" , "content" : f"{delimiter} {user_input} {delimiter} " },
{'role' : 'assistant' , 'content' : f"相关商品信息:\n{product_information} " }
]
final_response = get_completion_from_messages(all_messages+messages)
if debug:print ("第四步,生成用户回答" )
all_messages = all_messages + messages[1 :]
response = client.moderations.create(input =final_response)
moderation_output = response.results[0 ]
if moderation_output.flagged:
if debug: print ("第五步:输出被 Moderation 拒绝" )
return "抱歉,我们不能提供该信息"
if debug: print ("第五步:输出经过 Moderation 检查" )
user_message = f"""
用户信息:{delimiter} {user_input} {delimiter}
代理回复:{delimiter} {final_response} {delimiter}
回复是否足够回答问题
如果足够,回答 Y
如果不足够,回答 N
仅回答上述字母即可
"""
messages = [
{'role' : 'system' , 'content' : system_message},
{'role' : 'user' , 'content' : user_message}
]
evaluation_response = get_completion_from_messages(messages)
if debug: print ("第六步:模型评估该回答" )
if "Y" in evaluation_response:
if debug: print ("第七步:模型赞同了该回答." )
return final_response, all_messages
else :
if debug: print ("第七步:模型不赞成该回答." )
neg_str = "很抱歉,我无法提供您所需的信息。我将为您转接到一位人工客服代表以获取进一步帮助。"
return neg_str, all_messages
3.4 可视化调用 使用 panel 库将整个流程在可视化界面上进行可交互操作,保存为 window.py。
import panel as pn
from qa import process_user_message_ch
def collect_messages_ch (debug=True ):
"""
用于收集用户的输入并生成助手的回答
参数:
debug: 用于觉得是否开启调试模式
"""
user_input = inp.value
if debug: print (f"User Input = {user_input} " )
if user_input == "" :
return
inp.value = ''
global context
response, context = process_user_message_ch(user_input, context, debug=False )
context.append({'role' :'assistant' , 'content' :f"{response} " })
panels.append(
pn.Row('User:' , pn.pane.Markdown(user_input, width=600 )))
panels.append(
pn.Row('Assistant:' , pn.pane.Markdown(response, width=600 , style={'background-color' : '#F6F6F6' })))
return pn.Column(*panels)
pn.extension()
panels = []
context = [ {'role' :'system' , 'content' :"You are Service Assistant" } ]
inp = pn.widgets.TextInput( placeholder='Enter text here…' )
button_conversation = pn.widgets.Button(name="Service Assistant" )
interactive_conversation = pn.bind(collect_messages_ch, button_conversation)
dashboard = pn.Column(
inp,
pn.Row(button_conversation),
pn.panel(interactive_conversation, loading_indicator=True , height=300 ),
)
dashboard
pn.serve(dashboard, show=True )
此程序执行,即在浏览器打开页面,测试使用结果如下。
可以看出在提出价格是多少的时候,是有报因为模型评估不赞同答案而给出的提示语,这里我没有去深入调试,造成原因有可能是中文理解或者是文件读取等导致的,有兴趣的小伙伴可以自行尝试。
总结 本文介绍了如何基于 ChatGPT 构建一个简单的电商客服问答系统。通过链式调用语言模型,结合多个 Prompt 实现复杂的问答与推理功能。主要涉及了环境配置、API 调用封装、数据处理以及可视化界面的搭建。在实际应用中,可以根据具体需求调整 Prompt 策略和优化模型参数。
相关免费在线工具 加密/解密文本 使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
RSA密钥对生成器 生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
Mermaid 预览与可视化编辑 基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
随机西班牙地址生成器 随机生成西班牙地址(支持马德里、加泰罗尼亚、安达卢西亚、瓦伦西亚筛选),支持数量快捷选择、显示全部与下载。 在线工具,随机西班牙地址生成器在线工具,online
Gemini 图片去水印 基于开源反向 Alpha 混合算法去除 Gemini/Nano Banana 图片水印,支持批量处理与下载。 在线工具,Gemini 图片去水印在线工具,online
curl 转代码 解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online