Skip to content

Add option to set a client for GenerativeModel #136

Closed
@kartikc727

Description

@kartikc727

Description of the feature request:

Currently, the Python Quickstart documentation for using the Gemini API suggests setting the API key in the default client configuration.

import google.generativeai as genai

genai.configure(api_key=GOOGLE_API_KEY)
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("What is the meaning of life?")
print(response.text) # Model response gets printed

In addition to this, the package should also give an option to set a different key when creating the model. Something like:

model = genai.GenerativeModel('gemini-pro', api_key=OTHER_GOOGLE_API_KEY)

Currently, a workaround is to manually set the private _client variable for the model

import google.generativeai as genai
from google.ai import generativelanguage as glm

client = glm.GenerativeServiceClient(
    client_options={'api_key':OTHER_GOOGLE_API_KEY})
model = genai.GenerativeModel('gemini-pro')
model._client = client
response = model.generate_content("What is the meaning of life?")
print(response.text) # Model response gets printed

What problem are you trying to solve with this feature?

The current implementation requires the API key to be set globally. Allowing the keys to be set with a smaller scope will be useful for applications that use multiple API keys concurrently for different tasks.

Any other information you'd like to share?

One potential implementation for this feature could be to take an additional keyword argument for the client and set it as the _client during the __init__ method of the GenerativeModel class.

# google/generative_ai/generative_models.py
class GenerativeModel:
    def __init__(
        self,
        model_name: str = "gemini-m",
        safety_settings: safety_types.SafetySettingOptions | None = None,
        generation_config: generation_types.GenerationConfigType | None = None,
        client = None, # Additional kwarg
    ):
    ...
    self._client = client
    ...

Then, the user can optionally supply the client with their API key if they do not want the default credentials to be used.

# main.py
import google.generativeai as genai
from google.ai import generativelanguage as glm

client = glm.GenerativeServiceClient(
    client_options={'api_key':OTHER_GOOGLE_API_KEY})
model = genai.GenerativeModel('gemini-pro', client=client)
response = model.generate_content("What is the meaning of life?")
print(response.text) # Model response gets printed

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions