|
| 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