Hướng dẫn kỹ thuật lời nhắc. Kỹ thuật viết lời nhắc. Mô hình Ngôn ngữ Hỗ trợ Chương trình (PAL)

Thứ bảy - 15/11/2025 06:09
Hướng dẫn kỹ thuật lời nhắc. Kỹ thuật viết lời nhắc. Mô hình Ngôn ngữ Hỗ trợ Chương trình (PAL)

PAL (Program-Aided Language Models)

Theo: https://www.promptingguide.ai/techniques/pal

Gao và cộng sự (2022) trình bày một phương pháp sử dụng LLM để đọc các bài toán ngôn ngữ tự nhiên và tạo ra các chương trình như các bước suy luận trung gian. Được đặt tên là mô hình ngôn ngữ hỗ trợ chương trình - PAL (Program-Aided Language Model), phương pháp này khác với phương pháp lời nhắc chuỗi tư duy (CoT) ở chỗ thay vì sử dụng văn bản dạng tự do để tìm ra lời giải, nó chuyển giao bước giải cho một chương trình thực thi như trình thông dịch Python.

Image Source: Gao et al., (2022)

Hãy xem xét một ví dụ sử dụng LangChain và OpenAI GPT-3. Chúng tôi quan tâm đến việc phát triển một ứng dụng đơn giản có khả năng diễn giải câu hỏi được hỏi và đưa ra câu trả lời bằng cách tận dụng trình thông dịch Python.

Cụ thể, chúng tôi quan tâm đến việc tạo ra một chức năng cho phép sử dụng LLM để trả lời các câu hỏi yêu cầu hiểu biết về dữ liệu. Chúng tôi sẽ cung cấp cho LLM một lời nhắc bao gồm một vài ví dụ được áp dụng từ đây.

Đây là những nội dung chúng tôi cần:

import openai
from datetime import datetime
from dateutil.relativedelta import relativedelta
import os
from langchain.llms import OpenAI
from dotenv import load_dotenv

Trước tiên chúng ta hãy cấu hình một vài thứ:

load_dotenv()
# API configuration
openai.api_key = os.getenv("OPENAI_API_KEY")
# for LangChain
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")

Thiết lập phiên bản mô hình:

llm = OpenAI(model_name='text-davinci-003', temperature=0)

Thiết lập lời nhắc + câu hỏi:

