Skip to content

Commit ca1fd8c

Browse files
committed
Add Google Gemini API support to aisuite
Add support for Google Gemini API to `aisuite` and provide a guide. * **New Provider Implementation** - Add `GoogleGenaiProvider` class in `aisuite/providers/google_genai_provider.py` to handle Gemini API calls. - Implement `chat_completions_create`, `generate_content`, `list_models`, and `normalize_response` methods. - Handle authentication and API key management. * **Provider Factory Update** - Update `ProviderFactory` in `aisuite/provider.py` to include `GoogleGenaiProvider`. * **Documentation** - Add `guides/google_genai.md` with instructions for setting up and using the Gemini API with `aisuite`. - Update `README.md` to include the Gemini API as a supported provider and provide a brief example of how to use it. * **Dependencies** - Add `google-genai` to dependencies in `pyproject.toml`.
1 parent 043c0c7 commit ca1fd8c

File tree

4 files changed

+164
-2
lines changed

4 files changed

+164
-2
lines changed

README.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Simple, unified interface to multiple Generative AI providers.
88
`aisuite` makes it easy for developers to use multiple LLM through a standardized interface. Using an interface similar to OpenAI's, `aisuite` makes it easy to interact with the most popular LLMs and compare the results. It is a thin wrapper around python client libraries, and allows creators to seamlessly swap out and test responses from different LLM providers without changing their code. Today, the library is primarily focussed on chat completions. We will expand it cover more use cases in near future.
99

1010
Currently supported providers are -
11-
OpenAI, Anthropic, Azure, Google, AWS, Groq, Mistral, HuggingFace Ollama, Sambanova and Watsonx.
11+
OpenAI, Anthropic, Azure, Google, AWS, Groq, Mistral, HuggingFace Ollama, Sambanova, Watsonx, and Google Gemini.
1212
To maximize stability, `aisuite` uses either the HTTP endpoint or the SDK for making calls to the provider.
1313

1414
## Installation
@@ -119,3 +119,59 @@ We follow a convention-based approach for loading providers, which relies on str
119119
in providers/openai_provider.py
120120

