Skip to content

Add flexible authentication for Gemini AI client #79

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
GEMINI_PROJECT_ID=<GEMINI_PROJECT_ID>
# NOTE: Authentication is mutually exclusive. Please provide only one method at a time.
# [REQUIRED] By default, please set GEMINI_API_KEY for API key authentication.
# If you want to use Vertex AI authentication instead, unset GEMINI_API_KEY and set GOOGLE_APPLICATION_CREDENTIALS
GEMINI_API_KEY=<GEMINI_API_KEY>
# GOOGLE_APPLICATION_CREDENTIALS=</path/to/file.json>
GITHUB_TOKEN=<GITHUB_TOKEN>
OPENROUTER_API_KEY = <OPENROUTER_API_KEY>
OPENROUTER_MODEL = <OPENROUTER_MODEL>
24 changes: 13 additions & 11 deletions utils/call_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,19 @@ def call_llm(prompt: str, use_cache: bool = True) -> str:
return cache[prompt]

# Call the LLM if not in cache or cache disabled
# client = genai.Client(
# vertexai=True,
# # TODO: change to your own project id and location
# project=os.getenv("GEMINI_PROJECT_ID", "your-project-id"),
# location=os.getenv("GEMINI_LOCATION", "us-central1")
# )

# You can comment the previous line and use the AI Studio key instead:
client = genai.Client(
api_key=os.getenv("GEMINI_API_KEY", ""),
)
# You can override authentication by setting GEMINI_API_KEY (for API key) or GEMINI_PROJECT_ID/GEMINI_LOCATION (for Vertex AI) via a .env file or OS environment variables.
# Authentication: Use API key if GEMINI_API_KEY is set, otherwise use Vertex AI
if os.getenv("GEMINI_API_KEY"):
client = genai.Client(
api_key=os.getenv("GEMINI_API_KEY", ""),
)
else:
client = genai.Client(
vertexai=True,
project=os.getenv("GEMINI_PROJECT_ID", "your-project-id"),
location=os.getenv("GEMINI_LOCATION", "us-central1")
)

# model = os.getenv("GEMINI_MODEL", "gemini-2.5-pro-exp-03-25")
model = os.getenv("GEMINI_MODEL", "gemini-2.0-flash-exp")

Expand Down