【大语言模型LLM】-基于大语言模型搭建客服助手(2)

在这里插入图片描述

🔥博客主页西瓜WiFi

🎥系列专栏《大语言模型》

很多非常有趣的模型,值得收藏,满足大家的收集癖! 如果觉得有用,请三连👍⭐❤️,谢谢!

长期不定时更新,欢迎watch和fork!❤️❤️❤️

❤️感谢大家点赞👍 收藏⭐ 评论⭐


🎥大语言模型LLM基础-系列文章

【大语言模型LLM】- AI工具收录集合,一篇就够了!
【大语言模型LLM】-大语言模型如何编写Prompt?
【大语言模型LLM】-如何使用大语言模型提高工作效率?
【大语言模型LLM】-使用大语言模型搭建点餐机器人
【大语言模型LLM】-基础语言模型和指令微调的语言模型
【大语言模型LLM】-基于ChatGPT搭建客服助手(1)

持续更新中…


接上一篇【大语言模型LLM】-基于ChatGPT搭建客服助手(1),本篇我们将不局限于使用ChatGPT的API,还将调用使用Ollama+LLM部署在本地的大语言模型,实现机器人助手的私有化搭建。这部分内容融合了我们前面几篇的全部内容。并且加入了评估步骤。以下是该系统的核心操作流程:

  1. 根据用户输入,获取相关产品信息。
  2. 若产品搜索成功,我们将继续寻找相关的详细产品信息。
  3. 我们使用模型针对用户的问题进行回答。
  4. 最后,让模型自己评估回答是否符合用户需求。

最终,将审核通过的答案呈现给用户。

第一部分 根据用户的输入获取产品列表

