這是關於如何優化 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