Skip to main content

Google Vertex AI

Get started

To get started follow the steps outlined in the Get started section of Vertex AI Gemini integration tutorial to create a Google Cloud Platform account and establish a new project with access to Vertex AI API.

Add dependencies

Add the following dependencies to your project's pom.xml:

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-vertex-ai</artifactId>
<version>{your-version}</version> <!-- Specify langchain4j version here -->
</dependency>

or project's build.gradle:

implementation 'dev.langchain4j:langchain4j-vertex-ai:{your-version}'

Try out an example code:

An Example of using Vertex AI Embedding Model

The PROJECT_ID field represents the variable you set when creating a new Google Cloud project.

import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.model.vertexai.VertexAiEmbeddingModel;

public class VertexAiEmbeddingModelExample {

private static final String PROJECT_ID = "YOUR-PROJECT-ID";
private static final String MODEL_NAME = "textembedding-gecko@latest";

public static void main(String[] args) {

EmbeddingModel embeddingModel = VertexAiEmbeddingModel.builder()
.endpoint("us-central1-aiplatform.googleapis.com:443")
.project(PROJECT_ID)
.location("us-central1")
.publisher("google")
.modelName(MODEL_NAME)
.build();

Response<Embedding> response = embeddingModel.embed("Hello, how are you?");

Embedding embedding = response.content();

int dimension = embedding.dimension(); // 768
float[] vector = embedding.vector(); // [-0.06050122, -0.046411075, ...

System.out.println(dimension);
System.out.println(embedding.vectorAsList());
}
}

Available Embedding models

Model nameDescription
textembedding-gecko@latestthe newest stable embedding model with enhanced AI quality
textembedding-gecko-multilingual@latestoptimized for a wide range of non-English languages.

List of supported languages for multi lingual model

Model names suffixed with @latest reference the most recent version of the model.

The API accepts a maximum of 3,072 input tokens and outputs 768-dimensional vector embeddings.

References

Google Codelab on Vertex AI Embedding Model

Available stable Embedding Models

Latest Embedding Models version