Skip to main content
Select language: current language is Simplified Chinese
搜索或询问 Copilot
打开菜单

Microsoft Agent Framework 集成

用作 Copilot SDK Microsoft Agent Framework 中的代理提供程序,与其他 AI 提供程序一起生成和协调多代理工作流。

谁可以使用此功能?

GitHub Copilot SDK 适用于所有 Copilot 计划。

注意

          Copilot SDK 当前处于 公共预览版. 功能和可用性可能会发生更改。

用 Copilot SDK 作为 Microsoft Agent Framework (MAF) 中的代理提供程序,与 Azure OpenAI、Anthropic 和其他提供程序一起组合多代理工作流。

概述

Microsoft Agent Framework 是语义内核和 AutoGen 的统一继任者。 它提供用于生成、协调和部署 AI 代理的标准接口。 使用专用的集成包可以将 Copilot SDK 客户端封装为高级 MAF 代理,可以与框架中任何其他代理提供程序互换使用。

概念说明
Microsoft代理框架用于 .NET 和 Python 的单代理和多代理编排的开源框架
代理提供程序为助手(如 Copilot、Azure OpenAI、Anthropic 等)提供支持的后端。
协调器用于协调顺序、并发或交接工作流中代理的 MAF 组件
A2A 协议框架支持的代理到代理通信标准

注意

MAF 集成包适用于 .NET 和 Python。 对于 TypeScript 和 Go,请直接使用 Copilot SDK 标准 SDK API 提供工具调用、流式处理和自定义代理。

先决条件

在开始之前,请确保具备:

  • 在您选择的语言中可用的 Copilot SDK 设置。 请参阅“开始使用 Copilot SDK”。
  • 订阅 GitHub Copilot (个人、商业或企业)。
  •         Copilot 命令行界面(CLI) 通过 SDK 捆绑的 CLI 安装或可用。
    

安装

将Copilot SDK与MAF集成包一起安装。

dotnet add package GitHub.Copilot.SDK
dotnet add package Microsoft.Agents.AI.GitHub.Copilot --prerelease

有关 Python 中的示例,请参阅 microsoft-agent-framework 存储库中的 github/copilot-sdk 文章。

基本用法

使用单个方法调用将 Copilot SDK 客户端包装为 MAF 代理。 生成的代理符合框架的标准接口,可在预期 MAF 代理的任何位置使用。

using GitHub.Copilot.SDK;
using Microsoft.Agents.AI;

await using var copilotClient = new CopilotClient();
await copilotClient.StartAsync();

// Wrap as a MAF agent
AIAgent agent = copilotClient.AsAIAgent();

// Use the standard MAF interface
string response = await agent.RunAsync("Explain how dependency injection works in ASP.NET Core");
Console.WriteLine(response);

有关 Python 中的示例,请参阅 microsoft-agent-framework 存储库中的 github/copilot-sdk 文章。

添加自定义工具

使用自定义函数工具来扩展Copilot代理。 当代理在 MAF 中运行时,通过标准 Copilot SDK 定义的工具将自动可用。

using GitHub.Copilot.SDK;
using Microsoft.Extensions.AI;
using Microsoft.Agents.AI;

// Define a custom tool
AIFunction weatherTool = AIFunctionFactory.Create(
    (string location) => $"The weather in {location} is sunny with a high of 25°C.",
    "GetWeather",
    "Get the current weather for a given location."
);

await using var copilotClient = new CopilotClient();
await copilotClient.StartAsync();

// Create agent with tools
AIAgent agent = copilotClient.AsAIAgent(new AIAgentOptions
{
    Tools = new[] { weatherTool },
});

string response = await agent.RunAsync("What's the weather like in Seattle?");
Console.WriteLine(response);

你还可以将 Copilot SDK 的原生工具定义与 MAF 工具一起使用:

import { CopilotClient, DefineTool } from "@github/copilot-sdk";

const getWeather = DefineTool({
    name: "GetWeather",
    description: "Get the current weather for a given location.",
    parameters: { location: { type: "string", description: "City name" } },
    execute: async ({ location }) => `The weather in ${location} is sunny, 25°C.`,
});

const client = new CopilotClient();
const session = await client.createSession({
    model: "gpt-4.1",
    tools: [getWeather],
    onPermissionRequest: async () => ({ kind: "approved" }),
});

await session.sendAndWait({ prompt: "What's the weather like in Seattle?" });

警告

onPermissionRequest 设置为总是返回 { kind: "approved" } 可以允许在未经批准的情况下进行提示注入。 对于生产用途,请在授予权限之前实现基于策略的审批,以评估请求的工具及其参数。

有关 Python 中的示例,请参阅 microsoft-agent-framework 存储库中的 github/copilot-sdk 文章。

多代理工作流

MAF 集成的主要好处是能够与编排的工作流中的其他代理提供程序一起组合Copilot。 使用框架内置的编排器创建流水线,不同的代理负责不同的步骤。

顺序工作流

逐个运行代理,将输出从一个传递到下一个:

using GitHub.Copilot.SDK;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Orchestration;

await using var copilotClient = new CopilotClient();
await copilotClient.StartAsync();

// Copilot agent for code review
AIAgent reviewer = copilotClient.AsAIAgent(new AIAgentOptions
{
    Instructions = "You review code for bugs, security issues, and best practices. Be thorough.",
});

