吴恩达与OpenAI官方合作的ChatGPT提示工程课程笔记

这篇笔记详细介绍了如何利用吴恩达与OpenAI合作的课程进行ChatGPT提示工程,涉及LLMs的原理、提示指南、模型限制、迭代开发过程及在软件应用中的各种用途,如文本摘要、推理、转换和聊天机器人构建等。

吴恩达与OpenAI官方合作的ChatGPT提示工程课程笔记

LLMs(large language models)

  • Base LLM:基于文本训练数据来预测做“文字接龙”
  • Instruction Tuned LLM(指令调整型LLM):接受了遵循指示的培训,可以根据提前培训的输入输出对结果进行调整

提示指南

两个关键原则

编写明确和具体的指令(明确 ≠ 短)

  • 策略一:用分隔符清楚的指示输入的不同部分

    可以使用”””,`````,- - -< ><tag> </tag>。使用分隔符的好处:可以避免提示词冲突,提示冲突是指如果允许用户向提示中添加一些输入,则他们可能会给出与我们想要的任务不符的指令,导致模型遵循用户的指令而不是我们自己想要的指令

    • 栗子:

      import openai
      import os

      from dotenv import load_dotenv, find_dotenv
      _ = load_dotenv(find_dotenv())

      openai.api_key = os.environ.get("OPENAI_API_KEY")

      def get_completion(prompt, model="gpt-3.5-turbo"):
          messages = [{ "role""user""content": prompt}]
          response = openai.ChatCompletion.create(
              model=model,
              messages=messages,
              temperature=0# this is the degree of randomness of the model's output
          )
          return response.choices[0].message["content"]

      text = f"""
      You should express what you want a model to do by \
      providing instructions that are as clear and \
      specific as you can possibly make them. \
      This will guide the model towards the desired output, \
      and reduce the chances of receiving irrelevant \
      or incorrect responses. Don't confuse writing a \
      clear prompt with writing a short prompt. \
      In many cases, longer prompts provide more clarity \
      and context for the model, which can lead to \
      more detailed and relevant outputs.
      """

      prompt = f"""
      Summarize the text delimited by triple backticks \
      into a single sentence.
      ```{text}```
      """

      response = get_completion(prompt)
      print(response)

      输出:

      Clear and specific instructions should be provided to guide a model towards the desired output, and longer prompts can provide more clarity and context for the model, leading to more detailed and relevant outputs.
  • 策略二:要求结构化输出

    可以使用HTML或JSON等结构化输出

    • 栗子:

      import openai
      import os

      from dotenv import load_dotenv, find_dotenv
      _ = load_dotenv(find_dotenv())

      openai.api_key = os.environ.get("OPENAI_API_KEY")

      def get_completion(prompt, model="gpt-3.5-turbo"):
          messages = [{ "role""user""content": prompt}]
          response = openai.ChatCompletion.create(
              model=model,
              messages=messages,
              temperature=0# this is the degree of randomness of the model's output
          )
          return response.choices[0].message["content"]

      prompt = f"""
      Generate a list of three made-up book titles along \
      with their authors and genres.
      Provide them in JSON format with the following keys:
      book_id, title, author, genre.
      """

      response = get_completion(prompt)
      print(response)s s  

      输出:

      [
        {
          "book_id"1,
          "title""The Lost City of Zorath",
          "author""Aria Blackwood",
          "genre""Fantasy"
        },
        {
          "book_id"2,
          "title""The Last Survivors",
          "author""Ethan Stone",
          "genre""Science Fiction"
        },
        {
          "book_id"3,
          "title""The Secret of the Haunted Mansion",
          "author""Lila Rose",
          "genre""Mystery"
        }
      ]
  • 策略三:要求模型检查是否满足条件

    如果模型存在其他情况,可以要求模型检查并返回不满足条件时的响应

    • 栗子1


      import openai
      import os

      from dotenv import load_dotenv, find_dotenv
      _ = load_dotenv(find_dotenv())

      openai.api_key = os.environ.get("OPENAI_API_KEY")

      def get_completion(prompt, model="gpt-3.5-turbo"):
          messages = [{ "role""user""content": prompt}]
          response = openai.ChatCompletion.create(
              model=model,
              messages=messages,
              temperature=0# this is the degree of randomness of the model's output
          )
          return response.choices[0].message["content"]
      text_1 = f"""
      Making a cup of tea is easy! First, you need to get some \
      water boiling. While that's happening, \
      grab a cup and put a tea bag in it. Once the water is \
      hot enough, just pour it over the tea bag. \
      Let it sit for a bit so the tea can steep. After a \
      few minutes, take out the tea bag. If you \
      like, you can add some sugar or milk to taste. \
      And that's it! You've got yourself a delicious \
      cup of tea to enjoy.
      """

      prompt = f"""
      You will be provided with text delimited by triple quotes.
      If it contains a sequence of instructions, \
      re-write those instructions in the following format:

      Step 1 - ...
      Step 2 - …

      Step N - …

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

      \"\"\"{text_1}\"\"\"
      """

      response = get_completion(prompt)
      print("Completion for Text 1:")
      print(response)

      输出

      Completion for Text 1:
      Step 1 - Get some water boiling.
      Step 2 - Grab a cup and put a tea bag in it.
      Step 3 - Once the water is hot enough, pour it over the tea bag.
      Step 4 - Let it sit for a bit so the tea can steep.
      Step 5 - After a few minutes, take out the tea bag.
      Step 6 - Add some sugar or milk to taste.
      Step 7 - Enjoy your delicious cup of tea!
    • 栗子2

      import openai
      import os

      from dotenv import load_dotenv, find_dotenv
      _ = load_dotenv(find_dotenv())

      openai.api_key = os.environ.get("OPENAI_API_KEY")

      def get_completion(prompt, model="gpt-3.5-turbo"):
          messages = [{ "role""user""content": prompt}]
          response = openai.ChatCompletion.create(
              model=model,
              messages=messages,
              temperature=0# this is the degree of randomness of the model's output
          )
          return response.choices[0].message["content"]
      text_2 = f"""
      The sun is shining brightly today, and the birds are \
      singing. It's a beautiful day to go for a \ 
      walk in the park. The flowers are blooming, and the \ 
      trees are swaying gently in the breeze. People \ 
      are out and about, enjoying the lovely weather. \ 
      Some are having picnics, while others are playing \ 
      games or simply relaxing on the grass. It's a \ 
      perfect day to spend time outdoors and appreciate the \ 
      beauty of nature.
      """

      prompt = f"""
      You will be provided with text delimited by triple quotes. 
      If it contains a sequence of instructions, \ 
      re-write those instructions in the following format:

      Step 1 - ...
      Step 2 - …

      Step N - …

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

      \"\"\"{text_2}\"\"\"
      """

      response = get_completion(prompt)
      print("Completion for Text 2:")
      print(response)

      输出:

      Completion for Text 2:
      No steps provided.
  • 策略四:少量训练提示

    这是在要求模型执行任务之前,提示成功执行任务的示例

    • 栗子

      import openai
      import os

      from dotenv import load_dotenv, find_dotenv
      _ = load_dotenv(find_dotenv())

      openai.api_key = os.environ.get("OPENAI_API_KEY")

      def get_completion(prompt, model="gpt-3.5-turbo"):
          messages = [{ "role""user""content": prompt}]
          response = openai.ChatCompletion.create(
              model=model,
              messages=messages,
              temperature=0# this is the degree of randomness of the model's output
          )
          return response.choices[0].message["content"]
      prompt = f"""
      Your task is to answer in a consistent style.

      <child>: Teach me about patience.

      <grandparent>: The river that carves the deepest \
      valley flows from a modest spring; the \
      grandest symphony originates from a single note; \
      the most intricate tapestry begins with a solitary thread.

      <child>: Teach me about resilience.
      """

      response = get_completion(prompt)
      print(response)

      输出:

      <grandparent>: Resilience is like a tree that bends with the wind but never breaks. It is the ability to bounce back from adversity and keep moving forward, even when things get tough. Just like a tree that grows stronger with each storm it weathers, resilience is a quality that can be developed and strengthened over time.

给模型足够的时间来思考

如果模型急于做出错误的结论而出现推理错误,应该尝试重新查询请求相关推理的链或序列,直到模型提供最终答案。如果我们给模型一个太复杂的任务,让他在短时间内或用少数词完成,他可能会猜测结果,但结果可能不正确。就像人一样,如果我们要短时间内完成复杂的数学计算可能也会出现错误。

  • 策略一:指定完成任务所需的步骤

    • 栗子1

      text = f"""
      In a charming village, siblings Jack and Jill set out on \ 
      a quest to fetch water from a hilltop \ 
      well. As they climbed, singing joyfully, misfortune \ 
      struck—Jack tripped on a stone and tumbled \ 
      down the hill, with Jill following suit. \ 
      Though slightly battered, the pair returned home to \ 
      comforting embraces. Despite the mishap, \ 
      their adventurous spirits remained undimmed, and they \ 
      continued exploring with delight.
      """

      # example 1
      prompt_1 = f"""
      Perform the following actions: 
      1 - Summarize the following text delimited by triple \
      backticks with 1 sentence.
      2 - Translate the summary into French.
      3 - List each name in the French summary.
      4 - Output a json object that contains the following \
      keys: french_summary, num_names.

      Separate your answers with line breaks.

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

      response = get_completion(prompt_1)
      print("Completion for prompt 1:")
      print(response)

      输出:

      Completion for prompt 1:
      Two siblings, Jack and Jill, go on a quest to fetch water from a hilltop well, but misfortune strikes as they both fall down the hill, yet they return home slightly battered but with their adventurous spirits undimmed.

      Deux frères et sœurs, Jack et Jill, partent en quête d'eau d'un puits au sommet d'une colline, mais ils tombent tous les deux et retournent chez eux légèrement meurtris mais avec leur esprit d'aventure intact. 
      Noms: Jack, Jill
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值