LLM系列 | 14: 构建端到端智能客服

11 篇文章 1 订阅

简介

漠漠水田飞白鹭,阴阴夏木啭黄鹂。
在这里插入图片描述

小伙伴们好,我是微信公众号《小窗幽记机器学习》的小编:卖海蛎煎的小男孩。紧接前面几篇ChatGPT Prompt工程和应用系列文章:

今天这篇小作文是吴恩达《Building Systems with the ChatGPT API》课程的第3篇笔记。本文将综合之前多篇文章搭建一个带评估功能的端到端智能客服系统。整体流程大致如下:

  • 检查输入,看看用户输入的query是否能够通过审核API。
  • 抽取出商品和对应的类别。
  • 抽取出商品信息。
  • 模型根据商品信息回答用户问题。
  • 将模型的回复输入审核API对回复进行审核。

完整代码请在微信公众号:「小窗幽记机器学习」上添加小编微信。

准备工作

import os
import openai
import sys
sys.path.append('../..')
import utils_cn as utils

import panel as pn  # GUI
pn.extension()

openai.api_key  = "sk-XXX"
os.environ['HTTP_PROXY'] = "xxx"
os.environ['HTTPS_PROXY'] = "xxx"


def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0, max_tokens=500):
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=temperature, 
        max_tokens=max_tokens, 
    )
    return response.choices[0].message["content"]

智能客服

以智能客服为例,介绍完整的服务链路,包括对输入文本进行安全检查、通过用户咨询查找到相关产品和类别、回答用户问题、对回答结果进行内容安全检查、对回答结果进行问题关联检查。

def process_user_message(user_input, all_messages, debug=True):
    delimiter = "```"
    
    # Step 1: Check input to see if it flags the Moderation API or is a prompt injection
    response = openai.Moderation.create(input=user_input)
    moderation_output = response["results"][0]

    if moderation_output["flagged"]:
        print("Step 1: Input flagged by Moderation API.")
        return "Sorry, we cannot process this request."

    if debug: print("Step 1: Input passed moderation check.")
    
    products_and_category_dict = utils.get_products_and_category()
    if debug:
        print("products_and_category_dict=", products_and_category_dict)
        print("start do find_category_and_product_only")
    category_and_product_response = utils.find_category_and_product_only(user_input, products_and_category_dict)
    if debug: 
        print("category_and_product_response=", category_and_product_response)
        print("start do Extract the list of products")
    # Step 2: Extract the list of products
    category_and_product_list = utils.read_string_to_list(category_and_product_response)
    if debug: 
        print("category_and_product_list=", category_and_product_list)

    if debug: 
        print("Step 2: Extracted list of products.")

    # Step 3: If products are found, look them up
    product_information = utils.generate_output_string(category_and_product_list)
    if debug:
        print("product_information=", product_information)
    if debug:
        print("Step 3: Looked up product information.")

    # Step 4: Answer the user question
    system_message = f"""
    你是一位大型电子商店的客户助理。以友好和乐于助人的口吻回答,给出简明的答案。确保询问用户相关的后续问题。
    """
    messages = [
        {'role': 'system', 'content': system_message},
        {'role': 'user', 'content': f"{delimiter}{user_input}{delimiter}"},
        {'role': 'assistant', 'content': f"Relevant product information:\n{product_information}"}
    ]

    final_response = get_completion_from_messages(all_messages + messages)
    if debug:
        print("Step 4: Generated response to user question.")
        print("final_response=", final_response)
    all_messages = all_messages + messages[1:]

    # Step 5: Put the answer through the Moderation API
    response = openai.Moderation.create(input=final_response)
    moderation_output = response["results"][0]

    if moderation_output["flagged"]:
        if debug: print("Step 5: Response flagged by Moderation API.")
        return "Sorry, we cannot provide this information."

    if debug:
        print("Step 5: Response passed moderation check.")

    # Step 6: Ask the model if the response answers the initial user query well
    user_message = f"""
    你是一个客服助理,负责评估客服助理的回答是否足够回答客户的问题,并验证助理从产品信息中引用的所有事实是否正确。\
    产品信息、用户和客服助理的消息将用三个反引号分隔,即```.\
    请用一个字母回答,不带标点符号:\
    Y - 如果输出足够回答问题,并且回答正确使用了产品信息 \
    N - 否则 \
    
    只输出一个字母。
    
    Customer message: {delimiter}{user_input}{delimiter}
    Product information: {delimiter}{product_information}{delimiter}
    Agent response: {delimiter}{final_response}{delimiter}

    回复是否充分回答了问题?
    """
    messages = [
        {'role': 'system', 'content': system_message},
        {'role': 'user', 'content': user_message}
    ]
    evaluation_response = get_completion_from_messages(messages)
    if debug:
        print("Step 6: Model evaluated the response.")
        print("evaluation_response=", evaluation_response)

    # Step 7: If yes, use this answer; if not, say that you will connect the user to a human
    if "Y" in evaluation_response:  # Using "in" instead of "==" to be safer for model output variation (e.g., "Y." or "Yes")
        if debug: print("Step 7: Model approved the response.")
        return final_response, all_messages
    else:
        if debug: print("Step 7: Model disapproved the response.")
        neg_str = "很抱歉,我无法提供您所需的信息。我将为您转接到一位人工客服以获得进一步帮助。"
        return neg_str, all_messages

