自訂 Copilot

使用 GitHub Copilot 進行軟體開發

Thalia Barrera

AI Engineering Curriculum Manager, DataCamp

使用 GitHub Copilot 進行軟體開發

使用 GitHub Copilot 進行軟體開發

使用 GitHub Copilot 進行軟體開發

使用 GitHub Copilot 進行軟體開發

自訂指示的運作方式

$$

定義:

  • 程式碼慣例
  • 工具偏好
  • 專案脈絡

使用 GitHub Copilot 進行軟體開發

自訂指示的運作方式

$$

定義:

  • 程式碼慣例
  • 工具偏好
  • 專案脈絡

使用 GitHub Copilot 進行軟體開發

如何定義自訂指示

將它們放在:.github/copilot-instructions.md

## Project context
Python project for processing data pipelines with Airflow.

## Coding style - Use type hints for all functions
- Follow PEP8
- Use snake_case naming
- Write Google-style docstrings
## Testing - Use pytest
- Include one test per function
## More instructions...
使用 GitHub Copilot 進行軟體開發

什麼是有效的指示

$$

✅ 有效的指示:

  • 短且明確
  • 自給自足
  • 提供情境或相關資訊

$$

❌ 無效的指示:

  • 依賴外部文件或儲存庫
  • 要求特定語氣或寫作風格
  • 限制細節或格式
1 https://docs.github.com/en/copilot/how-tos/configure-custom-instructions/add-repository-instructions
使用 GitHub Copilot 進行軟體開發

$$

$$

## Style guidelines

- Conform to the coding styles defined 
in styleguide.md in repo my-org/my-repo

- Answer in the style of a friendly 
colleague, using informal language

- Answer in fewer than 1,000 characters

使用 GitHub Copilot 進行軟體開發

$$

$$

## Style guidelines

- Conform to the coding styles defined 
in styleguide.md in repo my-org/my-repo

- Answer in the style of a friendly 
colleague, using informal language

- Answer in fewer than 1,000 characters

使用 GitHub Copilot 進行軟體開發

如何啟用自訂指示

  • 儲存 copilot-instructions.md 檔案
  • 它會自動加入你的提示中

使用 GitHub Copilot 進行軟體開發

如何啟用自訂指示

  • 儲存 copilot-instructions.md 檔案
  • 它會自動加入你的提示中

使用 GitHub Copilot 進行軟體開發

使用多個指示檔案

使用 GitHub Copilot 進行軟體開發

使用多個指示檔案

使用 GitHub Copilot 進行軟體開發

使用多個指示檔案

$$

使用 GitHub Copilot 進行軟體開發

設定組織層級指示

$$

使用 GitHub Copilot 進行軟體開發

設定組織層級指示

$$

公司層級規範範例:

  • 使用內部工具
  • 強制指定函式庫
  • 套用一致風格
  • 對淘汰做法提出警示
使用 GitHub Copilot 進行軟體開發

多層級指示的優先順序

$$

$$

$$

範圍 優先順序
個人指示 最高
專案指示 中等
組織指示 最低
使用 GitHub Copilot 進行軟體開發

$$

提示範例: 「撰寫一個函式,計算數字清單的平均值。」

def calculate_average(numbers: List[float]) -> float:
    """Calculate the average of a list of numbers.

    Args:
        numbers (List[float]): A list of numbers to average.

    Returns:
        float: The average value of the numbers.

    Raises:
        ValueError: If the list is empty.
    """
    if not numbers:
        raise ValueError("The list of numbers cannot be empty.")
    return sum(numbers) / len(numbers)
使用 GitHub Copilot 進行軟體開發

$$

提示範例: 「撰寫一個函式,計算數字清單的平均值。」

def calculate_average(numbers: List[float]) -> float:
    """Calculate the average of a list of numbers.

    Args:
        numbers (List[float]): A list of numbers to average.

    Returns:
        float: The average value of the numbers.

    Raises:
        ValueError: If the list is empty.
    """
    if not numbers:
        raise ValueError("The numbers list cannot be empty.")
    return sum(numbers) / len(numbers)

$$

使用 GitHub Copilot 進行軟體開發

一起來練習吧!

使用 GitHub Copilot 進行軟體開發

Preparing Video For Download...