Prompt Engineering 简明教程
Prompt Engineering - CONVERT Prompt
提示工程提供了一系列技巧来增强 ChatGPT 的能力。在本节中,我们将探索 CONVERT 提示,一种强大的技术,它允许我们使用 ChatGPT 作为一种计算工具来执行转换、计算或单位转换。
Prompt engineering offers a wide range of techniques to enhance the capabilities of ChatGPT. In this chapter, we will explore the CONVERT prompt, a powerful technique that allows us to perform conversions, calculations, or unit conversions using ChatGPT as a computational tool.
利用 CONVERT 指令,我们可以利用 ChatGPT 的计算能力来获得各种转换任务的结果。
By utilizing the CONVERT directive, we can leverage ChatGPT’s computational abilities to obtain results for various conversion tasks.
Understanding the CONVERT Directive
CONVERT 指令使我们能够在提示中指定转换任务或计算,并指示 ChatGPT 执行转换或计算。此技术使我们能够利用 ChatGPT 作为计算引擎来满足各种转换或计算需求。
The CONVERT directive enables us to specify a conversion task or calculation within the prompt and instruct ChatGPT to perform the conversion or calculation. This technique empowers us to leverage ChatGPT as a computational engine for various conversion or calculation needs.
CONVERT 指令的基本语法如下:
The basic syntax for the CONVERT directive is as follows −
User: Convert 10 miles to kilometers.
ChatGPT: 10 miles is approximately equal to 16.09 kilometers.
在这个示例中,用户要求将 10 英里转换为千米。ChatGPT 的响应包括转换后的值,大约为 16.09 千米。
In this example, the user requests the conversion of 10 miles to kilometers. The response from ChatGPT includes the converted value, which is approximately 16.09 kilometers.
Best Practices for Using the CONVERT Directive
为了充分利用 CONVERT 指令,请考虑以下最佳实践:
To make the most of the CONVERT directive, consider the following best practices −
-
Clearly Specify the Conversion Task − Clearly state the conversion task or calculation we desire in the prompt. Provide all the necessary details, such as the units or values involved, to ensure accurate conversions or calculations.
-
Handle Ambiguity − Some conversion tasks may have multiple interpretations or units. Specify the context or the specific units to avoid ambiguity and obtain the desired result.
-
Format the Response − Format the response generated by ChatGPT to make it clear and easy to understand. Round the values, use appropriate units, and consider using standard conventions for displaying results.
-
Experiment and Verify − Test the accuracy of the conversions or calculations generated by ChatGPT with known values or established sources. Verify the results obtained and iterate on the prompt if necessary.
Example Application − Python Implementation
让我们探讨一个使用 CONVERT 指令和 Python 脚本与 ChatGPT 交互的实际示例。
Let’s explore a practical example of using the CONVERT directive with a Python script that interacts with ChatGPT.
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: Convert 10 miles to kilometers.\n"
chat_prompt = user_prompt + "ChatGPT: [CONVERT: 10 miles to kilometers]"
response = generate_chat_response(chat_prompt)
print(response)
在这个示例中,我们定义了一个函数 generate_chat_response(),它接收一个提示并使用 OpenAI API 来生成一个使用 ChatGPT 生成的响应。chat_prompt 变量包含用户的提示和 ChatGPT 响应,包括 CONVERT 指令,用于将 10 英里转换为公里。
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 CONVERT directive to perform the conversion of 10 miles to kilometers.
Output
当我们运行脚本时,我们会收到来自 ChatGPT 的生成的响应,包括在 CONVERT 指令中指定的转换值。
When we run the script, we will receive the generated response from ChatGPT, including the converted value specified within the CONVERT directive.
16.09 km
输出显示 10 英里大约相当于 16.09 公里。
The output shows that 10 miles is approximately 16.09 kilometers.
Conclusion
在本章中,我们探讨了 ChatGPT 中提示工程中的 CONVERT 指令。通过利用 CONVERT 指令,我们可以将 ChatGPT 用作计算工具来执行转换或计算。
In this chapter, we explored the CONVERT directive in prompt engineering for ChatGPT. By utilizing the CONVERT directive, we can leverage ChatGPT as a computational tool to perform conversions or calculations.