import json
from collections import defaultdict# 获取商品和目录
def get_products_and_category(products_file):try:with open(products_file, 'r') as file:products = json.load(file)except FileNotFoundError:print('产品文件不存在!')return Noneexcept json.JSONDecodeError:print('产品文件格式错误!')return None# 利用 defaultdict 创建一个空列表,用于存放不同类别的产品信息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 read_string_to_list(input_string):if input_string is None:return Nonetry:# 将单引号替换为双引号以生成有效的 JSONinput_string = input_string.replace("'", "\"")  data = json.loads(input_string)return dataexcept json.JSONDecodeError:print(input_string)print("错误:非法的 Json 格式")return None# 获取目录和产品
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 UltrabookBlueWave Gaming LaptopPowerLite ConvertibleTechPro DesktopBlueWave ChromebookSmartphones and Accessories category:SmartX ProPhoneMobiTech PowerCaseSmartX MiniPhoneMobiTech Wireless ChargerSmartX EarBudsTelevisions and Home Theater Systems category:CineView 4K TVSoundMax Home TheaterCineView 8K TVSoundMax SoundbarCineView OLED TVGaming Consoles and Accessories category:GameSphere XProGamer ControllerGameSphere YProGamer Racing WheelGameSphere VR HeadsetAudio Equipment category:AudioPhonic Noise-Canceling HeadphonesWaveSound Bluetooth SpeakerAudioPhonic True Wireless EarbudsWaveSound SoundbarAudioPhonic TurntableCameras and Camcorders category:FotoSnap DSLR CameraActionCam 4KFotoSnap Mirrorless CameraZoomMaster CamcorderFotoSnap Instant Camera只输出对象列表,不包含其他内容。"""messages =  [  {'role':'system', 'content': system_message},    {'role':'user', 'content': f"{delimiter}{user_input}{delimiter}"},  ] return get_gemma_response(messages)# 商品和目录的数据文件
products_file = 'products.json'
categories_file = 'categories.json'# 获取目录和产品
category_and_product_response = find_category_and_product_only('你好,有电视机嘛?',products_and_category)# 将字符串转换为列表
category_and_product_list = read_string_to_list(category_and_product_response)print("第一步:抽取出商品列表")
print('- '*65)
print(category_and_product_list)

输出结果:

第一步:抽取出商品列表
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
[{'category': 'Televisions and Home Theater Systems', 'products': ['CineView 4K TV', 'CineView 8K TV', 'CineView OLED TV']}]

第二部分 根据商品列表抽取商品信息

# 商品信息的搜索
def get_product_by_name(name):products = get_products()return products.get(name, None)# 根据商品列表抽取商品信息
def generate_output_string(data_list):"""根据商品列表抽取商品信息并生成输出字符串。参数:data_list:包含商品信息的列表。返回:输出字符串。"""output_string = ""# 如果输入列表为 None,则返回空字符串。if data_list is None:return output_string# 循环遍历数据列表中的每个项目。for data in data_list:try:# 如果项目包含 "products" 字段,则获取产品列表并循环遍历产品名称。if "products" in data:products_list = data["products"]for product_name in products_list:# 获取产品名称对应的产品对象。product = get_product_by_name(product_name)# 如果产品存在,则将其转换为 JSON 字符串并将其追加到输出字符串中。if product:output_string += json.dumps(product, indent=4) + "\n"# 如果产品不存在,则打印错误信息。else:print(f"错误: 商品 '{product_name}' 没有找到")# 如果项目包含 "category" 字段,则获取该类别的产品列表并循环遍历产品。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) + "\n"# 如果数据格式不正确,则打印错误信息。else:print("错误:非法的商品格式")# 捕获所有异常并打印错误信息。except Exception as e:print(f"Error: {e}")return output_stringproduct_information = generate_output_string(category_and_product_list)print("第二步:抽取商品信息")
print('- '*65)
print(product_information)

输出结果:

第二步:抽取商品信息
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
{"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
}
{"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
}
{"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
}

第三部分 根据信息生成回答

def get_assistant_response(user_input,all_messages):# 分隔符delimiter = '''```'''system_message = f"""您是一家大型电子商店的客户服务助理。\请以友好和乐于助人的语气回答问题,并提供详细的答案。\请确保向用户提出相关的后续问题。"""# 插入 messagemessages = [{'role': 'system', 'content': system_message},{'role': 'user', 'content': f"{delimiter}{user_input}{delimiter}"},{'role': 'assistant', 'content': f"相关商品信息:\n{product_information}"}]
#     print(messages)# 获取 LLM 的回答# 通过附加 all_messages 实现多轮对话final_response = get_gemma_response(all_messages + messages)# 将该轮信息加入到历史信息中all_messages = all_messages + messages[1:]return final_responsefinal_response = get_assistant_response('你好,有电视机嘛?',[])
print("第三步:获取助手回答")
print('- '*65)
print(final_response)

第四部分 检查模型是否很好的回答了问题

def check_assistant_reponse(final_response,all_messages,debug = True):# 分隔符delimiter = '''```'''system_message = f"""您是一家大型电子商店的客户服务助理。\请以友好和乐于助人的语气回答问题,并提供简洁明了的答案。\请确保向用户提出相关的后续问题。"""user_message = f"""用户信息: {delimiter}{user_input}{delimiter}代理回复: {delimiter}{final_response}{delimiter}回复是否足够回答问题如果足够,回答 Y如果不足够,回答 N仅回答上述字母即可"""print(user_message)messages = [{'role': 'system', 'content': system_message},{'role': 'user', 'content': user_message}]# 要求模型评估回答evaluation_response = get_gemma_response(messages)print(evaluation_response)if debug: print("第四步:模型评估该回答")# 第七步:如果评估为 Y,输出回答;如果评估为 N,反馈将由人工修正答案# 使用 in 来避免模型可能生成 Yesif "Y" in evaluation_response:  if debug: print("第五步:模型赞同了该回答.")return final_response, all_messageselse:if debug: print("第六步:模型不赞成该回答.")neg_str = "很抱歉,我无法提供您所需的信息。我将为您转接到一位人工客服代表以获取进一步帮助。"return neg_str, all_messagescheck_response_result = check_assistant_reponse(final_response,[])print(check_response_result )

输出结果:

用户信息: ```请告诉我关于 smartx pro phone 和 the fotosnap camera 的信息。另外,请告诉我关于你们的tvs的情况。```代理回复: ```...请问您对哪种电视机感兴趣?```回复是否足够回答问题如果足够,回答 Y如果不足够,回答 N仅回答上述字母即可N
第四步:模型评估该回答
第六步:模型不赞成该回答.
('很抱歉,我无法提供您所需的信息。我将为您转接到一位人工客服代表以获取进一步帮助。', [])

这里模型的回答没有通过自己的检查,我们需要注意到,在中文的理解和处理方面,由于模型的特性,我们可能会偶尔遇到不理想的结果。在这种情况下,可以多尝试几次,或者进行深入的研究,以找到更稳定的方法。

第五部分 客服助手全部代码

import json
import requests
from collections import defaultdict# 商品和目录的数据文件
products_file = 'products.json'
categories_file = 'categories.json'# 分隔符
delimiter = "####"
# 第二步(抽取商品)系统信息文本
step_2_system_message_content = f"""
您将获得一次客户服务对话。最近的用户查询将使用{delimiter}字符进行分隔。输出一个Python对象列表,其中每个对象具有以下格式:
'category': <包括以下几个类别:Computers and Laptops、martphones and Accessories、elevisions and Home Theater Systems、elevisions and Home Theater Systems、elevisions and Home Theater Systems、'category': <包括以下几个类别:Computers and Laptops、martphones and Accessories、elevisions and Home Theater Systems、elevisions and Home Theater Systems、elevisions and Home Theater Systems、相机和摄像机>,
或者
'products': <必须是下面的允许产品列表中找到的产品>类别和产品必须在客户服务查询中找到。
如果提到了产品,它必须与下面的允许产品列表中的正确类别相关联。
如果未找到任何产品或类别,请输出一个空列表。
只列出之前对话的早期部分未提及和讨论的产品和类别。允许的产品:Computers and Laptops类别:TechPro Ultrabook
BlueWave Gaming Laptop
PowerLite Convertible
TechPro Desktop
BlueWave ChromebookSmartphones and Accessories类别:
SmartX ProPhone
MobiTech PowerCase
SmartX MiniPhone
MobiTech Wireless Charger
SmartX EarBudsTelevisions and Home Theater Systems类别:
CineView 4K TV
SoundMax Home Theater
CineView 8K TV
SoundMax Soundbar
CineView OLED TVGaming Consoles and Accessories类别:
GameSphere X
ProGamer Controller
GameSphere Y
ProGamer Racing Wheel
GameSphere VR HeadsetAudio Equipment类别:
AudioPhonic Noise-Canceling Headphones
WaveSound Bluetooth Speaker
AudioPhonic True Wireless Earbuds
WaveSound Soundbar
AudioPhonic TurntableCameras and Camcorders类别:
FotoSnap DSLR Camera
ActionCam 4K
FotoSnap Mirrorless Camera
ZoomMaster Camcorder
FotoSnap Instant Camera只输出对象列表,不包含其他内容。
"""step_2_system_message = {'role':'system', 'content': step_2_system_message_content}    # 第四步(生成用户回答)的系统信息
step_4_system_message_content = f"""你是一家大型电子商店的客户服务助理。以友好和乐于助人的语气回答,回答保持简洁明了。确保让用户提出相关的后续问题。
"""step_4_system_message = {'role':'system', 'content': step_4_system_message_content}    # 第六步(验证模型回答)的系统信息
step_6_system_message_content = f"""你是一个助手,评估客户服务代理的回答是否足够回答客户的问题,并验证助手从产品信息中引用的所有事实是否正确。对话历史、产品信息、用户和客户服务代理的消息将用```进行分隔。请用一个字母回答,不带标点符号:Y - 如果输出足够回答问题,并且回答正确使用了产品信息N - 输出不足够回答问题,或者没有正确使用产品信息只输出一个字母。
"""step_6_system_message = {'role':'system', 'content': step_6_system_message_content}    def get_gemma_response(messages,model='llama3'): '''prompt: 对应的提示词Data 参数:model:(必需)模型名称messages:聊天消息,这可用于保持聊天记忆消息对象具有以下字段:role:消息的角色,可以是system、user或assistantcontent:消息的内容images(可选):要包含在消息中的图像列表(用于多模态模型,如llava)高级参数(可选):format:返回响应的格式。当前唯一接受的值是jsonoptions:在Modelfile文档中列出的额外模型参数,如temperaturestream:如果为false,则响应将作为单个响应对象返回,而不是对象流keep_alive:控制模型在请求后保持加载到内存中的时间(默认值:5m)'''# Ollama 本地 API 接口url = 'http://localhost:11434/api/chat'# 定义请求的数据data = {"model": model,"messages": messages,"stream": False}# 发送 POST 请求response = requests.post(url, json=data)response_data = response.json()return response_data['message']['content']# 创建目录(如果没有本地目录文件,需要创建一份)
def create_categories():categories_dict = {'Billing': ['Unsubscribe or upgrade','Add a payment method','Explanation for charge','Dispute a charge'],'Technical Support':['General troubleshooting','Device compatibility','Software updates'],'Account Management':['Password reset','Update personal information','Close account','Account security'],'General Inquiry':['Product information','Pricing','Feedback','Speak to a human']}with open(categories_file, 'w') as file:json.dump(categories_dict, file)return categories_dict# 获取目录数据
def get_categories():with open(categories_file, 'r') as file:categories = json.load(file)return categories# 获取商品列表
def get_product_list():products = get_products()product_list = []for product in products.keys():product_list.append(product)return product_list# 获取商品和目录
def get_products_and_category():# 获取所有商品products = get_products()# 使用 defaultdict初始化一个空字典,用于将商品分组到目录中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():with open(products_file, 'r') as file:products = json.load(file)return products# 从用户问题中抽取商品和类别
def find_category_and_product(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': <必须是下面的允许产品列表中找到的产品列表>其中类别和产品必须在客户服务查询中找到。如果提到了产品,则必须将其与允许产品列表中的正确类别关联。如果未找到任何产品或类别,则输出一个空列表。除了列表外,不要输出其他任何信息!允许的产品以JSON格式提供。每个项的键表示类别。每个项的值是该类别中的产品列表。允许的产品:{products_and_category}"""messages =  [  {'role':'system', 'content': system_message},    {'role':'user', 'content': f"{delimiter}{user_input}{delimiter}"},  ] return get_gemma_response(messages)# 相比上一个函数,可获取的商品直接在 template 中限定
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 UltrabookBlueWave Gaming LaptopPowerLite ConvertibleTechPro DesktopBlueWave ChromebookSmartphones and Accessories category:SmartX ProPhoneMobiTech PowerCaseSmartX MiniPhoneMobiTech Wireless ChargerSmartX EarBudsTelevisions and Home Theater Systems category:CineView 4K TVSoundMax Home TheaterCineView 8K TVSoundMax SoundbarCineView OLED TVGaming Consoles and Accessories category:GameSphere XProGamer ControllerGameSphere YProGamer Racing WheelGameSphere VR HeadsetAudio Equipment category:AudioPhonic Noise-Canceling HeadphonesWaveSound Bluetooth SpeakerAudioPhonic True Wireless EarbudsWaveSound SoundbarAudioPhonic TurntableCameras and Camcorders category:FotoSnap DSLR CameraActionCam 4KFotoSnap Mirrorless CameraZoomMaster CamcorderFotoSnap Instant Camera只输出对象列表,不包含其他内容。"""messages =  [  {'role':'system', 'content': system_message},    {'role':'user', 'content': f"{delimiter}{user_input}{delimiter}"},  ] return get_gemma_response(messages)# 从问题中抽取商品
def get_products_from_query(user_msg):products_and_category = get_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': <必须是下面的允许产品列表中找到的产品列表>类别和产品必须在客户服务查询中找到。如果提到了某个产品,它必须与允许产品列表中的正确类别关联。如果未找到任何产品或类别,则输出一个空列表。除了列表外,不要输出其他任何信息!允许的产品以JSON格式提供。每个项目的键表示类别。每个项目的值是该类别中的产品列表。以下是允许的产品:{products_and_category}"""messages =  [  {'role':'system', 'content': system_message},    {'role':'user', 'content': f"{delimiter}{user_msg}{delimiter}"},  ] category_and_product_response = get_gemma_response(messages)return category_and_product_response# 商品信息的搜索
def get_product_by_name(name):products = get_products()return products.get(name, None)def get_products_by_category(category):products = get_products()return [product for product in products.values() if product["category"] == category]# 获取提及的商品信息
def get_mentioned_product_info(data_list):# 初始化一个空列表来保存商品信息product_info_l = []# 如果输入列表为空,则返回空列表if data_list is None:return product_info_l# 遍历数据列表for data in data_list:try:# 检查数据中是否存在 "products" 字段,如果是则获取商品列表if "products" in data:products_list = data["products"]# 遍历商品列表for product_name in products_list:# 获取商品信息,并将其添加到商品信息列表中product = get_product_by_name(product_name)if product:product_info_l.append(product)# 如果商品未找到,则打印错误信息else:print(f"错误: 商品 '{product_name}' 未找到")# 检查数据中是否存在 "category" 字段,如果是则获取该类别的商品信息elif "category" in data:category_name = data["category"]# 获取该类别的商品信息category_products = get_products_by_category(category_name)# 遍历商品信息列表并将其添加到商品信息列表中for product in category_products:product_info_l.append(product)# 如果数据格式非法,则打印错误信息else:print("错误:非法的商品格式")# 捕获任何异常并打印错误信息except Exception as e:print(f"Error: {e}")# 返回商品信息列表return product_info_ldef 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(input_string)print("错误:非法的 Json 格式")return None# 生成输出字符串
def generate_output_string(data_list):"""从数据列表中生成输出字符串。参数:data_list:包含商品或目录信息的列表。返回:输出字符串。"""output_string = ""# 如果数据列表为空,则返回空字符串。if data_list is None:return output_string# 遍历数据列表。for data in data_list:try:# 检查数据是否包含 "products" 或 "category" 字段。if "products" in data:# 获取产品列表。products_list = data["products"]# 遍历产品列表。for product_name in products_list:# 获取商品信息。product = get_product_by_name(product_name)# 如果商品存在,则将其转换为 JSON 字符串并添加到输出字符串中。if product:output_string += json.dumps(product, indent=4) + "\n"else:# 如果商品不存在,则打印错误信息。print(f"错误: 商品 '{product_name}' 没有找到")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) + "\n"else:# 打印错误信息。print("错误:非法的商品格式")except Exception as e:# 打印错误信息。print(f"Error: {e}")# 返回输出字符串。return output_string# Example usage:
#product_information_for_user_message_1 = generate_output_string(category_and_product_list)
#print(product_information_for_user_message_1)
# 回答用户问题
def answer_user_msg(user_msg,product_info):"""代码参见第五节课"""delimiter = "####"system_message = f"""您是一家大型电子商店的客户服务助理。\请用友好和乐于助人的口吻回答问题,提供简洁明了的答案。\确保向用户提出相关的后续问题。"""# user_msg = f"""# tell me about the smartx pro phone and the fotosnap camera, the dslr one. Also what tell me about your tvs"""messages =  [  {'role':'system', 'content': system_message},   {'role':'user', 'content': f"{delimiter}{user_msg}{delimiter}"},  {'role':'assistant', 'content': f"相关产品信息:\n{product_info}"},   ] response = get_gemma_response(messages)return response# 创建并存入商品数据
def create_products():# product information# fun fact: all these products are fake and were generated by a language modelproducts = {"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}}products_file = 'products.json'with open(products_file, 'w') as file:json.dump(products, file)return products'''
注意:限于模型对中文理解能力较弱,中文 Prompt 可能会随机出现不成功,可以多次运行;也非常欢迎同学探究更稳定的中文 Prompt
'''
def process_user_message_ch(user_input, all_messages, debug=True):"""对用户信息进行预处理参数:user_input : 用户输入all_messages : 历史信息debug : 是否开启 DEBUG 模式,默认开启"""# 分隔符delimiter = "```"# 第一步:抽取出商品和对应的目录,类似于之前课程中的方法,做了一个封装category_and_product_response = find_category_and_product_only(user_input, get_products_and_category())# 将抽取出来的字符串转化为列表category_and_product_list = read_string_to_list(category_and_product_response)# print(category_and_product_list)if debug: print("第一步:抽取出商品列表")# 第三步:查找商品对应信息product_information = generate_output_string(category_and_product_list)if debug: print("第二步:查找抽取出的商品信息")
#     print('商品信息:',product_information)# 第四步:根据信息生成回答system_message = f"""您是一家西瓜电器的客户服务助理。\请以友好和乐于助人的语气回答问题,并提供简洁明了的答案。\当用户向你咨询时,请友好的告诉用户你所知道的产品信息。\引导用户购买,我们的产品。\检查你的回答,如果你的回答不是中文简体,请把它翻译为中文简体。\请确保向用户提出相关的后续问题。"""# 插入 messagemessages = [{'role': 'system', 'content': system_message},{'role': 'user', 'content': f"{delimiter}{user_input}{delimiter}"},{'role': 'assistant', 'content': f"相关商品信息:\n{product_information}"}]# 获取 LLM 的回答# 通过附加 all_messages 实现多轮对话final_response = get_gemma_response(all_messages + messages)
#     print(messages)
#     print(final_response)if debug:print("第三步:生成用户回答")# 将该轮信息加入到历史信息中all_messages = all_messages + messages[1:]# 第四步:模型检查是否很好地回答了用户问题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_gemma_response(messages)# print(evaluation_response)if debug: print("第六步:模型评估该回答")# 第七步:如果评估为 Y,输出回答;如果评估为 N,反馈将由人工修正答案# 使用 in 来避免模型可能生成 Yesif "Y" in evaluation_response:  if debug: print("第七步:模型赞同了该回答.")return final_response, all_messageselse:if debug: print("第七步:模型不赞成该回答.")neg_str = "很抱歉,我无法提供您所需的信息。我将为您转接到一位人工客服代表以获取进一步帮助。"return neg_str, all_messagesuser_input = "请告诉我关于 smartx pro phone 和 the fotosnap camera 的信息。另外,请告诉我关于你们的tvs的情况。"
response,_ = process_user_message_ch(user_input,[],debug=False)
print(response)

