Principles of Prompt Engineering

Nowadays, using large models with provided interfaces, prompts have become indispensable. They can generate document templates based on specified formats; generate summaries with different focuses; check written documents for typos and grammatical issues; help us read long texts and extract the content we care about; and even when automatically answering customer questions, prompts can guide the model to assess the personality of the questioner and respond in different tones, inclinations, and languages based on different personalities.

Andrew Ng and OpenAI’s collaboration course (https://www.deeplearning.ai/short-courses/) proposed two principles, which, although I am not using ChatGPT, are still very valuable for learning.

These two principles, from my understanding, essentially convey the same message: clarify the task and express every detail you want to convey to the model clearly. It feels like explaining a slightly complex problem to elementary school students. You need to pick out every known condition; once you clearly identify the known conditions, you can solve the problem. The model seems not very good at analyzing a single sentence that contains multiple known conditions, but as long as each known condition is expressed in a separate sentence, it can perform well. If it’s really hard to explain clearly, then provide an example for it to imitate.

Let’s take a look at these two principles (the course includes examples):

https://learn.deeplearning.ai/chatgpt-prompt-eng/lesson/2/guidelinesPrinciple 1: Write clear and specific instructionsPrinciple 2: Give the model time to “think”

The principles come with several strategies:

Strategy for Principle 1: Tactic 1: Use delimiters to clearly indicate distinct parts of the input, e.g., use special parts like: ```, """, <, >, <tag> </tag> to mark them. Tactic 2: Ask for a structured output, e.g., JSON, please use the following format: Text: <text> Summary: <summary> Translation: <translation> Names: <name list> Output JSON: <JSON containing English_summary and num_names> Text: <{text}>""" The first two strategies clarify the expression through structure.
Tactic 3: Ask the model to check whether conditions are satisfied to avoid making random guesses when unsure, such as writing down answers in an exam without knowing. Tactic 4: "Few-shot" prompting. Language descriptions are never as clear as examples.
Strategy for Principle 2: Tactic 1: Specify the steps required to complete a task, which means telling it the execution process. If the problem is too difficult, it’s not enough to just have known conditions; you also need to tell it how to use the known conditions, allowing it to focus on one part of the question at a time. Sometimes, you can also let it list the steps based on the text. Tactic 2: Instruct the model to work out its own solution before rushing to a conclusion, which is similar to multiple-choice questions; first treat it as an application question, then compare the options.

In summary, it’s about guiding patiently or providing examples for it to draw inferences.

Although the examples do not use OpenAI, they are just examples and cannot be truly used online. Although the one written in curl is vicuna, I have actually tested several models, and the results are quite similar, such as bing, Llama-2-13b, 百川 13B, and I finally applied for 文言一心. For local deployment, I used: https://github.com/lm-sys/FastChat

python3 -m fastchat.serve.cli --model-path /home/aaa/vicuna/vicuna-13b-v1.3  --device cpu
python3 -m fastchat.serve.controller
python3 -m fastchat.serve.openai_api_server --host [ip] --port 7799
python3 -m fastchat.serve.gradio_web_server
curl http://localhost:7799/v1/chat/completions -H "Content-Type: application/json" -d '{ "model": "vicuna-13b-v1.3", "messages": [{"role": "user", "content": "Hello! What is your name?"}] }'
curl http://222.28.98.238:7799/v1/completions \ -H "Content-Type: application/json" -d '{ "model":"vicuna-13b-v1.3", "prompt":"A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the users questions. USER: ...'''...'''ASSISTANT:", "temperature":0, "repetition_penalty":1.0, "top_p":1.0, "max_new_tokens":512 }' Another: "role": "system" can be used to specify a global instruction, such as you are an HR, then use user to communicate with it about resume issues (So here's an example of a list of messages and so the first message is a system message which kind of gives an overall instruction). The temperature is set to 0 by default to provide consistent results for front-end applications.

Let’s start with a gentle guidance. Due to the privacy of the information involved, I will omit some details. Initially, I directly asked whether the resume content indicated if the person was a good programmer, and the result was predictable. Let’s not mention it.

To organize a bit: The content included in three backticks is a programmer's resume. Please create a multiple-choice question based on the resume, and choose: a. Yes b. No c. Don't know. ``` Resume Content ```
The response is likely to be something like: Options: a. Yes; b. No; c. Don't know. Answer: a. Yes. Based on the resume content, ...... Therefore, option a. Yes is the correct answer.

The output shows a fixed, extractable format, but still contains analytical content.

It may be due to the model's understanding not being strong enough; I didn’t want just an option, so I told it I only wanted the option (not exceeding n characters, which had a slight effect, but not significant. However, I found that sometimes, without this, the request could time out): The content included in three backticks is a programmer's resume. Please create a multiple-choice question based on the resume, and choose from: a. Yes; b. No; c. Don't know. Select the option and respond with only the option letter, and the answer should not exceed 3 characters. ``` Resume Content ```
Clearly indicate the question: The content included in three backticks is a programmer's resume. Please create a multiple-choice question: Is this person a good programmer? Options: a. Yes; b. No; c. Don't know. Please select and only choose the letter of the option, and answer not exceeding 5 characters. ``` Resume Content ```
Then it could just respond with a. Slightly modifying it to make it more universal, removing non-significant influences: Based on the content in three backticks, create a multiple-choice question: Is this person a good programmer? Options: a. Yes; b. No; c. Don't know. Please select and only choose the letter of the option, and answer not exceeding 5 characters. ``` Resume Content ```

Next, I will give it an example of the desired response format, which is to provide an example for it to imitate, and the effect is very good:

The content included in three backticks is a programmer's resume. Please create a multiple-choice question based on the resume, and choose from: a. Yes; b. No; c. Don't know. ``` Resume Content ``` Use the format in angle brackets for output: Answer: <option letter>

The last sentence ‘Use the format in <> for output: Answer: <option letter>’ does not work well when placed at the front; removing the word ‘Answer’ also does not work. It may be because related instructions are grouped together for better understanding. In the testing process, I felt that the later it appears, the greater its impact on the output. Children are the same; what is said last has a greater impact.

Principles of Prompt Engineering

Principles of Prompt Engineering

Principles of Prompt Engineering

Only the 文言一心 showed slightly different effects:

Principles of Prompt Engineering

After testing different temperatures, the responses became unstable:

The three most common responses: a a. Yes Answer: a. Yes. After reaching 0.75, it occasionally chose b and c.

Another example of a specified format:

"You are now a doctor. The text included in three backticks is a patient's case. Please tell me what diseases the patient has or has had: ```%s``` Only list the names of the diseases." Based on the provided case, the patient may have asthma and a history of left elbow joint fracture.
"You are now a doctor. The text included in three backticks is a patient's case. Answer in the format "Disease: ..." Only respond with the name of the disease the patient has, case: ```%s```" % (txt) Disease: Asthma.

I previously asked whether a fracture is a disease, and the model I used at that time answered no; different models have different understandings. Bing’s:

Principles of Prompt Engineering

Principles of Prompt Engineering

As for Baidu’s 文言:

Principles of Prompt Engineering

"You are now a doctor. The text included in three backticks is a patient's case. Answer in the format "Disease: ..." Only respond with the name of the disease the patient has." Another use of the word illness: Based on the case description, the patient has a history of asthma and fractures.
"You are now a doctor. The text included in three backticks is a patient's case. Answer in the format "Patient's medical history includes: ..." Only respond with the name of the disease the patient has." It can stably answer two: Patient's medical history includes: Asthma, Fracture.

These two examples also show that it always requires a process of continuous adjustment and iteration. The course also mentioned:

When I've been building applications with large language models, I don't think I've ever come to the prompt that I ended up using in the final application on my first attempt. https://learn.deeplearning.ai/chatgpt-prompt-eng/lesson/3/iterative

Now there are many shares of prompts available; just search Baidu to find many. Of course, due to some reasons, the prompts for generating images are organized more systematically, such as (https://prompthero.com/). After all, when SD first came out, there was the Elemental Codex, and the link in the previously published Image Sharing still exists. Now there is also ComfyUI, which is a magical tool that allows long text descriptions for drawing, and a new tool Fooocus has emerged.

Principles of Prompt Engineering

Leave a Comment