-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Python: Add Google AI (Gemini) connector #4492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Jeyaramjj
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
Jeyaramjj:google-chat-client
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .temp_e2e/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) Microsoft Corporation. | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| # Get Started with Microsoft Agent Framework Google | ||
|
|
||
| Please install this package via pip: | ||
|
|
||
| ```bash | ||
| pip install agent-framework-google --pre | ||
| ``` | ||
|
|
||
| ## Google AI (Gemini API) Integration | ||
|
|
||
| This package provides integration with Google's Gemini API for Agent Framework: | ||
|
|
||
| - **Google AI (Gemini API)**: Direct access to Google's Gemini models with API key authentication | ||
|
|
||
| > **Note**: This package uses the new `google-genai` SDK as recommended by Google. See the [migration guide](https://ai.google.dev/gemini-api/docs/migrate) for more information. | ||
|
|
||
| ### Current Features | ||
|
|
||
| **Available Now:** | ||
| - `GoogleAISettings`: Configuration class for Google AI (Gemini API) authentication and settings | ||
| - `GoogleAIChatClient`: Chat client for Google AI with streaming, function calling, and multi-turn conversation support | ||
| - Function calling with `@AIFunction` decorator and plain Python functions | ||
| - Multi-modal support (images) | ||
| - Full `ChatOptions` support (temperature, top_p, max_tokens, stop sequences) | ||
| - Usage tracking and OpenTelemetry observability | ||
|
|
||
| **Coming Soon:** | ||
| - Advanced features (context caching, safety settings, structured output) | ||
| - Thinking mode (Gemini 2.5) | ||
| - Enhanced error handling with retry policies | ||
|
|
||
| ### Configuration | ||
|
|
||
| #### Google AI Settings | ||
|
|
||
| ```python | ||
| from agent_framework_google import GoogleAISettings | ||
|
|
||
| # Configure via environment variables | ||
| # GOOGLE_AI_API_KEY=your_api_key | ||
| # GOOGLE_AI_CHAT_MODEL_ID=gemini-2.5-flash | ||
|
|
||
| settings = GoogleAISettings() | ||
|
|
||
| # Or pass parameters directly (pass SecretStr for type safety) | ||
| from pydantic import SecretStr | ||
|
|
||
| settings = GoogleAISettings( | ||
| api_key=SecretStr("your_api_key"), | ||
| chat_model_id="gemini-2.5-flash" | ||
| ) | ||
| ``` | ||
|
|
||
| ### Usage Examples | ||
|
|
||
| #### Basic Chat Completion | ||
|
|
||
| ```python | ||
| import asyncio | ||
| from agent_framework import ChatMessage, Role, ChatOptions | ||
| from agent_framework_google import GoogleAIChatClient | ||
|
|
||
| async def main(): | ||
| # Configure via environment variables | ||
| # GOOGLE_AI_API_KEY=your_api_key | ||
| # GOOGLE_AI_CHAT_MODEL_ID=gemini-2.5-flash | ||
|
|
||
| client = GoogleAIChatClient() | ||
|
|
||
| # Create a simple chat message | ||
| messages = [ | ||
| ChatMessage(role=Role.USER, text="What is the capital of France?") | ||
| ] | ||
|
|
||
| # Get response | ||
| response = await client.get_response( | ||
| messages=messages, | ||
| chat_options=ChatOptions() | ||
| ) | ||
|
|
||
| print(response.messages[0].text) | ||
| # Output: Paris is the capital of France. | ||
|
|
||
| # Run the async function | ||
| asyncio.run(main()) | ||
| ``` | ||
|
|
||
| #### Streaming Chat | ||
|
|
||
| ```python | ||
| import asyncio | ||
| from agent_framework import ChatMessage, Role, ChatOptions | ||
| from agent_framework_google import GoogleAIChatClient | ||
|
|
||
| async def main(): | ||
| client = GoogleAIChatClient() | ||
|
|
||
| messages = [ | ||
| ChatMessage(role=Role.USER, text="Write a short poem about programming.") | ||
| ] | ||
|
|
||
| # Stream the response | ||
| async for chunk in client.get_streaming_response( | ||
| messages=messages, | ||
| chat_options=ChatOptions() | ||
| ): | ||
| if chunk.text: | ||
| print(chunk.text, end="", flush=True) | ||
|
|
||
| # Run the async function | ||
| asyncio.run(main()) | ||
| ``` | ||
|
|
||
| #### Chat with System Instructions | ||
|
|
||
| ```python | ||
| import asyncio | ||
| from agent_framework import ChatMessage, Role, ChatOptions | ||
| from agent_framework_google import GoogleAIChatClient | ||
|
|
||
| async def main(): | ||
| client = GoogleAIChatClient() | ||
|
|
||
| messages = [ | ||
| ChatMessage(role=Role.SYSTEM, text="You are a helpful coding assistant."), | ||
| ChatMessage(role=Role.USER, text="How do I reverse a string in Python?") | ||
| ] | ||
|
|
||
| response = await client.get_response( | ||
| messages=messages, | ||
| chat_options=ChatOptions() | ||
| ) | ||
|
|
||
| print(response.messages[0].text) | ||
|
|
||
| # Run the async function | ||
| asyncio.run(main()) | ||
| ``` | ||
|
|
||
| #### Multi-Turn Conversation | ||
|
|
||
| ```python | ||
| import asyncio | ||
| from agent_framework import ChatMessage, Role, ChatOptions | ||
| from agent_framework_google import GoogleAIChatClient | ||
|
|
||
| async def main(): | ||
| client = GoogleAIChatClient() | ||
|
|
||
| messages = [ | ||
| ChatMessage(role=Role.USER, text="Hello! My name is Alice."), | ||
| ChatMessage(role=Role.ASSISTANT, text="Hello Alice! Nice to meet you."), | ||
| ChatMessage(role=Role.USER, text="What's my name?") | ||
| ] | ||
|
|
||
| response = await client.get_response( | ||
| messages=messages, | ||
| chat_options=ChatOptions() | ||
| ) | ||
|
|
||
| print(response.messages[0].text) | ||
| # Output: Your name is Alice! | ||
|
|
||
| # Run the async function | ||
| asyncio.run(main()) | ||
| ``` | ||
|
|
||
| #### Customizing Generation Parameters | ||
|
|
||
| ```python | ||
| import asyncio | ||
| from agent_framework import ChatMessage, Role, ChatOptions | ||
| from agent_framework_google import GoogleAIChatClient | ||
|
|
||
| async def main(): | ||
| client = GoogleAIChatClient() | ||
|
|
||
| messages = [ | ||
| ChatMessage(role=Role.USER, text="Generate a creative story.") | ||
| ] | ||
|
|
||
| # Customize temperature and token limit | ||
| chat_options = ChatOptions( | ||
| temperature=0.9, # Higher for more creativity | ||
| max_tokens=500, | ||
| top_p=0.95 | ||
| ) | ||
|
|
||
| response = await client.get_response( | ||
| messages=messages, | ||
| chat_options=chat_options | ||
| ) | ||
|
|
||
| print(response.messages[0].text) | ||
|
|
||
| # Run the async function | ||
| asyncio.run(main()) | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| ### Environment Variables | ||
|
|
||
| **Google AI:** | ||
| - `GOOGLE_AI_API_KEY`: Your Google AI API key ([Get one here](https://aistudio.google.com/app/apikey)) | ||
| - `GOOGLE_AI_CHAT_MODEL_ID`: Model to use (e.g., `gemini-2.5-flash`, `gemini-2.5-pro`) | ||
|
|
||
| ### Supported Models | ||
|
|
||
| - `gemini-2.5-flash`: Best price-performance, recommended for most use cases (stable) | ||
| - `gemini-2.5-pro`: Advanced thinking model for complex reasoning (stable) | ||
| - `gemini-2.0-flash`: Previous generation workhorse model (stable) | ||
| - `gemini-1.5-pro`: Legacy stable model | ||
| - `gemini-1.5-flash`: Legacy fast model | ||
|
|
||
| ## Features | ||
|
|
||
| ### Current Features | ||
| - ✅ Chat completion (streaming and non-streaming) | ||
| - ✅ System instructions | ||
| - ✅ Conversation history management | ||
| - ✅ Usage/token tracking | ||
| - ✅ Customizable generation parameters (temperature, max_tokens, top_p, stop) | ||
| - ✅ Function/tool calling (`@AIFunction` and plain Python functions) | ||
| - ✅ Multi-modal support (images) | ||
| - ✅ OpenTelemetry observability | ||
|
|
||
| ### Planned Features | ||
| - 🚧 Context caching | ||
| - 🚧 Safety settings configuration | ||
| - 🚧 Structured output (JSON mode) | ||
| - 🚧 Thinking mode (Gemini 2.5) | ||
|
|
||
| ## Development Status | ||
|
|
||
| This package is being developed incrementally: | ||
|
|
||
| - ✅ **Phase 1**: Package structure and settings classes | ||
| - ✅ **Phase 2**: Google AI chat client with streaming, function calling, and multi-modal support | ||
| - 🚧 **Phase 3**: Advanced features (context caching, safety settings, thinking mode) | ||
| - 🚧 **Phase 4**: Integration tests and comprehensive samples | ||
|
|
||
| ## Additional Information | ||
|
|
||
| For more information: | ||
| - [Google AI Studio](https://aistudio.google.com/) - Get an API key and test models | ||
| - [Google AI Documentation](https://ai.google.dev/gemini-api/docs) | ||
| - [Google GenAI SDK Migration Guide](https://ai.google.dev/gemini-api/docs/migrate) | ||
| - [Agent Framework Documentation](https://aka.ms/agent-framework) | ||
| - [Agent Framework Repository](https://github.com/microsoft/agent-framework) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| import importlib.metadata | ||
|
|
||
| from ._chat_client import GoogleAIChatClient, GoogleAIChatOptions | ||
|
|
||
| try: | ||
| __version__ = importlib.metadata.version(__name__) | ||
| except importlib.metadata.PackageNotFoundError: | ||
| __version__ = "0.0.0" # Fallback for development mode | ||
|
|
||
| __all__ = [ | ||
| "GoogleAIChatClient", | ||
| "GoogleAIChatOptions", | ||
| "__version__", | ||
| ] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there shouldn't be extra .gitignore files, if we need something excluded add it to the main one