输出结果:

####
Customer Service Query: 
####
category: Smartphones and Accessories
products: ["SmartX ProPhone", "MobiTech PowerCase"]category: Cameras and Camcorders
products: ["FotoSnap DSLR Camera", "ActionCam 4K"]category: Televisions and Home Theater Systems
products: ["CineView 4K TV", "SoundMax Home Theater"]
####
错误:非法的 Json 格式
1. SmartX Pro Phone:SmartX Pro Phone 是我们公司最新推出的智能手机产品。它具有高级的照相摄像技术、高速数据处理和智能化功能等特点。这款手机适用于日常生活和工作,能够满足您的多样需求。2. FotoSnap Camera:FotoSnap Camera 是我们的新一代相机产品。它拥有超高像素、高感光度和快速拍摄速度等特点。相机可以拍摄高清照片、视频,且具有智能化功能,可以自动调整拍摄参数、修复照相结果等功能。3. TV:关于我们公司的电视产品,我可以告诉您,我们提供了一系列的液晶电视、LED电视和OLED电视等多样化的选择。我们的电视具备高清显示、高对比度和智能化功能等特点,可以满足您的家庭娱乐需求。额外信息:
您是否想了解更多关于 SmartX Pro Phone 和 FotoSnap Camera 的详细信息?或者您也想了解关于我们公司的电视产品有什么优缺点?请随时提出您的问题,我们将尽快为您解答!

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

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

相关文章

ABAP 数据写入Excel 并保存

参考老白 https://www.cnblogs.com/liaojunbo/archive/2011/09/06/2168552.html 但是缺zcl_excel 。需要从 dotabap要引入abap2xlsx 英文版进入后 尝试了一下 1&#xff09;列的宽度自适应么有找到在哪里&#xff1f; 列宽设置 lo_worksheet->set_column_width( ip_co…

nuxt3使用记录六:禁用莫名其妙的Tailwind CSS(html文件大大减小)

发现这个问题是因为&#xff0c;今天我突然很好奇&#xff0c;我发现之前构建的自动产生的200.html和404.html足足290k&#xff0c;怎么这么大呢&#xff1f;不是很占用我带宽&#xff1f; 一个啥东西都没有的静态页面&#xff0c;凭啥这么大&#xff01;所以我就想着手动把他…

OpenWRT有线桥接部署教程

前言 之前咱们讲到OpenWRT部署WAN实现PPPoE拨号上网和自动获取IP模式上网的办法&#xff1a; OpenWRT设置PPPoE拨号教程 OpenWRT设置自动获取IP&#xff0c;作为二级路由器 这一次&#xff0c;咱们尝试用OpenWRT有线桥接上一级路由器的教程。 可能有小伙伴敏锐地发现了&am…