câu hỏi = "Hôm nay là ngày 27 tháng 2 năm 2023. Tôi sinh ra cách đây đúng 25 năm. Vậy ngày sinh của tôi là bao nhiêu theo MM/DD/YYYY?"
DATE_UNDERSTANDING_PROMPT = """
# Hỏi: 36 giờ nữa là năm 2015. Vậy một tuần nữa tính từ hôm nay theo định dạng MM/DD/YYYY là ngày nào?
# Nếu 36 giờ nữa là năm 2015, thì hôm nay là 36 giờ trước đó.
today = datetime(2015, 1, 1) - relativedelta(hours=36) 
# Một tuần nữa tính từ hôm nay,
one_week_from_today = today + relativedelta(weeks=1)
# Câu trả lời được định dạng bằng %m/%d/%Y là
one_week_from_today.strftime('%m/%d/%Y') 
# Hỏi: Ngày đầu tiên của năm 2019 là Thứ Ba, và hôm nay là Thứ Hai đầu tiên của năm 2019. Vậy hôm nay là ngày nào theo định dạng MM/DD/YYYY?
# Nếu ngày đầu tiên của năm 2019 là Thứ Ba, và hôm nay là Thứ Hai đầu tiên của 2019, thì hôm nay là 6 ngày sau.
today = datetime(2019, 1, 1) + relativedelta(days=6)
# Câu trả lời được định dạng bằng %m/%d/%Y là today.strftime('%m/%d/%Y')
# Hỏi: Buổi hòa nhạc dự kiến diễn ra vào ngày 06/01/1943, nhưng bị hoãn lại một ngày so với hôm nay. 10 ngày trước là ngày nào theo định dạng MM/DD/YYYY?
# Nếu buổi hòa nhạc dự kiến diễn ra vào ngày 06/01/1943, nhưng bị hoãn lại một ngày so với hôm nay, thì hôm nay là một ngày sau.
today = datetime(1943, 6, 1) + relativedelta(days=1)
# 10 ngày trước,
ten_days_ago = today - relativedelta(days=10)
# Câu trả lời được định dạng bằng %m/%d/%Y là ten_days_ago.strftime('%m/%d/%Y')
# Hỏi: Hôm nay là ngày 19/4/1969. Vậy 24 giờ sau đó tính theo MM/DD/YYYY là ngày nào?
# Hôm nay là ngày 19/4/1969.
today = datetime(1969, 4, 19)
# 24 giờ sau,
later = today + relativedelta(hours=24)
# Câu trả lời được định dạng theo %m/%d/%Y là today.strftime('%m/%d/%Y')
# Hỏi: Jane nghĩ hôm nay là ngày 11/3/2002, nhưng thực tế hôm nay là ngày 12/3, tức là muộn hơn 1 ngày. Vậy 24 giờ sau đó tính theo MM/DD/YYYY là ngày nào?
# Nếu Jane nghĩ hôm nay là ngày 11/3/2002, nhưng thực tế hôm nay là ngày 12/3, thì hôm nay là 3/12/2002.
today = datetime(2002, 3, 12)
# 24 giờ sau,
later = today + relativedelta(hours=24)
# Câu trả lời được định dạng theo %m/%d/%Y là later.strftime('%m/%d/%Y')
# Hỏi: Jane sinh vào ngày cuối cùng của tháng 2 năm 2001. Hôm nay là sinh nhật 16 tuổi của cô ấy. Vậy ngày hôm qua là ngày nào theo định dạng MM/DD/YYYY?
# Nếu Jane sinh vào ngày cuối cùng của tháng 2 năm 2001 và hôm nay là sinh nhật 16 tuổi của cô ấy, thì hôm nay là 16 năm sau.
today = datetime(2001, 2, 28) + relativedelta(years=16)
# Hôm qua,
yesterday = today - relativedelta(days=1)
# Câu trả lời được định dạng theo %m/%d/%Y là ngày hôm qua.strftime('%m/%d/%Y')
# Q: {câu hỏi}
""".strip() + '\n'
llm_out = llm(DATE_UNDERSTANDING_PROMPT.format(question=question))
print(llm_out)

Điều này sẽ đưa ra kết quả sau:

# Nếu hôm nay là ngày 27 tháng 2 năm 2023 và tôi sinh ra đúng 25 năm trước, thì tôi đã sinh ra 25 năm trước đó.
today = datetime(2023, 2, 27)
# Tôi sinh ra 25 năm trước,
born = today - relativedelta(years=25)
# Câu trả lời được định dạng bằng %m/%d/%Y là born.strftime('%m/%d/%Y')


 

Nội dung của llm_out là một đoạn mã Python. Dưới đây, lệnh exec được sử dụng để thực thi đoạn mã Python này.

exec(llm_out)
print(born)

Điều này sẽ đưa ra kết quả sau: 27/02/1998

 

Về ‘Kỹ thuật viết lời nhắc’ ………. Phần trước ………. Phần tiếp theo

Gao et al., (2022) presents a method that uses LLMs to read natural language problems and generate programs as the intermediate reasoning steps. Coined, program-aided language models (PAL), it differs from chain-of-thought prompting in that instead of using free-form text to obtain solution it offloads the solution step to a programmatic runtime such as a Python interpreter.

Image Source: Gao et al., (2022)

Let's look at an example using LangChain and OpenAI GPT-3. We are interested to develop a simple application that's able to interpret the question being asked and provide an answer by leveraging the Python interpreter.

Specifically, we are interested to create a functionality that allows the use of the LLM to answer questions that require date understanding. We will provide the LLM a prompt that includes a few exemplars which are adopted from here.

These are the imports we need:

import openai
from datetime import datetime
from dateutil.relativedelta import relativedelta
import os
from langchain.llms import OpenAI
from dotenv import load_dotenv

Let's first configure a few things:

load_dotenv()
 
# API configuration
openai.api_key = os.getenv("OPENAI_API_KEY")
 
# for LangChain
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")

Setup model instance:

llm = OpenAI(model_name='text-davinci-003', temperature=0)

Setup prompt + question:

question = "Today is 27 February 2023. I was born exactly 25 years ago. What is the date I was born in MM/DD/YYYY?"
 