user_input = "给我介绍下SmartX Pro phone和fotosnap DSLR camera。还有,我也想了解下你们的电视。"
response,_ = process_user_message(user_input,[])
print(response)

输出结果如下:

Step 1: Input passed moderation check.
products_and_category_dict= {'计算机和笔记本电脑': ['TechPro超极本', 'BlueWave游戏笔记本电脑', 'PowerLite可转换本', 'TechPro台式机', 'BlueWave Chromebook'], '智能手机和配件': ['SmartX ProPhone', 'MobiTech PowerCase', 'SmartX MiniPhone', 'MobiTech Wireless Charger', 'SmartX EarBuds'], '电视和家庭影院系统': ['CineView 4K TV', 'SoundMax Home Theater', 'CineView 8K TV', 'SoundMax Soundbar', 'CineView OLED TV'], '游戏机和配件': ['GameSphere X', 'ProGamer Controller', 'GameSphere Y', 'ProGamer Racing Wheel', 'GameSphere VR Headset'], '音频设备': ['AudioPhonic Noise-Canceling Headphones', 'WaveSound Bluetooth Speaker', 'AudioPhonic True Wireless Earbuds', 'WaveSound Soundbar', 'AudioPhonic Turntable'], '相机和摄像机': ['FotoSnap DSLR Camera', 'ActionCam 4K', 'FotoSnap Mirrorless Camera', 'ZoomMaster Camcorder', 'FotoSnap Instant Camera']}
start do find_category_and_product_only
category_and_product_response= [{'category': '智能手机及配件', 'products': ['MobiTech PowerCase', 'SmartX MiniPhone', 'MobiTech Wireless Charger', 'SmartX EarBuds']}, {'category': '相机和摄像机', 'products': ['ActionCam 4K', 'FotoSnap Mirrorless Camera', 'ZoomMaster Camcorder', 'FotoSnap Instant Camera']}, {'category': '电视和家庭影院系统', 'products': ['CineView 4K TV', 'SoundMax Home Theater', 'CineView 8K TV', 'SoundMax Soundbar', 'CineView OLED TV']}]
start do Extract the list of products
category_and_product_list= [{'category': '智能手机及配件', 'products': ['MobiTech PowerCase', 'SmartX MiniPhone', 'MobiTech Wireless Charger', 'SmartX EarBuds']}, {'category': '相机和摄像机', 'products': ['ActionCam 4K', 'FotoSnap Mirrorless Camera', 'ZoomMaster Camcorder', 'FotoSnap Instant Camera']}, {'category': '电视和家庭影院系统', 'products': ['CineView 4K TV', 'SoundMax Home Theater', 'CineView 8K TV', 'SoundMax Soundbar', 'CineView OLED TV']}]
Step 2: Extracted list of products.
product_information= {
    "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": "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": "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
}

