Ollama 允许在本地运行开源模型,并将其与 Agent Framework 配合使用。 这非常适合用于在本地保留数据的开发、测试和方案。
以下示例演示如何使用 Ollama 创建代理:
using System;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
// Create an Ollama agent using Microsoft.Extensions.AI.Ollama
// Requires: dotnet add package Microsoft.Extensions.AI.Ollama --prerelease
var chatClient = new OllamaChatClient(
new Uri("http://localhost:11434"),
modelId: "llama3.2");
AIAgent agent = chatClient.AsAIAgent(
instructions: "You are a helpful assistant running locally via Ollama.");
Console.WriteLine(await agent.RunAsync("What is the largest city in France?"));
注释
通过 OpenAI 兼容的 API 提供对 Ollama 的 Python 支持。 使用 OpenAIChatClient 指向 Ollama 实例的自定义基 URL。