Prompt Engineering 简明教程

Prompt Engineering - INCLUDE Prompt

INCLUDE 提示允许我们在由 ChatGPT 生成的应答中包含特定信息。通过使用 INCLUDE 指令,我们可以指示语言模型在其输出中包含某些细节、事实或短语,从而增强对生成应答的控制。

The INCLUDE prompt allows us to include specific information in the response generated by ChatGPT. By using the INCLUDE directive, we can instruct the language model to include certain details, facts, or phrases in its output, thereby enhancing control over the generated response.

Understanding the INCLUDE Directive

INCLUDE 指令是一条特殊指令,可以嵌入到提示中,以引导 ChatGPT 的行为。它使我们能够指定我们希望该模型将其内容纳入其响应中的内容。当模型遇到 INCLUDE 指令时,它将其解释为在其生成的输出中包括以下信息的信号。

The INCLUDE directive is a special instruction that can be embedded within the prompt to guide ChatGPT’s behavior. It enables us to specify the content that we want the model to incorporate into its response. When the model encounters the INCLUDE directive, it interprets it as a signal to include the following information in its generated output.

INCLUDE 指令的基本 syntax 如下 −

The basic syntax for the INCLUDE directive is as follows −

User: How does photosynthesis work?
ChatGPT: Photosynthesis is a process by which plants convert sunlight into energy. [INCLUDE: Chlorophyll, Carbon dioxide, and Water]

在这个示例中,用户询问有关光合作用的问题,而 ChatGPT 的响应包括 INCLUDE 指令中指定的内容,即“叶绿素、二氧化碳和水”。通过使用 INCLUDE 指令,我们可以确保在响应中包含特定详细信息,从而提供更全面的答案。

In this example, the user asks a question about photosynthesis, and the response from ChatGPT includes the content specified within the INCLUDE directive, namely "Chlorophyll, Carbon dioxide, and Water." By using the INCLUDE directive, we can ensure that specific details are included in the response, providing a more comprehensive answer.

Best Practices for Using the INCLUDE Directive

为了充分利用 INCLUDE 指令,以下是一些需要牢记的最佳实践 −

To make the most of the INCLUDE directive, here are some best practices to keep in mind −

  1. Be Specific − Specify the exact details, facts, or phrases that we want to include in the response. This helps ensure that the model includes the desired information accurately.

  2. Limit the Length − While the INCLUDE directive can be useful for including additional information, be mindful of the response length. Including too much content may result in excessively long or verbose responses. Strike a balance and include only the most relevant details.

  3. Use Contextual Prompts − Incorporate the INCLUDE directive within a contextually rich prompt. By providing relevant context along with the directive, we can guide the model’s understanding and produce more accurate and coherent responses.

  4. Experiment and Iterate − Prompt engineering is an iterative process. Test different variations of the INCLUDE directive and observe how the model responds. Adjust and refine our prompts based on the results we obtain.

Example − Python Implementation

我们来探索一个在 Python 脚本中使用 INCLUDE 指令的实际示例。我们将利用 OpenAI API 与 ChatGPT 进行交互。

Let’s explore a practical example of using the INCLUDE directive in a Python script. We will utilize the OpenAI API to interact with ChatGPT.

在这个示例中,用户询问“光合作用是如何进行的?”,他特别提到响应应包括“叶绿素”、“二氧化碳”和“水”等词。

In this example, the user asks "How does photosynthesis work?" and he specifically mentions that the response should INCLUDE the words "Chlorophyll", "Carbon dioxide, and "Water".

import openai
# Set your API key here
openai.api_key = 'YOUR_API_KEY'

def generate_chat_response(prompt):
   response = openai.Completion.create(
      engine="text-davinci-003",
      prompt=prompt,
      max_tokens=50,
      temperature=0.8,
      n=1,
      stop=None
   )
   return response

user_prompt = "User: How does photosynthesis work?\n"
chat_prompt = user_prompt + "ChatGPT: Photosynthesis is a processby which plants convert sunlight into energy. [INCLUDE: Chlorophyll, Carbon dioxide, and Water]"

response = generate_chat_response(chat_prompt)
print(response)

Output

Sunlight is absorbed by chlorophyll, which is located in the leaves of a plant. The energy from the sunlight is then used to convert carbon dioxide and water into glucose (sugar) and oxygen. The glucose is then used by the plant to produce energy.

Conclusion

在本章中,我们探索了 INCLUDE 指令在提示工程中的强大功能。通过使用 INCLUDE 指令,我们可以引导 ChatGPT 将特定细节、事实或短语纳入其生成的响应中。

In this chapter, we explored the power of the INCLUDE directive in prompt engineering. By using the INCLUDE directive, we can guide ChatGPT to incorporate specific details, facts, or phrases into its generated responses.

我们讨论了 INCLUDE 指令的语法,并提供了有关其用法的最佳实践,包括具体化、限制包含内容的长度、使用上下文提示以及迭代来优化提示。

We discussed the syntax of the INCLUDE directive and provided best practices for its usage, including being specific, limiting the length of included content, using contextual prompts, and iterating to refine our prompts.

此外,我们展示了一个实用的 Python 实现,演示了如何使用 INCLUDE 指令与 OpenAI API 一起使用,与 ChatGPT 进行交互并获得包括指定信息的响应。

Furthermore, we presented a practical Python implementation demonstrating how to use the INCLUDE directive with the OpenAI API to interact with ChatGPT and obtain responses that include the specified information.