# 🟡 Code as Reasoning

Program-aided Language Models (PAL)1 are another example of a MRKL system. When given a question, PALs are able to write code that solves this question. They send the code to a programmatic runtime to get the result. PAL works in contrast to CoT; PAL's intermediate reasoning is code, while CoT's is natural language.

PAL Example (Gao et al.)

One important thing to note it that PAL actually interleaves natural language (NL) and code. In the above image, in blue are natural language reasoning that PAL generates. Although it is not shown in the image, PAL actually generates '#' before each line of NL reasoning, so that they are interpreted as comments by the programmatic runtime.

## Example​

Let's look at an example of PAL solving a math question. I use a 3-shot prompt, which is a simplified version of this one1.

I will use langchain, a Python package for chaining LLM functionality for this. First, a few installations are needed:

!pip install langchain==0.0.26!pip install openaifrom langchain.llms import OpenAIimport osos.environ["OPENAI_API_KEY"] = "sk-YOUR_KEY_HERE"

Then, we can create an instance of GPT-3 davinci-002 (an API call happens when we use this object)

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

Here is the few shot prompt:

MATH_PROMPT = '''Q: There were nine computers in the server room. Five more computers were installed each day, from monday to thursday. How many computers are now in the server room?# solution in Python:"""There were nine computers in the server room. Five more computers were installed each day, from monday to thursday. How many computers are now in the server room?"""computers_initial = 9computers_per_day = 5num_days = 4  # 4 days between monday and thursdaycomputers_added = computers_per_day * num_dayscomputers_total = computers_initial + computers_addedresult = computers_totalreturn resultQ: Shawn has five toys. For Christmas, he got two toys each from his mom and dad. How many toys does he have now?# solution in Python:"""Shawn has five toys. For Christmas, he got two toys each from his mom and dad. How many toys does he have now?"""toys_initial = 5mom_toys = 2dad_toys = 2total_received = mom_toys + dad_toystotal_toys = toys_initial + total_receivedresult = total_toysQ: Jason had 20 lollipops. He gave Denny some lollipops. Now Jason has 12 lollipops. How many lollipops did Jason give to Denny?# solution in Python:"""Jason had 20 lollipops. He gave Denny some lollipops. Now Jason has 12 lollipops. How many lollipops did Jason give to Denny?"""jason_lollipops_initial = 20jason_lollipops_after = 12denny_lollipops = jason_lollipops_initial - jason_lollipops_afterresult = denny_lollipopsQ: {question}# solution in Python:'''

Now we can pass the combined prompt to GPT-3.

llm_out = llm(MATH_PROMPT.format(question=question))print(llm_out)

The output is:

"""Emma took a 60 minute plane ride to seattle. She then took a 2 hour train ride to portland, and then a 30 minute bus ride to vancouver. How long did it take her to get to vancouver?"""plane_ride = 60train_ride = 2 * 60  # 2 hours in minutesbus_ride = 30total_time = plane_ride + train_ride + bus_rideresult = total_time

Finally, we can pass this code to a Python runtime to get the answer:

exec(llm_out)print(result)

The output is 210, which is correct.

See the Jupyter notebook for this example here.

## More​

Also see PAL's colab example.

1. Gao, L., Madaan, A., Zhou, S., Alon, U., Liu, P., Yang, Y., Callan, J., & Neubig, G. (2022). PAL: Program-aided Language Models.