more notes

pull/20/head
Elvis Saravia 2023-02-19 00:54:43 -06:00
parent 45e225c5d8
commit 7950d419ab
2 changed files with 194 additions and 87 deletions

View File

@ -15,7 +15,7 @@ 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.
[Yao et al., 2022](https://arxiv.org/abs/2210.03629) 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.

View File

@ -5,7 +5,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Getting Started with Prompt Engineering [WIP]\n",
"# Getting Started with Prompt Engineering\n",
"by DAIR.AI | Elvis Saravia\n",
"\n",
"\n",
@ -71,18 +71,19 @@
"metadata": {},
"outputs": [],
"source": [
"load_dotenv()\n",
"\n",
"# API configuration\n",
"openai.api_key = os.getenv(\"OPENAI_KEY\")\n",
"\n",
"# for LangChain\n",
"load_dotenv()\n",
"os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_KEY\")\n",
"os.environ[\"SERPAPI_API_KEY\"] = os.getenv(\"SERPAPI_API_KEY\")"
]
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
@ -131,7 +132,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
@ -145,16 +146,16 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\" blue\\n\\nThe sky is blue during the day as a result of the way the Earth's atmosphere scatters sunlight. The blue color of the sky is due to Rayleigh scattering, which is the scattering of light by molecules in the air. When sunlight passes through the atmosphere, shorter wavelength colors like blue are scattered more widely than longer wavelength colors like red, making the sky appear blue.\""
"' blue\\n\\nThe sky is blue during the day on a clear day. It can also turn other colors, such as pink or orange, during sunrises and sunsets. On a cloudy day, the sky can be gray or white.'"
]
},
"execution_count": 25,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
@ -165,7 +166,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@ -173,13 +174,13 @@
"text/markdown": [
" blue\n",
"\n",
"The sky is blue during the day as a result of the way the Earth's atmosphere scatters sunlight. The blue color of the sky is due to Rayleigh scattering, which is the scattering of light by molecules in the air. When sunlight passes through the atmosphere, shorter wavelength colors like blue are scattered more widely than longer wavelength colors like red, making the sky appear blue."
"The sky is blue during the day on a clear day. It can also turn other colors, such as pink or orange, during sunrises and sunsets. On a cloudy day, the sky can be gray or white."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 26,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
@ -198,7 +199,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@ -206,13 +207,13 @@
"text/markdown": [
" blue\n",
"\n",
"The sky is blue because of the way the atmosphere scatters sunlight. When sunlight passes through the atmosphere, the blue wavelengths are scattered more than the other colors, making the sky appear blue."
"The sky is blue during the day, when the sun is out. At night, the sky is usually black, but can sometimes be a deep blue or purple."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 32,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
@ -234,20 +235,19 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"\n",
"Antibiotics are medications used to treat bacterial infections by killing or inhibiting the growth of bacteria, but they are not effective against viral infections and should be used appropriately to avoid antibiotic resistance."
" Antibiotics are medications used to treat bacterial infections by killing or inhibiting the growth of bacteria, and must be used appropriately to avoid contributing to antibiotic resistance."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 34,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
@ -280,19 +280,19 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
" Mice."
" Mice"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 35,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
@ -336,7 +336,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@ -348,7 +348,7 @@
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 36,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@ -382,19 +382,19 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
" Sure. A black hole is a region of spacetime where gravity is so strong that nothing, not even light, can escape its pull. Black holes are formed when a massive star runs out of fuel and collapses under its own gravity. The collapse causes the star to become so dense that it creates an event horizon, which is the point of no return."
" Certainly! A black hole is an object with such a strong gravitational pull that not even light can escape its grasp. It is formed when a very large star runs out of nuclear fuel and collapses under its own gravity. The collapse causes the star to become incredibly dense, resulting in the black hole."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 37,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@ -429,7 +429,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@ -438,15 +438,13 @@
"\n",
"SELECT StudentId, StudentName \n",
"FROM students \n",
"WHERE DepartmentId = (SELECT DepartmentId \n",
" FROM departments \n",
" WHERE DepartmentName = 'Computer Science')"
"WHERE DepartmentId = (SELECT DepartmentId FROM departments WHERE DepartmentName='Computer Science');"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 38,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@ -468,23 +466,23 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"\n",
" \n",
"\n",
"Odd numbers: 15, 5, 13, 7, 1\n",
"Total = 41\n",
"Sum of odd numbers: 41\n",
"41 is an odd number."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 39,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
@ -515,8 +513,7 @@
"\n",
"Objectives:\n",
"\n",
"- Cover more advanced techniques for prompting: few-shot, chain-of-thoughts,...\n",
"- Review more advanced applications"
"- Cover more advanced techniques for prompting: few-shot, chain-of-thoughts,..."
]
},
{
@ -524,7 +521,143 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.X PAL - Code as Reasoning"
"### 2.2 Few-shot prompts"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
" The answer is True."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt = \"\"\"The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1.\n",
"A: The answer is False.\n",
"\n",
"The odd numbers in this group add up to an even number: 17, 10, 19, 4, 8, 12, 24.\n",
"A: The answer is True.\n",
"\n",
"The odd numbers in this group add up to an even number: 16, 11, 14, 4, 8, 13, 24.\n",
"A: The answer is True.\n",
"\n",
"The odd numbers in this group add up to an even number: 17, 9, 10, 12, 13, 4, 2.\n",
"A: The answer is False.\n",
"\n",
"The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1. \n",
"A:\"\"\"\n",
"\n",
"response = get_completion(params, prompt)\n",
"IPython.display.Markdown(response.choices[0].text)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.3 Chain-of-Thought (CoT) Prompting"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
" Adding all the odd numbers (15, 5, 13, 7, 1) gives 41. The answer is False."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt = \"\"\"The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1.\n",
"A: Adding all the odd numbers (9, 15, 1) gives 25. The answer is False.\n",
"\n",
"The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1. \n",
"A:\"\"\"\n",
"\n",
"response = get_completion(params, prompt)\n",
"IPython.display.Markdown(response.choices[0].text)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.4 Zero-shot CoT"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
" \n",
"\n",
"You started with 10 apples. You gave away 4 apples, so you would have 6 apples remaining. Then you bought 5 more apples, so now you have 11 apples. Finally, you ate 1 apple, so you would have 10 apples remaining."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt = \"\"\"I went to the market and bought 10 apples. I gave 2 apples to the neighbor and 2 to the repairman. I then went and bought 5 more apples and ate 1. How many apples did I remain with?\n",
"\n",
"Let's think step by step.\"\"\"\n",
"\n",
"response = get_completion(params, prompt)\n",
"IPython.display.Markdown(response.choices[0].text)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.5 Self-Consistency\n",
"As an exercise, check examples in our [guide](https://github.com/dair-ai/Prompt-Engineering-Guide/blob/main/guides/prompts-advanced-usage.md#self-consistency) and try them here. \n",
"\n",
"### 2.6 Generate Knowledge Prompting\n",
"\n",
"As an exercise, check examples in our [guide](https://github.com/dair-ai/Prompt-Engineering-Guide/blob/main/guides/prompts-advanced-usage.md#generated-knowledge-prompting) and try them here. "
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.6 PAL - Code as Reasoning"
]
},
{
@ -539,7 +672,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
@ -549,7 +682,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
@ -558,7 +691,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
@ -646,7 +779,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 20,
"metadata": {},
"outputs": [
{
@ -674,7 +807,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 21,
"metadata": {},
"outputs": [
{
@ -738,7 +871,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
@ -748,7 +881,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
@ -760,7 +893,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 24,
"metadata": {},
"outputs": [
{
@ -795,7 +928,7 @@
"\"Jason Sudeikis, Olivia Wilde's boyfriend, is 47 years old and his age raised to the 0.23 power is 2.4242784855673896.\""
]
},
"execution_count": 29,
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
@ -823,33 +956,6 @@
"Code example adopted from [LangChain Documentation](https://langchain.readthedocs.io/en/latest/modules/chains/combine_docs_examples/qa_with_sources.html). We are only using the examples for educational purposes."
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--2023-02-18 18:42:47-- https://raw.githubusercontent.com/hwchase17/langchain/a83ba44efacb9f27648e770927fd93eb60d7cf3f/docs/modules/state_of_the_union.txt\n",
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.109.133, 185.199.111.133, 185.199.108.133, ...\n",
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.109.133|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 39027 (38K) [text/plain]\n",
"Saving to: state_of_the_union.txt\n",
"\n",
"state_of_the_union. 100%[===================>] 38.11K --.-KB/s in 0.02s \n",
"\n",
"2023-02-18 18:42:47 (2.29 MB/s) - state_of_the_union.txt saved [39027/39027]\n",
"\n"
]
}
],
"source": [
"!wget https://raw.githubusercontent.com/hwchase17/langchain/a83ba44efacb9f27648e770927fd93eb60d7cf3f/docs/modules/state_of_the_union.txt"
]
},
{
"attachments": {},
"cell_type": "markdown",
@ -860,7 +966,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
@ -875,7 +981,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
@ -889,7 +995,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 27,
"metadata": {},
"outputs": [
{
@ -907,7 +1013,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
@ -925,7 +1031,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
@ -935,7 +1041,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 30,
"metadata": {},
"outputs": [
{
@ -944,7 +1050,7 @@
"{'output_text': ' The president thanked Justice Breyer for his service.\\nSOURCES: 30-pl'}"
]
},
"execution_count": 9,
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
@ -965,7 +1071,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 31,
"metadata": {},
"outputs": [
{
@ -974,7 +1080,7 @@
"{'output_text': '\\nEl Presidente no dijo nada acerca de la Justicia Breyer.\\n\\nFUENTES: 30, 31, 33'}"
]
},
"execution_count": 10,
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
@ -1001,11 +1107,12 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": []
"source": [
"Exercise: Try using a different dataset from the internet and try different prompt, including all the techniques you learned in the lecture."
]
}
],
"metadata": {