Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Mit Ollama können Sie Open-Source-Modelle lokal ausführen und mit Agent Framework verwenden. Dies eignet sich ideal für Entwicklung, Tests und Szenarien, in denen Sie Daten lokal aufbewahren müssen.
Voraussetzungen
- Installieren und starten Sie Ollama.
- Laden Sie ein Modell herunter, z. B
ollama pull llama3.2. .
Installation
dotnet add package OllamaSharp
dotnet add package Microsoft.Agents.AI --prerelease
Configuration
OLLAMA_ENDPOINT="http://localhost:11434"
OLLAMA_MODEL_NAME="llama3.2"
Erstellen eines Ollama-Agents
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OllamaSharp;
var endpoint = Environment.GetEnvironmentVariable("OLLAMA_ENDPOINT") ?? throw new InvalidOperationException("OLLAMA_ENDPOINT is not set.");
var modelName = Environment.GetEnvironmentVariable("OLLAMA_MODEL_NAME") ?? throw new InvalidOperationException("OLLAMA_MODEL_NAME is not set.");
// Get a chat client for Ollama and use it to construct an AIAgent.
AIAgent agent = new OllamaApiClient(new Uri(endpoint), modelName)
.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."));
Voraussetzungen
Stellen Sie sicher, dass Ollama lokal mit einem heruntergeladenen Modell installiert und ausgeführt wird, bevor Sie Beispiele ausführen:
ollama pull llama3.2
Note
Nicht alle Modelle unterstützen Funktionsaufrufe. Für die Toolverwendung versuchen llama3.2 oder qwen3:4b.
Installation
pip install agent-framework-ollama --pre
Configuration
OLLAMA_MODEL="llama3.2"
Der native Client stellt standardmäßig eine Verbindung mit dem systemeigenen Client bereit http://localhost:11434 . Überschreiben Sie sie mit der OLLAMA_HOST Umgebungsvariable oder dem host Konstruktorargument.
Erstellen von Ollama Agents
OllamaChatClient bietet native Ollama-Integration mit vollständiger Unterstützung für Funktionstools und Streaming.
import asyncio
from agent_framework import Agent
from agent_framework.ollama import OllamaChatClient
async def main():
agent = Agent(
client=OllamaChatClient(),
name="HelpfulAssistant",
instructions="You are a helpful assistant running locally via Ollama.",
)
result = await agent.run("What is the largest city in France?")
print(result)
asyncio.run(main())
Tools
Die Python Ollama-Clients (OllamaChatClient und OpenAIChatClient auf einen Ollama-kompatiblen Endpunkt verwiesen) unterstützen lokal aufgerufene Tools. Gehostete Tooltypen sind nicht vorhanden, da Ollama eine lokale Modelllaufzeit ist.
| Werkzeug | Status | Hinweise |
|---|---|---|
| Funktionswerkzeuge | ✅ | Normale Python-aufrufbare Objekte oder @ai_function. Ob das ausgewählte Modell sie tatsächlich aufrufen kann, hängt vom Modell selbst ab. |
| Toolgenehmigung | ✅ | Bereitgestellt durch den Chatclient des Frameworks zum Aufrufen von Funktionen; funktioniert mit jedem Funktions- oder Tool-Aufruf. |
| Codedolmetscher | ❌ | Kein gehosteter Codedolmetscher. |
| Dateisuche | ❌ | Keine gehostete Dateisuche. |
| Websuche | ❌ | Keine gehostete Websuche. |
| Gehostete MCP-Tools | ❌ | Ollama macht keine gehosteten MCP verfügbar. |
| Lokale MCP-Tools | ✅ | Wird in Ihrem Prozess ausgeführt und funktioniert mit jedem Chatclient. |
Funktionstools
import asyncio
from datetime import datetime
from agent_framework import Agent
from agent_framework.ollama import OllamaChatClient
def get_time(location: str) -> str:
"""Get the current time."""
return f"The current time in {location} is {datetime.now().strftime('%I:%M %p')}."
async def main():
agent = Agent(
client=OllamaChatClient(),
name="TimeAgent",
instructions="You are a helpful time agent.",
tools=get_time,
)
result = await agent.run("What time is it in Seattle?")
print(result)
asyncio.run(main())
Streaming
from agent_framework import Agent
from agent_framework.ollama import OllamaChatClient
async def streaming_example():
agent = Agent(
client=OllamaChatClient(),
instructions="You are a helpful assistant.",
)
print("Agent: ", end="", flush=True)
async for chunk in agent.run("Tell me about Python.", stream=True):
if chunk.text:
print(chunk.text, end="", flush=True)
print()
Note
Unterstützung für dieses Feature wird in Kürze verfügbar sein. Den neuesten Status finden Sie im Agent Framework Go-Repository .