Using Gemini v2.5 free tier
This commit is contained in:
@@ -12,7 +12,7 @@ import java.util.concurrent.TimeUnit;
|
||||
public class GeminiService {
|
||||
private static final Logger logger = LoggerFactory.getLogger(GeminiService.class);
|
||||
private static final int MAX_TWEET_LENGTH = 280;
|
||||
private static final String GEMINI_API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent";
|
||||
private static final String GEMINI_API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent";
|
||||
private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
||||
|
||||
private final String apiKey;
|
||||
@@ -27,7 +27,7 @@ public class GeminiService {
|
||||
.writeTimeout(60, TimeUnit.SECONDS)
|
||||
.build();
|
||||
this.objectMapper = new ObjectMapper();
|
||||
logger.info("Gemini service initialized with model: gemini-2.0-flash-exp");
|
||||
logger.info("Gemini service initialized with model: gemini-2.5-flash");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,19 +39,16 @@ public class GeminiService {
|
||||
try {
|
||||
logger.info("Generating tweet with prompt: {}", prompt);
|
||||
|
||||
// Build the request body
|
||||
String systemInstruction = "You are a creative social media content creator. Generate engaging tweets that are concise, " +
|
||||
"interesting, and within Twitter's character limit. Do not include hashtags unless specifically " +
|
||||
"requested. Keep it under 280 characters.";
|
||||
|
||||
String userPrompt = prompt + " Keep it under " + MAX_TWEET_LENGTH + " characters.";
|
||||
// Build the request body with simplified prompt
|
||||
String fullPrompt = "You are a creative social media content creator. Generate an engaging tweet (under 280 characters) about: " + prompt;
|
||||
|
||||
String jsonBody = String.format(
|
||||
"{\"contents\":[{\"parts\":[{\"text\":\"%s\\n\\n%s\"}]}],\"generationConfig\":{\"temperature\":0.8,\"maxOutputTokens\":100}}",
|
||||
escapeJson(systemInstruction),
|
||||
escapeJson(userPrompt)
|
||||
"{\"contents\":[{\"parts\":[{\"text\":\"%s\"}]}],\"generationConfig\":{\"temperature\":0.8,\"maxOutputTokens\":100000}}",
|
||||
escapeJson(fullPrompt)
|
||||
);
|
||||
|
||||
logger.debug("Request body: {}", jsonBody);
|
||||
|
||||
// Build the request
|
||||
Request request = new Request.Builder()
|
||||
.url(GEMINI_API_URL + "?key=" + apiKey)
|
||||
@@ -84,7 +81,21 @@ public class GeminiService {
|
||||
|
||||
JsonNode parts = content.get("parts");
|
||||
if (parts == null || parts.isEmpty()) {
|
||||
throw new RuntimeException("No parts in Gemini response");
|
||||
// Check for finish reason to understand why content was blocked
|
||||
JsonNode finishReason = candidates.get(0).get("finishReason");
|
||||
JsonNode safetyRatings = candidates.get(0).get("safetyRatings");
|
||||
|
||||
String errorMsg = "No parts in Gemini response.";
|
||||
if (finishReason != null) {
|
||||
errorMsg += " Finish reason: " + finishReason.asText();
|
||||
}
|
||||
if (safetyRatings != null) {
|
||||
errorMsg += " Safety ratings: " + safetyRatings.toString();
|
||||
}
|
||||
errorMsg += " Full candidate: " + candidates.get(0).toString();
|
||||
|
||||
logger.error(errorMsg);
|
||||
throw new RuntimeException(errorMsg);
|
||||
}
|
||||
|
||||
String generatedText = parts.get(0).get("text").asText().trim();
|
||||
|
||||
Reference in New Issue
Block a user