LLM(三):基于ChatGPT构建一个问答系统

news2024/9/27 9:23:58

以电商客服助手为例,通过链式调用语言模型,结合多个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 环境准备

安装Anoconda、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 comption,在最新版的的示例如下

还有审查api

三,具体代码

3.1 封装聊天函数

主要是通过调用Openai API来实现和模型的对话,并返回模型的对话,封装为get_completion_from_messages,并保存在tool.py文件中

tool.py

from openai import OpenAI

#此处换成自己的API Key
API_SECRET_KEY  = "sk-..."

#因为我用的第三方要求,直接用Openai申请下的key的话这行可不要
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]

## 解析输入字符串
def read_string_to_list(input_string):
    """
将输入的字符串转换为 Python 列表。
参数:
input_string: 输入的字符串,应为有效的 JSON 格式。
返回:
list 或 None: 如果输入字符串有效,则返回对应的 Python 列表,否则返回 None。
    """

    if input_string is None:
        return None
    
    try:
        # 将输入字符串中的单引号替换为双引号,以满足 JSON 格式的要求
        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)

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文件

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 = "```"

    #第一步:使用Openai的Moderation API 对用户输入进行审核,判断是否包含敏感信息
    response = client.moderations.create(
        input=user_input
    )
    moderation_output = response.results[0]

    # 经过检查,发现包含敏感信息,则直接返回提示语
    if moderation_output.flagged:
        print("第一步:输入被Moderation API检测到敏感信息,直接返回提示语")
        return "你的输入被检测到包含敏感信息,请重新输入!"
    
    #如果开启了DEBUG模式,则打印出Moderation API检测到的敏感信息
    if debug: print("第一步:输入通过Moderation API检测")

    # 第二步:抽取出商品和对应的目录
    category_and_product_response = utils_zh.find_category_and_product_only(user_input,utils_zh.get_products_and_category())

    #将抽取出来的字符串转化为list
    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"""
您是一家大型电子商店的客户服务助理。\
请以友好和乐于助人的语气回答问题,并提供简洁明了的答案。\
请确保向用户提出相关的后续问题。
"""
    
    #插入message
    messages = [
        {"role": "system", "content": system_message},
        {"role": "user", "content": f"{delimiter}{user_input}{delimiter}"},
        {'role': 'assistant', 'content': f"相关商品信息:\n{product_information}"}
    ]

    #获取GPT-3.5的回答
    #通过附加all_messages来实现多轮对话
    final_response = get_completion_from_messages(all_messages+messages)
    if debug:print("第四步,生成用户回答")

    # 将该轮信息加入到历史信息中
    all_messages = all_messages + messages[1:]

    # 第五步:基于 Moderation API 检查输出是否合规
    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
    仅回答上述字母即可
    """

    # print(final_response)
    messages = [
        {'role': 'system', 'content': system_message},
        {'role': 'user', 'content': user_message}
    ]

    # 要求模型评估回答
    evaluation_response = get_completion_from_messages(messages)

    # print(evaluation_response)
    if debug: print("第六步:模型评估该回答")


    # 第七步:如果评估为 Y,输出回答;如果评估为 N,反馈将由人工修正答案
    if "Y" in evaluation_response: # 使用 in 来避免模型可能生成 Yes
        if debug: print("第七步:模型赞同了该回答.")
        return final_response, all_messages
    else:
        if debug: print("第七步:模型不赞成该回答.")
        neg_str = "很抱歉,我无法提供您所需的信息。我将为您转接到一位人工客服代表以获取进一步帮助。"
        return neg_str, all_messages

3.4 可视化调用

使用panel库将整个流程在可视化界面上进行可交互操作,保存为window.py

window.py

import panel as pn
from qa import process_user_message_ch

def collect_messages_ch(debug=True):
    """
