# 首条自定义说明

使用这个简单的示例创建并测试你的首条自定义说明。

> \[!NOTE]
>
> * 此库中的示例旨在提供灵感，建议进行相应调整，使其更特定于你的项目、语言和团队流程。
> * 如需获取社区提供的有关特定语言和场景的自定义指令示例，请参阅[出色的 GitHub Copilot 自定义内容](https://github.com/github/awesome-copilot/blob/main/docs/README.instructions.md)仓库。
> * 可以跨不同范围应用自定义指令，具体取决于要在其中创建自定义指令的平台或 IDE。 有关详细信息，请参阅“[关于自定义GitHub Copilot 响应](/zh/copilot/concepts/prompting/response-customization)”。

## 关于自定义

可以使用两类文件来自定义 GitHub Copilot 的响应：

* **自定义说明**为 GitHub Copilot 在你的所有交互中的行为方式提供持续指导。
* **提示文件（公共预览版）** 定义你可在需要时调用的特定任务的可重用提示。 提示文件仅适用于 VS Code、Visual Studio 和 JetBrains IDE。 有关介绍性示例，请参阅 [第一个提示文件](/zh/copilot/tutorials/customization-library/prompt-files/your-first-prompt-file)。

虽然自定义说明有助于将上下文添加到每个 AI 工作流，但提示文件使你能够向特定聊天交互添加说明。

最常用且受到最广泛支持的是仓库自定义说明，但你也可以定义个人和组织的自定义说明，仅面向 GitHub Copilot Chat in GitHub。 可以通过两种方式创建仓库自定义指令：

* 全仓库内的指令：在仓库根目录中创建一个应用于仓库中所有文件的 \*\*\*\* 文件`copilot-instructions.md`。
* 特定于路径的指令：使用 \*\*\*\* 字段创建一个或多个仅应用于特定文件或目录的 `.instructions.md` 文件`applyTo`。 目前，Copilot Chat、\*\*\*\* 和 Visual Studio Code 中的 Copilot cloud agent 支持特定于路径的指令。

## 你的首条说明

从这些核心自定义说明开始，这些说明帮助 GitHub Copilot 了解你的编码首选项。

### 关于编写函数的说明

```markdown copy
When writing functions, always:
- Add descriptive JSDoc comments
- Include input validation
- Use early returns for error conditions
- Add meaningful variable names
- Include at least one example usage in comments
```

这些说明将改变 GitHub Copilot 生成函数的方式。

## 测试一下

为了快速测试自定义指令可以产生的差异，可以在 Copilot Chat 中使用个人自定义指令。

1. 首先，转到 [github.com/copilot](https://github.com/copilot?ref_product=copilot\&ref_type=trial\&ref_style=text)并输入以下提示：

   `Create a JavaScript function that calculates the area of a circle`

**在没有自定义说明的情况下**，你可能会得到以下输出：

````
```javascript
function areaOfCircle(radius) {
    if (typeof radius !== 'number' || radius < 0) {
        throw new Error('Radius must be a non-negative number');
    }
    return Math.PI * radius * radius;
}
```
````

1. 现在点击页面左下角的个人资料图片，访问**自定义说明**。 将上述说明粘贴到文本框中，然后单击“Save”\*\*\*\*。

   现在输入你之前输入过的提示。

**在有自定义说明的情况下**，你将得到以下输出：

````
```javascript
/**
 * Calculates the area of a circle given its radius.
 * * @param {number} radius - The radius of the circle. Must be a positive number.
 * @returns {number|null} The area of the circle, or null if the input is invalid.
 * * @example
 * // returns 78.53981633974483
 * areaOfCircle(5);
 * * @example
 * // returns null (invalid input)
 * areaOfCircle(-2);
 */
function areaOfCircle(radius) {
  if (typeof radius !== "number" || isNaN(radius) || radius <= 0) {
    // Invalid input: radius must be a positive number
    return null;
  }

  const area = Math.PI * Math.pow(radius, 2);
  return area;
}

// Example usage:
console.log(areaOfCircle(5));   // 78.53981633974483
console.log(areaOfCircle(-2));  // null
```
````

## 其他阅读材料

* [关于自定义GitHub Copilot 响应](/zh/copilot/concepts/prompting/response-customization) - GitHub Copilot 中的响应自定义概述
* [为 Copilot 添加自定义说明](/zh/copilot/how-tos/copilot-on-github/customize-copilot/add-custom-instructions) - 如何配置自定义指令
* [出色的 GitHub Copilot 自定义内容](https://github.com/github/awesome-copilot/blob/main/README.md) - 社区提供的自定义指令的仓库以及特定语言和场景的其他自定义内容