LangChain Agent Skills 实战:构建 GitHub 仓库分析能力
为什么需要 Skill?
在构建深度代理时,直接通过系统提示(System Prompt)描述复杂逻辑往往会导致上下文膨胀。使用 Skill(技能)可以将特定领域的逻辑封装到独立文件中,既保持提示词简洁,又能让代理按需加载上下文。
本案例将演示如何创建一个名为 github-analysis 的 Skill,使代理能够自动分析 GitHub 仓库,包括获取基本信息、问题统计和提交历史。
1. 搭建技能目录结构
首先,我们需要规划好技能的存储位置。建议采用以下目录结构,这样代理能清晰地识别文件边界:
skills/
└── github-analysis/
├── SKILL.md
└── github_analysis.py
2. 定义技能规范 (SKILL.md)
SKILL.md 是代理理解该技能用途的关键。它包含元数据和使用说明。这里我们定义了三个核心工具:获取仓库信息、分析问题统计、获取提交历史。
---
name: github-analysis
description: Use this skill to analyze GitHub repositories, including fetching repository information, issue statistics, and commit history.
---
# GitHub Repository Analysis Skill
## Overview
This skill enables the agent to analyze GitHub repositories by fetching repository information, issue statistics, and commit history. It uses the GitHub API to retrieve relevant data and provides insights for the user.
## Instructions
### 1. Set Up GitHub API Token
Before using this skill, ensure the agent has access to a GitHub API token. The token should be stored in an environment variable `GITHUB_TOKEN`.
### 2. Fetch Repository Information
Use the `fetch_github_repo` tool to get basic repository information:
- Repository name
- Owner
- Description
- Stars
- Forks
- Watchers
- Language
- License
Example: `fetch_github_repo(owner="langchain-ai", repo="langchain")`
### 3. Analyze Issue Statistics
Use the `analyze_issues` tool to get issue statistics:
- Total issues
- Open issues
- Closed issues
Average time to close
Most active contributors
Example:
Use the tool to retrieve commit history:
Total commits
Recent commits
Commit authors
Changes per commit
Example:
After fetching the necessary data, provide a comprehensive analysis of the repository, highlighting key metrics and trends.


