BigData

ChatGPT의 입력 처리 - Inputs: Chaining Prompts

IT오이시이 2023. 10. 6. 10:01
728x90

Chain of Thought Reasoning에 대한 내용과 영어로 작성된 사고사슬(SoT)로도 한국어로 답변할 수 있도록  예시를 작성해 보았습니다.

 

Inputs: Chaining Prompts 

"Chain of Thought Reasoning" 은 입력을 받아 중간에 일련의 추론 단계를 통해 복잡한 추론 기능을 가능하게하는 작업입니다.

대규모 언어모델(LLM)은 산술 문제나 상식을 추론하는 능력이 떨어지는 경우 모델이 잘못된 오류에 도달할 수 있습니다. 모델이 최종 답변을 제공하기 전에 관련 추론 단계로 풀이과정을 추가 요청하여 모델이 문제에 대해 더 생각 할 수 있도록 하는 방법입니다.

아래 그림과 같이 일반적인 단답형의 Standard Prompting 은 오류를 direct로 떨어 뜨리지만 오른쪽의 Chain-of-Thoutht Prompting은 중간에 추론 과정으로 추가하여 문제를 푸는 과정에서 올바른 정답지에 이르도록 하여 정확성을 높이는 방법입니다.

논문  :  'Chain-of-Thought Prompting Elicits Reasoning in Large Language Models'라는(논문링크)

 


Chain of Thought Reasoning 을 이용한  예시

 

import openai
openai.api_key = "{YOUR_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 Reasoning  작성

다음과 같이 입력에 대한 다양한 경우에 대한 구문을 작성하여 제공 합니다.

영어로 작성된 Chain에 아래와 같이 답변을 한국어로 하라는 것을 추가하면 답변이 한글로 출력 됩니다.

[ 한국어 출력 조건 ]
remember that your response to the user must be in korean:

 

 

delimiter = "####"
system_message = f"""
remember that your response to the user must be in korean: 

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.
"""

 

[질문1] 상품간의 가격 을 비교하는 질문

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)

 

[영어 답변]


Step 1:#### The user is asking about the price difference between the BlueWave Chromebook and the TechPro Desktop.

Step 2:#### Both the BlueWave Chromebook and the TechPro Desktop are available products.

Step 3:#### The user assumes that the BlueWave Chromebook is more expensive than the TechPro Desktop.

Step 4:#### The assumption made by the user is incorrect. The TechPro Desktop is priced at $999.99, while the BlueWave Chromebook is priced at $249.99, making the TechPro Desktop more expensive. Response to user:#### The BlueWave Chromebook is actually cheaper than the TechPro Desktop.

The BlueWave Chromebook is priced at $249.99, while the TechPro Desktop is priced at $999.99.

 

[한국어 답변 만들기]

위와 같이 "system_message"  에 한국어로 답변하라는 규칙을 추가하면 아래와 같이 한글로 출력됩니다.

Step 1:#### 사용자가 특정 제품에 대해 질문하고 있는지 확인합니다.
Step 2:#### 사용자가 블루웨이브 크롬북과 테크프로 데스크톱에 대해 언급하고 있습니다.
Step 3:#### 사용자는 블루웨이브 크롬북이 테크프로 데스크톱보다 더 비쌌다고 가정하고 있습니다.
Step 4:#### 블루웨이브 크롬북과 테크프로 데스크톱의 가격을 비교하여 사용자의 가정이 맞는지 확인합니다. Response to user:#### 블루웨이브 크롬북은 $249.99이고, 테크프로 데스크톱은 $999.99입니다. 따라서, 블루웨이브 크롬북이 테크프로 데스크톱보다 $750 더 저렴합니다.

 

 

 


[질문2] 가장 가격이 싼 제품 찾기

 

user_message = f"""
어떤 제품이 가격이 싸나요?"""


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

response = get_completion_from_messages(messages)
print(response)

 

 

 


[질문3]  판매 상품이 있는지 확인 하는 질문 

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)

 

Step 1:#### The user is asking if the store sells TVs.
Step 2:#### The list of available products does not include any TVs. Response to user:####
죄송합니다, 저희 가게에서는 TV를 판매하지 않습니다.
다른 제품에 대해 궁금한 점이 있으시면 알려주세요.

 

 

 


Inner Monologue 를 이용한  추론 과정을 숨기고 간결한 출력 

- Inner Monologue 를 이용하여 추론 과정을 숨기는 전술로, 모델의 추론 과정을 사용자에게 숨기고 간결하게 출력이 가능합니다. 
  LLM에게 추론 단계를 구분자로 구분하도록 요청했기 때문에 사용자가 보는 최종 출력에서는 사고 과정을 숨길 수 있습니다.

다음 문장은 응답 결과 리스트의 가장 마지막 값을 출력 하는 것입니다. response.split(delimiter)[-1] (가장 마지막 값)

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)
죄송합니다, 저희 가게에서는 TV를 판매하지 않습니다. 다른 제품에 대해 궁금한 점이 있으시면 알려주세요

 

 

[참고]

https://learn.deeplearning.ai/chatgpt-building-system

728x90
반응형