提示是你向 GitHub Copilot 发出的请求。 例如,你提出的 副驾驶聊天问题或要 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函数的作用是什么?- 上一个回复中的代码的作用是什么?
多义性也适用于编程库:
- 如果正在使用不常见的库,请描述库的作用。
- 如果希望使用特定的库,请在文件的顶部设置导入语句,或者指定要使用的库。
指示相关代码
如果你在 IDE 中使用 Copilot 来在编写代码时获取建议,请打开所有相关文件,并关闭无关文件。 Copilot 将使用打开的文件来了解你的请求。
如果你正在 IDE 中使用 副驾驶聊天,请打开该文件,或选中你希望 Copilot 引用的代码。 您还可以使用关键字手动向 副驾驶聊天 提供上下文。 例如,可以在 VS Code 中添加 @workspace 聊天参与者,或者在 JetBrains IDE 中添加 @project。 请参阅 GitHub Copilot 对话助手 速查表。
试验和迭代
如果未获得所需的结果,请反复修改提示并重试。
如果您在编写代码时使用 Copilot 获取建议,您可以完全删除该建议并重新开始。 也可以保留建议并请求修改。
如果使用 副驾驶聊天,可以在下一个请求中引用以前的响应。 也可以删除上一个回复并重新开始。
保持历史的相关性
副驾驶聊天 使用聊天历史记录获取有关请求的上下文。 仅向 Copilot 提供相关的历史记录:
- 利用线程为新任务启动一个新对话
- 删除不再相关或未提供所需结果的请求
遵循良好的编码实践
如果在代码库中提出 Copilot 建议或说明时未收到所需的响应,请确保现有代码遵循最佳做法并易于阅读。 例如:
- 使用一致的代码样式和模式
- 对变量和函数使用描述性名称
- 请注释你的代码
- 将代码构建成具有作用域的模块化组件
- 包括单元测试
用于 Copilot 帮助代码遵循最佳做法。 例如,要求 Copilot 添加注释或将大型函数分解为较小的函数。
延伸阅读
- 如何使用 GitHub Copilot:提示词、技巧和用例(发表于GitHub博客)
- 在 IDE 中使用GitHub Copilot:博客中的GitHub提示、技巧和最佳做法
- GitHub 博客中的提示工程和大语言模型开发者指南
- 提示 GitHub Copilot 对话助手 成为你的无障碍功能个人 AI 助手(发表于 GitHub 博客)