diff --git a/.github/workflows/_integration_test.yml b/.github/workflows/_integration_test.yml index 0c680c55..cd3c062b 100644 --- a/.github/workflows/_integration_test.yml +++ b/.github/workflows/_integration_test.yml @@ -48,6 +48,7 @@ jobs: GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }} GOOGLE_SEARCH_API_KEY: ${{ secrets.GOOGLE_SEARCH_API_KEY }} GOOGLE_CSE_ID: ${{ secrets.GOOGLE_CSE_ID }} + GOOGLE_VERTEX_AI_WEB_CREDENTIALS: ${{ secrets.GOOGLE_VERTEX_AI_WEB_CREDENTIALS }} run: | make integration_tests diff --git a/.github/workflows/_release.yml b/.github/workflows/_release.yml index 3ca3374f..acd18b0f 100644 --- a/.github/workflows/_release.yml +++ b/.github/workflows/_release.yml @@ -174,6 +174,7 @@ jobs: GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }} GOOGLE_SEARCH_API_KEY: ${{ secrets.GOOGLE_SEARCH_API_KEY }} GOOGLE_CSE_ID: ${{ secrets.GOOGLE_CSE_ID }} + GOOGLE_VERTEX_AI_WEB_CREDENTIALS: ${{ secrets.GOOGLE_VERTEX_AI_WEB_CREDENTIALS }} run: make integration_tests working-directory: ${{ inputs.working-directory }} diff --git a/libs/vertexai/langchain_google_vertexai/_base.py b/libs/vertexai/langchain_google_vertexai/_base.py index cc6c85a5..dd377f96 100644 --- a/libs/vertexai/langchain_google_vertexai/_base.py +++ b/libs/vertexai/langchain_google_vertexai/_base.py @@ -121,7 +121,10 @@ def validate_params_base(cls, values: dict) -> Any: @model_validator(mode="after") def validate_project(self) -> Any: if self.project is None: - self.project = initializer.global_config.project + if self.credentials and hasattr(self.credentials, "project_id"): + self.project = self.credentials.project_id + else: + self.project = initializer.global_config.project return self @property diff --git a/libs/vertexai/tests/integration_tests/test_chat_models.py b/libs/vertexai/tests/integration_tests/test_chat_models.py index 2fffb601..d3384d02 100644 --- a/libs/vertexai/tests/integration_tests/test_chat_models.py +++ b/libs/vertexai/tests/integration_tests/test_chat_models.py @@ -2,6 +2,7 @@ import base64 import json +import os from typing import List, Optional, cast import pytest @@ -11,6 +12,7 @@ Content, Part, ) +from google.oauth2 import service_account from langchain_core.messages import ( AIMessage, AIMessageChunk, @@ -912,3 +914,12 @@ def test_langgraph_example() -> None: tools=[{"function_declarations": [add_declaration, multiply_declaration]}], ) assert isinstance(step2, AIMessage) + + +def test_init_from_credentials_obj() -> None: + credentials_dict = json.loads(os.environ["GOOGLE_VERTEX_AI_WEB_CREDENTIALS"]) + credentials = service_account.Credentials.from_service_account_info( + credentials_dict + ) + llm = ChatVertexAI(model="gemini-1.5-flash", credentials=credentials) + llm.invoke("how are you")