Dapr 交談建構元素會透過 Dapr 側車路由模型推論,並公開一個可支援 Agent Framework .NET Agent 的IChatClient。 模型提供者與憑證是在 Dapr 對話元件中設定,而非直接在代理程序中設定。
Prerequisites
- .NET 10 或更新版本。
- Docker 與 Dapr CLI。
- 一個已設定好的 Dapr 交談元件,例如由 Ollama 支援的元件。
安裝套件
dotnet add package Dapr.AI.Microsoft.Extensions
dotnet add package Microsoft.Agents.AI --prerelease
Configuration
DAPR_GRPC_ENDPOINT="http://localhost:3501"
DAPR_GRPC_ENDPOINT 是選擇性的,預設為 http://localhost:3501。 在應用程式代碼中設定 ConversationComponentName 為 Dapr 會話元件的名稱,例如 ollama。
建立一個由 Dapr 支援的代理
透過依賴注入配置 Dapr 側車端點與會話元件,解析 IChatClient,並將其轉換為代理。
// the sidecar (see this sample's README). Override it with the DAPR_GRPC_ENDPOINT environment
// variable if you run the sidecar on a different port.
var daprGrpcEndpoint = Environment.GetEnvironmentVariable("DAPR_GRPC_ENDPOINT") ?? "http://localhost:3501";
// Register the Dapr Conversation client with dependency injection.
var app = Host.CreateDefaultBuilder()
.ConfigureServices(services =>
{
// Configure the gRPC endpoint for the Dapr sidecar.
services.AddDaprConversationClient((_, builder) => builder.UseGrpcEndpoint(daprGrpcEndpoint));
// Provide the name of the Conversation component loaded in the sidecar to use.
services.AddDaprChatClient(opt => opt.ConversationComponentName = "ollama");
}).Build();
// Get an instance of the Dapr chat client from the dependency injection container.
using var scope = app.Services.CreateScope();
var daprChatClient = scope.ServiceProvider.GetRequiredService<IChatClient>();
// Use this chat client to construct an AIAgent.
AIAgent agent = daprChatClient.AsAIAgent(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."));
提供者的能力取決於 Dapr 對話元件及其背後的模型。
Tools
工具支援則繼承自配置的 Dapr 會話元件與模型。
| Tool | 現況 | 註釋 |
|---|---|---|
| 函式工具 | 情況不一 | 需要來自已設定元件和模型的函式呼叫支援。 |
| 工具核准 | 情況不一 | 當模型產生函式工具呼叫時,即可使用。 |
| 由提供者主機代管的工具 | ❌ | Dapr 不會另外增加一個獨立的 Agent Framework 代管工具介面。 |
| 本地 MCP 工具 | ✅ | 在應用程式程序中執行。 |