Microsoft Agent Framework 支持为提供 Microsoft.Extensions.AI.IChatClient 实现的任何推理服务创建代理。 这意味着可以使用非常广泛的服务来创建代理,包括可在本地运行的开放源代码模型。
在本文档中,我们将使用 Ollama 作为示例。
入门
将所需的 NuGet 包添加到项目。
dotnet add package Microsoft.Agents.AI --prerelease
还需要为要使用的特定 IChatClient 实现添加包。 在此示例中,我们将使用 OllamaSharp。
dotnet add package OllamaSharp
创建 ChatClientAgent
若要基于 IChatClient 接口创建代理,可以使用该 ChatClientAgent 类。
ChatClientAgent 类将 IChatClient 作为构造函数的参数。
首先,创建一个 OllamaApiClient 来访问 Ollama 服务。
using System;
using Microsoft.Agents.AI;
using OllamaSharp;
using OllamaApiClient chatClient = new(new Uri("http://localhost:11434"), "phi3");
OllamaApiClient实现IChatClient接口,因此你可以使用它来创建一个 ChatClientAgent。
AIAgent agent = new ChatClientAgent(
chatClient,
instructions: "You are good at telling jokes.",
name: "Joker");
// Invoke the agent and output the text result.
Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate."));
重要
若要确保充分利用代理,请确保选择一个适合聊天任务且支持函数调用的服务和模型。
使用代理
代理是标准 AIAgent 代理,支持所有标准代理操作。
有关如何运行和与代理交互的详细信息,请参阅 代理入门教程 。
Microsoft Agent Framework 支持为提供与 ChatClientProtocol 兼容的聊天客户端实现的任何推理服务创建代理。 这意味着可以使用非常广泛的服务来创建代理,包括可在本地运行的开放源代码模型。
入门
将所需的 Python 包添加到项目。
pip install agent-framework --pre
可能还需要为要使用的特定聊天客户端实现添加包:
# For Azure AI
pip install agent-framework-azure-ai --pre
# For custom implementations
# Install any required dependencies for your custom client
内置聊天客户端
该框架提供多个内置聊天客户端实现:
OpenAI 聊天客户端
from agent_framework import ChatAgent
from agent_framework.openai import OpenAIChatClient
# Create agent using OpenAI
agent = ChatAgent(
chat_client=OpenAIChatClient(model_id="gpt-4o"),
instructions="You are a helpful assistant.",
name="OpenAI Assistant"
)
Azure OpenAI 聊天客户端
from agent_framework import ChatAgent
from agent_framework.azure import AzureOpenAIChatClient
# Create agent using Azure OpenAI
agent = ChatAgent(
chat_client=AzureOpenAIChatClient(
model_id="gpt-4o",
endpoint="https://your-resource.openai.azure.com/",
api_key="your-api-key"
),
instructions="You are a helpful assistant.",
name="Azure OpenAI Assistant"
)
Azure AI 代理客户端
from agent_framework import ChatAgent
from agent_framework.azure import AzureAIAgentClient
from azure.identity.aio import AzureCliCredential
# Create agent using Azure AI
async with AzureCliCredential() as credential:
agent = ChatAgent(
chat_client=AzureAIAgentClient(async_credential=credential),
instructions="You are a helpful assistant.",
name="Azure AI Assistant"
)
重要
若要确保充分利用代理,请确保选择一个适合对话任务的服务和模型,并且如果你打算使用工具,则支持函数调用。
使用代理
代理支持所有标准操作
有关如何运行和与代理交互的详细信息,请参阅 代理入门教程 。