Skip to content

Commit

Permalink
Add Vertex AI Text Embedding and Multimodal Embedding (run-llama#11561)
Browse files Browse the repository at this point in the history
  • Loading branch information
mustartt authored Mar 4, 2024
1 parent 71a20d7 commit c942af5
Show file tree
Hide file tree
Showing 13 changed files with 1,161 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
llama_index/_static
.DS_Store
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
bin/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
etc/
include/
lib/
lib64/
parts/
sdist/
share/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
.ruff_cache

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints
notebooks/

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
pyvenv.cfg

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# Jetbrains
.idea
modules/
*.swp

# VsCode
.vscode

# pipenv
Pipfile
Pipfile.lock

# pyright
pyrightconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
poetry_requirements(
name="poetry", module_mapping={"google-cloud-aiplatform": ["vertexai"]}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
GIT_ROOT ?= $(shell git rev-parse --show-toplevel)

help: ## Show all Makefile targets.
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[33m%-30s\033[0m %s\n", $$1, $$2}'

format: ## Run code autoformatters (black).
pre-commit install
git ls-files | xargs pre-commit run black --files

lint: ## Run linters: pre-commit (black, ruff, codespell) and mypy
pre-commit install && git ls-files | xargs pre-commit run --show-diff-on-failure --files

test: ## Run tests via pytest.
pytest tests

watch-docs: ## Build and watch documentation.
sphinx-autobuild docs/ docs/_build/html --open-browser --watch $(GIT_ROOT)/llama_index/
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# LlamaIndex Embeddings Integration: Vertex

Implements Vertex AI Embeddings Models:

| Model | Release Date |
| ------------------------------------ | ----------------- |
| textembedding-gecko@003 | December 12, 2023 |
| textembedding-gecko@002 | November 2, 2023 |
| textembedding-gecko-multilingual@001 | November 2, 2023 |
| textembedding-gecko@001 | June 7, 2023 |
| multimodalembedding | |

**Note**: Currently Vertex AI does not support async on `multimodalembedding`.
Otherwise, `VertexTextEmbedding` supports async interface.
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "3af200b75c4bd924",
"metadata": {},
"source": [
"# Vertex AI Multimodal Embedding\n",
"Uses APPLICATION_DEFAULT_CREDENTIALS if no credentials is specified. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7b43f20b2f09ff70",
"metadata": {},
"outputs": [],
"source": [
"from llama_index.embeddings.vertex import VertexMultiModalEmbedding\n",
"\n",
"embed_model = VertexMultiModalEmbedding(\n",
" project=\"speedy-atom-413006\", location=\"us-central1\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a10d4efa47801541",
"metadata": {},
"outputs": [],
"source": [
"image_url = \"https://upload.wikimedia.org/wikipedia/commons/4/43/Cute_dog.jpg\""
]
},
{
"cell_type": "markdown",
"id": "6e29951621ec9acc",
"metadata": {},
"source": [
"Download this image to `data/test-image.jpg`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "45aca848dd1d17e3",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<img src=\"https://upload.wikimedia.org/wikipedia/commons/4/43/Cute_dog.jpg\" width=\"500\"/>"
],
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.core.display import Image\n",
"\n",
"display(Image(url=image_url, width=500))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2d3394b6ce654ec4",
"metadata": {},
"outputs": [],
"source": [
"result = embed_model.get_image_embedding(\"data/test-image.jpg\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "75022fc91552014c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[-0.00822397694,\n",
" 0.0167199261,\n",
" 0.0195552949,\n",
" 0.00935372803,\n",
" 0.00746282,\n",
" 0.011754944,\n",
" -0.0363474153,\n",
" 0.00836938061,\n",
" -0.0170917399,\n",
" 0.0218462963]"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result[:10]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "103e4c523d039fdd",
"metadata": {},
"outputs": [],
"source": [
"text_result = embed_model.get_text_embedding(\n",
" \"a brown and white puppy laying in the grass with purple daisies in the background\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c14b8971981d61e5",
"metadata": {},
"outputs": [],
"source": [
"text_result_2 = embed_model.get_text_embedding(\"airplanes in the sky\")"
]
},
{
"cell_type": "markdown",
"id": "588f1585ba25bc57",
"metadata": {},
"source": [
"We expect that a similar description to the image will yield a higher similarity result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "23a129176e8d1007",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.20342717022759096"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"embed_model.similarity(result, text_result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "be0c503c3dd57412",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.009063958097860215"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"embed_model.similarity(result, text_result_2)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit c942af5

Please sign in to comment.