121121
This convention simplifies the addition of new providers and ensures consistency across provider implementations.
122+
123+
## Using Google Gemini API
124+
125+
To use the Google Gemini API with `aisuite`, follow these steps:
126+
127+
### Prerequisites
128+
129+
1. **Google Cloud Account**: Ensure you have a Google Cloud account. If not, create one at [Google Cloud](https://cloud.google.com/).
130+
2. **API Key**: Obtain an API key for the Google Gemini API. You can generate an API key from the [Google Cloud Console](https://console.cloud.google.com/).
131+
132+
### Installation
133+
134+
Install the `google-genai` Python client:
135+
136+
Example with pip:
137+
```shell
138+
pip install google-genai
139+
```
140+
141+
Example with poetry:
142+
```shell
143+
poetry add google-genai
144+
```
145+
146+
### Configuration
147+
148+
Set the `GEMINI_API_KEY` environment variable with your API key:
149+
150+
```shell
151+
export GEMINI_API_KEY="your-gemini-api-key"
152+
```
153+
154+
### Create a Chat Completion
155+
156+
In your code:
157+
```python
158+
import aisuite as ai
159+
client = ai.Client()
160+
161+
provider = "google_genai"
162+
model_id = "gemini-2.0-flash-exp"
163+
164+
messages = [
165+
{"role": "system", "content": "You are a helpful assistant."},
166+
{"role": "user", "content": "What’s the weather like in San Francisco?"},
167+
]
168+
169+
response = client.chat.completions.create(
170+
model=f"{provider}:{model_id}",
171+
messages=messages,
172+
)
173+
174+
print(response.choices[0].message.content)
175+
```
176+
177+
Happy coding! If you would like to contribute, please read our [Contributing Guide](https://github.com/andrewyng/aisuite/blob/main/CONTRIBUTING.md).
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import os
2+
from google import genai
3+
from google.genai import types
4+
from aisuite.provider import Provider, LLMError
5+
from aisuite.framework import ChatCompletionResponse
6+
7+
8+
class GoogleGenaiProvider(Provider):
9+
def __init__(self, **config):
10+
self.api_key = config.get("api_key") or os.getenv("GEMINI_API_KEY")
11+
if not self.api_key:
12+
raise ValueError(
13+
"Gemini API key is missing. Please provide it in the config or set the GEMINI_API_KEY environment variable."
14+
)
15+
self.client = genai.Client(api_key=self.api_key)
16+
17+
def chat_completions_create(self, model, messages, **kwargs):
18+
try:
19+
response = self.client.models.generate_content(
20+
model=model,
21+
contents=[message["content"] for message in messages],
22+
**kwargs
23+
)
24+
return self.normalize_response(response)
25+
except Exception as e:
26+
raise LLMError(f"Error in chat_completions_create: {str(e)}")
27+
28+
def generate_content(self, model, contents, **kwargs):
29+
try:
30+
response = self.client.models.generate_content(
31+
model=model,
32+
contents=contents,
33+
**kwargs
34+
)
35+
return self.normalize_response(response)
36+
except Exception as e:
37+
raise LLMError(f"Error in generate_content: {str(e)}")
38+
39+
def list_models(self):
40+
try:
41+
response = self.client.models.list()
42+
return [model.name for model in response]
43+
except Exception as e:
44+
raise LLMError(f"Error in list_models: {str(e)}")
45+
46+
def normalize_response(self, response):
47+
normalized_response = ChatCompletionResponse()
48+
normalized_response.choices[0].message.content = response.text
49+
return normalized_response

guides/google_genai.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Google Gemini API
2+
3+
To use the Google Gemini API with `aisuite`, follow these steps:
4+
5+
## Prerequisites
6+
7+
1. **Google Cloud Account**: Ensure you have a Google Cloud account. If not, create one at [Google Cloud](https://cloud.google.com/).
8+
2. **API Key**: Obtain an API key for the Google Gemini API. You can generate an API key from the [Google Cloud Console](https://console.cloud.google.com/).
9+
10+
## Installation
11+
12+
Install the `google-genai` Python client:
13+
14+
Example with pip:
15+
```shell
16+
pip install google-genai
17+
```
18+
19+
Example with poetry:
20+
```shell
21+
poetry add google-genai
22+
```
23+
24+
## Configuration
25+
26+
Set the `GEMINI_API_KEY` environment variable with your API key:
27+
28+
```shell
29+
export GEMINI_API_KEY="your-gemini-api-key"
30+
```
31+
32+
## Create a Chat Completion
33+
34+
In your code:
35+
```python
36+
import aisuite as ai
37+
client = ai.Client()
38+
39+
provider = "google_genai"
40+
model_id = "gemini-2.0-flash-exp"
41+
42+
messages = [
43+
{"role": "system", "content": "You are a helpful assistant."},
44+
{"role": "user", "content": "What’s the weather like in San Francisco?"},
45+
]
46+
47+
response = client.chat.completions.create(
48+
model=f"{provider}:{model_id}",
49+
messages=messages,
50+
)
51+
52+
print(response.choices[0].message.content)
53+
```
54+
55+
Happy coding! If you would like to contribute, please read our [Contributing Guide](../CONTRIBUTING.md).

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ groq = { version = "^0.9.0", optional = true }
1515
mistralai = { version = "^1.0.3", optional = true }
1616
openai = { version = "^1.35.8", optional = true }
1717
ibm-watsonx-ai = { version = "^1.1.16", optional = true }
18+
google-genai = { version = "^0.1.0", optional = true }
1819

1920
# Optional dependencies for different providers
2021
httpx = "~0.27.0"
@@ -30,7 +31,8 @@ mistral = ["mistralai"]
3031
ollama = []
3132
openai = ["openai"]
3233
watsonx = ["ibm-watsonx-ai"]
33-
all = ["anthropic", "aws", "google", "groq", "mistral", "openai", "cohere", "watsonx"] # To install all providers
34+
google_genai = ["google-genai"]
35+
all = ["anthropic", "aws", "google", "groq", "mistral", "openai", "cohere", "watsonx", "google_genai"] # To install all providers
3436

3537
[tool.poetry.group.dev.dependencies]
3638
pre-commit = "^3.7.1"

0 commit comments

Comments
 (0)