-
Notifications
You must be signed in to change notification settings - Fork 13.5k
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
google-vertexai: added langchain_google_vertexai package #15218
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
69905a3
added langchain_google_vertexai package
lkuligin a0659af
fixes after review
lkuligin 145d11f
Merge branch 'master' into vertex_partners
efriis c71c068
rm separate docs here
efriis 61791cc
cr
efriis ee2c14d
x
efriis 538557c
ser
efriis 26811d9
retry decorator
efriis 971547c
Merge branch 'langchain-ai:master' into vertex_partners
lkuligin 268768c
fixes after review
lkuligin File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 LangChain, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
.PHONY: all format lint test tests integration_tests docker_tests help extended_tests | ||
|
||
# Default target executed when no arguments are given to make. | ||
all: help | ||
|
||
# Define a variable for the test file path. | ||
TEST_FILE ?= tests/unit_tests/ | ||
|
||
test: | ||
poetry run pytest $(TEST_FILE) | ||
|
||
tests: | ||
poetry run pytest $(TEST_FILE) | ||
|
||
|
||
###################### | ||
# LINTING AND FORMATTING | ||
###################### | ||
|
||
# Define a variable for Python and notebook files. | ||
PYTHON_FILES=. | ||
MYPY_CACHE=.mypy_cache | ||
lint format: PYTHON_FILES=. | ||
lint_diff format_diff: PYTHON_FILES=$(shell git diff --relative=libs/partners/google-vertexai --name-only --diff-filter=d master | grep -E '\.py$$|\.ipynb$$') | ||
lint_package: PYTHON_FILES=langchain_google_vertexai | ||
lint_tests: PYTHON_FILES=tests | ||
lint_tests: MYPY_CACHE=.mypy_cache_test | ||
|
||
lint lint_diff lint_package lint_tests: | ||
poetry run ruff . | ||
poetry run ruff format $(PYTHON_FILES) --diff | ||
poetry run ruff --select I $(PYTHON_FILES) | ||
mkdir $(MYPY_CACHE); poetry run mypy $(PYTHON_FILES) --cache-dir $(MYPY_CACHE) | ||
|
||
format format_diff: | ||
poetry run ruff format $(PYTHON_FILES) | ||
poetry run ruff --select I --fix $(PYTHON_FILES) | ||
|
||
spell_check: | ||
poetry run codespell --toml pyproject.toml | ||
|
||
spell_fix: | ||
poetry run codespell --toml pyproject.toml -w | ||
|
||
check_imports: $(shell find langchain_google_vertexai -name '*.py') | ||
poetry run python ./scripts/check_imports.py $^ | ||
|
||
###################### | ||
# HELP | ||
###################### | ||
|
||
help: | ||
@echo '----' | ||
@echo 'check_imports - check imports' | ||
@echo 'format - run code formatters' | ||
@echo 'lint - run linters' | ||
@echo 'test - run unit tests' | ||
@echo 'tests - run unit tests' | ||
@echo 'test TEST_FILE=<test_file> - run all tests in file' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# langchain-google-vertexai |
5 changes: 5 additions & 0 deletions
5
libs/partners/google-vertexai/langchain_google_vertexai/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from langchain_google_vertexai.chat_models import ChatVertexAI | ||
from langchain_google_vertexai.embeddings import VertexAIEmbeddings | ||
from langchain_google_vertexai.llms import VertexAI, VertexAIModelGarden | ||
|
||
__all__ = ["ChatVertexAI", "VertexAIEmbeddings", "VertexAI", "VertexAIModelGarden"] |
88 changes: 88 additions & 0 deletions
88
libs/partners/google-vertexai/langchain_google_vertexai/_utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
"""Utilities to init Vertex AI.""" | ||
from importlib import metadata | ||
from typing import Any, Callable, Optional, Union | ||
|
||
import google.api_core | ||
from google.api_core.gapic_v1.client_info import ClientInfo | ||
from google.cloud import storage # type: ignore | ||
from langchain_core.callbacks import ( | ||
AsyncCallbackManagerForLLMRun, | ||
CallbackManagerForLLMRun, | ||
) | ||
from langchain_core.language_models.llms import create_base_retry_decorator | ||
from vertexai.preview.generative_models import Image # type: ignore | ||
|
||
|
||
def create_retry_decorator( | ||
*, | ||
max_retries: int = 1, | ||
run_manager: Optional[ | ||
Union[AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun] | ||
] = None, | ||
) -> Callable[[Any], Any]: | ||
"""Creates a retry decorator for Vertex / Palm LLMs.""" | ||
|
||
errors = [ | ||
google.api_core.exceptions.ResourceExhausted, | ||
google.api_core.exceptions.ServiceUnavailable, | ||
google.api_core.exceptions.Aborted, | ||
google.api_core.exceptions.DeadlineExceeded, | ||
google.api_core.exceptions.GoogleAPIError, | ||
] | ||
decorator = create_base_retry_decorator( | ||
error_types=errors, max_retries=max_retries, run_manager=run_manager | ||
) | ||
return decorator | ||
|
||
|
||
def raise_vertex_import_error(minimum_expected_version: str = "1.38.0") -> None: | ||
"""Raise ImportError related to Vertex SDK being not available. | ||
|
||
Args: | ||
minimum_expected_version: The lowest expected version of the SDK. | ||
Raises: | ||
ImportError: an ImportError that mentions a required version of the SDK. | ||
""" | ||
raise ImportError( | ||
"Please, install or upgrade the google-cloud-aiplatform library: " | ||
f"pip install google-cloud-aiplatform>={minimum_expected_version}" | ||
) | ||
|
||
|
||
def get_client_info(module: Optional[str] = None) -> "ClientInfo": | ||
r"""Returns a custom user agent header. | ||
|
||
Args: | ||
module (Optional[str]): | ||
Optional. The module for a custom user agent header. | ||
Returns: | ||
google.api_core.gapic_v1.client_info.ClientInfo | ||
""" | ||
langchain_version = metadata.version("langchain") | ||
client_library_version = ( | ||
f"{langchain_version}-{module}" if module else langchain_version | ||
) | ||
return ClientInfo( | ||
client_library_version=client_library_version, | ||
user_agent=f"langchain/{client_library_version}", | ||
) | ||
|
||
|
||
def load_image_from_gcs(path: str, project: Optional[str] = None) -> Image: | ||
"""Loads im Image from GCS.""" | ||
gcs_client = storage.Client(project=project) | ||
pieces = path.split("/") | ||
blobs = list(gcs_client.list_blobs(pieces[2], prefix="/".join(pieces[3:]))) | ||
if len(blobs) > 1: | ||
raise ValueError(f"Found more than one candidate for {path}!") | ||
return Image.from_bytes(blobs[0].download_as_bytes()) | ||
|
||
|
||
def is_codey_model(model_name: str) -> bool: | ||
"""Returns True if the model name is a Codey model.""" | ||
return "code" in model_name | ||
|
||
|
||
def is_gemini_model(model_name: str) -> bool: | ||
"""Returns True if the model name is a Gemini model.""" | ||
return model_name is not None and "gemini" in model_name |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't have to be right now, and adding a readme at some point would be great! This is what will be shown in the pypi page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added an initial version, we'll adjust it later then.