使用大型语言模(LLM)构建系统(三):思维链推理

今天我学习了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"]

连锁思维提示(Chain-of-Thought Prompting)

当人类遇到一些复杂的逻辑问题时,我们会有一套逻辑推理的思维逻辑,同样我们也希望ChatGPT也能像人类那样具备基本的逻辑推理能力,为了达到这个目的,我们有时候需要教导ChatGPT按照人类的思维方式来处理某些较为复杂的逻辑问题,我们来看下面一个例子,在这个例子中我们有5个产品,我们要让ChatGPT来充当客服来回答客户提出的各种关于特定产品的问题。

delimiter = "####"
system_message = f"""
Follow these steps to answer the customer queries.
The customer query will be delimited with four hashtags,\
i.e. {delimiter}. 

Step 1:{delimiter} First decide whether the user is \
asking a question about a specific product or products. \
Product cateogry doesn't count. 

Step 2:{delimiter} If the user is asking about \
specific products, identify whether \
the products are in the following list.
All available products: 
1. Product: 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

2. Product: 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

3. Product: 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

4. Product: 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

5. Product: 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

Step 3:{delimiter} If the message contains products \
in the list above, list any assumptions that the \
user is making in their \
message e.g. that Laptop X is bigger than \
Laptop Y, or that Laptop Z has a 2 year warranty.

Step 4:{delimiter}: If the user made any assumptions, \
figure out whether the assumption is true based on your \
product information. 

Step 5:{delimiter}: First, politely correct the \
customer's incorrect assumptions if applicable. \
Only mention or reference products in the list of \
5 available products, as these are the only 5 \
products that the store sells. \
Answer the customer in a friendly tone.

Use the following format:
Step 1:{delimiter} <step 1 reasoning>
Step 2:{delimiter} <step 2 reasoning>
Step 3:{delimiter} <step 3 reasoning>
Step 4:{delimiter} <step 4 reasoning>
Response to user:{delimiter} <response to customer>

Make sure to include {delimiter} to separate every step.
"""

下面我们将system_message翻译成中文,以便大家能更好的理解。

delimiter = "####"
system_message = f"""
按照以下步骤回答客户查询。
客户查询将用四个主题标签分隔,即 {delimiter}。

步骤1:{delimiter} 首先判断用户问的是关于一个或多个特定产品的问题。 产品类别不算数。

步骤2:{delimiter} 如果用户询问的是特定产品,请确定产品是否在以下列表中。
所有可用产品:
1.产品:TechPro超极本
类别:电脑和笔记本电脑
品牌: TechPro
型号:TP-UB100
质保:1年
评分:4.5
特点:13.3 英寸显示屏,8GB 内存,256GB 固态硬盘,英特尔酷睿 i5 处理器
描述:适合日常使用的时尚轻巧的超极本。
价格:799.99 美元

2.产品:BlueWave游戏笔记本电脑
类别:电脑和笔记本电脑
品牌:蓝波
型号:BW-GL200
质保:2年
评分:4.7
特点:15.6 英寸显示屏,16GB RAM,512GB SSD,NVIDIA GeForce RTX 3060
描述:高性能游戏笔记本电脑,带来身临其境的体验。
价格:1199.99 美元

3.产品:PowerLite敞篷车
类别:电脑和笔记本电脑
品牌:PowerLite
型号:PL-CV300
质保:1年
评分:4.3
特点:14 英寸触摸屏、8GB RAM、256GB SSD、360 度铰链
描述:一款带有灵敏触摸屏的多功能可转换笔记本电脑。
价格:699.99 美元

4. 产品:TechPro Desktop
类别:电脑和笔记本电脑
品牌: TechPro
型号:TP-DT500
质保:1年
评分:4.4
特点:英特尔酷睿 i7 处理器、16GB 内存、1TB 硬盘、NVIDIA GeForce GTX 1660
描述:一款功能强大的台式电脑,适合工作和娱乐。
价格:999.99 美元

5. 产品:BlueWave Chromebook
类别:电脑和笔记本电脑
品牌:蓝波
型号:BW-CB100
质保:1年
评分:4.1
特点:11.6 英寸显示屏、4GB 内存、32GB eMMC、Chrome 操作系统
描述:一款小巧实惠的 Chromebook,适合日常任务。
价格:249.99 美元

步骤3:{delimiter} 如果消息包含上面列表中的产品,请列出用户在消息中所做的任何假设,
例如 笔记本电脑 X 比笔记本电脑 Y 大,或者笔记本电脑 Z 有 2 年保修。

步骤4:{delimiter}:如果用户做出了任何假设,根据你的产品信息判断假设是否成立。

步骤5:{delimiter}:首先,如果适用,礼貌地纠正客户的错误假设。 
仅提及或参考 5 种可用产品列表中的产品,因为这是商店销售的仅有的 5 种产品。 
用友好的语气回答客户。

使用以下格式:
步骤1:{delimiter}
步骤2:{delimiter}
步骤3:{delimiter}
步骤4:{delimiter}
对用户的响应:{delimiter}

确保包括 {delimiter} 以分隔每个步骤。
"""

