{"meta":{"title":"GitHub Copilot 对话助手的提示设计","intro":"采用这些策略来改善您的 Copilot 结果。","product":"GitHub Copilot","breadcrumbs":[{"href":"/zh/copilot","title":"GitHub Copilot"},{"href":"/zh/copilot/concepts","title":"概念"},{"href":"/zh/copilot/concepts/prompting","title":"提示"},{"href":"/zh/copilot/concepts/prompting/prompt-engineering","title":"提示工程"}],"documentType":"article"},"body":"# GitHub Copilot 对话助手的提示设计\n\n采用这些策略来改善您的 Copilot 结果。\n\n提示是你向 GitHub Copilot 发出的请求。 例如，你提出的 Copilot Chat问题或要 Copilot 完成的代码片段。 除了提示之外， Copilot 还使用其他上下文（如当前文件和聊天历史记录中的代码）来生成响应。\n\n按照本文中的建议编写提示词，以从 Copilot 获得更好的回复。\n\n## 首先一般化，然后具体化\n\n为 Copilot 编写提示时，首先向 Copilot 提供对目标或场景的概括性描述。 然后列出任何具体要求。\n\n例如：\n\n```copilot copy prompt\nWrite a JavaScript function that tells me if a number is prime\n\nThe function should take an integer and return true if the integer is prime\n\nThe function should error if the input is not a positive integer\n```\n\n## 提供示例\n\n使用示例帮助 Copilot 了解所需内容。 可以提供示例输入数据、示例输出和示例实现。\n\n例如：\n\n```copilot copy prompt\nWrite a Go function that finds all dates in a string and returns them in an array. Dates can be formatted like:\n\n* 05/02/24\n* 05/02/2024\n* 5/2/24\n* 5/2/2024\n* 05-02-24\n* 05-02-2024\n* 5-2-24\n* 5-2-2024\n\nExample:\n\nfindDates(\"I have a dentist appointment on 11/14/2023 and book club on 12-1-23\")\n\nReturns: [\"11/14/2023\", \"12-1-23\"]\n```\n\n单元测试也可以用作示例。 在编写函数之前，可以使用 Copilot 为该函数编写单元测试。 然后，可以要求 Copilot 编写由这些单元测试描述的函数。\n\n## 将复杂任务分解成更简单的任务\n\n如果要 Copilot 完成复杂或大型任务，请将任务分解为多个简单小型任务。\n\n例如，无需要求 Copilot 生成单词搜索谜题，而是将过程分解为较小的任务，并逐个要求 Copilot 完成这些任务：\n\n* 编写一个函数，以生成一个 10 x 10 的字母网格。\n* 编写一个函数，以便在给定一组有效单词的情况下查找字母网格中的所有单词。\n* 编写一个函数，该函数使用之前的函数生成一个包含至少 10 个单词的 10x10 字母网格。\n* 更新上一个函数，以打印字母网格和网格中的 10 个随机单词。\n\n## 避免歧义\n\n避免含糊不清的用词。 例如，如果“this”可能指的是当前文件、上一条 Copilot 响应或某个特定的代码块，就不要问“这是做什么的？”。 相反，应当具体化：\n\n* `createUser` 函数的作用是什么？\n* 上一个回复中的代码的作用是什么？\n\n多义性也适用于编程库：\n\n* 如果正在使用不常见的库，请描述库的作用。\n* 如果希望使用特定的库，请在文件的顶部设置导入语句，或者指定要使用的库。\n\n## 指示相关代码\n\n如果你在 IDE 中使用 Copilot 来在编写代码时获取建议，请打开所有相关文件，并关闭无关文件。\nCopilot 将使用打开的文件来了解你的请求。\n\n如果你正在 IDE 中使用 Copilot Chat，请打开该文件，或选中你希望 Copilot 引用的代码。 您还可以使用关键字手动向 Copilot Chat 提供上下文。 例如，可以在 VS Code 中添加 `@workspace` 聊天参与者，或者在 JetBrains IDE 中添加 `@project`。 请参阅 [GitHub Copilot 对话助手 速查表](/zh/copilot/reference/chat-cheat-sheet)。\n\n## 试验和迭代\n\n如果未获得所需的结果，请反复修改提示并重试。\n\n如果您在编写代码时使用 Copilot 获取建议，您可以完全删除该建议并重新开始。 也可以保留建议并请求修改。\n\n如果使用 Copilot Chat，可以在下一个请求中引用以前的响应。 也可以删除上一个回复并重新开始。\n\n## 保持历史的相关性\n\nCopilot Chat 使用聊天历史记录获取有关请求的上下文。 仅向 Copilot 提供相关的历史记录：\n\n* 利用线程为新任务启动一个新对话\n* 删除不再相关或未提供所需结果的请求\n\n## 遵循良好的编码实践\n\n如果在代码库中提出 Copilot 建议或说明时未收到所需的响应，请确保现有代码遵循最佳做法并易于阅读。 例如：\n\n* 使用一致的代码样式和模式\n* 对变量和函数使用描述性名称\n* 请注释你的代码\n* 将代码构建成具有作用域的模块化组件\n* 包括单元测试\n\n> \\[!Tip]\n> 用于 Copilot 帮助代码遵循最佳做法。 例如，要求 Copilot 添加注释或将大型函数分解为较小的函数。\n\n## 延伸阅读\n\n* [如何使用 GitHub Copilot：提示词、技巧和用例](https://github.blog/2023-06-20-how-to-write-better-prompts-for-github-copilot/?ref_product=copilot\\&ref_type=engagement\\&ref_style=text)（发表于GitHub博客）\n* [在 IDE 中使用GitHub Copilot：](https://github.blog/2024-03-25-how-to-use-github-copilot-in-your-ide-tips-tricks-and-best-practices/?ref_product=copilot\\&ref_type=engagement\\&ref_style=text)博客中的GitHub提示、技巧和最佳做法\n* [一份面向开发人员的提示工程与 LLM 指南](https://github.blog/2023-07-17-prompt-engineering-guide-generative-ai-llms/)（发布于 GitHub 博客）\n* [利用提示将 GitHub Copilot 对话助手变为你的个人无障碍 AI 助手](https://github.blog/2023-10-09-prompting-github-copilot-chat-to-become-your-personal-ai-assistant-for-accessibility/?ref_product=copilot\\&ref_type=engagement\\&ref_style=text)（发布于 GitHub 博客）"}