-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* moving code --------- Co-authored-by: Erick Friis <[email protected]>
- Loading branch information
Showing
18 changed files
with
3,330 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
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) 2024 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,58 @@ | ||
.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/ | ||
|
||
integration_test integration_tests: TEST_FILE = tests/integration_tests/ | ||
|
||
test tests integration_test integration_tests: | ||
poetry run pytest --release $(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/tools --name-only --diff-filter=d main | grep -E '\.py$$|\.ipynb$$') | ||
lint_package: PYTHON_FILES=langchain_google_tools | ||
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_tools -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,107 @@ | ||
# langchain-google-genai | ||
|
||
This package contains the LangChain integrations for Gemini through their generative-ai SDK. | ||
|
||
## Installation | ||
|
||
```bash | ||
pip install -U langchain-google-genai | ||
``` | ||
|
||
### Image utilities | ||
To use image utility methods, like loading images from GCS urls, install with extras group 'images': | ||
|
||
```bash | ||
pip install -e "langchain-google-genai[images]" | ||
``` | ||
|
||
## 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 | ||
|
||
|
||
|
||
## Embeddings | ||
|
||
This package also adds support for google's embeddings models. | ||
|
||
``` | ||
from langchain_google_genai import GoogleGenerativeAIEmbeddings | ||
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001") | ||
embeddings.embed_query("hello, world!") | ||
``` | ||
|
||
## Semantic Retrieval | ||
|
||
Enables retrieval augmented generation (RAG) in your application. | ||
|
||
``` | ||
# Create a new store for housing your documents. | ||
corpus_store = GoogleVectorStore.create_corpus(display_name="My Corpus") | ||
# Create a new document under the above corpus. | ||
document_store = GoogleVectorStore.create_document( | ||
corpus_id=corpus_store.corpus_id, display_name="My Document" | ||
) | ||
# Upload some texts to the document. | ||
text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0) | ||
for file in DirectoryLoader(path="data/").load(): | ||
documents = text_splitter.split_documents([file]) | ||
document_store.add_documents(documents) | ||
# Talk to your entire corpus with possibly many documents. | ||
aqa = corpus_store.as_aqa() | ||
answer = aqa.invoke("What is the meaning of life?") | ||
# Read the response along with the attributed passages and answerability. | ||
print(response.answer) | ||
print(response.attributed_passages) | ||
print(response.answerable_probability) | ||
``` |
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,17 @@ | ||
from langchain_google_tools.bigquery_vector_search import BigQueryVectorSearch | ||
from langchain_google_tools.documentai_warehouse import DocumentAIWarehouseRetriever | ||
from langchain_google_tools.gmail.loader import GMailLoader | ||
from langchain_google_tools.gmail.toolkit import GmailToolkit | ||
from langchain_google_tools.vertex_ai_search import ( | ||
VertexAIMultiTurnSearchRetriever, | ||
VertexAISearchRetriever, | ||
) | ||
|
||
__all__ = [ | ||
"BigQueryVectorSearch", | ||
"DocumentAIWarehouseRetriever", | ||
"GMailLoader", | ||
"GmailToolkit", | ||
"VertexAIMultiTurnSearchRetriever", | ||
"VertexAISearchRetriever", | ||
] |
Oops, something went wrong.