This project demonstrates how to extend the agent-framework by creating a custom chat client for Google's Gemini large language models. It includes a working GeminiChatClient that implements the ChatClientProtocol and an example script showing how to use it.
- Custom Chat Client: Implements
GeminiChatClientto connect to the Google Gemini API. - Protocol Compliant: The client correctly implements the
ChatClientProtocol. - Streaming and Non-Streaming: Supports both standard request-response and streaming responses.
- Easy to Use: A simple
main.pyscript demonstrates how to instantiate and run an agent with the custom client.
- Python 3.8+
- A Google Gemini API Key. You can get one from Google AI Studio.
-
Clone the repository or download the files.
-
Install the required dependencies:
pip install -r requirements.txt
or
uv sync
-
Create a
.envfile:In the root directory of the project, create a file named
.env. -
Add your API Key to the
.envfile:Open the
.envfile and add your Google Gemini API key like this:GEMINI_API_KEY="YOUR_API_KEY_HERE"
Once the setup is complete, you can run the example script:
python main.pyor
uv run main.pyThe script will execute two interactions with the Gemini model:
- A non-streaming request for the "meaning of life," printing the complete JSON response at the end.
- A streaming request for the "purpose of life," printing the response text to the console as it is generated.
The output will look something like this:
Agent response: {"messages": [{"role": "assistant", "contents": [{"text": "The meaning of life is a profound question that has been debated by philosophers, theologians, and thinkers for centuries..."}]}], "response_id": "..."}
Agent streaming response:
The purpose of life, much like its meaning, is a deeply personal and philosophical question...
├── main.py # Main entry point, demonstrates agent usage.
├── gemini.py # Contains the GeminiChatClient implementation.
├── requirements.txt # Project dependencies.
├── .env # (You create this) For storing environment variables.
└── README.md # This file.
main.py: This script shows how to instantiateChatAgentwith the customGeminiChatClientand run it in both non-streaming (agent.run) and streaming (agent.run_stream) modes.gemini.py: This module contains theGeminiChatClientclass. It handles the logic for communicating with the Google Gemini API, including message formatting and handling both single and streaming responses.