Skip to main content
Google Vertex is a service that exposes all foundation models available in Google Cloud. This will help you get started with Google Vertex AI embedding models using LangChain. For detailed documentation on VertexAIEmbeddings features and configuration options, please refer to the API reference.

Overview

Integration details

Setup

LangChain.js supports two different authentication methods based on whether you’re running in a Node.js environment or a web environment. To access Vertex AI embedding models you’ll need to set up Google Vertex AI in your Google Cloud Platform (GCP) account, save the credentials file, and install the @langchain/google-vertexai integration package. On Node.js, that package uses @langchain/google-gauth for authentication (you do not need to install it separately).

Credentials

Head to your GCP account and generate a credentials file. Once you’ve done this set the GOOGLE_APPLICATION_CREDENTIALS environment variable:
Alternatively, on your local machine you can run gcloud auth application-default login to use Application Default Credentials. If running in a web environment, install @langchain/google-vertexai-web and set GOOGLE_WEB_CREDENTIALS to your service account JSON string (GOOGLE_VERTEX_AI_WEB_CREDENTIALS is deprecated). If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

Installation

The LangChain VertexAIEmbeddings integration lives in the @langchain/google-vertexai package:

Instantiation

Now we can instantiate our model object and embed text:

Indexing and retrieval

Embedding models are often used in retrieval-augmented generation (RAG) flows, both as part of indexing data as well as later retrieving it. For more detailed instructions, please see our RAG tutorials under the Learn tab. Below, see how to index and retrieve data using the embeddings object we initialized above. In this example, we will index and retrieve a sample document using the demo MemoryVectorStore.

Direct usage

Under the hood, the vectorstore and retriever implementations are calling embeddings.embedDocument(...) and embeddings.embedQuery(...) to create embeddings for the text(s) used in fromDocuments and the retriever’s invoke operations, respectively. You can directly call these methods to get embeddings for your own use cases.

Embed single texts

You can embed queries for search with embedQuery. This generates a vector representation specific to the query:

Embed multiple texts

You can embed multiple texts for indexing with embedDocuments. The internals used for this method may (but do not have to) differ from embedding queries:

API reference

For detailed documentation of all VertexAIEmbeddings features and configurations head to the API reference.