-
Notifications
You must be signed in to change notification settings - Fork 16k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Partner] Add langchain-google-genai package (gemini) (#14621)
Add a new ChatGoogleGenerativeAI class in a `langchain-google-genai` package. Still todo: add a deprecation warning in PALM --------- Co-authored-by: Erick Friis <[email protected]> Co-authored-by: Leonid Kuligin <[email protected]> Co-authored-by: Bagatur <[email protected]>
- Loading branch information
1 parent
4574749
commit 405d111
Showing
19 changed files
with
2,356 additions
and
0 deletions.
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
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,61 @@ | ||
.PHONY: all format lint test tests integration_tests help | ||
|
||
# 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) | ||
|
||
check_imports: $(shell find langchain_google_genai -name '*.py') | ||
poetry run python ./scripts/check_imports.py $^ | ||
|
||
integration_tests: | ||
poetry run pytest tests/integration_tests | ||
|
||
###################### | ||
# 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 --name-only --diff-filter=d master | grep -E '\.py$$|\.ipynb$$') | ||
lint_package: PYTHON_FILES=langchain_google_genai | ||
lint_tests: PYTHON_FILES=tests | ||
lint_tests: MYPY_CACHE=.mypy_cache_test | ||
|
||
lint lint_diff lint_package lint_tests: | ||
./scripts/check_pydantic.sh . | ||
./scripts/lint_imports.sh | ||
poetry run ruff . | ||
[ "$(PYTHON_FILES)" = "" ] || poetry run ruff format $(PYTHON_FILES) --diff | ||
[ "$(PYTHON_FILES)" = "" ] || poetry run mypy $(PYTHON_FILES) | ||
|
||
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 | ||
|
||
###################### | ||
# HELP | ||
###################### | ||
|
||
help: | ||
@echo '----' | ||
@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,58 @@ | ||
# langchain-google-genai | ||
|
||
This package contains the LangChain integrations for Gemini through their generative-ai SDK. | ||
|
||
## Installation | ||
|
||
```python | ||
pip install -U langchain-google-genai | ||
``` | ||
|
||
## Chat Models | ||
|
||
This package contains the `ChatGoogleGenerativeAI` class, which is the recommended way to interface with the Google Gemini series of models. | ||
|
||
To use, install the requirements, and configure your environment. | ||
|
||
```bash | ||
export GOOGLE_API_KEY=your-api-key | ||
``` | ||
|
||
Then initialize | ||
|
||
```python | ||
from langchain_google_genai import ChatGoogleGenerativeAI | ||
|
||
llm = ChatGoogleGenerativeAI(model="gemini-pro") | ||
llm.invoke("Sing a ballad of LangChain.") | ||
``` | ||
|
||
#### Multimodal inputs | ||
|
||
Gemini vision model supports image inputs when providing a single chat message. Example: | ||
|
||
``` | ||
from langchain_core.messages import HumanMessage | ||
from langchain_google_genai import ChatGoogleGenerativeAI | ||
llm = ChatGoogleGenerativeAI(model="gemini-pro-vision") | ||
# example | ||
message = HumanMessage( | ||
content=[ | ||
{ | ||
"type": "text", | ||
"text": "What's in this image?", | ||
}, # You can optionally provide text parts | ||
{"type": "image_url", "image_url": "https://picsum.photos/seed/picsum/200/300"}, | ||
] | ||
) | ||
llm.invoke([message]) | ||
``` | ||
|
||
The value of `image_url` can be any of the following: | ||
|
||
- A public image URL | ||
- An accessible gcs file (e.g., "gcs://path/to/file.png") | ||
- A local file path | ||
- A base64 encoded image (e.g., `data:image/png;base64,abcd124`) | ||
- A PIL image |
3 changes: 3 additions & 0 deletions
3
libs/partners/google-genai/langchain_google_genai/__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,3 @@ | ||
from langchain_google_genai.chat_models import ChatGoogleGenerativeAI | ||
|
||
__all__ = ["ChatGoogleGenerativeAI"] |
Oops, something went wrong.