DATE_UNDERSTANDING_PROMPT = """
# Q: 2015 is coming in 36 hours. What is the date one week from today in MM/DD/YYYY?
# If 2015 is coming in 36 hours, then today is 36 hours before.
today = datetime(2015, 1, 1) - relativedelta(hours=36)
# One week from today,
one_week_from_today = today + relativedelta(weeks=1)
# The answer formatted with %m/%d/%Y is
one_week_from_today.strftime('%m/%d/%Y')
# Q: The first day of 2019 is a Tuesday, and today is the first Monday of 2019. What is the date today in MM/DD/YYYY?
# If the first day of 2019 is a Tuesday, and today is the first Monday of 2019, then today is 6 days later.
today = datetime(2019, 1, 1) + relativedelta(days=6)
# The answer formatted with %m/%d/%Y is
today.strftime('%m/%d/%Y')
# Q: The concert was scheduled to be on 06/01/1943, but was delayed by one day to today. What is the date 10 days ago in MM/DD/YYYY?
# If the concert was scheduled to be on 06/01/1943, but was delayed by one day to today, then today is one day later.
today = datetime(1943, 6, 1) + relativedelta(days=1)
# 10 days ago,
ten_days_ago = today - relativedelta(days=10)
# The answer formatted with %m/%d/%Y is
ten_days_ago.strftime('%m/%d/%Y')
# Q: It is 4/19/1969 today. What is the date 24 hours later in MM/DD/YYYY?
# It is 4/19/1969 today.
today = datetime(1969, 4, 19)
# 24 hours later,
later = today + relativedelta(hours=24)
# The answer formatted with %m/%d/%Y is
today.strftime('%m/%d/%Y')
# Q: Jane thought today is 3/11/2002, but today is in fact Mar 12, which is 1 day later. What is the date 24 hours later in MM/DD/YYYY?
# If Jane thought today is 3/11/2002, but today is in fact Mar 12, then today is 3/12/2002.
today = datetime(2002, 3, 12)
# 24 hours later,
later = today + relativedelta(hours=24)
# The answer formatted with %m/%d/%Y is
later.strftime('%m/%d/%Y')
# Q: Jane was born on the last day of Feburary in 2001. Today is her 16-year-old birthday. What is the date yesterday in MM/DD/YYYY?
# If Jane was born on the last day of Feburary in 2001 and today is her 16-year-old birthday, then today is 16 years later.
today = datetime(2001, 2, 28) + relativedelta(years=16)
# Yesterday,
yesterday = today - relativedelta(days=1)
# The answer formatted with %m/%d/%Y is
yesterday.strftime('%m/%d/%Y')
# Q: {question}
""".strip() + '\n'
llm_out = llm(DATE_UNDERSTANDING_PROMPT.format(question=question))
print(llm_out)

Gao et al., (2022) presents a method that uses LLMs to read natural language problems and generate programs as the intermediate reasoning steps. Coined, program-aided language models (PAL), it differs from chain-of-thought prompting in that instead of using free-form text to obtain solution it offloads the solution step to a programmatic runtime such as a Python interpreter.

Let's look at an example using LangChain and OpenAI GPT-3. We are interested to develop a simple application that's able to interpret the question being asked and provide an answer by leveraging the Python interpreter.

Specifically, we are interested to create a functionality that allows the use of the LLM to answer questions that require date understanding. We will provide the LLM a prompt that includes a few exemplars which are adopted from here.

These are the imports we need:

import openai
from datetime import datetime
from dateutil.relativedelta import relativedelta
import os
from langchain.llms import OpenAI
from dotenv import load_dotenv

Let's first configure a few things:

load_dotenv()
 
# API configuration
openai.api_key = os.getenv("OPENAI_API_KEY")
 
# for LangChain
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")

Setup model instance:

llm = OpenAI(model_name='text-davinci-003', temperature=0)

Setup prompt + question:

question = "Today is 27 February 2023. I was born exactly 25 years ago. What is the date I was born in MM/DD/YYYY?"
 