这里我们指导ChatGPT形成像人类那样逻辑推理的思维链,即在解决一个复杂问题时候我们需要有若干个步骤,其中每个步骤解决一个小问题,且各个步骤存在前后逻辑关系即只有完成了之前的步骤才能执行后面的步骤,当完成所有的步骤后就可以解决这个复杂问题。下面我们看看ChatGPT是用逻辑推理来解决用户的各种问题的。

user_message = f"""
by how much is the BlueWave Chromebook more expensive \
than the TechPro Desktop"""

messages =  [  
{'role':'system', 
 'content': system_message},    
{'role':'user', 
 'content': f"{delimiter}{user_message}{delimiter}"},  
] 

response = get_completion_from_messages(messages)
print(response)

 这里客户向ChatGPT提出的问题是:“BlueWave Chromebook 比 TechPro Desktop贵多少”。首先BlueWave Chromebook和TechPro Desktop都在我们的产品名单中,步骤1ChatGPT判断正确,在步骤2中ChatGPT列出了这两个商品对应的价格,这也符合我们对它的要求,在步骤3中,ChatGPT也安装我们对它的要求列举出了用户的假设,即:BlueWave Chromebook 比 TechPro Desktop 要贵。在步骤4中ChatGPT基于产品信息判断出用户的假设不正确,因为BlueWave Chromebook要比TechPro Desktop便宜而不是更贵。在最后回复客户时ChatGPT纠正了用户的错误的假设,并给出了两个产品的实际价格加以佐证。

下面我们将ChatGPT的回复翻译成中文:

 下面我们再询问一个产品问题,但它并不在目前的产品名单中:

user_message = f"""
do you sell tvs"""
messages =  [  
{'role':'system', 
 'content': system_message},    
{'role':'user', 
 'content': f"{delimiter}{user_message}{delimiter}"},  
] 
response = get_completion_from_messages(messages)
print(response)

 

 这里可以看到,当用户询问的商品不在名单中时,步骤3没有执行,当用户没有做假设时步骤4没有被执行,所以此时ChatGPT只执行了我们给他设计好的步骤1,步骤2,和步骤5,这完成正确。

格式化输出结果

由于我们要求 LLM 用分隔符分隔其推理步骤,但是对于用户来说他们并不需要知道ChatGPT的思维链的推理过程,因此我们需要在最终的输出结果中隐藏掉步骤1至步骤4的消息:

try:
    final_response = response.split(delimiter)[-1].strip()
except Exception as e:
    final_response = "Sorry, I'm having trouble right now, please try asking another question."
    
print(final_response)

 总结

 今天我们学习了如何指导ChatGPT像人类那样形成逻辑推理的思维逻辑,我们让ChatGPT将解决复杂问题的过程拆分成若干个步骤,每个步骤解决一个小问题,且各个步骤存在前后逻辑关系即只有完成了之前的步骤才能执行后面的步骤,当完成了所有的步骤后就可以解决这个复杂问题。最后我们还学习了如何来隐藏ChatGPT的逻辑推理过程。

参考资料

DLAI - Learning Platform Beta

猜你喜欢

转载自blog.csdn.net/weixin_42608414/article/details/131056676