用于收集用户的输入并生成助手的回答
参数:
debug: 用于觉得是否开启调试模式
    """
    user_input = inp.value_input
    if debug: print(f"User Input = {user_input}")
    if user_input == "":
        return
    inp.value = ''
    global context
    # 调用 process_user_message 函数
    #response, context = process_user_message(user_input, context,utils.get_products_and_category(),debug=True)
    response, context = process_user_message_ch(user_input, context, debug=False)
    # print(response)
    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 = [] # collect display
# 系统信息
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)

此程序执行,即在浏览器打开页面,测试使用结果如下

可以看出在提出价格是多少的时候,是有报因为模型评估不赞同答案而给出的提示语,这里我没有去深入调试,造成原因有可能是中文理解或者是文件读取等导致的,有兴趣的小伙伴可以自行尝试

在这里插入图片描述

大模型&AI产品经理如何学习

求大家的点赞和收藏,我花2万买的大模型学习资料免费共享给你们,来看看有哪些东西。

1.学习路线图

在这里插入图片描述

第一阶段: 从大模型系统设计入手,讲解大模型的主要方法;

第二阶段: 在通过大模型提示词工程从Prompts角度入手更好发挥模型的作用;

第三阶段: 大模型平台应用开发借助阿里云PAI平台构建电商领域虚拟试衣系统;

第四阶段: 大模型知识库应用开发以LangChain框架为例,构建物流行业咨询智能问答系统;

第五阶段: 大模型微调开发借助以大健康、新零售、新媒体领域构建适合当前领域大模型;

第六阶段: 以SD多模态大模型为主,搭建了文生图小程序案例;

第七阶段: 以大模型平台应用与开发为主,通过星火大模型,文心大模型等成熟大模型构建大模型行业应用。

2.视频教程

网上虽然也有很多的学习资源,但基本上都残缺不全的,这是我自己整理的大模型视频教程,上面路线图的每一个知识点,我都有配套的视频讲解。

在这里插入图片描述

在这里插入图片描述

(都打包成一块的了,不能一一展开,总共300多集)

因篇幅有限,仅展示部分资料,需要点击下方图片前往获取

3.技术文档和电子书

这里主要整理了大模型相关PDF书籍、行业报告、文档,有几百本,都是目前行业最新的。
在这里插入图片描述

4.LLM面试题和面经合集

这里主要整理了行业目前最新的大模型面试题和各种大厂offer面经合集。
在这里插入图片描述

👉学会后的收获:👈
• 基于大模型全栈工程实现(前端、后端、产品经理、设计、数据分析等),通过这门课可获得不同能力;

• 能够利用大模型解决相关实际项目需求: 大数据时代,越来越多的企业和机构需要处理海量数据,利用大模型技术可以更好地处理这些数据,提高数据分析和决策的准确性。因此,掌握大模型应用开发技能,可以让程序员更好地应对实际项目需求;

• 基于大模型和企业数据AI应用开发,实现大模型理论、掌握GPU算力、硬件、LangChain开发框架和项目实战技能, 学会Fine-tuning垂直训练大模型(数据准备、数据蒸馏、大模型部署)一站式掌握;

• 能够完成时下热门大模型垂直领域模型训练能力,提高程序员的编码能力: 大模型应用开发需要掌握机器学习算法、深度学习框架等技术,这些技术的掌握可以提高程序员的编码能力和分析能力,让程序员更加熟练地编写高质量的代码。
在这里插入图片描述

1.AI大模型学习路线图
2.100套AI大模型商业化落地方案
3.100集大模型视频教程
4.200本大模型PDF书籍
5.LLM面试题合集
6.AI产品经理资源合集

👉获取方式:
😝有需要的小伙伴,可以保存图片到wx扫描二v码免费领取【保证100%免费】🆓

在这里插入图片描述

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SCP拷贝失败解决办法

1. 现象&#xff1a; 今天在提交代码的时候&#xff0c;提示hooks文件不存在&#xff0c;需要下载。 我之前用scp -p -P 1111 xxxxgerrit.xxxx:hooks/commit-msg$(gitdir)/hooks/ 就能下载&#xff0c; 但是更新系统后下载失败&#xff0c;显示 subsystem request failed …

哪个牌子的电容笔好用又实惠?西圣、绿联、摩米士电容笔实测大比拼

​现在市面上的电容笔很多&#xff0c;在选择时会让人感到很纠结。那么多的选择&#xff0c;究竟哪个牌子的电容笔好用又实惠呢&#xff1f;一款优质的电容笔应考虑握持舒适度、笔尖材质、电池续航能力以及书写流畅度等因素。作为一位多年的数码爱好者&#xff0c;我今天将针对…

Elasticsearch 再次开源

作者&#xff1a;来自 Elastic Shay Banon [D.N.A] Elasticsearch 和 Kibana 可以再次被称为开源了。很难表达这句话让我有多高兴。我真的激动得跳了起来。Elastic 的所有人都是这样的。开源已经融入我的 DNA&#xff0c;也融入了 Elastic 的 DNA。能够再次将 Elasticsearch 称…

Linux驱动(一):环境搭建及介绍

目录 前言一、硬件配置及SDK包1.硬件核心芯片2.瑞芯微原厂SDK包 二、环境镜像文件的获取1.镜像文件的组成及启动流程2.获取环境所需的镜像文件2.1 uboot.img2.2 boot.img2.3 rootfs.img2.4 整体编译 三、镜像文件烧录 前言 自用自用自用&#xff0c;晚上睡觉前复盘用。当然&…

免费批量Excel文件合并、拆分软件

软件介绍 下载地址&#xff1a;https://pan.quark.cn/s/ae860a4e2ccb 1.多个XLS或XLSX格式EXCEL文件合并&#xff0c;合并后可使用数据透视表进行相关操作。 2.自动合并多个EXCEL文件的第一个工作表&#xff0c;并汇总成一张表&#xff0c;可根据所有列标题需要指定需要的列。 …

pdf压缩到指定大小,分享2024年最新7款pdf压缩免费版

pdf是一种全球广泛使用的文件格式&#xff0c;它可以在不损失质量或文本对齐的情况下安全传输文档。然而&#xff0c;许多文件共享应用和网站对于可以上传或分享的pdf文件大小设定了限制。在这种情况下&#xff0c;您可能需要一款可以帮助您减小pdf文件大小的软件。pdf压缩免费…

《汇编语言》第11章——实验11编写子程序

编写一个子程序&#xff0c;包含任意字符&#xff0c;以0结尾的字符串中的小写字母变成大写字母&#xff0c;描述如下。 名称&#xff1a;letterc 功能&#xff1a;将以0结尾的字符串中的小写字母变成大写字母 参数&#xff1a;ds:si指向的字符串首地址 assume cs:codesg d…

本地部署 Google Gemma 开源模型

Google Gemma 是 Google AI 团队开发的基于Transformer的轻量级大语言模型&#xff0c;Gemma 模型已开源并在各种硬件平台上保持良好性能&#xff0c;记录下本地部署过程并在多种情景下使用情况。 一、环境配置 1、下载并安装Ollama 在 Windows 上下载 Ollama &#xff0c;选…

什么牌子的可视耳勺最好用?力荐四款热门品牌!

可视挖耳勺近几年越来越火爆&#xff0c;各种款式层出不穷&#xff0c;在可视挖耳勺的挑选上&#xff0c;我需要提醒大家一定要避雷劣质可视挖耳勺产品&#xff0c;劣质可视挖耳勺产品普遍不做核心性能的反复调教&#xff0c;成像清晰度低、耳勺出现发烫、性能不稳定等问题是时…

web渗透:CSRF漏洞(跨站请求伪造)

目录 CSRF漏洞 深入研究 CSRF攻击常见于哪些类型的网站&#xff1f; 如何通过HTTP Referer头来防范CSRF攻击&#xff1f; 为什么设置Cookie的SameSite属性对预防CSRF有帮助&#xff1f; 导图 CSRF漏洞 CSRF&#xff08;Cross-Site Request Forgery&#xff0c;跨站请求伪…

MySQL——事务与存储过程(一)事务管理(4)事务的隔离级别

数据库是多线程并发访问的&#xff0c;所以很容易出现多个线程同时开启事务的情况&#xff0c;这样就会出现脏读、重复读以及幻读的情况&#xff0c;为了避免这种情况的发生&#xff0c;就需要为事务设置隔离级别。在 MySQL 中&#xff0c;事务有4种隔离级别&#xff0c;接下来…

Linux安装Docker与基本指令

1、什么是Docker Dokcer是一种开源平台&#xff0c;主要用于创建、部署和管理容器化应用程序&#xff0c;它通过将应用程序以及所有的依赖打包到一个轻量级的、可移植的容器中&#xff0c;使得应用可以在任何环境中一致的运行! 1.1、Docker的优点 一致性和可移植性 跨环境一致…

删除微信重复文件的简易工具

去我的个人博客观看&#xff0c;观感更佳哦&#xff0c;&#x1f619;&#x1f619; 前言 !!!!温馨提示&#xff1a;假如你是小白&#xff0c;啥都不懂&#xff0c;请直接跳到文章的最后!!! 原本我是没想写这篇文章&#xff0c;契机是某一天我突然打开微信的存储空间占用才发…

5款手机版的影视解说文案生成器,无须下载更方便!

在这个自媒体的红利时期&#xff0c;影视解说创作占据了内容创作的很大一部分。而影视解说文案则是帮助观众更好地理解和欣赏影视作品的重要点。随着手机的普及&#xff0c;越来越多的人希望能够在手机上方便地生成影视解说文案创作。今天&#xff0c;我们就来介绍5款手机版的影…

Shared memory bank conflicts

共享内存和bank: 在CUDA架构中&#xff0c;共享内存是一个非常快速的内存类型&#xff0c;它位于每个线程块内部并为该线程块内的所有线程提供服务。为了实现高吞吐量的访问&#xff0c;共享内存被划分为多个独立的存储区域&#xff0c;称为“banks”。每个bank可以在单个时钟…

创新微MinewSemi推出基于Nordic nRF54系列芯片 SoC 低功耗蓝牙5.4模组

在物联网技术飞速发展的今天&#xff0c;创新微MinewSemi隆重推出基于Nordic最新nRF54系列芯片SoC的ME54BS01和ME54BS02全新低功耗蓝牙5.4模组。这两款模组的问世&#xff0c;标志着我们在推动物联网设备智能化、高效化道路上的迈出了坚实的一步&#xff0c;展示了对未来技术的…

回归预测|基于卷积神经网络-鲸鱼优化-最小二乘支持向量机的数据回归预测Matlab程序 CNN-WOA-LSSVM

回归预测|基于卷积神经网络-鲸鱼优化-最小二乘支持向量机的数据回归预测Matlab程序 CNN-WOA-LSSVM 文章目录 一、基本原理1. 数据预处理2. 特征提取&#xff08;CNN&#xff09;3. 参数优化&#xff08;WOA&#xff09;4. 模型训练&#xff08;LSSVM&#xff09;5. 模型评估和优…

World of Warcraft [CLASSIC][80][Grandel] Mount with 310% speed

310%速度的坐骑【被感染的始祖幼龙】 永恒之眼&#xff08;3个&#xff09; 纳克萨玛斯&#xff08;9个&#xff09; 10人副本&#xff0c; 白马 &#xff08;DPS N&#xff09;黑马&#xff08;DPS N&#xff09;绿马&#xff08;T N DPS&#xff09;红马&#xff08;T …

诚信为金:揭秘好征信的六大黄金标准,并实践守护好它的五大秘籍

什么是好征信&#xff0c;怎么守护它 咱们平时总听人说“征信”&#xff0c;“征信”到底是个什么东西&#xff0c;但很少人能透彻的了解它。它是一个有点神秘但又挺重要的东西&#xff0c;简单说&#xff0c;它就是咱在金融界的一张“身份证”&#xff0c;记录着咱们的信用表…

【C++ Qt day3】

2、设计一个Per类&#xff0c;类中包含私有成员:姓名、年龄、指针成员身高、体重&#xff0c;再设计一个Stu类&#xff0c;类中包含私有成员:成绩、Per类对象p1&#xff0c;设计这两个类的构造函数、析构函数和拷贝构造函数。