GitHub Copilot SDK 5分钟快速入门:构建你的首个AI Agent
时间:2026-06-18 17:12
好的,我们直接进入正题。 核心判断:Copilot SDK 到底解决了什么问题? GitHub Copilot SDK 的核心价值,并非在于“调用大语言模型(LLM)”的便捷性——OpenAI SDK、LangChain 这些工具已将此项任务完成得十分出色。它真正的优势,在于提供了一个经过数百
好的,我们直接进入正题。
## 核心判断:Copilot SDK 到底解决了什么问题?
GitHub Copilot SDK 的核心价值,并非在于“调用大语言模型(LLM)”的便捷性——OpenAI SDK、LangChain 这些工具已将此项任务完成得十分出色。它真正的优势,在于提供了一个经过数百万开发者日常验证的生产级 AI 智能体(Agent)运行时环境。
它的价值,实际上体现在以下三个关键点:
* **编排复杂性**:规划器、工具路由、状态管理等最令人头疼的环节,已被内置集成。
* **稳定性**:每天有数百万开发者在使用,其可靠性经过了实战的充分检验。
* **演进能力**:新模型、新工具能力,通过 CLI 自动更新即可,你无需过多操心。
因此,当你计划构建下一个 AI 应用时,不妨先问自己两个问题:
* **我的核心价值在哪里?** 如果核心是业务逻辑与工具定义,那么直接使用 SDK 即可;如果底层编排本身就有创新,再考虑自建框架。
* **多久能进入生产环境?** SDK 能帮你跳过 80% 的基础设施工作,让你专注于打磨最后那 20% 的差异化能力。
AI Agent 的开发门槛确实降低了,但真正的挑战从未改变:定义有价值的工具、设计流畅的交互、解决真实的问题。技术不再是壁垒,想象力才是。
## 引言:为什么 Agent 开发不再是少数人的游戏
2026 年 1 月,GitHub 发布了 Copilot SDK。这可以被视为一个关键转折点:AI Agent 开发,正从“专家专属”迈入“大众工具”时代。
回想一下,在此之前,如果你想构建一个能自主规划、调用工具、编辑文件的 AI Agent,需要完成一套相当复杂的流程:
* 选择并集成 LLM 服务(OpenAI、Anthropic、Azure 等)
* 自建 Agent 编排器(规划器、工具路由、状态管理)
* 处理流式输出、错误重试、上下文管理
* 实现工具定义标准(function calling schema)
这套流程不仅复杂,而且相当脆弱。尽管像 LangChain、AutoGPT 这样的开源框架降低了门槛,但你仍需深入理解 Agent 的运行机制。真正的转折点在于:GitHub 直接将 Copilot CLI 内部的生产级 Agent 运行时,以 SDK 形式开放出来。
这意味着什么?意味着你可以用极短的代码,启动一个完整的 Agent 运行时。下面的例子虽然简短,但背后是 Copilot CLI 在数百万开发者实战中验证过的能力:模型接入、Prompt 工程、响应解析……这些你都不必再操心,只需定义业务逻辑,剩下的交给 SDK。
本文的目标,是通过一个完整的天气助手案例,带你逐步理解三个核心概念:
* SDK 如何与 CLI 通信(即架构本质)
* 工具调用机制是如何工作的(LLM 如何“决定”调用你的代码)
* 从“玩具”到“工具”的关键跃迁点(流式响应、事件监听、状态管理)
无论你是想快速验证一个 AI 应用的创意,还是想为企业构建定制化的 Agent,这篇文章都会是一个不错的起点。
## 准备工作:环境搭建
在开始编码之前,请确保你的开发环境满足以下条件。
### 前置条件清单
1. **安装 GitHub Copilot CLI**
SDK 本身不包含 AI 推理能力,它通过 JSON-RPC 协议与 Copilot CLI 通信。打个比方,CLI 是真正的“引擎”,SDK 只是那个“方向盘”。
```
# macOS/Linux
brew install copilot-cli
# Verify installation
copilot --version
```
2. **认证 GitHub 账号**
```
copilot login
```
你需要一个 GitHub Copilot 订阅(个人版或企业版)。如果使用 BYOK(Bring Your Own Key)模式,可以跳过此步骤。
### 验证环境
运行以下命令以确认 CLI 工作正常:
```
copilot -p "Explain recursion in one sentence"
```
如果看到 AI 的回答,说明环境已就绪。
## 第一步:发送你的第一条消息
### 安装 SDK
创建项目目录并安装 Python SDK:
```
mkdir copilot-demo && cd copilot-demo
# 最好在虚拟环境中操作
python -m venv venv && source venv/bin/activate
pip install github-copilot-sdk
```
### 最简代码示例
创建一个 `main.py` 文件,写入以下代码:
```python
import asyncio
from copilot import CopilotClient
async def main():
client = CopilotClient()
await client.start()
session = await client.create_session({"model": "gpt-4.1"})
response = await session.send_and_wait({"prompt": "量子纠缠是什么?"})
print(response.data.content)
await client.stop()
asyncio.run(main())
```
运行 `python main.py`,你将看到 AI 的完整回答。仅用 9 行代码,一个完整的 AI 对话就完成了。这个过程背后发生了以下几件事:
1. `client.start()` —— SDK 启动 Copilot CLI 进程(在后台运行)
2. `create_session()` —— 通过 JSON-RPC 请求 CLI 创建会话
3. `send_and_wait()` —— 发送提示词,CLI 将其转发给 LLM
4. LLM 推理 —— 响应通过 CLI 返回给 SDK
5. `response.data` —— SDK 解析 JSON 响应,提取内容
### 架构本质:SDK 是 CLI 的“遥控器”
GitHub 的设计哲学是“关注点分离”。各组件的职责非常清晰:
| 组件 | 职责 |
| --- | --- |
| Copilot CLI | 智能体运行时(规划、工具调用、LLM 通信) |
| SDK | 进程管理、JSON-RPC 包装器、事件监听 |
| 你的代码 | 业务逻辑和工具定义 |
这种架构的优势很明显:
* **CLI 可独立升级**:新的模型或工具能力,无需修改 SDK
* **多语言支持成本低**:各语言 SDK 只需实现一个 JSON-RPC 客户端
* **调试友好**:CLI 可独立运行,方便观察日志和排查问题
理解这一点很重要:SDK 不是在“调用 LLM”,而是在“调用 CLI”。CLI 已经为你封装了所有复杂性。
### JSON-RPC 通信示例
当你调用 `send_and_wait()` 时,SDK 实际发送的请求是这样的:
```json
{
"jsonrpc": "2.0",
"method": "session.send",
"params": {
"sessionId": "abc123",
"prompt": "What is quantum entanglement?"
},
"id": 1
}
```
CLI 的响应:
```json
{
"jsonrpc": "2.0",
"result": {
"data": {
"content": "Quantum entanglement refers to a phenomenon where two or more quantum systems..."
}
},
"id": 1
}
```
## 第二步:让 AI 实时响应——流式输出
### 为什么需要流式响应
使用 `send_and_wait()` 时,你必须等待 LLM 生成完整回答后,才能看到任何输出。对于长文本生成(如代码解释、文档编写),用户可能得对着空白屏幕等上 10 到 30 秒。
流式响应能让 AI 像打字机一样逐字输出,不仅大大提升用户体验,还能让你提前发现模型是否“跑偏”。
### 事件监听机制
修改 `main.py`,启用流式输出:
```python
import asyncio
import sys
from copilot import CopilotClient
from copilot.generated.session_events import SessionEventType
async def main():
client = CopilotClient()
await client.start()
session = await client.create_session({
"model": "gpt-4.1",
"streaming": True, # 启用流式模式
})
# 监听响应片段
def handle_event(event):
if event.type == SessionEventType.ASSISTANT_MESSAGE_DELTA:
sys.stdout.write(event.data.delta_content)
sys.stdout.flush()
if event.type == SessionEventType.SESSION_IDLE:
print() # 完成时换行
session.on(handle_event)
await session.send_and_wait({"prompt": "Write a code example of quicksort"})
await client.stop()
asyncio.run(main())
```
运行后,你会看到结果像打字一样“流”出来,而不是一次性全部显示。
### 事件驱动模型的设计哲学
SDK 采用了观察者模式来处理 CLI 的异步事件流:CLI 生成事件 → SDK 解析 → 分发到监听器 → 你的 `handle_event()` 执行。
主要事件类型包括:
| 事件 | 触发时机 | 典型用途 |
| --- | --- | --- |
| ASSISTANT_MESSAGE_DELTA | AI 生成部分内容时 | 实时显示 |
| ASSISTANT_MESSAGE | AI 完成完整信息时 | 获取最终内容 |
| SESSION_IDLE | 会话进入空闲状态 | 标记任务完成 |
| TOOL_CALL | AI 决定调用工具时 | 日志记录、权限检查 |
### 代码对比:同步 vs 流式
**同步模式**,适合短回答的场景:
```python
response = await session.send_and_wait({"prompt": "1+1=?"})
print(response.data.content)
# 等待并一次性打印
```
**流式模式**,适合长文本场景:
```python
session.on(lambda event: print(event.data.delta_content, end="") if event.type == SessionEventType.ASSISTANT_MESSAGE_DELTA else None)
await session.send_and_wait({"prompt": "Write an article"})
```
### 背后的技术细节
流式响应基于 Server-Sent Events (SSE) 或 WebSocket:CLI 从 LLM 接收 token 流,每收到一个 token,就向 SDK 发送一个 `message_delta` 事件,SDK 触发你的事件监听器,用户立刻看到新内容。这种设计让你的应用能感知 AI 的“思考过程”,而不仅仅是拿到最终结果。
## 第三步:赋予 AI 能力——自定义工具
### 工具的本质:让 LLM 调用你的代码
到目前为止,AI 只能“说话”,无法与外部世界互动。工具(Tools)才是 Agent 的核心能力:你定义好函数,AI 来决定何时调用它们。
举个例子:
1. 用户问:“北京今天天气怎么样?”
2. AI 开始思考:我需要天气数据 → 调用 `get_weather("Beijing")`
3. 你的代码返回:`{"temperature": "15°C", "condition": "sunny"}`
4. AI 合成回答:“北京今天晴朗,15°C。”
关键点在于,是否调用工具、传递什么参数,这一切都由 AI 自主决定。
### 工具定义三要素
一个工具包含三个部分:
* **描述(description)**:告诉 AI 这个工具的功能
* **参数模式(parameters)**:定义输入参数的结构(使用 Pydantic)
* **处理器(handler)**:实际执行的 Python 函数
### 完整天气助手示例
创建一个 `weather_assistant.py` 文件:
```python
import asyncio
import random
import sys
from copilot import CopilotClient
from copilot.tools import define_tool
from copilot.generated.session_events import SessionEventType
from pydantic import BaseModel, Field
# 1. 定义参数模式
class GetWeatherParams(BaseModel):
city: str = Field(description="City name, e.g., Beijing, Shanghai")
# 2. 定义工具(描述 + 处理器)
@define_tool(description="Get current weather for a specified city")
async def get_weather(params: GetWeatherParams) -> dict:
city = params.city
# 生产环境中,这里应该调用真实的天气 API
# 这里为了演示,使用模拟数据
conditions = ["sunny", "cloudy", "rainy", "overcast"]
temp = random.randint(10, 30)
condition = random.choice(conditions)
return {
"city": city,
"temperature": f"{temp}°C",
"condition": condition
}
async def main():
client = CopilotClient()
await client.start()
# 3. 将工具传递给会话
session = await client.create_session({
"model": "gpt-4.1",
"streaming": True,
"tools": [get_weather], # 注册工具
})
# 监听流式响应
def handle_event(event):
if event.type == SessionEventType.ASSISTANT_MESSAGE_DELTA:
sys.stdout.write(event.data.delta_content)
sys.stdout.flush()
if event.type == SessionEventType.SESSION_IDLE:
print("")
session.on(handle_event)
# 发送一个需要调用工具的提示词
await session.send_and_wait({"prompt": "What's the weather like in Beijing and Shanghai? Compare them."})
await client.stop()
asyncio.run(main())
```
运行 `python weather_assistant.py`。
### 执行流程详解
当你问“What's the weather in Beijing and Shanghai”时,发生了一系列事情:
1. AI 分析问题 → 需要天气数据
2. AI 检查可用工具 → 找到 `get_weather` 函数
3. AI 决定调用 → `get_weather(city="Beijing")`
4. SDK 触发处理器 → 你的函数返回 `{"temperature": "22°C", ...}`
5. AI 收到结果 → 再次调用 `get_weather(city="Shanghai")`
6. AI 合成答案 → “北京晴,气温 22°C;上海阴,气温 18°C...”
AI 会自动决定调用工具的次数(这里北京一次、上海一次),你完全不需要编写任何循环逻辑。
### 参数模式的重要性
为什么用 Pydantic 定义参数?SDK 会将这个模式转换为 JSON Schema,然后传递给 LLM。描述写得越清晰,AI 调用工具的准确率就越高。
```python
class GetWeatherParams(BaseModel):
city: str = Field(description="City name")
unit: str = Field(default="celsius", description="Temperature unit: celsius or fahrenheit")
```
生成的 JSON Schema 如下:
```json
{
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"},
"unit": {"type": "string", "description": "Temperature unit"}
},
"required": ["city"]
}
```
LLM 会根据这个 schema 来提取参数。
## 第四步:构建交互式助手
现在,我们把所有能力组合起来:流式输出 + 工具调用 + 命令行交互。
### 完整可运行代码
创建一个 `interactive_assistant.py` 文件:
```python
import asyncio
import random
import sys
from copilot import CopilotClient
from copilot.tools import define_tool
from copilot.generated.session_events import SessionEventType
from pydantic import BaseModel, Field
# 定义工具
class GetWeatherParams(BaseModel):
city: str = Field(description="City name, e.g., Beijing, Shanghai, Guangzhou")
@define_tool(description="Get current weather for a specified city")
async def get_weather(params: GetWeatherParams) -> dict:
city = params.city
conditions = ["sunny", "cloudy", "rainy", "overcast", "hazy"]
temp = random.randint(5, 35)
condition = random.choice(conditions)
humidity = random.randint(30, 90)
return {
"city": city,
"temperature": f"{temp}°C",
"condition": condition,
"humidity": f"{humidity}%"
}
async def main():
client = CopilotClient()
await client.start()
session = await client.create_session({
"model": "gpt-4.1",
"streaming": True,
"tools": [get_weather],
})
# 事件监听
def handle_event(event):
if event.type == SessionEventType.ASSISTANT_MESSAGE_DELTA:
sys.stdout.write(event.data.delta_content)
sys.stdout.flush()
if event.type == SessionEventType.SESSION_IDLE:
print() # 完成时换行
session.on(handle_event)
# 交互式对话循环
print("☁️ Weather Assistant (type 'exit' to quit)")
print("Try: 'What's the weather in Beijing?' or 'Compare weather in Guangzhou and Shenzhen'")
while True:
try:
user_input = input("You: ")
except EOFError:
break
if user_input.lower() in ["exit", "quit"]:
break
if not user_input.strip():
continue
sys.stdout.write("Assistant: ")
await session.send_and_wait({"prompt": user_input})
print() # 额外换行
await client.stop()
print("Goodbye!")
asyncio.run(main())
```
运行 `python interactive_assistant.py`,你会看到一个示例对话:
```
☁️ Weather Assistant (type 'exit' to quit)
Try: 'What's the weather in Beijing?' or 'Compare weather in Guangzhou and Shenzhen'
You: Compare weather in Guangzhou and Shenzhen
Assistant: Guangzhou: 21°C, sunny, 84% humidity.
Shenzhen: 33°C, hazy, 77% humidity.
Shenzhen is significantly warmer and hazier, while Guangzhou is cooler and sunnier with slightly higher humidity.
You: What's the weather in Shenzhen
Assistant: The weather in Shenzhen is 8°C, overcast, with 47% humidity.
You: quit
Goodbye!
```
### 关键设计点
1. **会话持久化**:注意我们只创建了一次 `session`,整个对话循环中持续复用。这意味着 AI 能记住之前的对话内容,你可以追问“What about tomorrow?”,它会知道你指的是哪个城市。工具调用的历史也同样被保留。
2. **异步 I/O 的正确姿势**:代码中的 `input()` 是同步阻塞的,但在这里是可以接受的,因为我们是在等待用户输入。真正的异步操作发生在与 CLI 通信时。
3. **优雅退出**:通过捕获 `EOFError` 和常见的退出命令(`exit`、`quit`),确保用户体验流畅。
### 扩展思路
基于这个框架,可以快速扩展出很多功能。
**添加更多工具**:
```python
@define_tool(description="Query real-time stock price")
async def get_stock_price(params): ...
@define_tool(description="Search information on the web")
async def web_search(params): ...
session = await client.create_session({
"tools": [get_weather, get_stock_price, web_search],
})
```
AI 会根据用户问题,自动选择合适的工具。
**添加系统提示词**:
```python
session = await client.create_session({
"model": "gpt-4.1",
"tools": [get_weather],
"system_message": {
"content": "You are a professional weather assistant. Keep answers concise but informative."
}
})
```
**记录工具调用日志**:
```python
def handle_event(event):
if event.type == SessionEventType.TOOL_CALL:
print(f"[Debug] AI called tool: {event.data.tool_name}")
print(f"[Debug] Arguments: {event.data.arguments}")
```
### 调试技巧
开发过程中,观察 CLI 的日志对于理解 Agent 的行为至关重要。
**启动独立的 CLI Server**:
```
# 启动调试模式的 CLI Server
copilot --headless --log-level debug --port 9999
# 可选:指定日志目录
copilot --headless --log-level debug --port 9999 --log-dir ./logs
```
**在代码中连接到 Server**:
```python
client = CopilotClient({
'cli_url': 'https://localhost:9999',
})
await client.start()
# 不会启动新进程,直接连接到已有的 Server
```
**查看日志**:默认情况下,日志保存在 `~/.copilot/logs/` 目录中,每个 Server 进程都有独立的日志文件。可以用 `tail -f` 实时观察。
**调试工具调用**:
```python
def handle_event(event):
# 工具调用开始
if event.type == SessionEventType.TOOL_USER_REQUESTED:
print(f"[Tool Call] {event.data.tool_name}")
print(f"Arguments: {event.data.arguments}")
# 工具执行结果
if event.type == SessionEventType.TOOL_EXECUTION_COMPLETE:
print(f"[Tool Result] {event.data.tool_name}")
print(f"Result: {event.data.result}")
# AI 的最终响应
if event.type == SessionEventType.ASSISTANT_MESSAGE:
print(f"[Assistant] {event.data.content[:100]}...")
session.on(handle_event)
```
这种调试模式能让你清晰地看到整个工具调用链路:AI 决策 → 工具执行 → 结果返回 → 最终响应。
---
**参考资料**
[1] GitHub Copilot SDK: [链接]
[2] Copilot SDK: [链接]
来源:https://cloud.tencent.com.cn/developer/article/2692711
本站内容用于信息整理与展示,如有侵权或内容问题请及时联系处理。