more notes

pull/20/head
Elvis Saravia 1 year ago
parent 21f94433b2
commit fae914028f

@ -4,8 +4,22 @@ In this section, we discuss other miscellaneous but important topics in prompt e
**Note that this section is under construction.**
--
## Program-Aided Language Models
[Gao et al., (2023)](https://arxiv.org/abs/2211.10435) 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.
[Gao et al., (2022)](https://arxiv.org/abs/2211.10435) 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.
![](../img/pal.png)
Full example coming soon!
---
## ReAct
[Yao et al., 2022](Yao) introduced a framework where LLMs are used to generate both reasoning traces and task-specific actions in an interleaved manner. Generating reasoning traces allow the model to induce, track, and update action plans, and even handle exceptions. The action step allows to interface with and gather information from external sources such as knowledge bases or environments.
The ReAct framework can allow LLMs to interact with external tools to retrieve additional information that leads to more reliable and factual responses.
![](../img/react.png)
Full example coming soon!

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 196 KiB

@ -46,7 +46,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@ -55,7 +55,7 @@
"True"
]
},
"execution_count": 3,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
@ -79,12 +79,15 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# API configuration\n",
"openai.api_key = os.getenv(\"OPENAI_KEY\")"
"openai.api_key = os.getenv(\"OPENAI_KEY\")\n",
"\n",
"# for LangChain\n",
"os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_KEY\")"
]
},
{
@ -535,8 +538,229 @@
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": []
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3. Tools and Applications\n",
"\n",
"Objective:\n",
"\n",
"- Demonstrate how to use LangChain to develop a simple application leveraging the PAL prompting technique"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"We are developing a simple application that's able to reason about the question being asked through code. \n",
"\n",
"Specifically, the application takes in some data and answers a question about the data input. The prompt includes a few exemplars which are adopted from [here](https://github.com/reasoning-machines/pal/blob/main/pal/prompt/penguin_prompt.py). "
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.1 PAL"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# lm instance\n",
"llm = OpenAI(model_name='text-davinci-003', temperature=0)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"question = \"Which is the oldest penguin?\""
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"PENGUIN_PROMPT = '''\n",
"\"\"\"\n",
"Q: Here is a table where the first line is a header and each subsequent line is a penguin:\n",
"name, age, height (cm), weight (kg) \n",
"Louis, 7, 50, 11\n",
"Bernard, 5, 80, 13\n",
"Vincent, 9, 60, 11\n",
"Gwen, 8, 70, 15\n",
"For example: the age of Louis is 7, the weight of Gwen is 15 kg, the height of Bernard is 80 cm. \n",
"We now add a penguin to the table:\n",
"James, 12, 90, 12\n",
"How many penguins are less than 8 years old?\n",
"\"\"\"\n",
"# Put the penguins into a list.\n",
"penguins = []\n",
"penguins.append(('Louis', 7, 50, 11))\n",
"penguins.append(('Bernard', 5, 80, 13))\n",
"penguins.append(('Vincent', 9, 60, 11))\n",
"penguins.append(('Gwen', 8, 70, 15))\n",
"# Add penguin James.\n",
"penguins.append(('James', 12, 90, 12))\n",
"# Find penguins under 8 years old.\n",
"penguins_under_8_years_old = [penguin for penguin in penguins if penguin[1] < 8]\n",
"# Count number of penguins under 8.\n",
"num_penguin_under_8 = len(penguins_under_8_years_old)\n",
"answer = num_penguin_under_8\n",
"\"\"\"\n",
"Q: Here is a table where the first line is a header and each subsequent line is a penguin:\n",
"name, age, height (cm), weight (kg) \n",
"Louis, 7, 50, 11\n",
"Bernard, 5, 80, 13\n",
"Vincent, 9, 60, 11\n",
"Gwen, 8, 70, 15\n",
"For example: the age of Louis is 7, the weight of Gwen is 15 kg, the height of Bernard is 80 cm.\n",
"Which is the youngest penguin?\n",
"\"\"\"\n",
"# Put the penguins into a list.\n",
"penguins = []\n",
"penguins.append(('Louis', 7, 50, 11))\n",
"penguins.append(('Bernard', 5, 80, 13))\n",
"penguins.append(('Vincent', 9, 60, 11))\n",
"penguins.append(('Gwen', 8, 70, 15))\n",
"# Sort the penguins by age.\n",
"penguins = sorted(penguins, key=lambda x: x[1])\n",
"# Get the youngest penguin's name.\n",
"youngest_penguin_name = penguins[0][0]\n",
"answer = youngest_penguin_name\n",
"\"\"\"\n",
"Q: Here is a table where the first line is a header and each subsequent line is a penguin:\n",
"name, age, height (cm), weight (kg) \n",
"Louis, 7, 50, 11\n",
"Bernard, 5, 80, 13\n",
"Vincent, 9, 60, 11\n",
"Gwen, 8, 70, 15\n",
"For example: the age of Louis is 7, the weight of Gwen is 15 kg, the height of Bernard is 80 cm.\n",
"What is the name of the second penguin sorted by alphabetic order?\n",
"\"\"\"\n",
"# Put the penguins into a list.\n",
"penguins = []\n",
"penguins.append(('Louis', 7, 50, 11))\n",
"penguins.append(('Bernard', 5, 80, 13))\n",
"penguins.append(('Vincent', 9, 60, 11))\n",
"penguins.append(('Gwen', 8, 70, 15))\n",
"# Sort penguins by alphabetic order.\n",
"penguins_alphabetic = sorted(penguins, key=lambda x: x[0])\n",
"# Get the second penguin sorted by alphabetic order.\n",
"second_penguin_name = penguins_alphabetic[1][0]\n",
"answer = second_penguin_name\n",
"\"\"\"\n",
"{question}\n",
"\"\"\"\n",
"'''.strip() + '\\n'"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that we have the prompt and question. We can send it to the model. It should output the steps, in code, needed to get the solution to the answer."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"# Put the penguins into a list.\n",
"penguins = []\n",
"penguins.append(('Louis', 7, 50, 11))\n",
"penguins.append(('Bernard', 5, 80, 13))\n",
"penguins.append(('Vincent', 9, 60, 11))\n",
"penguins.append(('Gwen', 8, 70, 15))\n",
"# Sort the penguins by age.\n",
"penguins = sorted(penguins, key=lambda x: x[1], reverse=True)\n",
"# Get the oldest penguin's name.\n",
"oldest_penguin_name = penguins[0][0]\n",
"answer = oldest_penguin_name\n"
]
}
],
"source": [
"llm_out = llm(PENGUIN_PROMPT.format(question=question))\n",
"print(llm_out)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Vincent\n"
]
}
],
"source": [
"exec(llm_out)\n",
"print(answer)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"That's the correct answer! Vincent is the oldest penguin. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise: Try a different question and see what's the result."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],

Loading…
Cancel
Save