Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the Gemini model version from 1.5-flash to 2.0-flash in the GeminiClient. The review feedback suggests refactoring the client to externalize the model name into a configuration file for better maintainability and recommends passing the API key via an HTTP header instead of a query parameter to enhance security.
|
|
||
| private static final String GEMINI_URL = | ||
| "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent"; | ||
| "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent"; |
There was a problem hiding this comment.
모델 이름(gemini-2.0-flash)이 URL에 하드코딩되어 있습니다. 이번 PR의 계기가 된 것처럼 모델 이름은 변경될 수 있으므로, application.properties와 같은 설정 파일에서 관리하는 것이 유지보수 측면에서 더 좋습니다. 이렇게 하면 코드 변경 및 재배포 없이 모델을 업데이트할 수 있습니다.
더불어, 현재 API 키가 URL의 쿼리 파라미터로 전달되고 있습니다. 이 방식은 서버 로그, 브라우저 히스토리 등에 API 키가 노출될 수 있어 보안상 권장되지 않습니다. Gemini API는 x-goog-api-key HTTP 헤더를 지원하므로, 이를 사용하는 것이 더 안전합니다.
아래와 같이 리팩토링하는 것을 제안합니다.
// application.properties
gemini.model=gemini-2.0-flash
// GeminiClient.java
@Component
public class GeminiClient {
private static final String GEMINI_URL_FORMAT = "https://generativelanguage.googleapis.com/v1beta/models/%s:generateContent";
private final RestClient restClient;
private final String apiKey;
private final String geminiUrl;
public GeminiClient(@Value("${gemini.api-key:}") String apiKey,
@Value("${gemini.model}") String model) {
this.apiKey = apiKey;
this.restClient = RestClient.create();
this.geminiUrl = String.format(GEMINI_URL_FORMAT, model);
}
public String generateComment(String systemInstruction, String userPrompt) {
// ...
try {
Map<?, ?> response = restClient.post()
.uri(geminiUrl)
.header("x-goog-api-key", apiKey)
.header("Content-Type", "application/json")
.body(requestBody)
.retrieve()
.body(Map.class);
return extractText(response);
} // ...
}
// ...
}이 변경을 적용하면 코드의 유연성과 보안이 모두 향상됩니다.
gemini-1.5-flash 404 에러 → gemini-2.0-flash로 변경