Prompt Engineering 简明教程

Prompt Engineering - CALCULATE Prompt

在本章中,我们将探索计算提示,这是一种强大的技术,使我们能够将 ChatGPT 作为计算器或计算工具使用。

通过利用计算指令,我们可以指示 ChatGPT 执行数学计算、求解方程式或评估表达式。

Understanding the CALCULATE Directive

计算指令允许我们在提示中指定一个数学计算、方程式或表达式,并指示 ChatGPT 提供计算结果。通过结合计算指令,我们可以将 ChatGPT 转变为一个多功能的计算资源。

计算指令的基本 syntax 如下 −

User: What is the result of 5 + 8?
ChatGPT: The result of 5 + 8 is 13.

在这个示例中,用户请求加法运算 5 + 8 的结果。ChatGPT 的响应包括计算结果,即 13。

Best Practices for Using the CALCULATE Directive

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

  1. Clearly Specify the Calculation − 在提示中明确说明我们需要的计算、方程式或表达式。确保数学语法正确,并提供了准确计算所需的所有必要元素。

  2. Handle Complex Calculations − ChatGPT 可以处理各种计算,包括算术运算、代数方程式、三角函数、对数等。指定计算任务,提供足够的详细信息,以指导 ChatGPT 执行所需的计算。

  3. Format the Response − 格式化 ChatGPT 生成的响应,使其清晰易懂。确保计算结果以用户熟悉和有意义的方式呈现。

  4. Experiment and Verify − 使用已知值或既定来源测试 ChatGPT 生成的计算的准确性。验证获得的结果,必要时迭代提示。

Example Application − Python Implementation

让我们探索一个使用与 ChatGPT 交互的 Python 脚本的计算指令的实际示例。

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: What is the result of 5 + 8?\n"
chat_prompt = user_prompt + "ChatGPT: The answer is: [CALCULATE: 5 + 8]"

response = generate_chat_response(chat_prompt)
print(response)

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 响应,包括执行加法运算 5 + 8 的计算指令。

Output

当我们运行脚本时,我们将收到 ChatGPT 生成的响应,包括计算指令中指定的计算结果。

The answer is: 13

Conclusion

在本章中,我们探讨了用于 ChatGPT 提示工程中的计算指令。通过利用计算指令,我们可以将 ChatGPT 转变为计算器或计算工具。