不求谌解

不求谌解

💻 Web Dev / Creative 💗 ⚽ 🎧 🏓
twitter
github
jike
email

Prompt Engineering for Developers

title: Prompt Engineering for Developers
date: 2023-06-20
tags:


Before Getting Started#

Prompt Engineering is the process of effectively communicating with AI to achieve the desired results. The focus of this article is not on why Prompt Engineering (PE) and its related principles are needed, but interested readers can refer to this article 1.

This article aims to introduce some useful techniques for developers in Prompt Engineering 2, as well as analyze their specific applications in open-source projects.

Some Techniques#

Write Clear and Specific Instructions#

Use Separators#

Separators can take any form, for example:

'''text'''

"""text"""

< text >

<tag>text</tag>
const text = `
Express the task you want the model to perform by providing clear and specific instructions.
This will guide the model towards the desired output and reduce the likelihood of receiving irrelevant or incorrect responses.
Do not confuse writing clear prompts with writing short prompts.
In most cases,
longer prompts can provide the model with more explicit context, resulting in more detailed and relevant output.
`

const prompt = `
Summarize the text enclosed in three double quotation marks in one sentence.
"""${text}"""
`

Structure the Output#

For example, the output format can be JSON or HTML.

const prompt = `
Generate a list of book titles, authors, and genres for three fictional books.
Provide the following keys in JSON format: book_id, title, author, genre.
`

Check if Conditions are Met#

const text = `
Making a cup of tea is easy! First, you need to boil some water.
While the water is boiling, take a cup and put a tea bag in it.
Then pour the boiling water over the tea bag.
Let it steep for a while, and the tea will be ready.
After a few minutes, remove the tea bag. You can also add some sugar or milk if you like.
That's it! You can enjoy your freshly brewed tea.
`

const prompt = `
If the text contains a series of instructions, rewrite these instructions in the following format:

Step 1 - ...
Step 2 - ...
...
Step N - ...

If the text does not contain a series of instructions, simply write "No steps provided."

"""${text}"""
`

"Few-shot" Prompts#

"Few-shot" prompts 3 refer to providing a limited number of examples to guide the AI model in performing tasks. This is a technique commonly used in training large language models (LLMs).

The steps for few-shot prompting are as follows:

  1. Choose the domain or topic in which you want the model to generate responses. It can be a text type, language style, etc.

  2. Provide a small number of examples (prompts) to the model as conditioning for subsequent examples. Usually, providing 2-5 examples is sufficient for "few-shot" learning.

  3. The model will analyze patterns, styles, and structures in the prompts. It will learn the properties that define the desired responses in that domain.

  4. Let the model generate new responses in the same domain. Through conditioning on the prompts, it can generate responses that match the desired style, structure, etc.

  5. Evaluate the responses and provide feedback to further improve the model. This can be direct feedback to the model or simply noting areas for improvement in the set of prompts generated in the domain.

Give the Model Time to "Think"#

Specify the Steps Required to Complete the Task#

const text = `
In a beautiful village, there were two siblings, Jack and Jill. One day, they set out to fetch water from a well at the top of a hill.
As they joyfully sang songs while climbing the hill, an unfortunate event occurred - Jack tripped on a stone, rolled down the slope, and Jill followed suit.
Although they were slightly injured, fortunately, both of them returned home safely. Despite the mishap, their adventurous spirit remained undiminished, and they would continue to explore nature.
`

// Example
const prompt = `
Perform the following actions:
1- Summarize the given text in one sentence.
2- Translate the summary into French.
3- List each name in the French summary.
4- Output a JSON object with the following keys: french_summary, num_names.

Provide the answers using line breaks.

Text:
"""${text}"""
`

Instruct the Model to Solve the Problem on Its Own Before Making a Decision#

const prompt = `
Your task is to determine if the student's solution is correct.
To solve the problem, do the following:
- First, solve the problem on your own.
- Then compare your solution with the student's solution and evaluate if the student's solution is correct.
Do not decide if the student's solution is correct before solving the problem on your own.

Use the following format:

Problem:

"""
Problem
"""

Student's Solution:

"""
Student's Solution
"""

Actual Solution:

"""
Steps of the solution and your solution go here
"""

Is the student's solution the same as the actual solution?

""
Yes or No
"""

Student's Grade:

"""
Correct or Incorrect
"""

Problem:

"""
I am building a solar power plant and need help with a financial problem.
-The cost of land is $100 per square foot.
-I can buy solar panels at a price of $250 per square foot.
-I have negotiated a maintenance contract that will cost a fixed $100,000 per year, plus an additional $10 per square foot.
Please calculate the total cost for the first year.
"""

Student's Solution:

"""
Let x be the installation area in square feet.
Cost:
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000+10x
Total cost: 100x+250x+100,000+10x=360x+100,000
"""

Actual Solution:
`

Lessons from Others#

ai-code-translator#

This project 4 allows code translation between different programming languages. It utilizes some of the techniques mentioned earlier, such as using separators to indicate the input and output programming languages, and providing "few-shot" prompts.

const prompt = `
You are an expert programmer in all programming languages. Translate the "${inputLanguage}" code to "${outputLanguage}" code. Do not include \`\`\`.
  
      Example translating from JavaScript to Python:
  
      JavaScript code:
      for (let i = 0; i < 10; i++) {
        console.log(i);
      }
  
      Python code:
      for i in range(10):
        print(i)
      
      ${inputLanguage} code:
      ${inputCode}

      ${outputLanguage} code (no \`\`\`):
     `;
`

gpt-engineer#

Let's take a look at a more complex project - generating an entire codebase based on descriptions 5. This project uses a lot of prompts. Here are some steps analyzed with the prompts used in each step.

Mermaid Loading...
const prompt_on_respec = `
You are a pragmatic principal engineer at Google.
You have been asked to review a specification for a new feature by a previous version of yourself

You have been asked to give feedback on the following:
- Is there anything that might not work the way intended by the instructions?
- Is there anything in the specification missing for the program to work as expected?
- Is there anything that can be simplified without significant drawback?

You are asked to make educated assumptions for each unclear item.
For each of these, communicate which assumptions you'll make when implementing the feature.

Think step by step to make sure we don't miss anything.
`
const prompt_on_gen_code = `
Please now remember the steps:

Think step by step and reason yourself to the right decisions to make sure we get it right.
First lay out the names of the core classes, functions, methods that will be necessary, As well as a quick comment on their purpose.

Then you will output the content of each file including ALL code.
Each file must strictly follow a markdown code block format, where the following tokens must be replaced such that
FILENAME is the lowercase file name including the file extension,
LANG is the markup code block language for the code's language, and CODE is the code:

FILENAME
CODE

Please note that the code should be fully functional. No placeholders.

You will start with the "entrypoint" file, then go to the ones that are imported by that file, and so on.
Follow a language and framework appropriate best practice file naming convention.
Make sure that files contain all imports, types etc. The code should be fully functional. Make sure that code in different files are compatible with each other.
Before you finish, double check that all parts of the architecture is present in the files.
`

Footnotes#

  1. How ChatGPT works: a deep dive

  2. The main examples in this article are from deeplearning.ai

  3. Few-Shot Prompting

  4. ai-code-translator

  5. gpt-engineer

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.