【软件工程】详细设计

目录 前言详细设计算法设计工具——判定表 前言 软件工程生命周期分为八个阶段&#xff1a; 问题定义—>可行性研究—>需求分析 —>概要设计—>详细设计—>编码与单元测试 —>综合测试—>软件维护 这节我们讲的是软件开发流程中的一个阶段&#xff0c;需求…

Docker-Compose编排LNMP并部署WordPress

前言 随着云计算和容器化技术的快速发展&#xff0c;使用 Docker Compose 编排 LNMP 环境已经成为快速部署 Web 应用程序的一种流行方式。LNMP 环境由 Linux、Nginx、MySQL 和 PHP 组成&#xff0c;为运行 Web 应用提供了稳定的基础。本文将介绍如何通过 Docker Compose 编排 …

C语言 | Leetcode C语言题解之第66题加一

题目&#xff1a; 题解&#xff1a; /*** Note: The returned array must be malloced, assume caller calls free().*/ int* plusOne(int* digits, int digitsSize, int* returnSize){for(int i digitsSize - 1; i > 0; --i){digits[i] digits[i] 1;//最后元素1判断是不…

ssh远程访问windows系统下的jupyterlab

网上配置这一堆那一堆&#xff0c;特别乱&#xff0c;找了好久整理后发在这里 由于既想打游戏又想做深度学习&#xff0c;不舍得显卡性能白白消耗&#xff0c;这里尝试使用笔记本连接主机 OpenSSH 最初是为 Linux 系统开发的&#xff0c;现在也支持包括 Windows 和 macOS 在内…

