提示是你向 GitHub Copilot 发出的请求。 例如,可以提出问题、描述任务或编写供 Copilot 完成的代码片段。 除了提示之外, Copilot 还使用当前文件和聊天历史记录中的代码等上下文来生成响应。
您可以通过多个界面与 Visual Studio Code 交互,包括 Visual Studio、JetBrains IDEs、在您的 IDE(GitHub、Copilot、GitHub Copilot app、Eclipse 和 Xcode)中,以及在 GitHub Copilot CLI 网站上。
从GitHub Copilot app或GitHub网站中,还可以启动一个Copilot cloud agent代理,该代理可为你处理任务,而不是以交互方式使用Copilot。
本文中的提示适用于各个 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 x 10 的字母网格。
- 编写一个函数,以便在给定一组有效单词的情况下查找字母网格中的所有单词。
- 编写一个函数,该函数使用之前的函数生成一个包含至少 10 个单词的 10x10 字母网格。
- 更新上一个函数,以打印字母网格和网格中的 10 个随机单词。
避免歧义
避免含糊不清的用词。 例如,如果“this”可能指的是当前文件、上一条 Copilot 响应或某个特定的代码块,就不要问“这是做什么的?”。 相反,应当具体化:
createUser函数的作用是什么?- 上一个回复中的代码的作用是什么?
多义性也适用于编程库:
- 如果正在使用不常见的库,请描述库的作用。
- 如果希望使用特定的库,请在文件的顶部设置导入语句,或者指定要使用的库。
直接陈述规则和要求,而不是作为元指令。 例如,编写“以 JSON 形式返回结果”,而不是“记住始终返回 JSON”。
提供相关上下文
Copilot 使用可供其使用的上下文来理解您的提示词。 提供上下文的方式取决于所使用的界面。
- IDE 中的代码补全: 打开任何相关文件并关闭无关的文件。 Copilot 会使用打开的文件来了解你的请求。
- Copilot 在 IDE 或 GitHub 网站上: 突出显示要 Copilot 引用的代码,并使用关键字附加上下文。 例如,在提示中键入
@以附加文件、问题、拉取请求或存储库等项。 某些 IDE 还支持斜杠命令(如/fix和/tests)、变量和其他用于增强 Copilot 体验的选项。 - : 使用 附加存储库文件,或使用 引用问题或拉取请求 。 对于要复用的存储库上下文,请添加自定义说明,例如
.github/copilot-instructions.md文件、特定于路径的说明文件或AGENTS.md文件。 - Copilot cloud agent: 通过选择存储库和基分支来限定任务范围,然后描述目标、验收条件和相关文件。 在视觉上下文重要时附加屏幕截图或设计稿。 当现有讨论应延续到该任务中时,从聊天会话启动任务;否则,请在任务提示中包含该背景信息。 在存储库、特定于路径或代理指令文件中保留长期适用的指导原则。
试验和迭代
如果未获得所需的结果,请反复修改提示并重试。
如果您在编写代码时使用 Copilot 获取建议,您可以完全删除该建议并重新开始。 也可以保留建议并请求修改。
如果使用 Copilot Chat,可以在下一个请求中引用以前的响应。 也可以删除上一个回复并重新开始。
保持历史的相关性
对话界面(你在 IDE 中或在 Copilot cloud agent 上使用的 GitHub Copilot app、GitHub Copilot CLI、Copilot,以及 GitHub)会使用你的对话历史记录作为上下文。 仅向 Copilot 提供相关的历史记录:
- 在开始新任务时启动新的对话或会话。
- 如果画布支持,请删除或回滚不再相关的操作。 例如,GitHub Copilot CLI 允许你启动新的会话、删除会话,以及使用
/undo或/rewind回退到较早的提示。 Copilot cloud agent 可启动新任务并停止或存档会话,但不允许删除会话或倒退各个轮次。
查看 Copilot的输出
像审查同事的工作一样验证 AI 生成的代码和响应:在依赖它之前评估、测试并确认其正确性。 这适用于所有 Copilot 表面。 有关详细信息,请参阅“查看 AI 生成的代码”。
遵循良好的编码实践
如果在代码库中提出 Copilot 建议或说明时未收到所需的响应,请确保现有代码遵循最佳做法并易于阅读。 例如:
- 使用一致的代码样式和模式。
- 对变量和函数使用描述性名称。
- 为代码提供注释。
- 将代码构造为模块化、限定范围的组件。
- 包含单元测试。
提示
用于 Copilot 帮助代码遵循最佳做法。 例如,要求 Copilot 添加注释或将大型函数分解为较小的函数。
延伸阅读
- GitHub Copilot 对话助手 速查表
- 如何使用 GitHub Copilot:提示词、技巧和用例(发表于GitHub博客)
- 在 IDE 中使用GitHub Copilot:博客中的GitHub提示、技巧和最佳做法
- 博客中的GitHub
- 提示 GitHub Copilot 对话助手 成为你的无障碍功能个人 AI 助手(发表于 GitHub 博客)