Skip to content

Commit 735bd7d

Browse files
committed
Adding Vertex AI Imagen models support
Closes gh-2133 Signed-off-by: Marzouki-Sami [email protected]
1 parent ab022fa commit 735bd7d

File tree

19 files changed

+2170
-0
lines changed

19 files changed

+2170
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2025-2026 the original author or authors.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xmlns="http://maven.apache.org/POM/4.0.0"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
<parent>
23+
<groupId>org.springframework.ai</groupId>
24+
<artifactId>spring-ai</artifactId>
25+
<version>1.0.0-M5</version>
26+
<relativePath>../../pom.xml</relativePath>
27+
</parent>
28+
29+
<artifactId>spring-ai-vertex-ai-imagen</artifactId>
30+
<packaging>jar</packaging>
31+
<name>Spring AI Model - Vertex AI Imagen</name>
32+
<description>Vertex AI Imagen models support</description>
33+
<url>https://github.com/spring-projects/spring-ai</url>
34+
35+
<scm>
36+
<url>https://github.com/spring-projects/spring-ai</url>
37+
<connection>git://github.com/spring-projects/spring-ai.git</connection>
38+
<developerConnection>[email protected]:spring-projects/spring-ai.git</developerConnection>
39+
</scm>
40+
41+
<properties>
42+
</properties>
43+
44+
<dependencyManagement>
45+
<dependencies>
46+
<dependency>
47+
<groupId>com.google.cloud</groupId>
48+
<artifactId>libraries-bom</artifactId>
49+
<version>${com.google.cloud.version}</version>
50+
<type>pom</type>
51+
<scope>import</scope>
52+
</dependency>
53+
</dependencies>
54+
</dependencyManagement>
55+
56+
<dependencies>
57+
58+
<dependency>
59+
<groupId>com.google.cloud</groupId>
60+
<artifactId>google-cloud-aiplatform</artifactId>
61+
<exclusions>
62+
<exclusion>
63+
<groupId>commons-logging</groupId>
64+
<artifactId>commons-logging</artifactId>
65+
</exclusion>
66+
</exclusions>
67+
</dependency>
68+
69+
<!-- production dependencies -->
70+
<dependency>
71+
<groupId>org.springframework.ai</groupId>
72+
<artifactId>spring-ai-core</artifactId>
73+
<version>${project.parent.version}</version>
74+
</dependency>
75+
76+
<dependency>
77+
<groupId>org.springframework.ai</groupId>
78+
<artifactId>spring-ai-retry</artifactId>
79+
<version>${project.parent.version}</version>
80+
</dependency>
81+
82+
<!-- Spring Framework -->
83+
<dependency>
84+
<groupId>org.springframework</groupId>
85+
<artifactId>spring-context-support</artifactId>
86+
</dependency>
87+
88+
<dependency>
89+
<groupId>org.springframework.boot</groupId>
90+
<artifactId>spring-boot-starter-logging</artifactId>
91+
</dependency>
92+
93+
<dependency>
94+
<groupId>io.micrometer</groupId>
95+
<artifactId>micrometer-observation-test</artifactId>
96+
<scope>test</scope>
97+
</dependency>
98+
99+
<!-- test dependencies -->
100+
<dependency>
101+
<groupId>org.springframework.ai</groupId>
102+
<artifactId>spring-ai-test</artifactId>
103+
<version>${project.version}</version>
104+
<scope>test</scope>
105+
</dependency>
106+
107+
</dependencies>
108+
109+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* Copyright 2025-2026 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ai.vertexai.imagen;
18+
19+
import java.io.IOException;
20+
21+
import com.google.cloud.aiplatform.v1.EndpointName;
22+
import com.google.cloud.aiplatform.v1.PredictionServiceSettings;
23+
24+
import org.springframework.util.StringUtils;
25+
26+
27+
/**
28+
* VertexAiImagenConnectionDetails represents the details of a connection to the Vertex AI imagen service.
29+
* It provides methods to access the project ID, location, publisher, and PredictionServiceSettings.
30+
*
31+
* @author Sami Marzouki
32+
*/
33+
public class VertexAiImagenConnectionDetails {
34+
35+
public static final String DEFAULT_ENDPOINT = "us-central1-aiplatform.googleapis.com:443";
36+
37+
public static final String DEFAULT_ENDPOINT_SUFFIX = "-aiplatform.googleapis.com:443";
38+
39+
public static final String DEFAULT_PUBLISHER = "google";
40+
41+
private static final String DEFAULT_LOCATION = "us-central1";
42+
43+
/**
44+
* Your project ID.
45+
*/
46+
private final String projectId;
47+
48+
/**
49+
* A location is a <a href="https://cloud.google.com/about/locations?hl=en">region</a>
50+
* you can specify in a request to control where data is stored at rest. For a list of
51+
* available regions, see <a href=
52+
* "https://cloud.google.com/vertex-ai/generative-ai/docs/learn/locations?hl=en">Generative
53+
* AI on Vertex AI locations</a>.
54+
*/
55+
private final String location;
56+
57+
private final String publisher;
58+
59+
private final PredictionServiceSettings predictionServiceSettings;
60+
61+
public VertexAiImagenConnectionDetails(String projectId, String location, String publisher,
62+
PredictionServiceSettings predictionServiceSettings) {
63+
this.projectId = projectId;
64+
this.location = location;
65+
this.publisher = publisher;
66+
this.predictionServiceSettings = predictionServiceSettings;
67+
}
68+
69+
public static Builder builder() {
70+
return new Builder();
71+
}
72+
73+
public String getProjectId() {
74+
return this.projectId;
75+
}
76+
77+
public String getLocation() {
78+
return this.location;
79+
}
80+
81+
public String getPublisher() {
82+
return this.publisher;
83+
}
84+
85+
public EndpointName getEndpointName(String modelName) {
86+
return EndpointName.ofProjectLocationPublisherModelName(this.projectId, this.location, this.publisher,
87+
modelName);
88+
}
89+
90+
public com.google.cloud.aiplatform.v1.PredictionServiceSettings getPredictionServiceSettings() {
91+
return this.predictionServiceSettings;
92+
}
93+
94+
public static class Builder {
95+
96+
/**
97+
* The Vertex AI embedding endpoint.
98+
*/
99+
private String endpoint;
100+
101+
/**
102+
* Your project ID.
103+
*/
104+
private String projectId;
105+
106+
/**
107+
* A location is a
108+
* <a href="https://cloud.google.com/about/locations?hl=en">region</a> you can
109+
* specify in a request to control where data is stored at rest. For a list of
110+
* available regions, see <a href=
111+
* "https://cloud.google.com/vertex-ai/generative-ai/docs/learn/locations?hl=en">Generative
112+
* AI on Vertex AI locations</a>.
113+
*/
114+
private String location;
115+
116+
/**
117+
*
118+
*/
119+
private String publisher;
120+
121+
/**
122+
* Allows the connection settings to be customised
123+
*/
124+
private PredictionServiceSettings predictionServiceSettings;
125+
126+
public Builder apiEndpoint(String endpoint) {
127+
this.endpoint = endpoint;
128+
return this;
129+
}
130+
131+
public Builder projectId(String projectId) {
132+
this.projectId = projectId;
133+
return this;
134+
}
135+
136+
public Builder location(String location) {
137+
this.location = location;
138+
return this;
139+
}
140+
141+
public Builder publisher(String publisher) {
142+
this.publisher = publisher;
143+
return this;
144+
}
145+
146+
public Builder predictionServiceSettings(PredictionServiceSettings predictionServiceSettings) {
147+
this.predictionServiceSettings = predictionServiceSettings;
148+
return this;
149+
}
150+
151+
public VertexAiImagenConnectionDetails build() {
152+
if (!StringUtils.hasText(this.endpoint)) {
153+
if (!StringUtils.hasText(this.location)) {
154+
this.endpoint = DEFAULT_ENDPOINT;
155+
this.location = DEFAULT_LOCATION;
156+
} else {
157+
this.endpoint = this.location + DEFAULT_ENDPOINT_SUFFIX;
158+
}
159+
}
160+
161+
if (!StringUtils.hasText(this.publisher)) {
162+
this.publisher = DEFAULT_PUBLISHER;
163+
}
164+
165+
if (this.predictionServiceSettings == null) {
166+
try {
167+
this.predictionServiceSettings = PredictionServiceSettings.newBuilder()
168+
.setEndpoint(this.endpoint)
169+
.build();
170+
} catch (IOException e) {
171+
throw new RuntimeException(e);
172+
}
173+
}
174+
175+
return new VertexAiImagenConnectionDetails(this.projectId, this.location, this.publisher,
176+
this.predictionServiceSettings);
177+
}
178+
179+
}
180+
181+
}

0 commit comments

Comments
 (0)