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
SPI factory interfaces for dependency injection frameworks and custom builder implementations. Enables integration with Spring, Quarkus, and custom DI systems.
import dev.langchain4j.model.vertexai.spi.VertexAiChatModelBuilderFactory;
import dev.langchain4j.model.vertexai.spi.VertexAiEmbeddingModelBuilderFactory;
import dev.langchain4j.model.vertexai.spi.VertexAiLanguageModelBuilderFactory;
import dev.langchain4j.model.vertexai.spi.VertexAiImageModelBuilderFactory;
import java.util.function.Supplier;public interface VertexAiChatModelBuilderFactory extends Supplier<VertexAiChatModel.Builder> {
VertexAiChatModel.Builder get();
}
public interface VertexAiEmbeddingModelBuilderFactory extends Supplier<VertexAiEmbeddingModel.Builder> {
VertexAiEmbeddingModel.Builder get();
}
public interface VertexAiLanguageModelBuilderFactory extends Supplier<VertexAiLanguageModel.Builder> {
VertexAiLanguageModel.Builder get();
}
public interface VertexAiImageModelBuilderFactory extends Supplier<VertexAiImageModel.Builder> {
VertexAiImageModel.Builder get();
}Note: VertexAiScoringModel does not have an SPI factory interface.
Model classes use Java's ServiceLoader mechanism:
builder() on model classServiceLoader searches for factory implementationsget() method for pre-configured builderpackage com.example.config;
import dev.langchain4j.model.vertexai.VertexAiChatModel;
import dev.langchain4j.model.vertexai.spi.VertexAiChatModelBuilderFactory;
public class CustomChatModelBuilderFactory implements VertexAiChatModelBuilderFactory {
@Override
public VertexAiChatModel.Builder get() {
return VertexAiChatModel.builder()
.endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
.project(System.getenv("GCP_PROJECT_ID"))
.location("us-central1")
.publisher("google")
.maxRetries(5);
}
}Create file: META-INF/services/dev.langchain4j.model.vertexai.spi.VertexAiChatModelBuilderFactory
Content:
com.example.config.CustomChatModelBuilderFactory// builder() automatically uses your factory
VertexAiChatModel model = VertexAiChatModel.builder()
.modelName("chat-bison@001") // Add model-specific config
.temperature(0.7)
.build();@Configuration
public class VertexAiConfig {
@Bean
public VertexAiChatModelBuilderFactory chatModelBuilderFactory(
@Value("${gcp.project-id}") String projectId,
@Value("${gcp.location}") String location) {
return () -> VertexAiChatModel.builder()
.endpoint("https://" + location + "-aiplatform.googleapis.com/v1/")
.project(projectId)
.location(location)
.publisher("google")
.maxRetries(3);
}
}@ApplicationScoped
public class VertexAiProducer {
@ConfigProperty(name = "gcp.project-id")
String projectId;
@ConfigProperty(name = "gcp.location")
String location;
@Produces
public VertexAiChatModelBuilderFactory chatModelBuilderFactory() {
return () -> VertexAiChatModel.builder()
.endpoint("https://" + location + "-aiplatform.googleapis.com/v1/")
.project(projectId)
.location(location)
.publisher("google");
}
}public class CentralConfigFactory implements VertexAiChatModelBuilderFactory {
private static final String PROJECT_ID = loadFromConfig("project.id");
private static final String LOCATION = loadFromConfig("location");
@Override
public VertexAiChatModel.Builder get() {
return VertexAiChatModel.builder()
.endpoint(buildEndpoint(LOCATION))
.project(PROJECT_ID)
.location(LOCATION)
.publisher("google")
.maxRetries(3)
.credentials(loadCredentials());
}
}public class EnvironmentAwareBuilderFactory implements VertexAiChatModelBuilderFactory {
@Override
public VertexAiChatModel.Builder get() {
String env = System.getenv("ENVIRONMENT");
return switch (env) {
case "production" -> productionBuilder();
case "staging" -> stagingBuilder();
default -> developmentBuilder();
};
}
private VertexAiChatModel.Builder productionBuilder() {
return VertexAiChatModel.builder()
.endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
.project("prod-project-id")
.location("us-central1")
.maxRetries(5);
}
private VertexAiChatModel.Builder stagingBuilder() {
return VertexAiChatModel.builder()
.endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
.project("staging-project-id")
.location("us-central1")
.maxRetries(3);
}
private VertexAiChatModel.Builder developmentBuilder() {
return VertexAiChatModel.builder()
.endpoint("http://localhost:8080/mock-vertex-ai")
.project("dev-project")
.location("us-central1")
.maxRetries(1);
}
}public class TestChatModelBuilderFactory implements VertexAiChatModelBuilderFactory {
@Override
public VertexAiChatModel.Builder get() {
return VertexAiChatModel.builder()
.endpoint("http://localhost:8080/mock-vertex-ai")
.project("test-project")
.location("us-central1")
.publisher("google")
.maxRetries(1);
}
}Example with all model types:
File: META-INF/services/dev.langchain4j.model.vertexai.spi.VertexAiChatModelBuilderFactory
com.example.config.CustomChatModelBuilderFactoryFile: META-INF/services/dev.langchain4j.model.vertexai.spi.VertexAiEmbeddingModelBuilderFactory
com.example.config.CustomEmbeddingModelBuilderFactoryFile: META-INF/services/dev.langchain4j.model.vertexai.spi.VertexAiLanguageModelBuilderFactory
com.example.config.CustomLanguageModelBuilderFactoryFile: META-INF/services/dev.langchain4j.model.vertexai.spi.VertexAiImageModelBuilderFactory
com.example.config.CustomImageModelBuilderFactorypackage com.example.config;
import dev.langchain4j.model.vertexai.VertexAiEmbeddingModel;
import dev.langchain4j.model.vertexai.spi.VertexAiEmbeddingModelBuilderFactory;
public class CustomEmbeddingModelBuilderFactory implements VertexAiEmbeddingModelBuilderFactory {
@Override
public VertexAiEmbeddingModel.Builder get() {
return VertexAiEmbeddingModel.builder()
.endpoint("us-central1-aiplatform.googleapis.com:443")
.project(System.getenv("GCP_PROJECT_ID"))
.location("us-central1")
.publisher("google")
.maxRetries(3)
.maxSegmentsPerBatch(100);
}
}All SPI interfaces located in:
dev.langchain4j.model.vertexai.spiServiceLoader is usedVertexAiScoringModel has no factory interfaceCheck if factories are loaded:
import java.util.ServiceLoader;
ServiceLoader<VertexAiChatModelBuilderFactory> loader =
ServiceLoader.load(VertexAiChatModelBuilderFactory.class);
for (VertexAiChatModelBuilderFactory factory : loader) {
System.out.println("Found factory: " + factory.getClass().getName());
}Issue: Custom factory not being used
Solutions:
META-INF/services/ file exists in classpathIssue: Multiple factories on classpath
Behavior: First one found by ServiceLoader is used (order undefined)
Solution: Remove conflicting factories or use explicit ordering mechanism
Install with Tessl CLI
npx tessl i tessl/maven-dev-langchain4j--langchain4j-vertex-ai