Skip to content
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

Using the GEMINI_API_KEY by default instead of the GOOGLE_API_KEY one #418

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Import the SDK and configure your API key.
import google.generativeai as genai
import os

genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
```

Create a model and run a prompt.
Expand Down
12 changes: 9 additions & 3 deletions google/generativeai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ def configure(
"""Initializes default client configurations using specified parameters or environment variables.

If no API key has been provided (either directly, or on `client_options`) and the
`GOOGLE_API_KEY` environment variable is set, it will be used as the API key.
`GEMINI_API_KEY` environment variable is set, it will be used as the API key. If not,
if the `GOOGLE_API_KEY` environement variable is set, it will be used as the API key.

Note: Not all arguments are detailed below. Refer to the `*ServiceClient` classes in
`google.ai.generativelanguage` for details on the other arguments.
Expand All @@ -140,8 +141,8 @@ def configure(
transport: A string, one of: [`rest`, `grpc`, `grpc_asyncio`].
api_key: The API-Key to use when creating the default clients (each service uses
a separate client). This is a shortcut for `client_options={"api_key": api_key}`.
If omitted, and the `GOOGLE_API_KEY` environment variable is set, it will be
used.
If omitted, and the `GEMINI_API_KEY` or the `GOOGLE_API_KEY` environment variable
are set, they will be used in this order of priority.
default_metadata: Default (key, value) metadata pairs to send with every request.
when using `transport="rest"` these are sent as HTTP headers.
"""
Expand All @@ -161,6 +162,11 @@ def configure(
if api_key is None:
# If no key is provided explicitly, attempt to load one from the
# environment.
api_key = os.getenv("GEMINI_API_KEY")

elif api_key is None:
MarkDaoust marked this conversation as resolved.
Show resolved Hide resolved
# If the GEMINI_API_KEY doesn't exist, attempt to load the
# GOOGLE_API_KEY from the environment.
api_key = os.getenv("GOOGLE_API_KEY")

client_options.api_key = api_key
Expand Down
2 changes: 1 addition & 1 deletion google/generativeai/types/discuss_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class ChatResponse(abc.ABC):
```
import google.generativeai as genai

genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
genai.configure(api_key=os.environ['GEMINI_API_KEY'])

response = genai.chat(messages=["Hello."])
print(response.last) # 'Hello! What can I help you with?'
Expand Down
Loading