开源博客项目Blog .NET Core源码学习(20:App.Hosting项目结构分析-8)

本文学习并分析App.Hosting项目中后台管理页面的个人资料页面、修改密码页面。 个人资料页面 个人资料页面用于显示和编辑个人信息&#xff0c;支持从本地上传个人头像。整个页面使用了layui中的表单、日期与时间选择、上传等样式或模块&#xff0c;通过layui.css文件设置样式…

Rust Web开发实战:打造高效稳定的服务端应用

Rust Web开发实战&#xff1a;打造高效稳定的服务端应用 本书将带领您从零开始构建Web应用程序&#xff0c;无论是API、微服务还是单体应用&#xff0c;都将一一涵盖。您将学到如何优雅地对外开放API&#xff0c;如何连接数据库以安全存储数据&#xff0c;以及如何对应用程序进…

MLP实现fashion_mnist数据集分类(2)-函数式API构建模型(tensorflow)

使用函数式API构建模型&#xff0c;使得模型可以处理多输入多输出。 1、查看tensorflow版本 import tensorflow as tfprint(Tensorflow Version:{}.format(tf.__version__)) print(tf.config.list_physical_devices())2、fashion_mnist数据集分类模型 2.1 使用Sequential构建…

用git上传本地文件到github

两种方式&#xff1a;都需要git软件&#xff08;1&#xff09;VScode上传 &#xff08;2&#xff09;直接命令行&#xff0c;后者不需要VScode软件 &#xff08;1&#xff09;vscode 上传非常方便&#xff0c;前提是下载好了vscode和git软件 1 在项目空白处右击&#xff0c;弹…

