From c7f20c8ec59be5ffeab90e8c3b79192e92d7e9dd Mon Sep 17 00:00:00 2001 From: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com> Date: Thu, 2 Oct 2025 08:14:29 -0700 Subject: [PATCH] Python: Added Ollama example using OpenAIChatClient (#1100) * Added Ollama example using OpenAIChatClient * Small improvement --- python/.env.example | 3 + .../agents/anthropic/README.md | 2 +- .../getting_started/agents/ollama/README.md | 30 +++++++ .../ollama/ollama_with_openai_chat_client.py | 80 +++++++++++++++++++ 4 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 python/samples/getting_started/agents/ollama/README.md create mode 100644 python/samples/getting_started/agents/ollama/ollama_with_openai_chat_client.py diff --git a/python/.env.example b/python/.env.example index 834d2446a5..82458a3fda 100644 --- a/python/.env.example +++ b/python/.env.example @@ -21,6 +21,9 @@ COPILOTSTUDIOAGENT__AGENTAPPID="" # Anthropic ANTHROPIC_API_KEY="" ANTHROPIC_MODEL="" +# Ollama +OLLAMA_ENDPOINT="" +OLLAMA_MODEL="" # Observability ENABLE_OTEL=true ENABLE_SENSITIVE_DATA=true diff --git a/python/samples/getting_started/agents/anthropic/README.md b/python/samples/getting_started/agents/anthropic/README.md index 664ae71583..be8944ae23 100644 --- a/python/samples/getting_started/agents/anthropic/README.md +++ b/python/samples/getting_started/agents/anthropic/README.md @@ -1,6 +1,6 @@ # Anthropic Examples -This folder contains examples demonstrating how to use Anthropic's Claude models with the Agent Framework through the OpenAI Chat Client interface. +This folder contains examples demonstrating how to use Anthropic's Claude models with the Agent Framework. ## Examples diff --git a/python/samples/getting_started/agents/ollama/README.md b/python/samples/getting_started/agents/ollama/README.md new file mode 100644 index 0000000000..e8c98f2147 --- /dev/null +++ b/python/samples/getting_started/agents/ollama/README.md @@ -0,0 +1,30 @@ +# 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 that supports function calling) + +## Examples + +| File | Description | +|------|-------------| +| [`ollama_with_openai_chat_client.py`](ollama_with_openai_chat_client.py) | Demonstrates how to configure OpenAI Chat Client to use local Ollama models. Shows both streaming and non-streaming responses with tool calling capabilities. | + +## Configuration + +The examples use environment variables for configuration: + +### Environment Variables + +Set the following environment variables before running the examples: + +- `OLLAMA_ENDPOINT`: The base URL for your Ollama server + - Example: `export OLLAMA_ENDPOINT="http://localhost:11434/v1/"` + +- `OLLAMA_MODEL`: The model name to use + - Example: `export OLLAMA_MODEL="mistral"` + - Must be a model you have pulled with Ollama diff --git a/python/samples/getting_started/agents/ollama/ollama_with_openai_chat_client.py b/python/samples/getting_started/agents/ollama/ollama_with_openai_chat_client.py new file mode 100644 index 0000000000..8033b54dc9 --- /dev/null +++ b/python/samples/getting_started/agents/ollama/ollama_with_openai_chat_client.py @@ -0,0 +1,80 @@ +# Copyright (c) Microsoft. All rights reserved. + +import asyncio +import os +from random import randint +from typing import Annotated + +from agent_framework.openai import OpenAIChatClient + +""" +Ollama with OpenAI Chat Client Example + +This sample demonstrates using Ollama models through OpenAI Chat Client by +configuring the base URL to point to your local Ollama server for local AI inference. +Ollama allows you to run large language models locally on your machine. + +Environment Variables: +- OLLAMA_ENDPOINT: The base URL for your Ollama server (e.g., "http://localhost:11434/v1/") +- OLLAMA_MODEL: The model name to use (e.g., "mistral", "llama3.2", "phi3") +""" + + +def get_weather( + location: Annotated[str, "The location to get the weather for."], +) -> str: + """Get the weather for a given location.""" + conditions = ["sunny", "cloudy", "rainy", "stormy"] + return f"The weather in {location} is {conditions[randint(0, 3)]} with a high of {randint(10, 30)}°C." + + +async def non_streaming_example() -> None: + """Example of non-streaming response (get the complete result at once).""" + print("=== Non-streaming Response Example ===") + + agent = OpenAIChatClient( + base_url=os.getenv("OLLAMA_ENDPOINT"), + model_id=os.getenv("OLLAMA_MODEL"), + ).create_agent( + name="WeatherAgent", + instructions="You are a helpful weather agent.", + tools=get_weather, + ) + + query = "What's the weather like in Seattle?" + 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 = OpenAIChatClient( + base_url=os.getenv("OLLAMA_ENDPOINT"), + model_id=os.getenv("OLLAMA_MODEL"), + ).create_agent( + name="WeatherAgent", + instructions="You are a helpful weather agent.", + tools=get_weather, + ) + + query = "What's the weather like in Portland?" + 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("=== Ollama with OpenAI Chat Client Agent Example ===") + + await non_streaming_example() + await streaming_example() + + +if __name__ == "__main__": + asyncio.run(main())