-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add Google Gemini API support to aisuite #181
Open
eliasjudin
wants to merge
1
commit into
andrewyng:main
Choose a base branch
from
eliasjudin:add-gemini
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 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
This file contains 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,49 @@ | ||
import os | ||
from google import genai | ||
from google.genai import types | ||
from aisuite.provider import Provider, LLMError | ||
from aisuite.framework import ChatCompletionResponse | ||
|
||
|
||
class GoogleGenaiProvider(Provider): | ||
def __init__(self, **config): | ||
self.api_key = config.get("api_key") or os.getenv("GEMINI_API_KEY") | ||
if not self.api_key: | ||
raise ValueError( | ||
"Gemini API key is missing. Please provide it in the config or set the GEMINI_API_KEY environment variable." | ||
) | ||
self.client = genai.Client(api_key=self.api_key) | ||
|
||
def chat_completions_create(self, model, messages, **kwargs): | ||
try: | ||
response = self.client.models.generate_content( | ||
model=model, | ||
contents=[message["content"] for message in messages], | ||
**kwargs | ||
) | ||
return self.normalize_response(response) | ||
except Exception as e: | ||
raise LLMError(f"Error in chat_completions_create: {str(e)}") | ||
|
||
def generate_content(self, model, contents, **kwargs): | ||
try: | ||
response = self.client.models.generate_content( | ||
model=model, | ||
contents=contents, | ||
**kwargs | ||
) | ||
return self.normalize_response(response) | ||
except Exception as e: | ||
raise LLMError(f"Error in generate_content: {str(e)}") | ||
|
||
def list_models(self): | ||
try: | ||
response = self.client.models.list() | ||
return [model.name for model in response] | ||
except Exception as e: | ||
raise LLMError(f"Error in list_models: {str(e)}") | ||
|
||
def normalize_response(self, response): | ||
normalized_response = ChatCompletionResponse() | ||
normalized_response.choices[0].message.content = response.text | ||
return normalized_response |
This file contains 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,55 @@ | ||
# Google Gemini API | ||
|
||
To use the Google Gemini API with `aisuite`, follow these steps: | ||
|
||
## Prerequisites | ||
|
||
1. **Google Cloud Account**: Ensure you have a Google Cloud account. If not, create one at [Google Cloud](https://cloud.google.com/). | ||
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/). | ||
|
||
## Installation | ||
|
||
Install the `google-genai` Python client: | ||
|
||
Example with pip: | ||
```shell | ||
pip install google-genai | ||
``` | ||
|
||
Example with poetry: | ||
```shell | ||
poetry add google-genai | ||
``` | ||
|
||
## Configuration | ||
|
||
Set the `GEMINI_API_KEY` environment variable with your API key: | ||
|
||
```shell | ||
export GEMINI_API_KEY="your-gemini-api-key" | ||
``` | ||
|
||
## Create a Chat Completion | ||
|
||
In your code: | ||
```python | ||
import aisuite as ai | ||
client = ai.Client() | ||
|
||
provider = "google_genai" | ||
model_id = "gemini-2.0-flash-exp" | ||
|
||
messages = [ | ||
{"role": "system", "content": "You are a helpful assistant."}, | ||
{"role": "user", "content": "What’s the weather like in San Francisco?"}, | ||
] | ||
|
||
response = client.chat.completions.create( | ||
model=f"{provider}:{model_id}", | ||
messages=messages, | ||
) | ||
|
||
print(response.choices[0].message.content) | ||
``` | ||
|
||
Happy coding! If you would like to contribute, please read our [Contributing Guide](../CONTRIBUTING.md). |
This file contains 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
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.
I had some issues with the parsing of the name so I just changed the name to Ggenai for the class and the file.
Also the genai api doesn't accept the temperature so you must change
``**kwargs to config=types.GenerateContentConfig(**kwargs)```
in generate_content and chat_completions_create.
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.
I’ll take a look. Appreciate any edits as I’m not too experienced integrating apis.
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.
Could i help on this ?
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.
I'll get this tomorrow
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.
I think that we should keep the logic in the same provider, but making it dynamic and configurable to run using main vertex SDK OR with this new genai SDK.
What do u think ??