PAL (Program-Aided Language Models) | Prompt Engineering Guide (original) (raw)

Gao et al., (2022) (opens in a new tab) 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.

PAL

Image Source: Gao et al., (2022) (opens in a new tab)

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 (opens in a new tab).

These are the imports we need:

Let's first configure a few things:

Setup model instance:

Setup prompt + question:

This will output the following:

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

This will output the following: 02/27/1998

Explore All Courses

Discover our full catalog of AI and prompt engineering courses. From beginners to advanced practitioners.Use code PROMPTING20 for 20% off!

Browse Academy

Directional Stimulus PromptingReAct