Prompt Engineering 简明教程

Prompt Engineering - FIND Prompt

FIND 提示使我们可以从 ChatGPT 生成的响应中提取特定信息或执行搜索。通过使用 FIND 指令,我们可以指示语言模型根据特定标准查找并呈现相关详细信息,增强生成的输出的精确性和有用性。

Understanding the FIND Directive

FIND 指令使我们能够指定搜索模式或标准以在 ChatGPT 生成的响应中找到特定信息。它提供了一种方式来从模型的输出中以编程方式搜索和提取相关详细信息。

用于 FIND 指令的基本 syntax 如下 −

User: Can you provide a summary of the novel "Pride and Prejudice"?
ChatGPT: "Pride and Prejudice" is a classic novel written by Jane Austen. It explores themes of love, class, and societal expectations. [FIND: themes]

在这个示例中,用户要求总结小说“傲慢与偏见”,ChatGPT 的响应包括 FIND 指令中指定的内容,在本例中是与“主题”相关的信息。

Best Practices for Using the FIND Directive

To make the most of the FIND directive, consider the following best practices −

  1. Be Specific − Clearly define the search pattern or criteria within the FIND directive. This helps ensure that the model locates the desired information accurately.

  2. Contextual Prompts − Incorporate the FIND directive within a contextually rich prompt. By providing relevant context along with the directive, we can guide the model’s understanding and improve the accuracy of the search.

  3. Iterate and Refine − Experiment with different search patterns and criteria to find the most effective way to extract the desired information. Iterate and refine our prompts based on the results obtained.

  4. Combine with Other Techniques − The FIND directive can be used in conjunction with other prompt engineering techniques, such as the INCLUDE directive or COLUMN directive, to further enhance the generated output. Consider combining multiple techniques to achieve our desired results.

Example Application − Python Implementation

Let’s explore a practical example of using the FIND directive with a Python script that interacts with ChatGPT.

In this example, we define a function generate_chat_response() that takes a prompt and uses the OpenAI API to generate a response using ChatGPT.

The chat_prompt variable contains the user’s prompt and the ChatGPT response, including the FIND directive to search for information related to "themes."

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=100,
      temperature=0.7,
      n=1,
      stop=None
   )
   return response

user_prompt = "User: Can you provide a summary of the novel 'Pride and Prejudice'?\n"
chat_prompt = user_prompt + "ChatGPT: 'Pride and Prejudice' is a classic novel written by Jane Austen. It explores themes of love, class, and societal expectations. [FIND: themes]"

response = generate_chat_response(chat_prompt)
print(response)

Output

When we run the script, we will receive the generated response from ChatGPT, including the extracted details based on the specified search pattern.

The novel follows the five Bennet sisters, Elizabeth, Jane, Lydia, Mary, and Kitty, who are all looking for love and marriage. Elizabeth and her older sister Jane both fall in love with different men, but are faced with obstacles as they must battle society's expectations, their own pride, and the prejudice of others. The novel ultimately ends with the two sisters finding true love and happiness.

Conclusion

In this chapter, we explored the power of the FIND directive in prompt engineering for ChatGPT. By using the FIND directive, we can extract specific information or perform searches within the generated responses.

We discussed the syntax of the FIND directive and provided best practices for its usage, including being specific, using contextual prompts, iterating and refining, and combining with other prompt engineering techniques.