01 July 2023

Prompt Engineering Tips

I found this interesting guide and corresponding GitHub page for Prompt Engineering. Here's a summary of it:

Large Language Models (LLM's) can have some parameters to control the output:

  • Temperature: High value gives more creativity. Low value gives more factual answers.
  • Top p: A sampling method with temperature. High and low values results are similar to temperature.

Select either temperature or top-p. Not both.

Good prompts can (but don't always have to) have an instruction, context, input and output type.


Without verbosity

What are some activation functions used in Neural Networks. No explanation. Direct answer.

Completion tasks. Zero-shot and few-shot prompting

For the LLM to complete a sentence, give it context about what you want it to do:

Rather than use "The grass is".  Try this:

Complete the sentence:

The grass is

You can even use comment slashes as prompt hints:

This is awesome! // Positive
This is bad! // Negative
Wow that movie was rad! // Positive
What a horrible show! //

The LLM will output "Negative".

Use markers like ### if necessary

### Instruction ###
Translate the text below to French:
Text: "How are you"

Be specific to extract text

Extract the names of vegetables and fruits in the following text:
Format as:
<vegetables>: <list of comma separated names of vegetables>
<fruits>: <list of comma separated names of fruits>

Input: "Ravi spent ten dollars on apples, fifty dollars on mangoes. Jyothi decided to buy some tomatoes, onions, mulberries, pineapple and drumstick. Tinku bought some bread and egg puffs."
 

Instruction: Classify these names into ethnicity like European, Asian or African:
Text: "Wong Fei Hung visited Europe to meet John Durham during the Summer when Abidemi Bamidele and Karan Sinha were planning to meet them in America"
Ethnicity:

 

Role prompting

This is a conversation of a person trying to explain smartphones in a way that their grandmother can understand it. The grandmother does not know technical terms, so the answers should be simple and should explain it with tasks and items that the grandmother is familiar with:
Grandmother: Explain how the phone is charged.
Helper: Similar to how you use a battery to run a clock, the smartphone has a battery which you don't have to replace when it runs out of charge. You can connect it to an electrical socket, and the electricity will get stored in the battery again. Like re-filling a bucket with water.
Grandmother: Explain how the smartphone knows which part of the screen you touched it at.
Helper:

It can be simpler:

You are an interviewer. Ask me ten questions one by one and give me feedback about each of my answers. The interview is for the role of a fullstack developer. Technologies used: Python, AWS, Django, Redis, PostgreSQL, MongoDB, Kubernetes, Docker, NGINX.

Code generation

I have two database tables db1 and db2. The db1 table has columns id, recipe_ingredient_name and quantity. The db2 table has columns id, recipe_name, ingredients_required and recipe text. Write an SQL query to join data from both tables based on id, where recipe ingredients match.

Chain of Thought (CoT) prompting

This is one of the most interesting examples of CoT I created.

You are driving a bus containing 50 people. At the first stop, a man named Jack gets off the bus. Now there are 49 people in the bus. At the second stop, 5 people named Tim, Brad, Janet, Julia and Yennifer enter the bus, and 6 people named Obama, Bush, Clinton, Bill, Jeff, Elon get out of the bus. What is the name of the bus driver?

ChatGPT couldn't figure it out.When I mentioned "The question mentions that you are the bus driver", it responded with "Apologies for misunderstanding your question. In that case, as the bus driver in this scenario, my name would be AI since I am an AI language model assisting you in this conversation".

It can also be given a hint like this (which is more useful in arithmetic scenarios):

You are driving a bus containing 50 people. At the first stop, a man named Jack gets off the bus. Now there are 49 people in the bus. At the second stop, 5 people named Tim, Brad, Janet, Julia and Yennifer enter the bus, and 6 people named Obama, Bush, Clinton, Bill, Jeff, Elon get out of the bus. What is the name of the bus driver?
Hint: Work step by step to determine the name of the bus driver even if the name is not explicitly mentioned.

But this works better:

You are driving a bus containing 50 people. At the first stop, a man named Jack gets off the bus. Now there are 49 people in the bus. At the second stop, 5 people named Tim, Brad, Janet, Julia and Yennifer enter the bus, and 6 people named Obama, Bush, Clinton, Bill, Jeff, Elon get out of the bus. What is the name of the bus driver?
Hint: ignore the number of passengers and consider who drives the bus even if the name is not mentioned

There are more prompting techniques: 

  • Auto-CoT: Clusters various types of questions, selects the appropriate cluster and generates reasoning using zero-shot technique.
  • Self consistency:Generates multiple outputs and selects the most consistent one.
  • Generate knowledge: Generate nuggets of knowledge which are then used for reasoning.
  • Tree of thought: Explores various paths of reasoning.
  • Retrieval augmented generation (RAG): Access and incorporate external knowledge to improve reasoning.
  • Automatic reasoning and tool use (ART): Stops the reasoning process in-between to incorporate new knowledge and then continue reasoning.
  • Auto-prompt engineer (APE): Automatically generates prompts that are better than what we generate.
  • Active prompt: Questions are selected for annotating various task-specific prompts, and the most uncertain ones are sent to humans for annotation. 
  • Directional stimulus prompting: Performing tasks using a hint which provides a direction of approach.
  • ReAct: Interleaves Reasoning and Act (performing a task) multiple times to find an answer.
  • Multimodal CoT: Incorporates computer vision and text to get an answer.
  • Graph prompting: Tasks find the most relevant knowledge from the pre-train model in a task-specific manner.

Text to image prompting

This is a field where one needs a lot of knowledge of how to prompt and also how to modify the image after it is generated. I found this repository, these prompt templates and this pdf to be helpful. You can try StableDiffusionOnline's playground too. To modify an image after generation, you can use img2img.


No comments: