Back to Llama Index

Vertex AI Text Embedding

llama-index-integrations/embeddings/llama-index-embeddings-vertex/examples/text_embedding.ipynb

0.14.211.1 KB
Original Source

Vertex AI Text Embedding

Imports the VertexTextEmbedding class and initializes an instance named embed_model with a specified project and location. Uses APPLICATION_DEFAULT_CREDENTIALS if no credentials is specified. The default model is textembedding-gecko@003 in document retrival mode.

python
from llama_index.embeddings.vertex import VertexTextEmbedding

embed_model = VertexTextEmbedding(project="speedy-atom-413006", location="us-central1")
python
embed_model.dict()

Document and Query Retrival

python
embed_text_result = embed_model.get_text_embedding("Hello World!")
python
embed_text_result[:5]
python
embed_query_result = embed_model.get_query_embedding("Hello World!")
python
embed_query_result[:5]
python
from llama_index.core.base.embeddings.base import SimilarityMode

embed_model.similarity(
    embed_text_result, embed_query_result, SimilarityMode.DOT_PRODUCT
)

Using the async interface

python
import nest_asyncio

nest_asyncio.apply()

result = await embed_model.aget_text_embedding("Hello World!")
python
result[:5]