深度学习:基于TensorFlow、Keras,使用长短期记忆神经网络模型(LSTM)对Microsoft股票进行预测分析

前言 系列专栏&#xff1a;机器学习&#xff1a;高级应用与实践【项目实战100】【2024】✨︎ 在本专栏中不仅包含一些适合初学者的最新机器学习项目&#xff0c;每个项目都处理一组不同的问题&#xff0c;包括监督和无监督学习、分类、回归和聚类&#xff0c;而且涉及创建深度学…

【Redis面试题】Redis常见的一些高频面试题

分享几个Redis入门级常见面试过程中遇到的题目! 你项目中哪里使用到了redis?可以讲一讲嘛 这个题目无论是大公司还是小公司都经常考,建议大家根据自己的项目做总结 redis的几种基础数据结构 redis为什么那么快&#xff1f; 1.基于内存实现&#xff1a;我们都知道内存读写是…

如何在postman上提交文件格式的数据

如何在postman上提交文件格式的数据 今天在写一个文件上传的功能接口时&#xff0c;想用postman进行提交&#xff0c;花了些时间才找到在postman提交文件格式的数据。记录一下吧&#xff01; 1.打开postman&#xff0c;选择POST提交方式&#xff0c;然后在Params那一行的Head…

protobuf在配置文件管理上的应用

