LangChain4j integration for Google Vertex AI models including chat, language, embedding, image, and scoring capabilities
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
public class VertexAiChatModel implements ChatModel {
public ChatResponse chat(ChatRequest chatRequest);
public static Builder builder();
}/**
* Generate chat completion from a chat request.
*
* @param chatRequest The chat request containing messages and configuration
* @return ChatResponse containing the generated response
*/
public ChatResponse chat(ChatRequest chatRequest);/**
* Create a new builder for configuring a VertexAiChatModel.
*
* @return A new Builder instance
*/
public static Builder builder();/**
* Set the GCP API endpoint URL.
*
* @param endpoint The API endpoint (e.g., "https://us-central1-aiplatform.googleapis.com/v1/")
* @return This builder
*/
public Builder endpoint(String endpoint);
/**
* Set the Google Cloud Project ID.
*
* @param project The GCP project ID
* @return This builder
*/
public Builder project(String project);
/**
* Set the GCP region/location.
*
* @param location The region (e.g., "us-central1", "europe-west1")
* @return This builder
*/
public Builder location(String location);
/**
* Set the model publisher.
*
* @param publisher The publisher name, typically "google"
* @return This builder
*/
public Builder publisher(String publisher);
/**
* Set the model name/version.
*
* @param modelName The model name (e.g., "chat-bison@001", "gemini-pro")
* @return This builder
*/
public Builder modelName(String modelName);/**
* Set the sampling temperature for randomness control.
*
* @param temperature Temperature value between 0.0 (deterministic) and 1.0 (random)
* @return This builder
*/
public Builder temperature(Double temperature);
/**
* Set the maximum number of output tokens.
*
* @param maxOutputTokens Maximum tokens in response (default: 200)
* @return This builder
*/
public Builder maxOutputTokens(Integer maxOutputTokens);
/**
* Set the top-K sampling parameter.
*
* @param topK Number of highest probability tokens to consider
* @return This builder
*/
public Builder topK(Integer topK);
/**
* Set the top-P (nucleus) sampling parameter.
*
* @param topP Cumulative probability threshold for token selection
* @return This builder
*/
public Builder topP(Double topP);
/**
* Set the maximum number of retry attempts on API failures.
*
* @param maxRetries Maximum retry attempts (default: 2)
* @return This builder
*/
public Builder maxRetries(Integer maxRetries);
/**
* Set custom Google Cloud credentials.
*
* @param credentials GoogleCredentials instance for authentication
* @return This builder
*/
public Builder credentials(GoogleCredentials credentials);
/**
* Build the VertexAiChatModel instance.
*
* @return Configured VertexAiChatModel instance
*/
public VertexAiChatModel build();/**
* @deprecated Since version 1.2.0, use builder() instead
*/
@Deprecated
public VertexAiChatModel(
String endpoint,
String project,
String location,
String publisher,
String modelName,
Double temperature,
Integer maxOutputTokens,
Integer topK,
Double topP,
Integer maxRetries
);https://{region}-aiplatform.googleapis.com/v1/https://us-central1-aiplatform.googleapis.com/v1/https://europe-west1-aiplatform.googleapis.com/v1/https://asia-northeast1-aiplatform.googleapis.com/v1/my-project-123us-central1, europe-west1, asia-northeast1"google" for Vertex AI modelschat-bison@001 - PaLM 2 chat modelchat-bison@002 - PaLM 2 chat model (updated)gemini-pro - Gemini Progemini-ultra - Gemini Ultra0.0 - Deterministic, focused responses0.5 - Balanced1.0 - Creative, varied responsespublic class ChatResponse {
public AiMessage aiMessage();
public TokenUsage tokenUsage();
public FinishReason finishReason();
}public class AiMessage {
public String text();
}public class TokenUsage {
public Integer inputTokenCount();
public Integer outputTokenCount();
public Integer totalTokenCount();
}public enum FinishReason {
STOP, // Natural completion
LENGTH, // Max tokens reached
SAFETY, // Safety filter triggered
RECITATION, // Recitation detected
OTHER // Other reason
}public class ChatRequest {
public static Builder builder();
}public Builder messages(ChatMessage... messages);
public Builder messages(List<ChatMessage> messages);
public ChatRequest build();// User message
public class UserMessage implements ChatMessage {
public static UserMessage from(String text);
}
// AI message
public class AiMessage implements ChatMessage {
public static AiMessage from(String text);
}
// System message
public class SystemMessage implements ChatMessage {
public static SystemMessage from(String text);
}The model automatically retries these errors up to maxRetries times:
These errors are not retried and throw exceptions immediately:
try {
ChatResponse response = model.chat(request);
} catch (RuntimeException e) {
// Catches all non-retryable errors after retries exhausted
}VertexAiChatModel instances are thread-safe and can be reused across multiple threads. Builder is not thread-safe.
response.tokenUsage() to track consumptionInstall with Tessl CLI
npx tessl i tessl/maven-dev-langchain4j--langchain4j-vertex-ai