Prompt Engineering 简明教程

Prompt Engineering - COLUMN Prompt

COLUMN 提示是一个强大的技术,它使我们能够对 ChatGPT 生成的响应进行结构化和格式化。通过使用 COLUMN 指令,我们可以创建结构化输出、以表格形式组织信息,并以清晰有序的方式呈现模型的响应。

The COLUMN prompt is a powerful technique that enables us to structure and format the responses generated by ChatGPT. By utilizing the COLUMN directive, we can create structured outputs, organize information in tabular form, and present the model’s responses in a clear and organized manner.

Understanding the COLUMN Directive

COLUMN 指令允许我们定义列,并在生成的响应中设置这些列中的内容的格式。当我们要以表格格式呈现信息或者我们需要以特定方式组织输出时,这尤其有用。

The COLUMN directive allows us to define columns and format the content within those columns in the generated response. This is particularly useful when we want to present information in a table-like format or when we need to structure the output in a specific way.

COLUMN 指令通过指定列标题以及每列中的相应内容起作用。

The COLUMN directive works by specifying column headers and the corresponding content within each column.

COLUMN 指令的基本 syntax 如下所示 −

The basic syntax for the COLUMN directive is as follows −

User: Can you compare the features of smartphones X and Y?
ChatGPT: Sure! Here's a comparison of the features:
------------------------------------------------------
| **Features** | **Smartphone X** | **Smartphone Y** |
|--------------|------------------|------------------|
| Camera       | 12 MP            | 16 MP            |
| Battery      | 3000 mAh         | 4000 mAh         |
| Storage      | 64 GB            | 128 GB           |
------------------------------------------------------

在这个示例中,用户要求比较智能手机 X 和 Y。来自 ChatGPT 的响应包括使用 COLUMN 指令创建的比较表格。表格包括列标题(“特性”、“智能手机 X”、“智能手机 Y”)和每列中的相应内容。

In this example, the user requests a comparison of smartphones X and Y. The response from ChatGPT includes the comparison table, created using the COLUMN directive. The table consists of column headers ("Features," "Smartphone X," "Smartphone Y") and the corresponding content within each column.

Best Practices for Using the COLUMN Directive

为了充分利用 COLUMN 指令,请考虑以下最佳实践 −

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

  1. Define Column Headers − Clearly define the headers for each column to provide context and facilitate understanding. Column headers act as labels for the information presented in each column.

  2. Organize Content − Ensure that the content within each column aligns correctly. Maintain consistent formatting and alignment to enhance readability.

  3. Limit Column Width − Consider the width of each column to prevent excessively wide tables. Narrower columns are easier to read, especially when the information is lengthy or there are many columns.

Use Markdown or ASCII Tables − COLUMN 指令可以与 Markdown 或 ASCII 表格格式化相结合来创建视觉上吸引人、结构完善的表格。Markdown 或 ASCII 表格生成器可以用于自动为我们设置表格格式。

Use Markdown or ASCII Tables − The COLUMN directive can be combined with Markdown or ASCII table formatting to create visually appealing and well-structured tables. Markdown or ASCII table generators can be used to automatically format the table for us.

Example Application − Python Implementation

我们来看一个使用 COLUMN 指令与 ChatGPT 交互的 Python 脚本的实际示例。

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

在这个示例中,我们定义了一个 generate_chat_response() 函数,它接受一个提示并使用 OpenAI API 使用 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.

chat_prompt 变量包含用户的提示和 ChatGPT 响应,包括使用 COLUMN 指令设置格式的比较表格。

The chat_prompt variable contains the user’s prompt and the ChatGPT response, including the comparison table formatted using the COLUMN directive.

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 compare the features of smartphones X and Y?\n"
chat_prompt = user_prompt + "ChatGPT: Sure! Here's a comparison of the features:\n\n| **Features** | **Smartphone X** | **Smartphone Y** "

response = generate_chat_response(chat_prompt)
print(response)

Output

运行脚本后,我们将收到来自 ChatGPT 的生成响应,包括以比较表格形式呈现的结构化输出。

Upon running the script, we will receive the generated response from ChatGPT, including the structured output in the form of a comparison table.

comparison table

Conclusion

在本章中,我们探索了在 ChatGPT 提示工程中 COLUMN 指令的功能。通过使用 COLUMN 指令,我们可以对 ChatGPT 生成的响应进行结构化和格式化,以表格格式或以特定的组织方式呈现信息。

In this chapter, we explored the power of the COLUMN directive in prompt engineering for ChatGPT. By using the COLUMN directive, we can structure and format the responses generated by ChatGPT, presenting information in a table-like format or in a specific organized manner.

我们讨论了 COLUMN 指令的语法,并提供了其用法的最佳实践,包括定义列标题、组织内容和考虑列宽。

We discussed the syntax of the COLUMN directive and provided best practices for its usage, including defining column headers, organizing content, and considering column width.