Step 3: Looked up product information.
Step 4: Generated response to user question.
final_response= 当然,我很乐意为您介绍我们的产品。SmartX Pro phone是一款高端智能手机,具有先进的功能和性能。它配备了大屏幕、高像素相机、大内存和快速处理器。而Fotosnap DSLR相机则是一款专业级别的相机,具有高像素传感器、4K视频和可更换镜头等功能。至于我们的电视,我们有多种型号可供选择,包括4K和8K分辨率、HDR和智能电视等功能。您有什么具体的问题或需求吗?我可以为您提供更多详细信息。
Step 5: Response passed moderation check.
Step 6: Model evaluated the response.
evaluation_response= Y
Step 7: Model approved the response.
当然,我很乐意为您介绍我们的产品。SmartX Pro phone是一款高端智能手机,具有先进的功能和性能。它配备了大屏幕、高像素相机、大内存和快速处理器。而Fotosnap DSLR相机则是一款专业级别的相机,具有高像素传感器、4K视频和可更换镜头等功能。至于我们的电视,我们有多种型号可供选择,包括4K和8K分辨率、HDR和智能电视等功能。您有什么具体的问题或需求吗?我可以为您提供更多详细信息。

以ChatBot方式交流

以下使用panel构建一个简单的服务,通过对话框的方式进行交流。

def collect_messages(debug=True):
    user_input = inp.value_input
    if debug:
        print(f"User Input = {user_input}")
    if user_input == "":
        return
    inp.value = ''
    global context
    #response, context = process_user_message(user_input, context, utils.get_products_and_category(),debug=True)
    print("user_input=", user_input)
    print("context=", context)
    response, context = process_user_message(user_input, context, debug=debug)
    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)

panels = [] # collect display 

context = [ {'role':'system', 'content':"你是一个客户助理"} ]  

inp = pn.widgets.TextInput(placeholder='Enter text here…')
button_conversation = pn.widgets.Button(name="Service Assistant")

interactive_conversation = pn.bind(collect_messages, button_conversation)

dashboard = pn.Column(
    inp,
    pn.Row(button_conversation),
    pn.panel(interactive_conversation, loading_indicator=True, height=300),
)

在这里插入图片描述

与ChatBot的交流结果如下:

User:

给我介绍下你们的相机

Assistant:

我们有多种不同类型的相机,包括数码相机、单反相机和无反相机。每种相机都有不同的功能和规格,您可以告诉我您需要相机用于什么目的,我可以为您提供更具体的信息。

User:

我想了解下Fotosnap DSLR

Assistant:

Fotosnap DSLR是一款功能强大的数码单反相机,具有24.2MP传感器、1080p视频、3英寸液晶屏和可更换镜头等功能。它适合那些想要更多控制和创造力的摄影师。您可以选择不同的镜头来适应不同的拍摄场景。这款相机的价格为599.99美元,我们还提供1年的保修服务。您是否还有其他问题需要了解?

User:

还想买一台手机,你们有哪些型号

Assistant:

很抱歉,我无法提供您所需的信息。我将为您转接到一位人工客服以获得进一步帮助。

User:

介绍下SmartX的产品

Assistant:

我们的SmartX品牌提供了多种电子产品,包括智能手机、平板电脑、笔记本电脑、智能手表和智能家居设备等。我们的产品都采用最新的技术和设计,以提供最佳的用户体验。您可以告诉我您需要哪种类型的产品,我可以为您提供更具体的信息。

User:

我想了解下SmartX ProPhone

Assistant:

SmartX ProPhone是一款功能强大的智能手机,具有6.1英寸显示屏、128GB存储、12MP双摄像头和5G等功能。它适合那些需要高速数据传输和大存储空间的用户。这款手机的价格为899.99美元,我们还提供1年的保修服务。您是否还有其他问题需要了解?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值