Skip to content

Commit 0f8f139

Browse files
Giom-VMarkDaoust
andauthored
Using the GEMINI_API_KEY by default instead of the GOOGLE_API_KEY one (google-gemini#418)
* Using the GEMINI_API_KEY by default instead of the GOOGLE_API_KEY one The Google API key can be misleading since it's not really an overall key for all of Google API and only for the Gemini ones. * Formatting * Update google/generativeai/client.py * revert elif --------- Co-authored-by: Mark Daoust <[email protected]>
1 parent c5ef6c0 commit 0f8f139

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ See the [Gemini API Cookbook](https://github.com/google-gemini/gemini-api-cookbo
2727
import google.generativeai as genai
2828
import os
2929

30-
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
30+
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
3131
```
3232

3333
3. Create a model and run a prompt.

google/generativeai/client.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ def configure(
132132
"""Initializes default client configurations using specified parameters or environment variables.
133133
134134
If no API key has been provided (either directly, or on `client_options`) and the
135-
`GOOGLE_API_KEY` environment variable is set, it will be used as the API key.
135+
`GEMINI_API_KEY` environment variable is set, it will be used as the API key. If not,
136+
if the `GOOGLE_API_KEY` environement variable is set, it will be used as the API key.
136137
137138
Note: Not all arguments are detailed below. Refer to the `*ServiceClient` classes in
138139
`google.ai.generativelanguage` for details on the other arguments.
@@ -141,8 +142,8 @@ def configure(
141142
transport: A string, one of: [`rest`, `grpc`, `grpc_asyncio`].
142143
api_key: The API-Key to use when creating the default clients (each service uses
143144
a separate client). This is a shortcut for `client_options={"api_key": api_key}`.
144-
If omitted, and the `GOOGLE_API_KEY` environment variable is set, it will be
145-
used.
145+
If omitted, and the `GEMINI_API_KEY` or the `GOOGLE_API_KEY` environment variable
146+
are set, they will be used in this order of priority.
146147
default_metadata: Default (key, value) metadata pairs to send with every request.
147148
when using `transport="rest"` these are sent as HTTP headers.
148149
"""
@@ -162,6 +163,11 @@ def configure(
162163
if api_key is None:
163164
# If no key is provided explicitly, attempt to load one from the
164165
# environment.
166+
api_key = os.getenv("GEMINI_API_KEY")
167+
168+
if api_key is None:
169+
# If the GEMINI_API_KEY doesn't exist, attempt to load the
170+
# GOOGLE_API_KEY from the environment.
165171
api_key = os.getenv("GOOGLE_API_KEY")
166172

167173
client_options.api_key = api_key

google/generativeai/types/discuss_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class ChatResponse(abc.ABC):
121121
```
122122
import google.generativeai as genai
123123
124-
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
124+
genai.configure(api_key=os.environ['GEMINI_API_KEY'])
125125
126126
response = genai.chat(messages=["Hello."])
127127
print(response.last) # 'Hello! What can I help you with?'

0 commit comments

Comments
 (0)