共用方式為


以任何 IChatClient 為基礎的代理程式

Microsoft Agent Framework 支援為任何提供 Microsoft.Extensions.AI.IChatClient 實作的推論服務建立代理程式。 這意味著有非常廣泛的服務可用於創建代理,包括可以在本地運行的開源模型。

本文以奧拉瑪為例。

使用者入門

將必要的 NuGet 套件新增至您的專案。

dotnet add package Microsoft.Agents.AI --prerelease

您還需要為要使用的特定 IChatClient 實作新增套件。 這個範例使用 OllamaSharp

dotnet add package OllamaSharp

建立一個聊天客戶端代理

若要根據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"
    )

這很重要

為確保您充分利用代理程式,如果您打算使用工具,請務必選擇非常適合交談任務並支援函數呼叫的服務和模型。

使用代理程式

代理程式支援所有標準代理程式作業。

想了解更多如何執行及與代理互動的資訊,請參閱代理 入門教學

後續步驟