[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.
"\" 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,54 @@
"\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,..."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 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",
"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",
"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."