|
19 | 19 | import java.io.IOException; |
20 | 20 |
|
21 | 21 | import com.google.auth.oauth2.GoogleCredentials; |
| 22 | +import com.google.common.collect.ImmutableMap; |
22 | 23 | import com.google.genai.Client; |
| 24 | +import com.google.genai.types.ClientOptions; |
| 25 | +import com.google.genai.types.HttpOptions; |
23 | 26 |
|
24 | 27 | import org.springframework.ai.google.genai.GoogleGenAiEmbeddingConnectionDetails; |
25 | 28 | import org.springframework.boot.autoconfigure.AutoConfiguration; |
@@ -49,28 +52,55 @@ public GoogleGenAiEmbeddingConnectionDetails googleGenAiEmbeddingConnectionDetai |
49 | 52 | GoogleGenAiEmbeddingConnectionProperties connectionProperties) throws IOException { |
50 | 53 |
|
51 | 54 | var connectionBuilder = GoogleGenAiEmbeddingConnectionDetails.builder(); |
| 55 | + Client client = buildGenAiClient(connectionProperties); |
| 56 | + connectionBuilder.genAiClient(client); |
| 57 | + return connectionBuilder.build(); |
| 58 | + } |
| 59 | + |
| 60 | + private Client buildGenAiClient(GoogleGenAiEmbeddingConnectionProperties connectionProperties) throws IOException { |
| 61 | + Client.Builder clientBuilder = Client.builder(); |
52 | 62 |
|
53 | 63 | if (StringUtils.hasText(connectionProperties.getApiKey())) { |
54 | 64 | // Gemini Developer API mode |
55 | | - connectionBuilder.apiKey(connectionProperties.getApiKey()); |
| 65 | + clientBuilder.apiKey(connectionProperties.getApiKey()); |
56 | 66 | } |
57 | 67 | else { |
58 | 68 | // Vertex AI mode |
59 | 69 | Assert.hasText(connectionProperties.getProjectId(), "Google GenAI project-id must be set!"); |
60 | 70 | Assert.hasText(connectionProperties.getLocation(), "Google GenAI location must be set!"); |
61 | 71 |
|
62 | | - connectionBuilder.projectId(connectionProperties.getProjectId()) |
63 | | - .location(connectionProperties.getLocation()); |
| 72 | + clientBuilder.project(connectionProperties.getProjectId()) |
| 73 | + .location(connectionProperties.getLocation()) |
| 74 | + .vertexAI(true); |
64 | 75 |
|
65 | 76 | if (connectionProperties.getCredentialsUri() != null) { |
66 | 77 | GoogleCredentials credentials = GoogleCredentials |
67 | 78 | .fromStream(connectionProperties.getCredentialsUri().getInputStream()); |
68 | | - // Note: Credentials are handled automatically by the SDK when using |
69 | | - // Vertex AI mode |
| 79 | + // Note: The new SDK doesn't have a direct setCredentials method, |
| 80 | + // credentials are handled automatically when vertexAI is true |
70 | 81 | } |
71 | 82 | } |
| 83 | + HttpOptions.Builder httpOptionsBuilder = HttpOptions.builder(); |
| 84 | + if (connectionProperties.getTimeout() != null) { |
| 85 | + httpOptionsBuilder.timeout((int) connectionProperties.getTimeout().getSeconds()); |
| 86 | + } |
| 87 | + if (!connectionProperties.getCustomHeaders().isEmpty()) { |
| 88 | + httpOptionsBuilder.headers(ImmutableMap.copyOf(connectionProperties.getCustomHeaders())); |
| 89 | + } |
72 | 90 |
|
73 | | - return connectionBuilder.build(); |
| 91 | + ClientOptions.Builder clientOptionsBuilder = ClientOptions.builder(); |
| 92 | + if (connectionProperties.getMaxConnections() != null) { |
| 93 | + clientOptionsBuilder.maxConnections(connectionProperties.getMaxConnections()); |
| 94 | + } |
| 95 | + |
| 96 | + if (connectionProperties.getMaxConnectionsPerHost() != null) { |
| 97 | + clientOptionsBuilder.maxConnectionsPerHost(connectionProperties.getMaxConnectionsPerHost()); |
| 98 | + } |
| 99 | + |
| 100 | + clientBuilder.httpOptions(httpOptionsBuilder.build()); |
| 101 | + clientBuilder.clientOptions(clientOptionsBuilder.build()); |
| 102 | + |
| 103 | + return clientBuilder.build(); |
74 | 104 | } |
75 | 105 |
|
76 | 106 | } |
0 commit comments