improve guides

pull/20/head
Elvis Saravia 2 years ago
parent 649f9c6170
commit b20d74a29e

6
.gitignore vendored

@ -0,0 +1,6 @@
.ipynb_checkpoints/
__pycache__/
*.swp
.env
.pylintrc
*.egg-info/

@ -37,6 +37,7 @@ The following are a set of guides on prompt engineering developed by us. Guides
- Surveys / Overviews:
- [Augmented Language Models: a Survey](https://arxiv.org/abs/2302.07842) (Feb 2023)
- [A Survey for In-context Learning](https://arxiv.org/abs/2301.00234) (Dec 2022)
- [Towards Reasoning in Large Language Models: A Survey](https://arxiv.org/abs/2212.10403) (Dec 2022)
- [Emergent Abilities of Large Language Models](https://arxiv.org/abs/2206.07682) (Jun 2022)
@ -44,6 +45,9 @@ The following are a set of guides on prompt engineering developed by us. Guides
- [Pre-train, Prompt, and Predict: A Systematic Survey of Prompting Methods in Natural Language Processing](https://arxiv.org/abs/2107.13586) (Jul 2021)
- Approaches/Techniques:
- [À-la-carte Prompt Tuning (APT): Combining Distinct Data Via Composable Prompting](https://arxiv.org/abs/2302.07994) (Feb 2023)
- [GraphPrompt: Unifying Pre-Training and Downstream Tasks for Graph Neural Networks](https://arxiv.org/abs/2302.08043) (Feb 2023)
- [The Capacity for Moral Self-Correction in Large Language Models](https://arxiv.org/abs/2302.07459) (Feb 2023)
- [SwitchPrompt: Learning Domain-Specific Gated Soft Prompts for Classification in Low-Resource Domains](https://arxiv.org/abs/2302.06868) (Feb 2023)
- [Evaluating the Robustness of Discrete Prompts](https://arxiv.org/abs/2302.05619) (Feb 2023)
- [Compositional Exemplars for In-context Learning](https://arxiv.org/abs/2302.05698) (Feb 2023)
@ -97,6 +101,8 @@ The following are a set of guides on prompt engineering developed by us. Guides
- [How Can We Know What Language Models Know?](https://direct.mit.edu/tacl/article/doi/10.1162/tacl_a_00324/96460/How-Can-We-Know-What-Language-Models-Know) (July 2020)
- Applications:
- [LabelPrompt: Effective Prompt-based Learning for Relation Classification](https://arxiv.org/abs/2302.08068) (Feb 2023)
- [Prompt Tuning of Deep Neural Networks for Speaker-adaptive Visual Speech Recognition](https://arxiv.org/abs/2302.08102) (Feb 2023)
- [The Capacity for Moral Self-Correction in Large Language Models](https://arxiv.org/abs/2302.07459) (Feb 2023)
- [Prompting for Multimodal Hateful Meme Classification](https://arxiv.org/abs/2302.04156) (Feb 2023)
- [PLACES: Prompting Language Models for Social Conversation Synthesis](https://arxiv.org/abs/2302.03269) (Feb 2023)
@ -209,6 +215,7 @@ The following are a set of guides on prompt engineering developed by us. Guides
- [Prompt Engineering Template](https://docs.google.com/spreadsheets/d/1-snKDn38-KypoYCk9XLPg799bHcNFSBAVu2HVvFEAkA/edit#gid=0)
- [Prompt Engineering Topic by GitHub](https://github.com/topics/prompt-engineering)
- [Prompt Engineering: From Words to Art](https://www.saxifrage.xyz/post/prompt-engineering)
- [Prompt Engineering with OpenAI's GPT-3 and other LLMs](https://youtube.com/watch?v=BP9fi_0XTlw&feature=shares)
- [Prompt injection attacks against GPT-3](https://simonwillison.net/2022/Sep/12/prompt-injection)
- [Prompt injection to read out the secret OpenAI API key](https://twitter.com/ludwig_stumpp/status/1619701277419794435?s=20&t=GtoMlmYCSt-UmvjqJVbBSA)
- [Prompting in NLP: Prompt-based zero-shot learning](https://savasy-22028.medium.com/prompting-in-nlp-prompt-based-zero-shot-learning-3f34bfdb2b72)

@ -2,7 +2,18 @@
In this section, we discuss other miscellaneous but important topics in prompt engineering.
**Note that this section is under construction.**
---
## Multimodal Prompting
In this section, we will cover some examples of multimodal prompting techniques and applications that leverage multiple modalities as opposed to just text alone.
In this section, we will cover some examples of multimodal prompting techniques and applications that leverage multiple modalities as opposed to just text alone.
More coming soon!
---
## GraphPrompts
[Liu et al., 2023](https://arxiv.org/abs/2302.08043) introduces GraphPrompt, a new prompting framework for graphs to improve performance on downstream tasks.
More coming soon!

@ -93,7 +93,7 @@ There is no consistency in the format above but that still affect the model from
## Chain-of-Thought Prompting
Introduced in [Wei et al. (2022)](https://arxiv.org/abs/2201.11903), chain-of-thought (CoT) prompting uses few chain of thought demonstrations are provided as exemplars in prompting to improve the ability of large language models to perform complex reasoning.
Introduced in [Wei et al. (2022)](https://arxiv.org/abs/2201.11903), chain-of-thought (CoT) prompting enables complex reasoning capabilities through intermediate reasoning steps. You can combine it with few-shot prompting to get better results on more complex tasks that require reasoning before responding.
```
The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1.
@ -138,7 +138,7 @@ Keep in mind that the authors claim that this is an emergent ability that arises
## Zero-Shot CoT
One recent idea that came out more recently is the idea of [zero-shot CoT](https://arxiv.org/abs/2205.11916) that essentially involves adding "Let's think step by step" to the the original prompt. Let's try a simple problem and see how the model performs:
One recent idea that came out more recently is the idea of [zero-shot CoT](https://arxiv.org/abs/2205.11916) that essentially involves adding "Let's think step by step" to the original prompt. Let's try a simple problem and see how the model performs:
```
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?
@ -149,7 +149,7 @@ Output
11 apples
```
The answer is incorrect Now Let's try with the special prompt.
The answer is incorrect! Now Let's try with the special 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?
@ -171,7 +171,7 @@ It's actually impressive that this simple prompt is effective at this task. This
## Self-Consistency
Perhaps one of the more advanced techniques out there for prompt engineering is self-consistency. Proposed by [Wang et al. (2022)](https://arxiv.org/pdf/2203.11171.pdf), self-consistency aims "to replace the naive greedy decoding used in chain-of-thought prompting". The idea is to sample multiple, diverse reasoning paths through few-shot CoT, and use the generations to select the most consistent answer. This helps to boost the performance of CoT prompting ont asks involving arithmetic and commonsense reasoning.
Perhaps one of the more advanced techniques out there for prompt engineering is self-consistency. Proposed by [Wang et al. (2022)](https://arxiv.org/pdf/2203.11171.pdf), self-consistency aims "to replace the naive greedy decoding used in chain-of-thought prompting". The idea is to sample multiple, diverse reasoning paths through few-shot CoT, and use the generations to select the most consistent answer. This helps to boost the performance of CoT prompting on tasks involving arithmetic and commonsense reasoning.
Let's try the following example for arithmetic reasoning:
@ -250,7 +250,7 @@ Computing for the final answer involves a few steps (check out the paper for the
LLMs continue to be improved and one popular technique include the ability to incorporate knowledge or information to help the model make more accurate predictions.
Using a similar idea, can the model also be used to generate knowledge before making a prediction? That's what attempted in the paper by [Liu et al. 2022](https://arxiv.org/pdf/2110.08387.pdf). In particular, how helpful is this for tasks such as commonsense reasoning?
Using a similar idea, can the model also be used to generate knowledge before making a prediction? That's what attempted in the paper by [Liu et al. 2022](https://arxiv.org/pdf/2110.08387.pdf) -- generate knowledge to be used as part of the prompt. In particular, how helpful is this for tasks such as commonsense reasoning?
Let's try a simple prompt:

@ -40,7 +40,6 @@ Antibiotics are medications used to treat bacterial infections by either killing
Without paying too much attention to the accuracy of the output above, which is something we will touch on in a later guide, the model tried to summarize the paragraph in one sentence. You can get clever with the instructions but we will leave that for a later chapter. Feel free to pause here an experiment to see if you get better results.
---
## Information Extraction
While language models are trained to perform natural language generation and related tasks, it's also very capable of performing classification and a range of other natural language processing (NLP) tasks.
@ -64,6 +63,28 @@ By now it should be obvious that you can ask the model to perform different task
Paragraph source: [ChatGPT: five priorities for research](https://www.nature.com/articles/d41586-023-00288-7)
---
## Question Answering
One of the best ways to get the model to respond specific answers is to improve the format of the prompt. As covered before, a prompt could combine instructions, context, input, and output indicator to get improved results. While not components are required, it becomes a good practice as the more specific you are with instruction, the better results you will get. Below is an example of how this would look following a more structured prompt.
```
Answer the question based on the context below. Keep the answer short and concise. Respond "Unsure about answer" if not sure about the answer.
Context: Teplizumab traces its roots to a New Jersey drug company called Ortho Pharmaceutical. There, scientists generated an early version of the antibody, dubbed OKT3. Originally sourced from mice, the molecule was able to bind to the surface of T cells and limit their cell-killing potential. In 1986, it was approved to help prevent organ rejection after kidney transplants, making it the first therapeutic antibody allowed for human use.
Question: What was OKT3 originally sourced from?
Answer:
```
Output
```
Mice.
```
Context obtained from [Nature](https://www.nature.com/articles/d41586-023-00400-x).
---
## Text Classification

@ -0,0 +1,569 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Getting Started with Prompt Engineering [WIP]\n",
"by DAIR.AI | Elvis Saravia\n",
"\n",
"\n",
"This notebook contains examples and exercises to learning about prompt engineering.\n",
"\n",
"We will be using the [OpenAI APIs](https://platform.openai.com/) for all examples. I am using the default settings `temperature=0.7` and `top-p=1`"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Prompt Engineering Basics\n",
"\n",
"Objectives\n",
"- Load the libraries\n",
"- Review the format\n",
"- Cover basic prompts\n",
"- Review common use cases"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Below we are loading the necessary libraries, utilities, and configurations."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import openai\n",
"import os\n",
"import IPython\n",
"from dotenv import load_dotenv\n",
"load_dotenv()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Make sure to replace `OPENAI_KEY` with your own key."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# API configuration\n",
"openai.api_key = os.getenv(\"OPENAI_KEY\")"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"def set_open_params(\n",
" model=\"text-davinci-003\",\n",
" temperature=0.7,\n",
" max_tokens=256,\n",
" top_p=1,\n",
" frequency_penalty=0,\n",
" presence_penalty=0,\n",
"):\n",
" \"\"\" set openai parameters\"\"\"\n",
"\n",
" openai_params = {} \n",
"\n",
" openai_params['model'] = model\n",
" openai_params['temperature'] = temperature\n",
" openai_params['max_tokens'] = max_tokens\n",
" openai_params['top_p'] = top_p\n",
" openai_params['frequency_penalty'] = frequency_penalty\n",
" openai_params['presence_penalty'] = presence_penalty\n",
" return openai_params\n",
"\n",
"def get_completion(params, prompt):\n",
" \"\"\" GET completion from openai api\"\"\"\n",
"\n",
" response = openai.Completion.create(\n",
" engine = params['model'],\n",
" prompt = prompt,\n",
" temperature = params['temperature'],\n",
" max_tokens = params['max_tokens'],\n",
" top_p = params['top_p'],\n",
" frequency_penalty = params['frequency_penalty'],\n",
" presence_penalty = params['presence_penalty'],\n",
" )\n",
" return response"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Basic prompt example:"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"# basic example\n",
"params = set_open_params()\n",
"\n",
"prompt = \"The sky is\"\n",
"\n",
"response = get_completion(params, prompt)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"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.\""
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response.choices[0].text"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"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."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"IPython.display.Markdown(response.choices[0].text)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Try with different temperature to compare results:"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"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."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"params = set_open_params(temperature=0)\n",
"prompt = \"The sky is\"\n",
"response = get_completion(params, prompt)\n",
"IPython.display.Markdown(response.choices[0].text)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.1 Text Summarization"
]
},
{
"cell_type": "code",
"execution_count": 34,
"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."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"params = set_open_params(temperature=0.7)\n",
"prompt = \"\"\"Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body's immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance. \n",
"\n",
"Explain the above in one sentence:\"\"\"\n",
"\n",
"response = get_completion(params, prompt)\n",
"IPython.display.Markdown(response.choices[0].text)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise: Instruct the model to explain the paragraph in one sentence like \"I am 5\". Do you see any differences?"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.2 Question Answering"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
" Mice."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt = \"\"\"Answer the question based on the context below. Keep the answer short and concise. Respond \"Unsure about answer\" if not sure about the answer.\n",
"\n",
"Context: Teplizumab traces its roots to a New Jersey drug company called Ortho Pharmaceutical. There, scientists generated an early version of the antibody, dubbed OKT3. Originally sourced from mice, the molecule was able to bind to the surface of T cells and limit their cell-killing potential. In 1986, it was approved to help prevent organ rejection after kidney transplants, making it the first therapeutic antibody allowed for human use.\n",
"\n",
"Question: What was OKT3 originally sourced from?\n",
"\n",
"Answer:\"\"\"\n",
"\n",
"response = get_completion(params, prompt)\n",
"IPython.display.Markdown(response.choices[0].text)\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Context obtained from here: https://www.nature.com/articles/d41586-023-00400-x"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise: Edit prompt and get the model to respond that it isn't sure about the answer. "
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.3 Text Classification"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
" Neutral"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt = \"\"\"Classify the text into neutral, negative or positive.\n",
"\n",
"Text: I think the food was okay.\n",
"\n",
"Sentiment:\"\"\"\n",
"\n",
"response = get_completion(params, prompt)\n",
"IPython.display.Markdown(response.choices[0].text)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise: Modify the prompt to instruct the model to provide an explanation to the answer selected. "
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.4 Role Playing"
]
},
{
"cell_type": "code",
"execution_count": 37,
"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."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt = \"\"\"The following is a conversation with an AI research assistant. The assistant tone is technical and scientific.\n",
"\n",
"Human: Hello, who are you?\n",
"AI: Greeting! I am an AI research assistant. How can I help you today?\n",
"Human: Can you tell me about the creation of blackholes?\n",
"AI:\"\"\"\n",
"\n",
"response = get_completion(params, prompt)\n",
"IPython.display.Markdown(response.choices[0].text)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise: Modify the prompt to instruct the model to keep AI responses concise and short."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.5 Code Generation"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"\n",
"SELECT StudentId, StudentName \n",
"FROM students \n",
"WHERE DepartmentId = (SELECT DepartmentId \n",
" FROM departments \n",
" WHERE DepartmentName = 'Computer Science')"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt = \"\\\"\\\"\\\"\\nTable departments, columns = [DepartmentId, DepartmentName]\\nTable students, columns = [DepartmentId, StudentId, StudentName]\\nCreate a MySQL query for all students in the Computer Science Department\\n\\\"\\\"\\\"\"\n",
"\n",
"response = get_completion(params, prompt)\n",
"IPython.display.Markdown(response.choices[0].text)\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.6 Reasoning"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"\n",
"\n",
"Odd numbers: 15, 5, 13, 7, 1\n",
"Total = 41\n",
"41 is an odd number."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prompt = \"\"\"The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1. \n",
"\n",
"Solve by breaking the problem into steps. First, identify the odd numbers, add them, and indicate whether the result is odd or even.\"\"\"\n",
"\n",
"response = get_completion(params, prompt)\n",
"IPython.display.Markdown(response.choices[0].text)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise: Improve the prompt to have a better structure and output format."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Advanced Prompting Techniques\n",
"\n",
"Objectives:\n",
"\n",
"- Cover more advanced techniques for prompting: few-shot, chain-of-thoughts,...\n",
"- Review more advanced applications"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "minprompts",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.15"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "872fbaa170678d9803e866eb8aab13838cd416716b835df572a04d4d73e81a04"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading…
Cancel
Save