Google Gemini 可以透過 Gemini 開發者 API 或 Vertex AI 來支援代理框架代理。 提供者專屬客戶端負責認證與 Gemini 請求選項,而代理框架則擁有代理定義與編排。
Important
Google Gemini 和 Vertex AI 是第三方系統。 在傳送應用程式資料前,請先審查服務條款、資料處理、區域邊界、模型存取及使用成本。
安裝雙子星 IChatClient
.NET 範例展示了官方的 Google GenAI 客戶端及社群Mscc.GenerativeAI.Microsoft實作。
dotnet add package Google.GenAI
dotnet add package Mscc.GenerativeAI.Microsoft
dotnet add package Microsoft.Agents.AI --prerelease
Configuration
GOOGLE_GENAI_API_KEY="<google-ai-studio-api-key>"
GOOGLE_GENAI_MODEL="gemini-2.5-flash"
const string JokerInstructions = "You are good at telling jokes.";
const string JokerName = "JokerAgent";
string apiKey = Environment.GetEnvironmentVariable("GOOGLE_GENAI_API_KEY") ?? throw new InvalidOperationException("Please set the GOOGLE_GENAI_API_KEY environment variable.");
string model = Environment.GetEnvironmentVariable("GOOGLE_GENAI_MODEL") ?? "gemini-2.5-flash";
// Using a Google GenAI IChatClient implementation
ChatClientAgent agentGenAI = new(
new Client(vertexAI: false, apiKey: apiKey).AsIChatClient(model),
name: JokerName,
instructions: JokerInstructions);
AgentResponse response = await agentGenAI.RunAsync("Tell me a joke about a pirate.");
Console.WriteLine($"Google GenAI client based agent response:\n{response}");
// Using a community driven Mscc.GenerativeAI.Microsoft package
ChatClientAgent agentCommunity = new(
new GeminiChatClient(apiKey: apiKey, model: model),
name: JokerName,
instructions: JokerInstructions);
response = await agentCommunity.RunAsync("Tell me a joke about a pirate.");
Console.WriteLine($"Community client based agent response:\n{response}");
選擇一個 IChatClient 實作並設定其 Gemini 開發者 API 或 Vertex AI 認證。
安裝套件
pip install agent-framework-gemini --pre
Configuration
可以使用 Gemini 開發者 API:
GEMINI_API_KEY="<api-key>"
GEMINI_MODEL="gemini-2.5-flash"
# GOOGLE_API_KEY and GOOGLE_MODEL are also supported.
或者設定 Vertex AI:
GOOGLE_GENAI_USE_VERTEXAI="true"
GOOGLE_CLOUD_PROJECT="<project-id>"
GOOGLE_CLOUD_LOCATION="us-central1"
GOOGLE_MODEL="gemini-2.5-flash"
GeminiChatClient 支援串流、函式工具、結構化輸出、延伸思考及提供者託管工具。
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")
該套件包含 Google 搜尋接地、Google 地圖接地、程式碼執行、檔案搜尋及 MCP 的工廠。
Google 搜尋接地
import asyncio
from agent_framework import Agent
from agent_framework.gemini import GeminiChatClient
from dotenv import load_dotenv
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())
Go SDK 提供 geminiprovider Gemini 推論。 透過提供者專屬建構器建立標準 *agent.Agent 。
請參閱 Gemini 供應商方案 及 範例。
工具
| Tool | C# | Python | Go | Notes |
|---|---|---|---|---|
| 函式工具 | ✅ | ✅ | ✅ | 標準模型函式呼叫。 |
| 工具核准 | ✅ | ✅ | ✅ | 透過框架工具迴圈應用。 |
| 程式碼解譯器 | ❌ | ✅ | ❌ |
GeminiChatClient.get_code_interpreter_tool()。 |
| 檔案搜尋 | ❌ | ✅ | ❌ |
GeminiChatClient.get_file_search_tool()。 |
| 網路搜尋 | ❌ | ✅ | ❌ | Google 搜尋接地資料。get_web_search_tool() |
| Google 地圖停飛 | ❌ | ✅ | ❌ |
GeminiChatClient.get_maps_grounding_tool()。 |
| 託管 MCP 工具 | ❌ | ✅ | ❌ |
GeminiChatClient.get_mcp_tool()。 |
| 本地 MCP 工具 | ✅ | ✅ | ✅ | 在申請過程中會跑。 |