{"meta":{"title":"首条自定义说明","intro":"使用这个简单的示例创建并测试你的首条自定义说明。","product":"GitHub Copilot","breadcrumbs":[{"href":"/zh/copilot","title":"GitHub Copilot"},{"href":"/zh/copilot/tutorials","title":"教程"},{"href":"/zh/copilot/tutorials/customization-library","title":"定制化库"},{"href":"/zh/copilot/tutorials/customization-library/custom-instructions","title":"自定义说明"},{"href":"/zh/copilot/tutorials/customization-library/custom-instructions/your-first-custom-instructions","title":"首条自定义说明"}],"documentType":"article"},"body":"# 首条自定义说明\n\n使用这个简单的示例创建并测试你的首条自定义说明。\n\n> \\[!NOTE]\n>\n> * 此库中的示例旨在提供灵感，建议进行相应调整，使其更特定于你的项目、语言和团队流程。\n> * 如需获取社区提供的有关特定语言和场景的自定义指令示例，请参阅[出色的 GitHub Copilot 自定义内容](https://github.com/github/awesome-copilot/blob/main/docs/README.instructions.md)仓库。\n> * 可以跨不同范围应用自定义指令，具体取决于要在其中创建自定义指令的平台或 IDE。 有关详细信息，请参阅“[关于自定义GitHub Copilot 响应](/zh/copilot/concepts/prompting/response-customization)”。\n\n## 关于自定义\n\n可以使用两类文件来自定义 GitHub Copilot 的响应：\n\n* **自定义说明**为 GitHub Copilot 在你的所有交互中的行为方式提供持续指导。\n* **提示文件（公共预览版）** 定义你可在需要时调用的特定任务的可重用提示。 提示文件仅适用于 VS Code、Visual Studio 和 JetBrains IDE。 有关介绍性示例，请参阅 [第一个提示文件](/zh/copilot/tutorials/customization-library/prompt-files/your-first-prompt-file)。\n\n虽然自定义说明有助于将上下文添加到每个 AI 工作流，但提示文件使你能够向特定聊天交互添加说明。\n\n最常用且受到最广泛支持的是仓库自定义说明，但你也可以定义个人和组织的自定义说明，仅面向 GitHub Copilot Chat in GitHub。 可以通过两种方式创建仓库自定义指令：\n\n* 全仓库内的指令：在仓库根目录中创建一个应用于仓库中所有文件的 \\*\\*\\*\\* 文件`copilot-instructions.md`。\n* 特定于路径的指令：使用 \\*\\*\\*\\* 字段创建一个或多个仅应用于特定文件或目录的 `.instructions.md` 文件`applyTo`。 目前，Copilot Chat、\\*\\*\\*\\* 和 Visual Studio Code 中的 Copilot cloud agent 支持特定于路径的指令。\n\n## 你的首条说明\n\n从这些核心自定义说明开始，这些说明帮助 GitHub Copilot 了解你的编码首选项。\n\n### 关于编写函数的说明\n\n```markdown copy\nWhen writing functions, always:\n- Add descriptive JSDoc comments\n- Include input validation\n- Use early returns for error conditions\n- Add meaningful variable names\n- Include at least one example usage in comments\n```\n\n这些说明将改变 GitHub Copilot 生成函数的方式。\n\n## 测试一下\n\n为了快速测试自定义指令可以产生的差异，可以在 Copilot Chat 中使用个人自定义指令。\n\n1. 首先，转到 [github.com/copilot](https://github.com/copilot?ref_product=copilot\\&ref_type=trial\\&ref_style=text)并输入以下提示：\n\n   `Create a JavaScript function that calculates the area of a circle`\n\n**在没有自定义说明的情况下**，你可能会得到以下输出：\n\n````\n```javascript\nfunction areaOfCircle(radius) {\n    if (typeof radius !== 'number' || radius < 0) {\n        throw new Error('Radius must be a non-negative number');\n    }\n    return Math.PI * radius * radius;\n}\n```\n````\n\n1. 现在点击页面左下角的个人资料图片，访问**自定义说明**。 将上述说明粘贴到文本框中，然后单击“Save”\\*\\*\\*\\*。\n\n   现在输入你之前输入过的提示。\n\n**在有自定义说明的情况下**，你将得到以下输出：\n\n````\n```javascript\n/**\n * Calculates the area of a circle given its radius.\n * * @param {number} radius - The radius of the circle. Must be a positive number.\n * @returns {number|null} The area of the circle, or null if the input is invalid.\n * * @example\n * // returns 78.53981633974483\n * areaOfCircle(5);\n * * @example\n * // returns null (invalid input)\n * areaOfCircle(-2);\n */\nfunction areaOfCircle(radius) {\n  if (typeof radius !== \"number\" || isNaN(radius) || radius <= 0) {\n    // Invalid input: radius must be a positive number\n    return null;\n  }\n\n  const area = Math.PI * Math.pow(radius, 2);\n  return area;\n}\n\n// Example usage:\nconsole.log(areaOfCircle(5));   // 78.53981633974483\nconsole.log(areaOfCircle(-2));  // null\n```\n````\n\n## 其他阅读材料\n\n* [关于自定义GitHub Copilot 响应](/zh/copilot/concepts/prompting/response-customization) - GitHub Copilot 中的响应自定义概述\n* [为 Copilot 添加自定义说明](/zh/copilot/how-tos/copilot-on-github/customize-copilot/add-custom-instructions) - 如何配置自定义指令\n* [出色的 GitHub Copilot 自定义内容](https://github.com/github/awesome-copilot/blob/main/README.md) - 社区提供的自定义指令的仓库以及特定语言和场景的其他自定义内容"}