Initial Python rewrite

This commit is contained in:
2026-02-19 17:50:14 +00:00
parent da95a64fb7
commit faa04a0d01
158 changed files with 5122 additions and 1144 deletions

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2025 Google. This software is provided as-is, without warranty or representation for any use or purpose.
* Your use of it is subject to your agreement with Google.
*/
package com.example.service.base;
import com.example.exception.GeminiClientException;
import com.google.genai.Client;
import com.google.genai.errors.GenAiIOException;
import com.google.genai.types.Content;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.GenerateContentResponse;
import com.google.genai.types.Part;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
/**
* Service for interacting with the Gemini API to generate content.
* It encapsulates the low-level API calls, handling prompt configuration,
* and error management to provide a clean and robust content generation interface.
*/
@Service
public class GeminiClientService {
private static final Logger logger = LoggerFactory.getLogger(GeminiClientService.class);
private final Client geminiClient;
public GeminiClientService(Client geminiClient) {
this.geminiClient = geminiClient;
}
public String generateContent(String prompt, Float temperature, Integer maxOutputTokens, String modelName,Float topP) throws GeminiClientException {
try {
Content content = Content.fromParts(Part.fromText(prompt));
GenerateContentConfig config = GenerateContentConfig.builder()
.temperature(temperature)
.maxOutputTokens(maxOutputTokens)
.topP(topP)
.build();
logger.debug("Sending request to Gemini model '{}'", modelName);
GenerateContentResponse response = geminiClient.models.generateContent(modelName, content, config);
if (response != null && response.text() != null) {
return response.text();
} else {
logger.warn("Gemini returned no content or an unexpected response structure for model '{}'.", modelName);
throw new GeminiClientException("No content generated or unexpected response structure.");
}
} catch (GenAiIOException e) {
logger.error("Gemini API I/O error while calling model '{}': {}", modelName, e.getMessage(), e);
throw new GeminiClientException("An API communication issue occurred: " + e.getMessage(), e);
} catch (Exception e) {
logger.error("An unexpected error occurred during Gemini content generation for model '{}': {}", modelName, e.getMessage(), e);
throw new GeminiClientException("An unexpected issue occurred during content generation.", e);
}
}
}