Gao等人(2022)(opens in a new tab)提出了一种使用LLMs读取自然语言问题并生成程序作为中间推理步骤的方法。被称为程序辅助语言模型(PAL),它与思维链提示不同,因为它不是使用自由形式文本来获得解决方案,而是将解决步骤卸载到类似Python解释器的编程运行时中。
    3.1 PAL(程序辅助语言模型) - 图1
    图片来源:Gao等人(2022)(opens in a new tab)
    让我们以LangChain和OpenAI GPT-3为例。我们有兴趣开发一个简单的应用程序,它能够解释所提出的问题,并利用Python解释器提供答案。
    具体来说,我们有兴趣创建一个功能,允许使用LLM回答需要日期理解的问题。我们将为LLM提供一个提示,其中包括一些示例,这些示例是从这里(opens in a new tab)采用的。
    这是我们需要导入的包:

    1. import openai
    2. from datetime import datetime
    3. from dateutil.relativedelta import relativedelta
    4. import os
    5. from langchain.llms import OpenAI
    6. from dotenv import load_dotenv

    让我们先配置一些环境:

    1. load_dotenv()
    2. # API configuration
    3. openai.api_key = os.getenv("OPENAI_API_KEY")
    4. # for LangChain
    5. os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")

    设置模型实例:

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

    设置提示+问题:

    1. question = "Today is 27 February 2023. I was born exactly 25 years ago. What is the date I was born in MM/DD/YYYY?"
    2. DATE_UNDERSTANDING_PROMPT = """
    3. # Q: 2015 is coming in 36 hours. What is the date one week from today in MM/DD/YYYY?
    4. # If 2015 is coming in 36 hours, then today is 36 hours before.
    5. today = datetime(2015, 1, 1) - relativedelta(hours=36)
    6. # One week from today,
    7. one_week_from_today = today + relativedelta(weeks=1)
    8. # The answer formatted with %m/%d/%Y is
    9. one_week_from_today.strftime('%m/%d/%Y')
    10. # 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?
    11. # If the first day of 2019 is a Tuesday, and today is the first Monday of 2019, then today is 6 days later.
    12. today = datetime(2019, 1, 1) + relativedelta(days=6)
    13. # The answer formatted with %m/%d/%Y is
    14. today.strftime('%m/%d/%Y')
    15. # 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?
    16. # 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.
    17. today = datetime(1943, 6, 1) + relativedelta(days=1)
    18. # 10 days ago,
    19. ten_days_ago = today - relativedelta(days=10)
    20. # The answer formatted with %m/%d/%Y is
    21. ten_days_ago.strftime('%m/%d/%Y')
    22. # Q: It is 4/19/1969 today. What is the date 24 hours later in MM/DD/YYYY?
    23. # It is 4/19/1969 today.
    24. today = datetime(1969, 4, 19)
    25. # 24 hours later,
    26. later = today + relativedelta(hours=24)
    27. # The answer formatted with %m/%d/%Y is
    28. today.strftime('%m/%d/%Y')
    29. # 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?
    30. # If Jane thought today is 3/11/2002, but today is in fact Mar 12, then today is 3/1/2002.
    31. today = datetime(2002, 3, 12)
    32. # 24 hours later,
    33. later = today + relativedelta(hours=24)
    34. # The answer formatted with %m/%d/%Y is
    35. later.strftime('%m/%d/%Y')
    36. # 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?
    37. # 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.
    38. today = datetime(2001, 2, 28) + relativedelta(years=16)
    39. # Yesterday,
    40. yesterday = today - relativedelta(days=1)
    41. # The answer formatted with %m/%d/%Y is
    42. yesterday.strftime('%m/%d/%Y')
    43. # Q: {question}
    44. """.strip() + '\n'
    1. llm_out = llm(DATE_UNDERSTANDING_PROMPT.format(question=question))
    2. print(llm_out)
    1. exec(llm_out)
    2. print(born)

    这将输出以下内容:02/27/1998