-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from HanaPiece/feat/gemini-api-improve
feat/#15 구조화 된 응답을 위한 프롬프트 관리 클래스 생성 & AI API 온도 매개변수 값 조정
- Loading branch information
Showing
7 changed files
with
91 additions
and
14 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/main/java/com/project/hana_piece/ai/dto/GeminiCallRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.project.hana_piece.ai.dto; | ||
|
||
import com.project.hana_piece.ai.vo.GeminiPrompt; | ||
import java.util.List; | ||
|
||
public record GeminiCallRequest(List<Content> contents, List<SafetySetting> safetySettings, GenerationConfig generationConfig) { | ||
|
||
private static final String safetySettingCategory = "HARM_CATEGORY_DANGEROUS_CONTENT"; | ||
private static final String safetySettingThreshold = "BLOCK_ONLY_HIGH"; | ||
private static final Double generationConfigTemperature = 0.1; | ||
private static final Double generationConfigTopP = 0.1; | ||
private static final Integer generationConfigTopK = 10; | ||
|
||
public static record Part(String text) {} | ||
|
||
public static record Content(List<Part> parts) {} | ||
|
||
public static record SafetySetting(String category, String threshold) {} | ||
|
||
public static record GenerationConfig(Double temperature, Double topP, Integer topK) {} | ||
|
||
public static GeminiCallRequest fromPrompt(GeminiPrompt geminiPrompt){ | ||
Part part = new Part(geminiPrompt.getTotalPrompt()); | ||
Content content = new Content(List.of(part)); | ||
List<Content> contents = List.of(content); | ||
|
||
SafetySetting safetySetting = new SafetySetting(safetySettingCategory, safetySettingThreshold); | ||
List<SafetySetting> safetySettings = List.of(safetySetting); | ||
|
||
GenerationConfig generationConfig = new GenerationConfig(generationConfigTemperature, generationConfigTopP, generationConfigTopK); | ||
|
||
return new GeminiCallRequest(contents, safetySettings, generationConfig); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 22 additions & 5 deletions
27
src/main/java/com/project/hana_piece/ai/vo/GeminiPrompt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,34 @@ | ||
package com.project.hana_piece.ai.vo; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class GeminiPrompt { | ||
|
||
private final String totalPrompt; | ||
private String requests; | ||
private String constraints; | ||
private String responseFormat; | ||
private String exampleData; | ||
|
||
public GeminiPrompt(String prompt) { | ||
this.totalPrompt = createPromptJson(prompt); | ||
public synchronized String getTotalPrompt() { | ||
StringBuilder totalPromptBuilder = new StringBuilder(); | ||
totalPromptBuilder.append(requests) | ||
.append(constraints) | ||
.append(responseFormat) | ||
.append(exampleData); | ||
return totalPromptBuilder.toString(); | ||
} | ||
|
||
private String createPromptJson(String prompt) { | ||
return "{\"contents\":[{\"parts\":[{\"text\":\""+prompt+"\"}]}]}"; | ||
@Builder | ||
public GeminiPrompt(String requests, String constraints, String responseFormat, | ||
String exampleData) { | ||
this.requests = "Requests: " + requests + "\n"; | ||
this.constraints = "Constraints: " + constraints + "\n"; | ||
this.responseFormat = "ResponseFormat: " + responseFormat + "\n"; | ||
this.exampleData = "ExampleData: "+ exampleData + "\n"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters