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

Add credentials parameter to GoogleGenerativeAI, ChatGoogleGenerativeAI, and GoogleGenerativeAIEmbedded #78

Merged
merged 6 commits into from
Mar 21, 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
27 changes: 17 additions & 10 deletions libs/genai/langchain_google_genai/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,17 +483,24 @@ def is_lc_serializable(self) -> bool:
@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
"""Validates params and passes them to google-generativeai package."""
google_api_key = get_from_dict_or_env(
values, "google_api_key", "GOOGLE_API_KEY"
)
if isinstance(google_api_key, SecretStr):
google_api_key = google_api_key.get_secret_value()
if values.get("credentials"):
genai.configure(
credentials=values.get("credentials"),
transport=values.get("transport"),
client_options=values.get("client_options"),
)
else:
google_api_key = get_from_dict_or_env(
values, "google_api_key", "GOOGLE_API_KEY"
)
if isinstance(google_api_key, SecretStr):
google_api_key = google_api_key.get_secret_value()

genai.configure(
api_key=google_api_key,
transport=values.get("transport"),
client_options=values.get("client_options"),
)
genai.configure(
api_key=google_api_key,
transport=values.get("transport"),
client_options=values.get("client_options"),
)
if (
values.get("temperature") is not None
and not 0 <= values["temperature"] <= 1
Expand Down
38 changes: 26 additions & 12 deletions libs/genai/langchain_google_genai/embeddings.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from typing import Dict, List, Optional
from typing import Dict, List, Optional, Any

# TODO: remove ignore once the google package is published with types
import google.generativeai as genai # type: ignore[import]
from langchain_core.embeddings import Embeddings
from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr, root_validator
from langchain_core.utils import get_from_dict_or_env

from langchain_google_genai._common import GoogleGenerativeAIError


class GoogleGenerativeAIEmbeddings(BaseModel, Embeddings):

Check failure on line 12 in libs/genai/langchain_google_genai/embeddings.py

View workflow job for this annotation

GitHub Actions / cd libs/genai / - / make lint #3.8

Ruff (I001)

langchain_google_genai/embeddings.py:1:1: I001 Import block is un-sorted or un-formatted
"""`Google Generative AI Embeddings`.

To use, you must have either:
Expand Down Expand Up @@ -43,6 +43,13 @@
description="The Google API key to use. If not provided, "
"the GOOGLE_API_KEY environment variable will be used.",
)
credentials: Any = Field(
default=None,
exclude=True,
description="The default custom credentials (google.auth.credentials.Credentials) "

Check failure on line 49 in libs/genai/langchain_google_genai/embeddings.py

View workflow job for this annotation

GitHub Actions / cd libs/genai / - / make lint #3.8

Ruff (E501)

langchain_google_genai/embeddings.py:49:89: E501 Line too long (91 > 88)
"to use when making API calls. If not provided, credentials will be ascertained from "

Check failure on line 50 in libs/genai/langchain_google_genai/embeddings.py

View workflow job for this annotation

GitHub Actions / cd libs/genai / - / make lint #3.8

Ruff (E501)

langchain_google_genai/embeddings.py:50:89: E501 Line too long (94 > 88)
"the GOOGLE_API_KEY envvar"
)
client_options: Optional[Dict] = Field(
None,
description=(
Expand All @@ -58,17 +65,24 @@
@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
"""Validates params and passes them to google-generativeai package."""
google_api_key = get_from_dict_or_env(
values, "google_api_key", "GOOGLE_API_KEY"
)
if isinstance(google_api_key, SecretStr):
google_api_key = google_api_key.get_secret_value()

genai.configure(
api_key=google_api_key,
transport=values.get("transport"),
client_options=values.get("client_options"),
)
if values.get("credentials"):
genai.configure(
credentials=values.get("credentials"),
transport=values.get("transport"),
client_options=values.get("client_options"),
)
else:
google_api_key = get_from_dict_or_env(
values, "google_api_key", "GOOGLE_API_KEY"
)
if isinstance(google_api_key, SecretStr):
google_api_key = google_api_key.get_secret_value()

genai.configure(
api_key=google_api_key,
transport=values.get("transport"),
client_options=values.get("client_options"),
)
return values

def _embed(
Expand Down
35 changes: 23 additions & 12 deletions libs/genai/langchain_google_genai/llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Any, Callable, Dict, Iterator, List, Optional, Union

import google.api_core
import google.cloud.aiplatform
jackklika marked this conversation as resolved.
Show resolved Hide resolved
import google.generativeai as genai # type: ignore[import]
from langchain_core.callbacks import (
AsyncCallbackManagerForLLMRun,
Expand Down Expand Up @@ -122,6 +123,10 @@ class _BaseGoogleGenerativeAI(BaseModel):
)
"""Model name to use."""
google_api_key: Optional[SecretStr] = None
credentials: Any = None
"The default custom credentials (google.auth.credentials.Credentials) to use "
"when making API calls. If not provided, credentials will be ascertained from "
"the GOOGLE_API_KEY envvar"
temperature: float = 0.7
"""Run inference with this temperature. Must by in the closed interval
[0.0, 1.0]."""
Expand Down Expand Up @@ -203,22 +208,28 @@ class GoogleGenerativeAI(_BaseGoogleGenerativeAI, BaseLLM):
@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
"""Validates params and passes them to google-generativeai package."""
google_api_key = get_from_dict_or_env(
values, "google_api_key", "GOOGLE_API_KEY"
)
if values.get("credentials"):
genai.configure(
credentials=values.get("credentials"),
transport=values.get("transport"),
client_options=values.get("client_options"),
)
else:
google_api_key = get_from_dict_or_env(
values, "google_api_key", "GOOGLE_API_KEY"
)
if isinstance(google_api_key, SecretStr):
google_api_key = google_api_key.get_secret_value()
genai.configure(
api_key=google_api_key,
transport=values.get("transport"),
client_options=values.get("client_options"),
)

model_name = values["model"]

safety_settings = values["safety_settings"]

if isinstance(google_api_key, SecretStr):
google_api_key = google_api_key.get_secret_value()

genai.configure(
api_key=google_api_key,
transport=values.get("transport"),
client_options=values.get("client_options"),
)

if safety_settings and (
not GoogleModelFamily(model_name) == GoogleModelFamily.GEMINI
):
Expand Down
Loading