ChatGPT Prompt Engineering for Developers - 学习笔记

ChatGPT Prompt Engineering for Developers - 学习笔记


非常推荐大家上这个来自吴恩达(DeepLearning.AI)和OpenAI联合推出的课程,虽然这个课程只有一个多小时的视频内容,但它涵盖了许多ChatGPT Prompt(提示词)的实用技巧。在这个课程中,你将学习如何更好地使用提示词来激发ChatGPT的创造力和语言生成能力,并且了解ChatGPT的应用场景和原理。

课程网址https://www.deeplearning.ai/short-courses/chatgpt-prompt-engineering-for-developers/

课程简介:In ChatGPT Prompt Engineering for Developers, you will learn how to use a large language model (LLM) to quickly build new and powerful applications. Using the OpenAI API, you’ll be able to quickly build capabilities that learn to innovate and create value in ways that were cost-prohibitive, highly technical, or simply impossible before now.


Introduction

Two Types of large language models (LLMs)

  • Base LLM: Predicts next word, based on text training data
  • Instruction Tuned LLM: Tries to follow instructions. Fine-tune on instructions and good attempts at following those instructions.

Guidelines

Principle 1: Write clear and specific instructions

Tactics:

  1. Use delimiters:
    1. Triple quotes
    2. Triple backticks
    3. Triple dashes
    4. Angle brackets
    5. XML tags
  2. Ask for structured output
    1. HTML
    2. JSON
  3. Check whether conditions are satisfied
    1. Check assumptions required to do the task
  4. Few-shot prompting
    1. Give successful examples of completing tasks
    2. Then ask model to perform the task

Principle 2: Give the model time to think

Tactics:

  1. Specify the steps to complete a task:
    Step 1: ...
    Step 2: ...
    Step 3: ...
  2. Instruct the model to work out its own solution before rushing to a conclusion

Model Limitations

Hallucination: Makes statements that sound plausible but are not true

Reducing hallucinations: First find relevant information, then answer the question based on the relevant information

Iterative

Prompt Guidelines

  • Be clear and specific
  • Analyze why result does not give desired output
  • Refine the idea and the prompt
  • Repeat

Iterative Process

  • Try something
  • Analyze where the result does not give what you want
  • Clarify instructions, give more time to think
  • Refine prompts with a batch of examples

Summarizing

  • Summarizing: “Summarize the review below in at most 30 words”
  • Extracting: “Extract relevant information from a product review to give feedback to the shipping department”

Inferring

  • “What is the sentiment of the following …”
  • “Identify a list of emotions from …”
  • “Identify the following items from the review text…”
  • “Determine five topics that are being discussed in the following test…”

Transforming

Translation

  • “Translate the following English text to Spanish: …”
  • “Tell me which language this is: …”

Tone Transformation

  • “Translate the following from slang to a business letter…”

Format Conversion

  • “Translate the following python dictionary from JSON to an HTML table”

Spellcheck/Grammar Check

  • “Proofread and correct the following text and rewrite the corrected version.”
  • “Proofread and correct this review”

Expanding

Generate long piece of text from short piece of text.

For example:

prompt = f"""
You are a customer service AI assistant.
Your task is to send an email reply to a valued customer.
Given the customer email delimited by ```, \
Generate a reply to thank the customer for their review.
If the sentiment is positive or neutral, thank them for \
their review.
If the sentiment is negative, apologize and suggest that \
they can reach out to customer service.
Make sure to use specific details from the review.
Write in a concise and professional tone.
Sign the email as `AI customer agent`.
Customer review: ```{review}```
Review sentiment: {sentiment}
"""

Temperature

Degree of randomness of the model.

  • Use temperature 0 for tasks that require reliability, predictability
  • Use temperature >0 for tasks that require variety

Chatbot

Put conversation in messages and send to ChatGPT API:

messages =  [
{'role':'system', 'content':'You are an assistant that speaks like Shakespeare.'},
{'role':'user', 'content':'tell me a joke'},
{'role':'assistant', 'content':'Why did the chicken cross the road'},
{'role':'user', 'content':'I don\'t know'}  ]

Every new request needs to include previous conversation.