Skip to content

Commit 8db7d63

Browse files
committed
added generate multiple flashcards for word functionality
1 parent 6a5ddc1 commit 8db7d63

File tree

6 files changed

+93
-1
lines changed

6 files changed

+93
-1
lines changed

src/main/java/com/jakegodsall/services/flashcard/FlashcardService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ public interface FlashcardService {
3232
*/
3333
Flashcard generateFlashcard(String targetWord, FlashcardType flashcardType, Language language, Options options);
3434

35+
/**
36+
* Generates multiple flashcards for a single target word in one API call.
37+
*
38+
* @param targetWord the word for which multiple flashcards are generated
39+
* @param flashcardType the type of the flashcards (e.g., multiple choice, fill-in-the-blank)
40+
* @param language the language of the flashcards
41+
* @param options additional options to customize flashcard generation
42+
* @param count the number of flashcards to generate
43+
* @return a list of generated flashcards
44+
*/
45+
List<Flashcard> generateMultipleFlashcardsForWord(String targetWord, FlashcardType flashcardType, Language language, Options options, int count);
46+
3547
/**
3648
* Generates flashcards interactively by prompting the user for words until they exit.
3749
*

src/main/java/com/jakegodsall/services/flashcard/impl/FlashcardServiceGPTImpl.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,27 @@ public Flashcard generateFlashcard(String targetWord, FlashcardType flashcardTyp
7272
return null;
7373
}
7474

75+
@Override
76+
public List<Flashcard> generateMultipleFlashcardsForWord(String targetWord, FlashcardType flashcardType, Language language, Options options, int count) {
77+
try {
78+
// Generate the prompt for multiple flashcards
79+
String prompt = promptGenerator.generatePromptForMultipleFlashcards(targetWord, flashcardType, language, options, count);
80+
// Generate HTTP POST request body
81+
String requestBody = promptGenerator.generateRequestBody(prompt);
82+
// Send the POST request to the GPT API
83+
HttpResponse response = httpClientService.sendPostRequest(API_CHAT_URL, requestBody);
84+
// Get the result
85+
HttpEntity responseEntity = response.getEntity();
86+
String result = EntityUtils.toString(responseEntity);
87+
// Parse multiple flashcards from the result
88+
return jsonParseService.parseMultipleFlashcards(result, flashcardType);
89+
} catch (IOException ex) {
90+
logger.log(Level.SEVERE, ex.getMessage(), ex);
91+
System.err.println(ex.getMessage());
92+
return List.of();
93+
}
94+
}
95+
7596
@Override
7697
public List<Flashcard> generateFlashcardsInteractively(FlashcardType flashcardType, Language language, Options options) throws IOException {
7798
List<Flashcard> flashcards = new ArrayList<>();

src/main/java/com/jakegodsall/services/json/JsonParseService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,13 @@ public interface JsonParseService {
3232
* @return a parsed {@link Flashcard} object.
3333
*/
3434
Flashcard parseFlashcard(String responseBody, FlashcardType flashcardType);
35+
36+
/**
37+
* Parses multiple flashcards from a single API response.
38+
*
39+
* @param responseBody the raw JSON response from the API containing multiple flashcards.
40+
* @param flashcardType the type of flashcards to create, defined by the {@link FlashcardType} enum.
41+
* @return a list of parsed {@link Flashcard} objects.
42+
*/
43+
List<Flashcard> parseMultipleFlashcards(String responseBody, FlashcardType flashcardType);
3544
}

src/main/java/com/jakegodsall/services/json/impl/JsonParseServiceGPTImpl.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public Flashcard parseFlashcard(String responseBody, FlashcardType flashcardType
5050
try {
5151
String content = parseContentFromResponse(responseBody);
5252

53-
// Parse the JSON int oa JsonNode tree
53+
// Parse the JSON into a JsonNode tree
5454
JsonNode rootNode = objectMapper.readTree(content);
5555

5656
// Delegate to specific parsing logic based on FlashcardType
@@ -65,6 +65,34 @@ public Flashcard parseFlashcard(String responseBody, FlashcardType flashcardType
6565
}
6666
}
6767

68+
@Override
69+
public List<Flashcard> parseMultipleFlashcards(String responseBody, FlashcardType flashcardType) {
70+
try {
71+
String content = parseContentFromResponse(responseBody);
72+
JsonNode rootNode = objectMapper.readTree(content);
73+
74+
List<Flashcard> flashcards = new ArrayList<>();
75+
76+
if (!rootNode.isArray()) {
77+
throw new NoSuchElementException("Expected an array of flashcards in the response");
78+
}
79+
80+
for (JsonNode flashcardNode : rootNode) {
81+
Flashcard flashcard = switch (flashcardType) {
82+
case WORD -> parseWordFlashcardFromJson(flashcardNode);
83+
case SENTENCE -> parseSentenceFlashcardFromJson(flashcardNode);
84+
default -> throw new IllegalArgumentException("Unsupported FlashcardType: " + flashcardType);
85+
};
86+
flashcards.add(flashcard);
87+
}
88+
89+
return flashcards;
90+
} catch (JsonProcessingException ex) {
91+
System.err.println(ex.getMessage());
92+
return List.of();
93+
}
94+
}
95+
6896
/**
6997
* Parses a WordFlashcard from the given JsonNode.
7098
*/

src/main/java/com/jakegodsall/services/prompt/PromptService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ public interface PromptService {
3535
*/
3636
String generatePrompt(String targetWord, FlashcardType flashcardType, Language language, Options options);
3737

38+
/**
39+
* Generates a prompt for multiple flashcards based on a single target word.
40+
*
41+
* @param targetWord the word in the target language that will be used to generate multiple flashcards.
42+
* @param flashcardType the type of flashcard to generate.
43+
* @param language the language in which the flashcard content will be generated.
44+
* @param options additional options that influence the generation of the prompt.
45+
* @param count the number of flashcards to generate.
46+
* @return the generated prompt as a {@code String}, which will be sent to the API.
47+
*/
48+
String generatePromptForMultipleFlashcards(String targetWord, FlashcardType flashcardType, Language language, Options options, int count);
49+
3850
/**
3951
* Creates a simple prompt for the next word in a sequence.
4052
*

src/main/java/com/jakegodsall/services/prompt/impl/PromptServiceGPTImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ public String generatePrompt(String targetWord, FlashcardType flashcardType, Lan
4040
return generateBasePrompt() + getDescriptionOfContent(flashcardType) + getFlashcardStructure(flashcardType) + "The word is " + targetWord + " and the target language is " + language.getName() + ".\n";
4141
}
4242

43+
@Override
44+
public String generatePromptForMultipleFlashcards(String targetWord, FlashcardType flashcardType, Language language, Options options, int count) {
45+
return "Generate " + count + " different flashcards as a JSON array. For each flashcard in the array:\n" +
46+
generateBasePrompt() + getDescriptionOfContent(flashcardType) +
47+
"The structure for each flashcard in the array should be:\n" +
48+
getFlashcardStructure(flashcardType) +
49+
"The word is " + targetWord + " and the target language is " + language.getName() + ".\n" +
50+
"Each flashcard should use different example sentences and translations while maintaining accuracy.\n";
51+
}
52+
4353
@Override
4454
public String generatePromptForSubsequentWord(String targetWord) {
4555
return "The next word is " + targetWord;

0 commit comments

Comments
 (0)