提示(prompt)是你向 GitHub Copilot 发出的请求。例如,你向 Copilot Chat 提出的一个问题,或让 Copilot 完成的代码片段。除了你的提示外,Copilot 还会利用其他上下文信息,例如当前文件中的代码和聊天记录,来生成响应。
请遵循本文中的技巧,以编写能够让 Copilot 给出更佳响应的提示。
先从概括开始,然后具体化
在为 Copilot 编写提示时,先给出目标或场景的宽泛描述,然后再列出任何具体要求。
例如
Write a JavaScript function that tells me if a number is prime The function should take an integer and return true if the integer is prime The function should error if the input is not a positive integer
Write a JavaScript function that tells me if a number is prime The function should take an integer and return true if the integer is prime The function should error if the input is not a positive integer
Write a JavaScript function that tells me if a number is prime
The function should take an integer and return true if the integer is prime
The function should error if the input is not a positive integer
提供示例
使用示例帮助 Copilot 理解你的需求。你可以提供示例输入数据、示例输出以及示例实现。
例如
Write a Go function that finds all dates in a string and returns them in an array. Dates can be formatted like:
* 05/02/24
* 05/02/2024
* 5/2/24
* 5/2/2024
* 05-02-24
* 05-02-2024
* 5-2-24
* 5-2-2024
Example:
findDates("I have a dentist appointment on 11/14/2023 and book club on 12-1-23")
Returns: ["11/14/2023", "12-1-23"]
Write a Go function that finds all dates in a string and returns them in an array. Dates can be formatted like:
* 05/02/24
* 05/02/2024
* 5/2/24
* 5/2/2024
* 05-02-24
* 05-02-2024
* 5-2-24
* 5-2-2024
Example:
findDates("I have a dentist appointment on 11/14/2023 and book club on 12-1-23")
Returns: ["11/14/2023", "12-1-23"]
Write a Go function that finds all dates in a string and returns them in an array. Dates can be formatted like:
* 05/02/24
* 05/02/2024
* 5/2/24
* 5/2/2024
* 05-02-24
* 05-02-2024
* 5-2-24
* 5-2-2024
Example:
findDates("I have a dentist appointment on 11/14/2023 and book club on 12-1-23")
Returns: ["11/14/2023", "12-1-23"]
单元测试也可以作为示例。在编写函数之前,你可以让 Copilot 为该函数生成单元测试。随后,你可以让 Copilot 编写符合这些单元测试描述的函数。
将复杂任务拆分为更简单的任务
如果希望 Copilot 完成一个复杂或大型任务,请将该任务拆分为多个简短的小任务。
例如,不要一次性让 Copilot 生成字谜游戏,而是将过程拆解为更小的任务,并让 Copilot 逐一完成。
- 编写函数生成一个 10×10 的字母网格。
- 编写函数,在已给定有效单词列表的情况下,找出字母网格中的所有单词。
- 编写函数,利用前面的函数生成一个包含至少 10 个单词的 10×10 字母网格。
- 更新前一个函数,使其能够打印字母网格并输出网格中随机的 10 个单词。
避免歧义
避免使用模糊的表述。例如,不要问 “这段代码是做什么的”,因为 “这段” 可能指当前文件、上一次 Copilot 的回复,或是某个特定代码块。请明确指明。
createUser函数的作用是什么?- 你上一次回复中的代码是做什么的?
歧义同样会出现在库的使用上
- 如果使用了不常见的库,请描述该库的功能。
- 如果想使用特定的库,请在文件顶部加入相应的 import 语句,或明确指出要使用哪个库。
标明相关代码
在 IDE 中使用 Copilot 获得代码建议时,请打开所有相关文件并关闭无关文件。Copilot 会依据打开的文件来理解你的请求。
在 IDE 中使用 Copilot Chat 时,打开希望 Copilot 参考的文件或选中相应的代码段。你也可以使用关键字手动为 Copilot Chat 提供上下文,例如在 VS Code 中添加 @workspace 参与者,或在 JetBrains IDE 中添加 @project。详见 GitHub Copilot Chat 速查表。
实验并迭代
如果没有得到满意的结果,请对提示进行迭代并重新尝试。
在代码编辑时使用 Copilot 获取建议,你可以完全删除该建议并重新开始,或者保留建议并请求修改。
使用 Copilot Chat 时,你可以在后续请求中引用前一次的回复,或者删除前一次的回复并重新开始。
保持历史相关性
Copilot Chat 会利用聊天历史来获取请求的上下文。请仅保留与当前请求相关的历史记录。
- 使用线程在新任务上开启新的对话
- 删除不再相关或未得到预期结果的请求
遵循良好的编码实践
如果在代码库中向 Copilot 请求建议或解释时未得到期望的响应,请确保已有代码遵循最佳实践且易于阅读。例如:
- 使用统一的代码风格和模式
- 为变量和函数使用具描述性的名称
- 为代码添加注释
- 将代码结构化为模块化、作用域明确的组件
- 编写单元测试
使用 Copilot 帮助代码遵循最佳实践。例如,请求 Copilot 添加注释,或把大型函数拆分为多个小函数。
延伸阅读
- 如何使用 GitHub Copilot:提示、技巧与案例(GitHub 博客)
- 在 IDE 中使用 GitHub Copilot:技巧、窍门与最佳实践(GitHub 博客)
- 开发者的提示工程与大模型指南(GitHub 博客)
- 提示 GitHub Copilot Chat 成为你的个人 AI 助手以提升可访问性(GitHub 博客)