chore: import upstream snapshot with attribution
dotnet-build-and-test / dotnet-test-functions (push) Has been cancelled
dotnet-build-and-test / paths-filter (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Debug, windows-latest, net9.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net8.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-foundry-hosted-it (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test-check (push) Has been cancelled
dotnet-build-and-test / Integration Test Report (push) Has been cancelled
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
dotnet-build-and-test / dotnet-test-functions (push) Has been cancelled
dotnet-build-and-test / paths-filter (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Debug, windows-latest, net9.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net8.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-foundry-hosted-it (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test-check (push) Has been cancelled
dotnet-build-and-test / Integration Test Report (push) Has been cancelled
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
# Google Gemini Examples
|
||||
|
||||
This folder contains examples demonstrating how to use Google Gemini models with the Agent Framework.
|
||||
|
||||
## Examples
|
||||
|
||||
| File | Description |
|
||||
|------|-------------|
|
||||
| [`gemini_basic.py`](gemini_basic.py) | Basic agent with a weather tool, demonstrating both streaming and non-streaming responses. |
|
||||
| [`gemini_advanced.py`](gemini_advanced.py) | Extended thinking via `ThinkingConfig` for reasoning-heavy questions (Gemini 2.5+). |
|
||||
| [`gemini_with_google_search.py`](gemini_with_google_search.py) | Google Search grounding for up-to-date answers. |
|
||||
| [`gemini_with_google_maps.py`](gemini_with_google_maps.py) | Google Maps grounding for location and mapping information. |
|
||||
| [`gemini_with_code_execution.py`](gemini_with_code_execution.py) | Built-in code execution tool for computing precise answers in a sandboxed environment. |
|
||||
|
||||
## Environment Variables
|
||||
|
||||
- `GOOGLE_MODEL` or `GEMINI_MODEL`: The Gemini model to use (for example,
|
||||
`gemini-2.5-flash-lite` or `gemini-2.5-pro`)
|
||||
- For Gemini Developer API: `GEMINI_API_KEY` or `GOOGLE_API_KEY`
|
||||
- For Vertex AI: `GOOGLE_GENAI_USE_VERTEXAI=true`, `GOOGLE_CLOUD_PROJECT`, and `GOOGLE_CLOUD_LOCATION`
|
||||
@@ -0,0 +1 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
@@ -0,0 +1,59 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
"""Shows how to enable extended thinking with ThinkingConfig.
|
||||
|
||||
Allows the model to reason through complex problems before responding.
|
||||
|
||||
Requires ``GOOGLE_MODEL`` or ``GEMINI_MODEL`` and either Gemini Developer API credentials
|
||||
(``GEMINI_API_KEY`` or ``GOOGLE_API_KEY``) or Vertex AI settings
|
||||
(``GOOGLE_GENAI_USE_VERTEXAI``, ``GOOGLE_CLOUD_PROJECT``, and ``GOOGLE_CLOUD_LOCATION``).
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
from agent_framework import Agent
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from agent_framework_gemini import GeminiChatClient, GeminiChatOptions, ThinkingConfig
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
"""Example of extended thinking with a Python version comparison question."""
|
||||
print("=== Extended thinking ===")
|
||||
|
||||
# 1. Configure Gemini extended thinking for a reasoning-heavy request.
|
||||
options: GeminiChatOptions = {
|
||||
"thinking_config": ThinkingConfig(thinking_budget=2048),
|
||||
}
|
||||
|
||||
# 2. Create the agent with the Gemini chat client and default thinking options.
|
||||
agent = Agent(
|
||||
client=GeminiChatClient(),
|
||||
name="PythonAgent",
|
||||
instructions="You are a helpful Python expert.",
|
||||
default_options=options,
|
||||
)
|
||||
|
||||
# 3. Stream the answer so you can see the final response as it arrives.
|
||||
query = "What new language features were introduced in Python between 3.10 and 3.14?"
|
||||
print(f"User: {query}")
|
||||
print("Agent: ", end="", flush=True)
|
||||
async for chunk in agent.run(query, stream=True):
|
||||
if chunk.text:
|
||||
print(chunk.text, end="", flush=True)
|
||||
print("\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
|
||||
"""
|
||||
Sample output:
|
||||
=== Extended thinking ===
|
||||
User: What new language features were introduced in Python between 3.10 and 3.14?
|
||||
Agent: Python 3.11 introduced exception groups and TaskGroup.
|
||||
Python 3.12 added PEP 695 type parameter syntax.
|
||||
Python 3.13-3.14 continued improving typing, performance, and developer ergonomics.
|
||||
"""
|
||||
@@ -0,0 +1,93 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
"""Shows how to use GeminiChatClient with an agent and a custom tool.
|
||||
|
||||
Covers both non-streaming and streaming responses.
|
||||
|
||||
Requires ``GOOGLE_MODEL`` or ``GEMINI_MODEL`` and either Gemini Developer API credentials
|
||||
(``GEMINI_API_KEY`` or ``GOOGLE_API_KEY``) or Vertex AI settings
|
||||
(``GOOGLE_GENAI_USE_VERTEXAI``, ``GOOGLE_CLOUD_PROJECT``, and ``GOOGLE_CLOUD_LOCATION``).
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
from random import randint
|
||||
from typing import Annotated
|
||||
|
||||
from agent_framework import Agent, tool
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from agent_framework_gemini import GeminiChatClient
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
# NOTE: approval_mode="never_require" is for sample brevity. Use "always_require" in production
|
||||
@tool(approval_mode="never_require")
|
||||
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:
|
||||
"""Runs the agent and waits for the complete response before printing it."""
|
||||
print("=== Non-streaming ===")
|
||||
|
||||
# 1. Create the agent with the Gemini chat client and local weather tool.
|
||||
agent = Agent(
|
||||
client=GeminiChatClient(),
|
||||
name="WeatherAgent",
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=[get_weather],
|
||||
)
|
||||
|
||||
# 2. Ask the agent for a single weather lookup and print the final response.
|
||||
query = "What's the weather like in Karlsruhe, Germany?"
|
||||
print(f"User: {query}")
|
||||
result = await agent.run(query)
|
||||
print(f"Result: {result}\n")
|
||||
|
||||
|
||||
async def streaming_example() -> None:
|
||||
"""Runs the agent and prints each chunk as it is received."""
|
||||
print("=== Streaming ===")
|
||||
|
||||
# 1. Create the same agent configuration for a streaming tool-call example.
|
||||
agent = Agent(
|
||||
client=GeminiChatClient(),
|
||||
name="WeatherAgent",
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=[get_weather],
|
||||
)
|
||||
|
||||
# 2. Ask a multi-location question and stream the model output as it arrives.
|
||||
query = "What's the weather like in Portland and in Paris?"
|
||||
print(f"User: {query}")
|
||||
print("Agent: ", end="", flush=True)
|
||||
async for chunk in agent.run(query, stream=True):
|
||||
if chunk.text:
|
||||
print(chunk.text, end="", flush=True)
|
||||
print("\n")
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
"""Run non-streaming and streaming examples."""
|
||||
await non_streaming_example()
|
||||
await streaming_example()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
|
||||
"""
|
||||
Sample output:
|
||||
=== Non-streaming ===
|
||||
User: What's the weather like in Karlsruhe, Germany?
|
||||
Result: The weather in Karlsruhe, Germany is currently sunny with a high of 16°C.
|
||||
|
||||
=== Streaming ===
|
||||
User: What's the weather like in Portland and in Paris?
|
||||
Agent: In Portland, it is currently rainy with a high of 11°C. In Paris, it is cloudy with a high of 27°C.
|
||||
"""
|
||||
@@ -0,0 +1,52 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
"""Shows how to enable Gemini's built-in code execution tool.
|
||||
|
||||
Allows the model to write and run code in a sandboxed environment to answer questions.
|
||||
|
||||
Requires ``GOOGLE_MODEL`` or ``GEMINI_MODEL`` and either Gemini Developer API credentials
|
||||
(``GEMINI_API_KEY`` or ``GOOGLE_API_KEY``) or Vertex AI settings
|
||||
(``GOOGLE_GENAI_USE_VERTEXAI``, ``GOOGLE_CLOUD_PROJECT``, and ``GOOGLE_CLOUD_LOCATION``).
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
from agent_framework import Agent
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from agent_framework_gemini import GeminiChatClient
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
"""Run the code execution example."""
|
||||
print("=== Code execution ===")
|
||||
|
||||
# 1. Create the agent with Gemini and the built-in code execution tool.
|
||||
agent = Agent(
|
||||
client=GeminiChatClient(),
|
||||
name="CodeAgent",
|
||||
instructions="You are a helpful assistant. Use code execution to compute precise answers.",
|
||||
tools=[GeminiChatClient.get_code_interpreter_tool()],
|
||||
)
|
||||
|
||||
# 2. Ask for a computed answer and stream the generated code and final result.
|
||||
query = "What are the first 20 prime numbers? Compute them in code."
|
||||
print(f"User: {query}")
|
||||
print("Agent: ", end="", flush=True)
|
||||
async for chunk in agent.run(query, stream=True):
|
||||
if chunk.text:
|
||||
print(chunk.text, end="", flush=True)
|
||||
print("\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
|
||||
"""
|
||||
Sample output:
|
||||
=== Code execution ===
|
||||
User: What are the first 20 prime numbers? Compute them in code.
|
||||
Agent: The first 20 prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, and 71.
|
||||
"""
|
||||
@@ -0,0 +1,53 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
"""Shows how to enable Google Maps grounding.
|
||||
|
||||
Allows Gemini to retrieve location and mapping information before responding.
|
||||
|
||||
Requires ``GOOGLE_MODEL`` or ``GEMINI_MODEL`` and either Gemini Developer API credentials
|
||||
(``GEMINI_API_KEY`` or ``GOOGLE_API_KEY``) or Vertex AI settings
|
||||
(``GOOGLE_GENAI_USE_VERTEXAI``, ``GOOGLE_CLOUD_PROJECT``, and ``GOOGLE_CLOUD_LOCATION``).
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
from agent_framework import Agent
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from agent_framework_gemini import GeminiChatClient
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
"""Run the Google Maps grounding example."""
|
||||
print("=== Google Maps grounding ===")
|
||||
|
||||
# 1. Create the agent with Gemini and the built-in Google Maps grounding tool.
|
||||
agent = Agent(
|
||||
client=GeminiChatClient(),
|
||||
name="MapsAgent",
|
||||
instructions="You are a helpful travel assistant. Use Google Maps to provide accurate location information.",
|
||||
tools=[GeminiChatClient.get_maps_grounding_tool()],
|
||||
)
|
||||
|
||||
# 2. Ask a location-aware question and stream the grounded answer.
|
||||
query = "What are some highly rated restaurants in the city center of Karlsruhe, Germany?"
|
||||
print(f"User: {query}")
|
||||
print("Agent: ", end="", flush=True)
|
||||
async for chunk in agent.run(query, stream=True):
|
||||
if chunk.text:
|
||||
print(chunk.text, end="", flush=True)
|
||||
print("\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
|
||||
"""
|
||||
Sample output:
|
||||
=== Google Maps grounding ===
|
||||
User: What are some highly rated restaurants in the city center of Karlsruhe, Germany?
|
||||
Agent: Here are several highly rated restaurants near Karlsruhe city center,
|
||||
along with their cuisine styles and approximate walking distance.
|
||||
"""
|
||||
@@ -0,0 +1,52 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
"""Shows how to enable Google Search grounding.
|
||||
|
||||
Allows Gemini to retrieve up-to-date information from the web before responding.
|
||||
|
||||
Requires ``GOOGLE_MODEL`` or ``GEMINI_MODEL`` and either Gemini Developer API credentials
|
||||
(``GEMINI_API_KEY`` or ``GOOGLE_API_KEY``) or Vertex AI settings
|
||||
(``GOOGLE_GENAI_USE_VERTEXAI``, ``GOOGLE_CLOUD_PROJECT``, and ``GOOGLE_CLOUD_LOCATION``).
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
from agent_framework import Agent
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from agent_framework_gemini import GeminiChatClient
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
"""Run the Google Search grounding example."""
|
||||
print("=== Google Search grounding ===")
|
||||
|
||||
# 1. Create the agent with Gemini and the built-in Google Search grounding tool.
|
||||
agent = Agent(
|
||||
client=GeminiChatClient(),
|
||||
name="SearchAgent",
|
||||
instructions="You are a helpful assistant. Use Google Search to provide accurate, up-to-date answers.",
|
||||
tools=[GeminiChatClient.get_web_search_tool()],
|
||||
)
|
||||
|
||||
# 2. Ask a current-events style question and stream the grounded answer.
|
||||
query = "What is the latest stable release of the .NET SDK?"
|
||||
print(f"User: {query}")
|
||||
print("Agent: ", end="", flush=True)
|
||||
async for chunk in agent.run(query, stream=True):
|
||||
if chunk.text:
|
||||
print(chunk.text, end="", flush=True)
|
||||
print("\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
|
||||
"""
|
||||
Sample output:
|
||||
=== Google Search grounding ===
|
||||
User: What is the latest stable release of the .NET SDK?
|
||||
Agent: As of April 14, 2026, the latest stable release of the .NET SDK is .NET 10.0 (SDK 10.0.201).
|
||||
"""
|
||||
Reference in New Issue
Block a user