Python: Move ollama samples to samples getting started dir (#2921)

* Move ollama samples to samples getting started dir

* Address feedback
This commit is contained in:
Evan Mattson
2025-12-18 17:37:05 +09:00
committed by GitHub
Unverified
parent 360839782c
commit ca1532cf22
11 changed files with 85 additions and 56 deletions
@@ -0,0 +1,23 @@
# Copyright (c) Microsoft. All rights reserved.
import importlib
from typing import Any
IMPORT_PATH = "agent_framework_ollama"
PACKAGE_NAME = "agent-framework-ollama"
_IMPORTS = ["__version__", "OllamaChatClient", "OllamaSettings"]
def __getattr__(name: str) -> Any:
if name in _IMPORTS:
try:
return getattr(importlib.import_module(IMPORT_PATH), name)
except ModuleNotFoundError as exc:
raise ModuleNotFoundError(
f"The '{PACKAGE_NAME}' package is not installed, please do `pip install {PACKAGE_NAME}`"
) from exc
raise AttributeError(f"Module {IMPORT_PATH} has no attribute {name}.")
def __dir__() -> list[str]:
return _IMPORTS
@@ -0,0 +1,13 @@
# Copyright (c) Microsoft. All rights reserved.
from agent_framework_ollama import (
OllamaChatClient,
OllamaSettings,
__version__,
)
__all__ = [
"OllamaChatClient",
"OllamaSettings",
"__version__",
]
+1
View File
@@ -52,6 +52,7 @@ all = [
"agent-framework-devui",
"agent-framework-lab",
"agent-framework-mem0",
"agent-framework-ollama",
"agent-framework-purview",
"agent-framework-redis",
]
@@ -1,38 +0,0 @@
# Ollama Examples
This folder contains examples demonstrating how to use Ollama models with the Agent Framework.
## Prerequisites
1. **Install Ollama**: Download and install Ollama from [ollama.com](https://ollama.com/)
2. **Start Ollama**: Ensure Ollama is running on your local machine
3. **Pull a model**: Run `ollama pull mistral` (or any other model you prefer)
- For function calling examples, use models that support tool calling like `mistral` or `qwen2.5`
- For reasoning examples, use models that support reasoning like `qwen2.5:8b`
- For Multimodality you can use models like `gemma3:4b`
> **Note**: Not all models support all features. Function calling and reasoning capabilities depend on the specific model you're using.
## Examples
| File | Description |
|------|-------------|
| [`ollama_agent_basic.py`](ollama_agent_basic.py) | Demonstrates basic Ollama agent usage with the native Ollama Chat Client. Shows both streaming and non-streaming responses with tool calling capabilities. |
| [`ollama_agent_reasoning.py`](ollama_agent_reasoning.py) | Demonstrates Ollama agent with reasoning capabilities using the native Ollama Chat Client. Shows how to enable thinking/reasoning mode. |
| [`ollama_chat_client.py`](ollama_chat_client.py) | Ollama Chat Client with native Ollama Chat Client |
| [`ollama_chat_multimodal.py`](ollama_chat_multimodal.py) | Ollama Chat with multimodal native Ollama Chat Client |
## Configuration
The examples use environment variables for configuration. Set the appropriate variables based on which example you're running:
### For Native Ollama Examples (`ollama_agent_basic.py`, `ollama_agent_reasoning.py`)
Set the following environment variables:
- `OLLAMA_HOST`: The base URL for your Ollama server (optional, defaults to `http://localhost:11434`)
- Example: `export OLLAMA_HOST="http://localhost:11434"`
- `OLLAMA_CHAT_MODEL_ID`: The model name to use
- Example: `export OLLAMA_CHAT_MODEL_ID="qwen2.5:8b"`
- Must be a model you have pulled with Ollama
@@ -1,68 +0,0 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from datetime import datetime
from agent_framework_ollama import OllamaChatClient
"""
Ollama Agent Basic Example
This sample demonstrates implementing a Ollama agent with basic tool usage.
Ensure to install Ollama and have a model running locally before running the sample
Not all Models support function calling, to test function calling try llama3.2 or qwen3:4b
Set the model to use via the OLLAMA_CHAT_MODEL_ID environment variable or modify the code below.
https://ollama.com/
"""
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 non_streaming_example() -> None:
"""Example of non-streaming response (get the complete result at once)."""
print("=== Non-streaming Response Example ===")
agent = OllamaChatClient().create_agent(
name="TimeAgent",
instructions="You are a helpful time agent answer in one sentence.",
tools=get_time,
)
query = "What time is it in Seattle? Use a tool call"
print(f"User: {query}")
result = await agent.run(query)
print(f"Result: {result}\n")
async def streaming_example() -> None:
"""Example of streaming response (get results as they are generated)."""
print("=== Streaming Response Example ===")
agent = OllamaChatClient().create_agent(
name="TimeAgent",
instructions="You are a helpful time agent answer in one sentence.",
tools=get_time,
)
query = "What time is it in San Francisco? Use a tool call"
print(f"User: {query}")
print("Agent: ", end="", flush=True)
async for chunk in agent.run_stream(query):
if chunk.text:
print(chunk.text, end="", flush=True)
print("\n")
async def main() -> None:
print("=== Basic Ollama Chat Client Agent Example ===")
await non_streaming_example()
await streaming_example()
if __name__ == "__main__":
asyncio.run(main())
@@ -1,46 +0,0 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from agent_framework import TextReasoningContent
from agent_framework_ollama import OllamaChatClient
"""
Ollama Agent Reasoning Example
This sample demonstrates implementing a Ollama agent with reasoning.
Ensure to install Ollama and have a model running locally before running the sample
Not all Models support reasoning, to test reasoning try qwen3:8b
Set the model to use via the OLLAMA_CHAT_MODEL_ID environment variable or modify the code below.
https://ollama.com/
"""
async def reasoning_example() -> None:
print("=== Response Reasoning Example ===")
agent = OllamaChatClient().create_agent(
name="TimeAgent",
instructions="You are a helpful agent answer in one sentence.",
additional_chat_options={"think": True}, # Enable Reasoning on agent level
)
query = "Hey what is 3+4? Can you explain how you got to that answer?"
print(f"User: {query}")
# Enable Reasoning on per request level
result = await agent.run(query)
reasoning = "".join(c.text for c in result.messages[-1].contents if isinstance(c, TextReasoningContent))
print(f"Reasoning: {reasoning}")
print(f"Answer: {result}\n")
async def main() -> None:
print("=== Basic Ollama Chat Client Agent Reasoning ===")
await reasoning_example()
if __name__ == "__main__":
asyncio.run(main())
@@ -1,36 +0,0 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from datetime import datetime
from agent_framework_ollama import OllamaChatClient
# Ensure to install Ollama and have a model running locally before running the sample
# Not all Models support function calling, to test function calling try llama3.2
# Set the model to use via the OLLAMA_CHAT_MODEL_ID environment variable or modify the code below.
# https://ollama.com/
def get_time():
"""Get the current time."""
return f"The current time is {datetime.now().strftime('%I:%M %p')}."
async def main() -> None:
client = OllamaChatClient()
message = "What time is it? Use a tool call"
stream = False
print(f"User: {message}")
if stream:
print("Assistant: ", end="")
async for chunk in client.get_streaming_response(message, tools=get_time):
if str(chunk):
print(str(chunk), end="")
print("")
else:
response = await client.get_response(message, tools=get_time)
print(f"Assistant: {response}")
if __name__ == "__main__":
asyncio.run(main())
@@ -1,54 +0,0 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from agent_framework import ChatMessage, DataContent, Role, TextContent
from agent_framework_ollama import OllamaChatClient
"""
Ollama Agent Multimodal Example
This sample demonstrates implementing a Ollama agent with multimodal input capabilities.
Ensure to install Ollama and have a model running locally before running the sample
Not all Models support multimodal input, to test multimodal input try gemma3:4b
Set the model to use via the OLLAMA_CHAT_MODEL_ID environment variable or modify the code below.
https://ollama.com/
"""
def create_sample_image() -> str:
"""Create a simple 1x1 pixel PNG image for testing."""
# This is a tiny red pixel in PNG format
png_data = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=="
return f"data:image/png;base64,{png_data}"
async def test_image() -> None:
"""Test image analysis with Ollama."""
client = OllamaChatClient()
image_uri = create_sample_image()
message = ChatMessage(
role=Role.USER,
contents=[
TextContent(text="What's in this image?"),
DataContent(uri=image_uri, media_type="image/png"),
],
)
response = await client.get_response(message)
print(f"Image Response: {response}")
async def main() -> None:
print("=== Testing Ollama Multimodal ===")
await test_image()
if __name__ == "__main__":
asyncio.run(main())