Spring AI with Gemini API: Easy Onboarding via OpenAI Compatibility

Within Google’s Gemini product lineup, although both the Gemini API and Vertex AI Gemini are based on the Gemini model, they differ significantly in their target audiences and application scenarios:

  • Gemini API:
    This API adopts an OpenAI-compatible interface, primarily targeting developers. It offers a lightweight and easy-to-use approach, enabling developers to quickly access and utilize the latest Gemini models via familiar OpenAI libraries or REST APIs—ideal for rapid prototyping and proof-of-concept projects.

  • Vertex AI Gemini:
    Part of the Google Cloud Vertex AI suite, this service is geared toward enterprise use. In addition to offering Gemini models, it integrates advanced security, scalability, and comprehensive operational management features, making it well-suited for large-scale production environments and enterprise applications.

As a result, while Spring AI does not yet support native integration with the Gemini API, we can leverage the OpenAI compatibility mode (refer to the official documentation at https://ai.google.dev/gemini-api/docs/openai) to access Gemini models. This approach allows developers to utilize the familiar OpenAI interface within Spring AI for quick and efficient integration of Gemini API features.

Spring AI Integration Implementation

Here’s the complete integration code with detailed comments:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

import java.nio.charset.StandardCharsets;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.ai.retry.RetryUtils;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.StreamUtils;
import org.springframework.web.client.RestClient;
import org.springframework.web.reactive.function.client.WebClient;

// Create RestClient.Builder with logging interceptor
RestClient.Builder builder = RestClient.builder()
.requestFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()))
.requestInterceptor((request, body, execution) -> {
log.info("=== Gemini Request ===");
log.info("URL: {} {}", request.getMethod(), request.getURI());
log.info("Headers: {}", request.getHeaders());
log.info("Body: {}", new String(body, StandardCharsets.UTF_8))
ClientHttpResponse response = execution.execute(request, body)
log.info("=== Gemini Response ===");
log.info("Status: {}", response.getStatusCode());
log.info("Headers: {}", response.getHeaders());
log.info("Body: {}", StreamUtils.copyToString(response.getBody(), StandardCharsetUTF_8));
return response;
});
WebClient.Builder webClientBuilder = WebClient.builder();
// Configure Gemini API parameters
String baseUrl = "https://generativelanguage.googleapis.com/v1beta";
String completionsPath = "/openai/chat/completions";
String embeddingsPath = "/openai/embeddings";
String apiKey = "YOUR_API_KEY";
// Create OpenAiApi instance
var openAiApi = new OpenAiApi(baseUrl, apiKey, completionsPath, embeddingsPath,
builder, webClientBuilder, RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER);
// Configure Chat options using gemini-1.5-flash model
var openAiChatOptions = OpenAiChatOptions.builder()
.model("gemini-1.5-flash")
.build();
// Create ChatModel
var chatModel = new OpenAiChatModel(openAiApi, openAiChatOptions);
// Use ChatClient to send request and get response
ChatResponse response = ChatClient.create(chatModel).prompt()
// .advisors(new SimpleLoggerAdvisor())
.user("Generate the names of 5 famous pirates.")
.call()
.chatResponse();
// Print response
log.info(response.toString());

Conclusion

Although Spring AI doesn’t directly support Gemini API, we can seamlessly integrate Gemini API into our applications through its OpenAI compatibility mode.

References