LangChain Agent Skills 使用案例:GitHub 仓库分析
本文演示了如何为 LangChain Agent 创建 GitHub 仓库分析技能。通过构建包含 SKILL.md 说明和 Python 脚本的技能目录,实现了获取仓库元数据、Issue 统计及 Commit 历史的功能。提供了完整的代码示例,包括环境配置、API 调用及代理集成流程。文章分析了技能模式相较于传统工具的优势,如降低系统提示负载、增强上下文理解及模块化复用,适用于项目健康度评估及技术调研场景。

本文演示了如何为 LangChain Agent 创建 GitHub 仓库分析技能。通过构建包含 SKILL.md 说明和 Python 脚本的技能目录,实现了获取仓库元数据、Issue 统计及 Commit 历史的功能。提供了完整的代码示例,包括环境配置、API 调用及代理集成流程。文章分析了技能模式相较于传统工具的优势,如降低系统提示负载、增强上下文理解及模块化复用,适用于项目健康度评估及技术调研场景。

本案例将展示如何创建和使用一个名为 github-analysis 的 Agent Skill,使 LangChain 深度代理能够分析 GitHub 仓库,包括获取仓库信息、问题统计和提交历史。这个技能将帮助用户快速获取 GitHub 仓库的关键指标,无需手动查询。
首先,我们需要创建一个技能目录结构:
skills/ └── github-analysis/
├── SKILL.md
└── github_analysis.py
--- 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: analyze_issues(owner="langchain-ai", repo="langchain")
### 4. Get Commit History
Use the `get_commit_history` tool to retrieve commit history:
- Total commits
- Recent commits
- Commit authors
- Changes per commit
Example: get_commit_history(owner="langchain-ai", repo="langchain", branch="main")
### 5. Provide Comprehensive Analysis
After fetching the necessary data, provide a comprehensive analysis of the repository, highlighting key metrics and trends.
## Example Usage
User: "Can you analyze the LangChain repository on GitHub?"
Agent:
1. Uses `fetch_github_repo` to get basic repository info
2. Uses `analyze_issues` to get issue statistics
3. Uses `get_commit_history` to get commit history
4. Combines the data to provide a comprehensive analysis
Response: "The LangChain repository (langchain-ai/langchain) has 25,000 stars and 5,000 forks. It has a total of 12,000 issues, with 3,000 open and 9,000 closed. The average time to close an issue is 14 days. The main contributors are @gchhablani, @michael-007, and @joseph-lee. The commit history shows 15,000 commits on the main branch, with the most recent 10 commits focusing on improving the LangGraph integration."
import os
import requests
from typing import Dict, List, Optional, Tuple
from datetime import datetime
def fetch_github_repo(owner: str, repo: str) -> Dict:
"""Fetch basic repository information from GitHub API."""
url = f"https://api.github.com/repos/{owner}/{repo}"
headers = {"Authorization": f"token {os.getenv('GITHUB_TOKEN')}"}
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
def analyze_issues(owner: str, repo: str) -> Dict:
"""Analyze issue statistics for a GitHub repository."""
url = f"https://api.github.com/repos/{owner}/{repo}/issues?state=all"
headers = {"Authorization": f"token {os.getenv('GITHUB_TOKEN')}"}
response = requests.get(url, headers=headers)
response.raise_for_status()
issues = response.json()
total_issues = len(issues)
open_issues = sum(1 for issue in issues if issue['state'] == 'open')
closed_issues = total_issues - open_issues
close_times = []
issue issues:
issue[] == issue[]:
created_at = issue[]
closed_at = issue[]
time_diff = (datetime.strptime(closed_at, ) - datetime.strptime(created_at, )).total_seconds()
close_times.append(time_diff)
avg_close_time = (close_times) / (close_times) close_times
contributors = {}
issue issues:
issue[]:
user = issue[][]
contributors[user] = contributors.get(user, ) +
top_contributors = (contributors.items(), key= x: x[], reverse=)[:]
{
: total_issues,
: open_issues,
: closed_issues,
: avg_close_time / ( * ) avg_close_time ,
: top_contributors
}
() -> :
url =
headers = {: }
response = requests.get(url, headers=headers)
response.raise_for_status()
commits = response.json()
{
: (commits),
: commits[:],
: {commit[][]: commit commits},
: []
}
以下是一个完整的代码示例,展示如何在 LangChain 深度代理中使用这个 GitHub 分析技能:
import os
import json
from datetime import datetime
from urllib.request import urlopen
from deepagents import create_deep_agent
from langgraph.checkpoint.memory import MemorySaver
from deepagents.backends.filesystem import FilesystemBackend
# 设置环境变量(实际应用中应从安全位置获取)
os.environ["GITHUB_TOKEN"] = "your_github_token_here" # 请替换为实际的 GitHub Token
# 创建技能目录结构
skills_dir = "./skills/github-analysis"
os.makedirs(skills_dir, exist_ok=True)
# 创建 SKILL.md 文件
skill_md_content = """
--- 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: analyze_issues(owner="langchain-ai", repo="langchain")
### 4. Get Commit History
Use the `get_commit_history` tool to retrieve commit history:
- Total commits
- Recent commits
- Commit authors
- Changes per commit
Example: get_commit_history(owner="langchain-ai", repo="langchain", branch="main")
### 5. Provide Comprehensive Analysis
After fetching the necessary data, provide a comprehensive analysis of the repository, highlighting key metrics and trends.
## Example Usage
User: "Can you analyze the LangChain repository on GitHub?"
Agent:
1. Uses `fetch_github_repo` to get basic repository info
2. Uses `analyze_issues` to get issue statistics
3. Uses `get_commit_history` to get commit history
4. Combines the data to provide a comprehensive analysis
Response: "The LangChain repository (langchain-ai/langchain) has 25,000 stars and 5,000 forks. It has a total of 12,000 issues, with 3,000 open and 9,000 closed. The average time to close an issue is 14 days. The main contributors are @gchhablani, @michael-007, and @joseph-lee. The commit history shows 15,000 commits on the main branch, with the most recent 10 commits focusing on improving the LangGraph integration."
"""
(os.path.join(skills_dir, ), ) f:
f.write(skill_md_content)
github_analysis_content = """
import os
import requests
from typing import Dict, List, Optional, Tuple
from datetime import datetime
def fetch_github_repo(owner: str, repo: str) -> Dict:
"""Fetch basic repository information from GitHub API."""
url = f"https://api.github.com/repos/{owner}/{repo}"
headers = {"Authorization": f"token {os.getenv('GITHUB_TOKEN')}"}
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
def analyze_issues(owner: str, repo: str) -> Dict:
"""Analyze issue statistics for a GitHub repository."""
url = f"https://api.github.com/repos/{owner}/{repo}/issues?state=all"
headers = {"Authorization": f"token {os.getenv('GITHUB_TOKEN')}"}
response = requests.get(url, headers=headers)
response.raise_for_status()
issues = response.json()
total_issues = len(issues)
open_issues = sum(1 for issue in issues if issue['state'] == 'open')
closed_issues = total_issues - open_issues
# Calculate average time to close
close_times = []
for issue in issues:
if issue['state'] == 'closed' and issue['closed_at']:
created_at = issue['created_at']
closed_at = issue['closed_at']
# Convert to seconds
time_diff = (datetime.strptime(closed_at, "%Y-%m-%dT%H:%M:%SZ") - datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%SZ")).total_seconds()
close_times.append(time_diff)
avg_close_time = sum(close_times) / len(close_times) if close_times else 0
# Get most active contributors
contributors = {}
for issue in issues:
if issue['user']:
user = issue['user']['login']
contributors[user] = contributors.get(user, 0) + 1
top_contributors = sorted(contributors.items(), key=lambda x: x[1], reverse=True)[:3]
return {
"total_issues": total_issues,
"open_issues": open_issues,
"closed_issues": closed_issues,
"avg_close_time_days": avg_close_time / (24 * 3600) if avg_close_time else 0,
"top_contributors": top_contributors
}
def get_commit_history(owner: str, repo: str, branch: str = "main") -> Dict:
"""Get commit history for a GitHub repository branch."""
url = f"https://api.github.com/repos/{owner}/{repo}/commits?sha={branch}"
headers = {"Authorization": f"token {os.getenv('GITHUB_TOKEN')}"}
response = requests.get(url, headers=headers)
response.raise_for_status()
commits = response.json()
return {
"total_commits": len(commits),
"recent_commits": commits[:10],
"commit_authors": {commit['author']['login']: 0 for commit in commits},
"changes_per_commit": []
}
"""
with open(os.path.join(skills_dir, ), ) f:
f.write(github_analysis_content)
checkpointer = MemorySaver()
agent = create_deep_agent(
backend=FilesystemBackend(root_dir="./"),
skills=["./skills/"],
interrupt_on={"write_file": True, "read_file": False, "edit_file": True},
checkpointer=checkpointer
)
# 运行代理
result = agent.invoke(
{"messages": [{"role": "user", "content": "Can you analyze the LangChain repository on GitHub?"}]},
config={"configurable": {"thread_id": "github_analysis_1"}}
)
# 打印结果
print("Agent Response:")
print(json.dumps(result, indent=2))
当运行上述代码时,代理将使用 github-analysis 技能分析 LangChain 仓库,并返回类似以下的响应:
{"messages": [{"role": "assistant", "content": "The LangChain repository (langchain-ai/langchain) has 25,000 stars and 5,000 forks. It has a total of 12,000 issues, with 3,000 open and 9,000 closed. The average time to close an issue is 14 days. The main contributors are @gchhablani, @michael-007, and @joseph-lee. The commit history shows 15,000 commits on the main branch, with the most recent 10 commits focusing on improving the LangGraph integration."}]}
SKILL.md 和必要的脚本文件。fetch_github_repo, analyze_issues, get_commit_history)由代理自动调用。通过这个案例,我们可以看到 Agent Skills 如何有效扩展 LangChain 代理的功能,使代理能够执行更复杂的任务,同时保持系统提示的简洁和高效。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML 转 Markdown 互为补充。 在线工具,Markdown 转 HTML在线工具,online