这是关于如何优化 ChatGPT 编程提示的实用指南。在这里,我的目的是提供技巧和技术来帮助开发人员优化他们的 chatgpt 提示,以避免常见问题并改善他们的代码生成体验。本指南特别适合那些使用 chat.openai.com 订阅的用户,允许他们针对 gpt4 优化每 3 小时 25 条消息。
欢迎来到PromptMaster存储库!此存储库的目的是为开发人员提供优化 ChatGPT 编程任务提示的实用指导。我们的目标是分享技巧、技术和最佳实践,帮助您避免常见陷阱并改善 ChatGPT 的代码生成体验。该存储库还将涵盖使用 ChatGPT 与流行的开发工具以及保持最新的 AI 模型。我们鼓励开发者社区的协作和贡献,以使该资源尽可能全面和有用。
为了从 ChatGPT 获得最佳结果,正确设置提示格式至关重要。确保您的提示清晰、简洁且具体。您还可以使用格式化技术,例如将说明放在单独的行中或使用项目符号来提高清晰度。
ChatGPT 是一个强大的人工智能模型,拥有庞大的知识库。确保充分利用其功能来发挥自己的优势。您可以提供上下文或要求模型为特定编程语言、库或框架生成代码。此外,您可以要求模型在生成实际代码之前逐步思考或生成伪代码。
请注意,ChatGPT 与所有 AI 模型一样,并不完美,可能会生成具有潜在陷阱或偏差的代码。为了避免这些问题,您可以:
问题:生成的代码太长并且被切断,当要求 ChatGPT 从停止处继续时,它会重新开始而不是继续代码。为了防止这种情况,您可以:
完美提示:
ChatGPT, your answer got truncated. Please continue the previously generated code without repeating any part of it.
The last part of the previous code snippet was:
< Insert the last few lines of the generated code here >
Continue generating the remaining code from this point onwards, ensuring that the solution picks up where it left off and does not start over.
问题:有时,ChatGPT 以纯文本形式而不是在代码块内响应代码。当将代码复制并粘贴到开发环境中时,这可能会导致缩进错误和语法问题。为了避免这种情况,您可以使用以下提示在代码块中显式请求代码。
完美提示:
ChatGPT, please provide a solution to the problem in a properly formatted Python code block. The problem is:
< Insert problem description here >
通过显式请求代码块,您更有可能收到正确格式的生成代码,从而减少复制代码时遇到缩进或语法错误的机会。
问题:由于 ChatGPT 的知识截止日期为 2021 年,因此它可能无法访问更新的库或框架或其中所做的更改。因此,它可能会遇到不熟悉的库和框架。
建议的解决方案:为了更好地帮助 ChatGPT 理解和适应不熟悉的库和框架,您可以:
- 提供库或框架的名称。 - 描述库或框架的用途。 - 举例说明如何使用库或框架。 - 提及您需要使用的任何特定功能或方法。 - 包括任何最新的相关文档或资源。
完美提示:
ChatGPT, I need help with a problem using the <library/framework> version <version_number>, which is a <brief description of the library/framework>.
Here is a code snippet or documentation that demonstrates its usage:
< Insert code snippet or documentation link here >
Please provide a solution to the following problem, taking into account the updated version and any changes in the library/framework:
< Insert problem description here >
通过提供上下文和相关信息,您可以增加 ChatGPT 生成考虑更新的库或框架的有用解决方案的机会。
提示示例:
ChatGPT, I am working on a project that requires the use of the Dask library in Python.
Dask is a library for parallel and distributed computing that allows processing of large datasets,
and provides support for computations on multi-dimensional arrays, data frames, and machine learning tasks.
I need to perform parallel computation on large datasets efficiently.
Specifically, I need to use Dask's DataFrame API to perform a groupby operation followed by aggregation.
Here are snippets of the latest documentation and resources:
- Dask documentation: https://docs.dask.org/en/latest/ < provide the snippets that has been updated and chatgpt might not be familair with >
Please provide a Python script that uses the Dask library to achieve the following tasks:
1 . Load a large dataset with millions of rows and columns.
2 . Perform a groupby operation based on a specific column.
3 . Aggregate the results using a sum operation.
4 . Output the resulting dataset.
问题:为了优化 ChatGPT 的输出,为生成的代码提供特定的约束或要求可能会有所帮助。例如,如果速度或内存使用情况是一个问题,您可以在提示中包含此信息,以帮助 ChatGPT 优化其输出。
要优化 ChatGPT 的输出,请为生成的代码提供特定的约束或要求。这可能包括有关编程语言、数据类型、速度或内存使用情况、排序顺序或任何其他相关约束的信息。
完美提示:
ChatGPT, I need help generating code that meets the following constraints and requirements:
- < Insert constraint or requirement >
- < Insert constraint or requirement >
- < Insert constraint or requirement >
- < Insert constraint or requirement >
- < Insert constraint or requirement >
Can you help me generate code that meets these requirements?
提示示例:
"ChatGPT, I need to generate a C++ program that sorts a large array of integers, but the program needs to meet the following constraints:
- The program needs to be optimized for speed and should be able to handle large arrays efficiently.
- The program should be memory-efficient and should use a sorting algorithm that minimizes memory usage.
- The program should be able to handle both ascending and descending sorts and should allow the user to specify the sorting order.
- The program should be able to handle arrays with duplicate values and should sort them in a stable manner.
- The program should be easy to read and maintain, with clear variable names and comments.
Can you help me write a C++ program that meets these requirements?"
问题: ChatGPT 是一个有价值的助手,但不是完美的开发人员。当要求 ChatGPT 生成代码或帮助编写代码时,必须确保有足够的错误处理和防御性编程,以便更轻松地查看和解决任何错误。要生成包含防御性编程技术和错误处理的代码,您可以提供有关提示中需要处理的潜在错误或边缘情况的上下文和信息。为了在以后的调试过程中更容易识别错误,您可以要求 ChatGPT 添加输出发生的任何错误的打印语句,以及输入验证、try- except 块和日志记录等防御性编程结构。
完美提示:
ChatGPT, I need a Python script that < describes the task >.
Please ensure that the code is robust and implements defensive programming techniques, such as input validation, error handling,
and appropriate exception handling with print statements to output any errors that occur.
The script should:
< list the specific tasks or requirements of the script >
使用完美提示之前和之后的示例:
前:
def process_data(data):
result = data_processing_function(data)
return result
data = input("Enter the data: ")
result = process_data(data)
print("The processed data is:", result)
使用完美后提示:
ChatGPT, I need a Python script that takes user input for data, processes it using a custom 'data_processing_function', and prints the result.
Please ensure that the code is robust and implements defensive programming techniques,
such as input validation, error handling, and appropriate exception handling with print statements to output any errors that occur.
生成的代码:
def validate_input(data):
# Implement validation logic according to your requirements
return True
def process_data(data):
try:
if not validate_input(data):
raise ValueError("Invalid input data")
result = data_processing_function(data)
return result
except ValueError as ve:
print("Error: Invalid input data:", ve)
return None
except Exception as e:
print("Error:", e)
return None
data = input("Enter the data: ")
result = process_data(data)
if result is not None:
print("The processed data is:", result)
else:
print("An error occurred during data processing.")
通过使用完美的提示,ChatGPT 生成包含错误处理和防御性编程的代码,使其更易于调试和维护。
问题:在项目中使用 ChatGPT 时,必须让其知道您已经安装了要求并设置了相关 API。这有助于 ChatGPT 避免在其响应中解释您已经完成的操作,并根据您的特定需求定制输出。要优化 ChatGPT 的输出,您可以在提示中提供项目字典和相关 API。这可以帮助 ChatGPT 了解您需要生成的代码的上下文和用途。
完美提示:
ChatGPT, I am working on a project that involves < project description >.
I have already set up the required libraries, APIs, and dependencies. My project's structure looks like this:
< Describe your project structure here >
Please help me with the following task, taking into consideration the existing setup:
< Insert task description here >
使用完美提示之前和之后的示例:
前:
ChatGPT, I want to create a Telegram bot to track ERC20 tokens.
ChatGPT 响应(之前):
要创建 Telegram 机器人来跟踪 ERC20 代币,您需要:
通过与 BotFather 交谈,在 Telegram 上设置一个新机器人。获取您的机器人 API 令牌。安装 python-telegram-bot 库。配置机器人与以太坊区块链通信......
使用完美提示:
ChatGPT, I am working on a project that involves creating a Telegram bot to track ERC20 tokens.
I have already set up the required libraries (python-telegram-bot), created a bot on Telegram, and obtained an API token.
My project's structure looks like this:
- main.py < share code >
- config.py (API token stored here)
- erc20_tracker.py < share code >
Please help me with the following task, taking into consideration the existing setup:
Implement the functionality in the erc20_tracker.py file to track ERC20 tokens.
生成的代码(之后):
import requests
from config import TELEGRAM_API_TOKEN
def get_erc20_balance(address, contract_address):
# Implement the function to get the balance of ERC20 tokens for a given address
def track_erc20_tokens(update, context):
# Implement the function to track ERC20 tokens using the Telegram bot
if __ name __ == " __ main __ ":
from telegram.ext import Updater, CommandHandler
updater = Updater(token=TELEGRAM_API_TOKEN, use_context=True)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler("track", track_erc20_tokens))
updater.start_polling()
updater.idle()
通过提供项目字典和先决条件信息,ChatGPT 会根据您的特定需求生成量身定制的代码,同时考虑现有设置。
问题:有时,在生成代码时,ChatGPT 可能会包含项目中已有的冗余或不必要的导入,从而导致生成的代码混乱和重复。为了防止这种情况,您可以指示 ChatGPT 在生成的代码中省略导入语句,并专注于您需要的特定功能。
完美提示:
ChatGPT, I am working on a project that involves < project description >.
I have already imported all the required libraries and dependencies in my project.
Please help me with the following task, but do not include any import statements in the generated code:
< Insert task description here >
问题:当向 ChatGPT 寻求代码方面的帮助时,有时您可能只想收到针对代码特定部分的更改或改进建议,而不是重写整个代码。为了实现这一目标,您可以:
- 指定代码中需要改进或修改的确切部分。 - 要求 ChatGPT 提供更改的高级描述,而不是重写整个代码。
完美提示:
ChatGPT, I have a piece of code that I think can be improved or modified in some way.
I would like you to provide suggestions for changes or improvements to only the following specific part(s) of the code:
<Insert the specific part(s) of your code here>
Please provide a high-level description of the changes you suggest, without rewriting the whole code from the beginning.
问题:当向 ChatGPT 寻求代码方面的帮助时,您可能希望生成的代码仅包含编辑或改进的部分,同时保持原始标头和函数不变。此外,您可能希望 ChatGPT 显示代码中已编辑部分的位置(在哪一行之后和哪一行之前)。为此,您可以向 ChatGPT 提供有关需要编辑的特定部分以及这些编辑的所需位置的明确说明。
完美提示:
ChatGPT, I have a piece of code that I think can be improved or modified in some way.
I would like you to provide suggestions for changes or improvements to only the following specific part(s) of the code:
<Insert the specific part(s) of your code here>
Please generate only the edited sections of the code while keeping the original headers and functions intact.
Also, indicate the placement of the edited sections by specifying after which line and before which line the changes should be inserted in the existing code.
示例(之前):
def load_data(file_path):
# Load data from a file
pass
def process_data(data):
# Process the data
pass
def save_data(data, file_path):
# Save the processed data to a file
pass
使用完美提示:
ChatGPT, I have a piece of code that I think can be improved or modified in some way.
I would like you to provide suggestions for changes or improvements to only the following specific part(s) of the code:
def process_data(data):
# Process the data
pass
Please generate only the edited sections of the code while keeping the original headers and functions intact.
Also, indicate the placement of the edited sections by specifying after which line and before which line the changes should be inserted in the existing code.
生成的代码(之后):
Edited section:
Replace the pass statement in the process_data function with the following lines
Place after line: 5
Place before line: 6
data = data.upper()
result = data.strip()
通过使用完美的提示,ChatGPT 仅生成代码的已编辑部分,并提供有关在现有代码中进行更改的位置的明确说明。
问题:在处理长且复杂的代码时,可能很难快速找到并引用特定部分。添加注释并将代码划分为多个部分可以帮助改进导航,并使在向 ChatGPT 寻求帮助时更容易参考某些部分。
- 使用注释将代码划分为多个部分,用描述性标识符标记每个部分(例如#Part 1、#Part 2等) - 当向ChatGPT寻求帮助时,您可以通过其标签直接引用特定部分,使您和 ChatGPT 更容易专注于相关部分。
完美提示:
ChatGPT, I have a piece of code that is divided into sections using comments. Here is a brief overview of the sections:
#Part 1: <Description of Part 1>
#Part 2: <Description of Part 2>
...
#Part N: < Description of Part N >
I would like you to provide suggestions or assistance for the following part(s) of the code:
<Specify which part(s) you want ChatGPT to focus on>
Please provide your suggestions or assistance with reference to the specified part(s) without rewriting the whole code from the beginning.
问题: ChatGPT 的知识仅限于截至 2021 年 9 月的信息,这意味着它可能不知道自那时以来发生的库更新或更改。当寻求新库或更新版本的帮助时,务必告知 ChatGPT 情况并提供相关代码和文档片段。
- 明确声明您正在使用的库或特定版本的更新超出了 ChatGPT 的知识截止范围。 - 分享您的代码并包含相关文档片段以更新资源,以帮助 ChatGPT 了解上下文并提供更准确的帮助。
完美提示:
ChatGPT, I am working with a library (or a specific version of a library) that has been updated since your knowledge cutoff in September 2021.
I understand that you may not be aware of the changes, so I will provide you with the relevant code and some documentation snippets to help you assist me.
Here's my code:
< Your code here >
Here are the documentation snippets related to the library or the specific version I am using:
< Documentation snippet >
Considering the provided code and documentation, could you please help me with the following issue(s) or question(s):
<Specify your issue(s) or question(s)>
问题: ChatGPT 可能不熟悉用户定义的函数或方法,导致在讨论代码时难以理解上下文。
- 提供有关自定义函数或方法的详细信息,例如其目的、输入、输出以及任何相关的实现细节。这将有助于ChatGPT提供更好的帮助和建议。
完美提示:
I have a custom function called function_name(arguments), which performs a specific task.
The function takes these arguments: arg1 (type), arg2 (type), and so on. It returns the result of the operation as a specific type.
Can you suggest improvements or potential issues with this function?
问题用户可能需要帮助优化他们的代码或使其更加简洁。
- 向 ChatGPT 询问有关如何提高代码效率或简洁性的建议,并提供您遇到的任何具体问题或限制。
完美提示:
I have a piece of code that performs a specific task, but I'd like to make it more efficient or concise.
Here's my current implementation:
def function_name(arguments):
< Your code here >
Can you suggest an alternative way to implement this functionality?
问题: ChatGPT 可能无法访问您的特定数据库或数据集结构,这可能会妨碍其提供相关指导的能力。
- 分享您的数据库或数据集结构的片段,包括表、列、数据类型以及表或数据点之间的关系的信息,以帮助 ChatGPT 更好地理解您的查询并提供适当的指导。 - 使用通用提示示例作为起点,并根据您的具体情况进行定制。
完美提示:
I'm working with a database or dataset that has the following structure:
(provide information on tables, columns, data types, and relationships).
Can you help me with a query or data manipulation task related to this structure?
heres the first 10 rows in my dataset_1.csv
... paste here
例子:
I'm working with a database that has the following structure:
Table orders: order_id (integer), customer_id (integer), order_date (date), total_amount (float)
Table customers: customer_id (integer), customer_name (varchar), email (varchar)
The tables are related by the customer_id field.
Sample data (first 10 lines):
orders:
1, 1, 2023-01-01, 100.00
2, 2, 2023-01-02, 150.00
3, 3, 2023-01-02, 200.00
customers:
1, Alice, [email protected]
2, Bob, [email protected]
3, Carol, [email protected]
I want to retrieve a list of all orders with the customer's name and email.
Can you help me write a SQL query for this task?
问题:有时 chatgpt 提供的代码答案不起作用,并且被卡住并固定在它上面。在这种情况下,请向 chatgpt 询问替代解决方案或解决问题的方法列表,以便您根据您的特定需求和限制选择最佳选项。
完美提示:
I need help solving a specific problem in my code, but I'm looking for multiple alternative solutions or approaches.
Can you provide a list of different ways to solve this problem, considering my specific needs and constraints?
Here's my code:
< Your code here >
问题:用户可能需要有关长代码片段或多个文件的帮助,这对于以会话格式进行管理可能具有挑战性。
-通知chatgpt您将共享多个文件或长文件,这些文件将通过多于1条消息发送。 -要求chatgpt理解、记下并跟踪所有消息,并声明您将在分享完成后通知它,然后提出您的问题。 -要求它回复它理解。与 ChatGPT 共享代码或文件,并向 chatgpt 提供有关您正在共享哪些文件以及哪些部分的尽可能多的详细信息。
完美提示:
I will be sharing multiple files or long files that span across more than one message.
Please understand, keep note of, and track all the messages.
I will inform you when I finish sharing the files, and then I will ask my questions.
Please respond back that you understand.
<Share the code or files with ChatGPT sequentially or as needed, providing as many details as possible about which files you are sharing and which parts they pertain to.>
集成开发环境/工具 | 扩展/插件名称 | 描述 |
---|---|---|
视觉工作室代码 | 适用于 VSCode 的 ChatGPT 扩展 | 允许您使用非官方 ChatGPT API 在编辑器中从 OpenAI 的 ChatGPT 生成自然语言响应。 |
视觉工作室代码 | 人工智能精灵 | 在 Visual Studio Code 中提示 OpenAI 的 GPT-4、GPT-3.5、GPT-3 和 Codex 模型。 |
视觉工作室代码 | 选项卡九 | 虽然 TabNine 不是 ChatGPT,但它是一个强大的人工智能驱动的代码完成工具。 |
Jupyter笔记本 | 聊天-GPT Jupyter 扩展 | 将 ChatGPT 集成到 Jupyter Notebook 中以进行代码生成和建议。 |
智能IDEA | ChatGPT IntelliJ 插件 | 将 ChatGPT 集成到 IntelliJ IDEA 中以进行代码生成、自动完成和协助。 |
我希望本指南有助于改善您使用 ChatGPT 进行代码生成的体验。随着 ChatGPT 的发展,使用它的最佳实践也会不断发展。如果您有任何建议、想要添加的贡献或反馈,请随时向本指南的 GitHub 存储库发送拉取请求。快乐编码和提示! <3