TextFormat::ParseFromString 是 Google Protocol Buffers&#xff08;通常简称为 Protobuf&#xff09;库中的一个函数&#xff0c;用于从文本格式解析消息。Protobuf 是一种用于序列化结构化数据的库&#xff0c;它允许你定义数据的结构&#xff0c;然后自动生成源代码来处理…

可视化大屏应用场景:智慧安防,保驾护航

hello&#xff0c;我是大千UI工场&#xff0c;本篇分享智慧安防的大屏设计&#xff0c;关注我们&#xff0c;学习N多UI干货&#xff0c;有设计需求&#xff0c;我们也可以接单。 实时监控与预警 可视化大屏可以将安防系统中的监控画面、报警信息、传感器数据等实时展示在大屏上…

【C++】学习笔记——string_5

文章目录 六、string类7. string类的模拟实现8. string类的模拟实现的完整代码string.h头文件test.c源文件 9. string收尾写时拷贝 未完待续 六、string类 7. string类的模拟实现 我们之前讲了实现 insert &#xff0c;但是那个插入函数仅仅是在 pos 位置插入一个字符而且&am…

SpirngBoot整合快递100

目录 一、注册快递100 二、技术文档地址 三、需要认证的key和comcumer 四、spring boot 整合快递 100使用 4.1 引入快递100和hutool的依赖 4.2 将key和comcumer写入application.properties文件中 4.3 新建一个modle,用于将查出来的json数据转成对象 4.4 新建一个controll…

网络安全实训Day16

网络空间安全实训-渗透测试 漏洞扫描 定义 扫描和探测目标范围内的主机存在哪些安全漏洞&#xff0c;或扫描目标范围内的那些主机存在某个指定的漏洞 漏扫工具 AWVS APPScan MSF 使用MSF扫描漏洞并利用 1.搜索需要的攻击模块 search ms17-010 2.使用攻击模块 use 模块名称…

Python 植物大战僵尸

文章目录 效果图项目结构实现思路源代码 效果图 项目结构 实现思路 下面是代码的实现思路&#xff1a; 导入必要的库和模块&#xff1a;首先&#xff0c;我们导入了Python的os、time库以及pygame库&#xff0c;还有植物大战僵尸游戏中用到的各个植物和僵尸的类。 初始化游戏和…