今天我学习了DeepLearning.AI的 Building Systems with LLM 的在线课程,我想和大家一起分享一下该门课程的一些主要内容。
下面是我们访问大型语言模(LLM)的主要代码:
import openai#您的openai的api key
openai.api_key ='YOUR-OPENAI-API-KEY' 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"]
实现具有多个提示的复杂任务
我们在之前的博客:"使用大型语言模(LLM)构建系统(三):思维链推理"中介绍了如何教会了大型语言模型LLM如何进行逻辑推理,今天我们来学习另外一种应对复杂任务的技巧:多个提示协同工作来完成复杂任务。下面我们来看一个例子,在这个例子中我们要LLM担任一家电子产品公司的客服,LLM的主要任务是接受客户对公司相关产品的提问,LLM需要根据公司产品的相关信息给客户解答关于产品的各种问题。对于公司的产品信息它由两部分组成:
- 产品目录清单
- 产品信息清单
这里我们可以将它们理解为从数据库中导出来的两个主从结构的表即产品目录表和产品信息表。当客户提出问题时,我们首先让LLM从 产品目录清单 中查询是否存在相关产品:
delimiter = "####"
system_message = f"""
You will be provided with customer service queries. \
The customer service query will be delimited with \
{delimiter} characters.
Output a python list of objects, where each object has \
the following format:'category': <one of Computers and Laptops, \Smartphones and Accessories, \Televisions and Home Theater Systems, \Gaming Consoles and Accessories, Audio Equipment, Cameras and Camcorders>,
OR'products': <a list of products that must \be found in the allowed products below>Where the categories and products must be found in \
the customer service query.
If a product is mentioned, it must be associated with \
the correct category in the allowed products list below.
If no products or categories are found, output an \
empty list.Allowed products: Computers and Laptops category:
TechPro Ultrabook
BlueWave Gaming Laptop
PowerLite Convertible
TechPro Desktop
BlueWave ChromebookSmartphones and Accessories category:
SmartX ProPhone
MobiTech PowerCase
SmartX MiniPhone
MobiTech Wireless Charger
SmartX EarBudsTelevisions and Home Theater Systems category:
CineView 4K TV
SoundMax Home Theater
CineView 8K TV
SoundMax Soundbar
CineView OLED TVGaming Consoles and Accessories category:
GameSphere X
ProGamer Controller
GameSphere Y
ProGamer Racing Wheel
GameSphere VR HeadsetAudio Equipment category:
AudioPhonic Noise-Canceling Headphones
WaveSound Bluetooth Speaker
AudioPhonic True Wireless Earbuds
WaveSound Soundbar
AudioPhonic TurntableCameras and Camcorders category:
FotoSnap DSLR Camera
ActionCam 4K
FotoSnap Mirrorless Camera
ZoomMaster Camcorder
FotoSnap Instant CameraOnly output the list of objects, with nothing else.
"""
下面我们将system_message翻译成中文以便大家能更好的理解。
delimiter = "####"
system_message = f"""
您将收到客户服务查询。
客户服务查询将以 {delimiter} 字符分隔。
输出一个python对象列表,其中每个对象具有以下格式:
'类别':<电脑和笔记本电脑、智能手机和配件、
电视和家庭影院系统、游戏机和配件、音响设备、相机和摄像机>,
或者
'products':<必须在下面允许的产品中找到的产品列表>其中类目和产品必须在客服查询中找到。
如果某个产品被提及,则它必须与下面允许的产品列表中的正确类别相关联。
如果未找到产品或类别,则输出一个空列表。允许的产品:电脑和笔记本电脑类别:
TechPro超极本
BlueWave 游戏笔记本电脑
PowerLite 投影仪
泰宝 桌面电脑
蓝波 Chromebook智能手机及配件类别:
SmartX ProPhone
MobiTech 电源箱
SmartX 迷你手机
MobiTech 无线充电器
SmartX 耳塞电视和家庭影院系统类别:
CineView 4K 电视
SoundMax 家庭影院
CineView 8K 电视
SoundMax 条形音箱
CineView OLED 电视游戏机及配件类别:
游戏圈X
ProGamer 控制器
GameSphere Y
ProGamer 赛车方向盘
GameSphere VR 耳机音响器材类:
AudioPhonic 降噪耳机
WaveSound 蓝牙音箱
AudioPhonic 真无线耳塞
WaveSound 条形音箱
AudioPhonic 转盘相机和摄像机类别:
FotoSnap 数码单反相机
酷拍4K
FotoSnap 无反相机
ZoomMaster 摄像机
FotoSnap 拍立得相机只输出对象列表,不输出其他内容。
"""
这里我们要求LLM根据用户的提问输出一个包含相关产品分类和产品名称的python list对象。如果没有找到用户问题中的产品,则输出一个空列表,接下来我们看看用户的提问:
user_message_1 = f"""tell me about the smartx pro phone and \the fotosnap camera, the dslr one. \Also tell me about your tvs """
messages = [
{'role':'system', 'content': system_message},
{'role':'user', 'content': f"{delimiter}{user_message_1}{delimiter}"},
]
category_and_product_response_1 = get_completion_from_messages(messages)
print(category_and_product_response_1)
这里用户询问了2个具体的产品和一个产品的分类,因此LLM根据system_message的要求返回了两个产品(包含分类信息)和一个分类信息。这完全符合我们对LLM的要求。接下来,用户提了一个不存在的产品的问题:
user_message_2 = f"""
my router isn't working"""
messages = [
{'role':'system','content': system_message},
{'role':'user','content': f"{delimiter}{user_message_2}{delimiter}"},
]
response = get_completion_from_messages(messages)
print(response)
这里客户询问了有个router的问题,但是router并不在我们的产品目录清单中,因此LLM返回了一个空的列表,这也是正确的。
检索提取的产品和类别的详细产品信息
前面我们在system_message中定义了产品的目录信息,接下来我们需要定义具体的产品信息,同样产品信息中也包含了该产品所属的类别:
# product information
products = {"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}
}
接下来我们还需要定义两个查询产品和分类的函数,这样便于从产品信息根据名称和分类来抽取相关的产品。
def get_product_by_name(name):return products.get(name, None)def get_products_by_category(category):return [product for product in products.values() if product["category"] == category]print(get_product_by_name("TechPro Ultrabook"))
print(get_products_by_category("Computers and Laptops"))
将 Python 字符串转换成 Python 字典列表
之前LL接收用户查询信息后,LLM从产品目录清单中返回的相关的产品目录信息,但是LLM返回的消息都是字符串,为了要更进一步查询产品的信息,我们需要将LLM返回的字符串转换成功程序能读懂的python的list,这样才能实现下一步的产品信息查询。
import json def read_string_to_list(input_string):if input_string is None:return Nonetry:# Replace single quotes with double quotes for valid JSONinput_string = input_string.replace("'", "\"") data = json.loads(input_string)return dataexcept json.JSONDecodeError:print("Error: Invalid JSON string")return None category_and_product_list = read_string_to_list(category_and_product_response_1)category_and_product_list
此时我们就将先前的字符串category_and_product_response_1 转换成了python的list 。
检索相关产品和类别的详细产品信息
下面我们需要定义一个查询产品详细信息的函数generate_output_string,该函数接受类型为list的产品目录信息的参数data_list。该参数为之前LLM返回的用户对产品的查询结果并将其转换成了list。
def generate_output_string(data_list):output_string = ""if data_list is None:return output_stringfor data in data_list:try:if "products" in data:products_list = data["products"]for product_name in products_list:product = get_product_by_name(product_name)if product:output_string += json.dumps(product, indent=4) + "\n"else:print(f"Error: Product '{product_name}' not found")elif "category" in data:category_name = data["category"]category_products = get_products_by_category(category_name)for product in category_products:output_string += json.dumps(product, indent=4) + "\n"else:print("Error: Invalid object format")except Exception as e:print(f"Error: {e}")return output_string product_information_for_user_message_1 = generate_output_string(category_and_product_list)
print(product_information_for_user_message_1)
由于篇幅的原因,上面的结果为查询出来的部分产品信息。
根据详细的产品信息生成用户最终查询结果
最后我们将查询出来的具体产品信息回复给客户,并询问客户是否还有进一步的问题:
system_message = f"""
You are a customer service assistant for a \
large electronic store. \
Respond in a friendly and helpful tone, \
with very concise answers. \
Make sure to ask the user relevant follow up questions.
"""
user_message_1 = f"""
tell me about the smartx pro phone and \
the fotosnap camera, the dslr one. \
Also tell me about your tvs"""
messages = [
{'role':'system','content': system_message},
{'role':'user','content': user_message_1},
{'role':'assistant','content': f"""Relevant product information:\n\{product_information_for_user_message_1}"""},
]
final_response = get_completion_from_messages(messages)
print(final_response)
最后的思考
这里我们将客户的查询拆分成了两个步骤,在第一个步骤中我们先让LLM接受了客户的查询后返回产品目录信息,然后我们根据产品目录信息查询出具体的产品信息。第二步我们再将产品信息喂给了LLM后生成了最终的给用户的回复信息,我们为什么要这么做?为什么不把两步并成一步,一次性将所有的产品信息喂给LLM,然后生成回复信息?其实,这里涉及到一个 token数量的问题,要知道LLM不是免费的,它是根据token数量来收费的,因为我们的产品信息清单中包含了大量的产品数据,产品数量越多耗费的token数量也会越多,使用LLM的成本约会越来越高,为了降低使用成本,我们设计出了这种精巧的结构,通过两个步骤的查询,使得喂给LLM的token数量减少到最低,这样大大节省了我们使用LLM的成本。
总结
今天我们学习了如何通过使用多个提示语来让LLM完成一个复杂的任务,以及如何节约使用LLM的成本,为了节省LLM的使用成本,我们不能把所有的产品信息喂给LLM,而是通过2次查询来获取少量的产品信息,这样有效的减少了token数量。
参考资料
DLAI - Learning Platform Beta