DATE_UNDERSTANDING_PROMPT = """
# Q: 2015 is coming in 36 hours. What is the date one week from today in MM/DD/YYYY?
# If 2015 is coming in 36 hours, then today is 36 hours before.
today = datetime(2015, 1, 1) - relativedelta(hours=36)
# One week from today,
one_week_from_today = today + relativedelta(weeks=1)
# The answer formatted with %m/%d/%Y is
one_week_from_today.strftime('%m/%d/%Y')
# Q: The first day of 2019 is a Tuesday, and today is the first Monday of 2019. What is the date today in MM/DD/YYYY?
# If the first day of 2019 is a Tuesday, and today is the first Monday of 2019, then today is 6 days later.
today = datetime(2019, 1, 1) + relativedelta(days=6)
# The answer formatted with %m/%d/%Y is
today.strftime('%m/%d/%Y')
# Q: The concert was scheduled to be on 06/01/1943, but was delayed by one day to today. What is the date 10 days ago in MM/DD/YYYY?
# If the concert was scheduled to be on 06/01/1943, but was delayed by one day to today, then today is one day later.
today = datetime(1943, 6, 1) + relativedelta(days=1)
# 10 days ago,
ten_days_ago = today - relativedelta(days=10)
# The answer formatted with %m/%d/%Y is
ten_days_ago.strftime('%m/%d/%Y')
# Q: It is 4/19/1969 today. What is the date 24 hours later in MM/DD/YYYY?
# It is 4/19/1969 today.
today = datetime(1969, 4, 19)
# 24 hours later,
later = today + relativedelta(hours=24)
# The answer formatted with %m/%d/%Y is
today.strftime('%m/%d/%Y')
# Q: Jane thought today is 3/11/2002, but today is in fact Mar 12, which is 1 day later. What is the date 24 hours later in MM/DD/YYYY?
# If Jane thought today is 3/11/2002, but today is in fact Mar 12, then today is 3/12/2002.
today = datetime(2002, 3, 12)
# 24 hours later,
later = today + relativedelta(hours=24)
# The answer formatted with %m/%d/%Y is
later.strftime('%m/%d/%Y')
# Q: Jane was born on the last day of Feburary in 2001. Today is her 16-year-old birthday. What is the date yesterday in MM/DD/YYYY?
# If Jane was born on the last day of Feburary in 2001 and today is her 16-year-old birthday, then today is 16 years later.
today = datetime(2001, 2, 28) + relativedelta(years=16)
# Yesterday,
yesterday = today - relativedelta(days=1)
# The answer formatted with %m/%d/%Y is
yesterday.strftime('%m/%d/%Y')
# Q: {question}
""".strip() + '\n'
llm_out = llm(DATE_UNDERSTANDING_PROMPT.format(question=question))
print(llm_out)

This will output the following:

# If today is 27 February 2023 and I was born exactly 25 years ago, then I was born 25 years before.
today = datetime(2023, 2, 27)
# I was born 25 years before,
born = today - relativedelta(years=25)
# The answer formatted with %m/%d/%Y is
born.strftime('%m/%d/%Y')

The contents of llm_out are a Python code snippet. Below, the exec command is used to execute this Python code snippet.

exec(llm_out)
print(born)

This will output the following: 02/27/1998

Dịch: Lê Trung Nghĩa

letrungnghia.foss@gmail.com

Tác giả: Nghĩa Lê Trung

Tổng số điểm của bài viết là: 0 trong 0 đánh giá

Click để đánh giá bài viết

  Ý kiến bạn đọc

Về Blog này

Blog này được chuyển đổi từ http://blog.yahoo.com/letrungnghia trên Yahoo Blog sang sử dụng NukeViet sau khi Yahoo Blog đóng cửa tại Việt Nam ngày 17/01/2013.Kể từ ngày 07/02/2013, thông tin trên Blog được cập nhật tiếp tục trở lại với sự hỗ trợ kỹ thuật và đặt chỗ hosting của nhóm phát triển...

Bài đọc nhiều nhất trong năm
Thăm dò ý kiến

Bạn quan tâm gì nhất ở mã nguồn mở?

Thống kê truy cập
  • Đang truy cập179
  • Máy chủ tìm kiếm3
  • Khách viếng thăm176
  • Hôm nay6,933
  • Tháng hiện tại363,132
  • Tổng lượt truy cập46,967,454
Bạn đã không sử dụng Site, Bấm vào đây để duy trì trạng thái đăng nhập. Thời gian chờ: 60 giây