// Azure OpenAI agent for generating documentation
AIAgent documentor = AIAgent.FromOpenAI(new OpenAIAgentOptions
{
    Model = "gpt-4.1",
    Instructions = "You write clear, concise documentation for code changes.",
});

// Compose in a sequential pipeline
var pipeline = new SequentialOrchestrator(new[] { reviewer, documentor });

string result = await pipeline.RunAsync(
    "Review and document this pull request: added retry logic to the HTTP client"
);
Console.WriteLine(result);

有关 Python 中的示例,请参阅 microsoft-agent-framework 存储库中的 github/copilot-sdk 文章。

并发工作流

并行运行多个代理并聚合其结果:

using GitHub.Copilot.SDK;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Orchestration;

await using var copilotClient = new CopilotClient();
await copilotClient.StartAsync();

AIAgent securityReviewer = copilotClient.AsAIAgent(new AIAgentOptions
{
    Instructions = "Focus exclusively on security vulnerabilities and risks.",
});

AIAgent performanceReviewer = copilotClient.AsAIAgent(new AIAgentOptions
{
    Instructions = "Focus exclusively on performance bottlenecks and optimization opportunities.",
});

// Run both reviews concurrently
var concurrent = new ConcurrentOrchestrator(new[] { securityReviewer, performanceReviewer });

string combinedResult = await concurrent.RunAsync(
    "Analyze this database query module for issues"
);
Console.WriteLine(combinedResult);

流式处理响应

在构建交互式应用程序时,通过流式传输代理的响应,以显示实时输出。 MAF 集成保留了 Copilot SDK 的流式处理功能。

using GitHub.Copilot.SDK;
using Microsoft.Agents.AI;

await using var copilotClient = new CopilotClient();
await copilotClient.StartAsync();

AIAgent agent = copilotClient.AsAIAgent(new AIAgentOptions
{
    Streaming = true,
});

await foreach (var chunk in agent.RunStreamingAsync("Write a quicksort implementation in C#"))
{
    Console.Write(chunk);
}
Console.WriteLine();

还可以直接通过Copilot SDK进行流式传输,而无需使用MAF。

import { CopilotClient } from "@github/copilot-sdk";

const client = new CopilotClient();
const session = await client.createSession({
    model: "gpt-4.1",
    streaming: true,
    onPermissionRequest: async () => ({ kind: "approved" }),
});

session.on("assistant.message_delta", (event) => {
    process.stdout.write(event.data.delta ?? "");
});

await session.sendAndWait({ prompt: "Write a quicksort implementation in TypeScript" });

有关 Python 中的示例,请参阅 microsoft-agent-framework 存储库中的 github/copilot-sdk 文章。

配置参考

MAF 代理选项

财产类型说明
Instructions字符串代理的系统提示
ToolsAIFunction[] 或 列表代理可用的自定义函数工具
Streaming布尔启用流式处理响应
Model字符串覆盖默认模型

Copilot SDK 选项(传递)

创建基础SessionConfig客户端时,所有标准Copilot选项都可用。 MAF 包装器在内部委托给 SDK。

SDK 功能MAF 支持
自定义工具(DefineToolAIFunctionFactory已与 MAF 工具合并
MCP 服务器在 SDK 客户端上配置
自定义代理和子代理在 Copilot 代理中可用
无限会话在 SDK 客户端上配置
模型选择可为每个代理或每次调用单独覆盖设置
流媒体完整增量事件支持

最佳做法

选择正确的集成级别

需要与协调工作流中的其他提供程序一起撰写 Copilot 时,请使用 MAF 包装器。 如果应用程序仅使用 Copilot,则独立 SDK 更简单,可让你完全控制:

// Standalone SDK — full control, simpler setup
import { CopilotClient } from "@github/copilot-sdk";

const client = new CopilotClient();
const session = await client.createSession({
    model: "gpt-4.1",
    onPermissionRequest: async () => ({ kind: "approved" }),
});
const response = await session.sendAndWait({ prompt: "Explain this code" });

使代理保持专注

生成多代理工作流时,请为每个代理提供一个具有明确说明的特定角色。 避免重叠责任:

// Too vague — overlapping roles
const agents = [
    { instructions: "Help with code" },
    { instructions: "Assist with programming" },
];

// Focused — clear separation of concerns
const agents = [
    { instructions: "Review code for security vulnerabilities. Flag SQL injection, XSS, and auth issues." },
    { instructions: "Optimize code performance. Focus on algorithmic complexity and memory usage." },
];

在编排级别处理错误

将代理调用封装在错误处理中,尤其是在多代理工作流程中,其中一个代理失败不应阻碍整个流程。

try
{
    string result = await pipeline.RunAsync("Analyze this module");
    Console.WriteLine(result);
}
catch (AgentException ex)
{
    Console.Error.WriteLine($"Agent {ex.AgentName} failed: {ex.Message}");
    // Fall back to single-agent mode or retry
}

延伸阅读

  •         [AUTOTITLE](/copilot/how-tos/copilot-sdk/sdk-getting-started)
    
  •         [Microsoft Agent Framework 文档](https://learn.microsoft.com/en-us/agent-framework/agents/providers/github-copilot)
    
  •         [博客:使用 GitHub Copilot SDK 和 Microsoft Agent Framework 生成 AI 代理](https://devblogs.microsoft.com/semantic-kernel/build-ai-agents-with-github-copilot-sdk-and-microsoft-agent-framework/)
    
Morty Proxy This is a proxified and sanitized view of the page, visit original site.