diff --git a/GermanApp/Application/Services/StoryGenerationService.cs b/GermanApp/Application/Services/StoryGenerationService.cs index 105c940..09234b9 100644 --- a/GermanApp/Application/Services/StoryGenerationService.cs +++ b/GermanApp/Application/Services/StoryGenerationService.cs @@ -344,21 +344,58 @@ public class StoryGenerationService private string BuildStoryPrompt(int levelId, string theme, IReadOnlyList vocabulary, int segmentCount) { var levelCode = GetLevelCode(levelId); - var vocabularyString = string.Join(", ", vocabulary.Take(20)); // Limit to first 20 words + var vocabularyString = string.Join(", ", vocabulary.Take(20)); if (vocabulary.Count > 20) { vocabularyString += ", ..."; } + var requirements = GetStoryRequirements(levelCode); return $@"Write a {segmentCount}-part continuous story for a German {levelCode} learner. The story theme is: {theme}. Include these German words and phrases: {vocabularyString}. -Use only {levelCode} level vocabulary and grammar. -Write each part as a separate paragraph. -Make the story engaging and appropriate for language learners."; + +REQUIREMENTS: +{requirements}"; } + private string GetStoryRequirements(string levelCode) => levelCode switch + { + "A1" => "- Use ONLY A1-level vocabulary and grammar\n" + + "- Use simple present tense (ich bin, ich habe, ich gehe)\n" + + "- Basic sentence structure: Subject-Verb-Object\n" + + "- Vocabulary: max 500 words, sentence length: max 10 words\n" + + "- Each part: 2-3 short sentences\n" + + "- Do NOT use: subjunctive, genitive, complex prepositions", + "A2" => "- Use ONLY A2-level vocabulary and grammar\n" + + "- Use present, past (Perfekt), future tense\n" + + "- Can use subordinate clauses with weil, dass, wenn\n" + + "- Vocabulary: 500-1000 words, sentence length: max 15 words\n" + + "- Each part: 3-4 sentences\n" + + "- Do NOT use: Konjunktiv II, Passiv, complex noun compounds", + "B1" => "- Use ONLY B1-level vocabulary and grammar\n" + + "- Use all tenses: present, past (Praeteritum/Perfekt), future\n" + + "- Use modal verbs: koennen, muessen, duerfen, sollen, wollen, moegen\n" + + "- Sentence structure: complex sentences with subordinate clauses\n" + + "- Vocabulary: 1000-2000 words, sentence length: max 20 words\n" + + "- Each part: 4-5 sentences", + "B2" => "- Use ONLY B2-level vocabulary and grammar\n" + + "- Use nuanced tenses: Present, Praeteritum, Perfekt, Plusquamperfekt, Future I/II\n" + + "- Use all cases: Nominativ, Akkusativ, Dativ, Genitiv\n" + + "- Use Konjunktiv I (indirect speech) and Konjunktiv II\n" + + "- Use Passiv where appropriate\n" + + "- Vocabulary: 2000-3000 words, sentence length: max 25 words\n" + + "- Each part: 5-6 sentences", + "C1" => "- Use C1-level vocabulary and grammar with precision\n" + + "- Use all tenses and moods: Indikativ, Konjunktiv I/II, Passiv in all forms\n" + + "- Sentence structure: highly complex with nested relative clauses\n" + + "- Use sophisticated vocabulary including idiomatic expressions\n" + + "- Vocabulary: 3000-5000 words, sentence length: can exceed 25 words\n" + + "- Each part: 6-8 sentences", + _ => "- Use only " + levelCode + " level vocabulary and grammar" + }; + /// /// Builds a prompt for a single story segment. /// @@ -375,13 +412,44 @@ Make the story engaging and appropriate for language learners."; vocabularyString += ", ..."; } - return $@"Write a short story segment (5-8 sentences) for a German {levelCode} learner. + var sentenceCount = levelCode switch { "A1" => "3-5", "A2" => "4-6", "B1" => "5-7", "B2" => "6-8", "C1" => "7-10", _ => "5-8" }; + var requirements = GetSegmentRequirements(levelCode); + return $@"Write a short story segment ({sentenceCount} sentences) for a German {levelCode} learner. The theme is: {theme}. Include these German words: {vocabularyString}. -Use only {levelCode} level vocabulary and grammar. -Make it engaging and appropriate for language learners."; + +REQUIREMENTS: +{requirements}"; } + private string GetSegmentRequirements(string levelCode) => levelCode switch + { + "A1" => "- Use ONLY A1-level vocabulary and grammar\n" + + "- Use simple present tense only (ich bin, ich habe, ich gehe)\n" + + "- Basic sentence structure: Subject-Verb-Object\n" + + "- Sentence length: max 8 words\n" + + "- Do NOT use: subjunctive, genitive, complex prepositions", + "A2" => "- Use ONLY A2-level vocabulary and grammar\n" + + "- Use present, past (Perfekt), future tense\n" + + "- Can use simple subordinate clauses with weil, dass, wenn\n" + + "- Sentence length: max 12 words", + "B1" => "- Use ONLY B1-level vocabulary and grammar\n" + + "- Use present, past (Praeteritum/Perfekt), future tense\n" + + "- Use modal verbs: koennen, muessen, duerfen, sollen, wollen\n" + + "- Sentence structure: complex sentences with subordinate clauses\n" + + "- Sentence length: max 20 words", + "B2" => "- Use ONLY B2-level vocabulary and grammar\n" + + "- Use nuanced tenses including Plusquamperfekt and Konjunktiv II\n" + + "- Use all cases: Nominativ, Akkusativ, Dativ, Genitiv\n" + + "- Use Passiv where appropriate\n" + + "- Sentence length: max 25 words", + "C1" => "- Use C1-level vocabulary and grammar with precision\n" + + "- Use all tenses, moods, and cases correctly\n" + + "- Use sophisticated vocabulary including idiomatic expressions\n" + + "- Sentence structure: complex nested sentences with multiple clauses", + _ => "- Use only " + levelCode + " level vocabulary and grammar" + }; + /// /// Splits a full story text into segments. /// diff --git a/docs/features/story-integration.md b/docs/features/story-integration.md index a63d4a9..b9bdd38 100644 --- a/docs/features/story-integration.md +++ b/docs/features/story-integration.md @@ -1,7 +1,7 @@ # Feature: Story Integration > **Status**: 🚀 In Progress -> **📊 Current Progress**: Phase 1-3 ✅ Complete (Database & Models, Backend Services, Unit Tests), Phase 4 Next (AI Integration) +> **📊 Current Progress**: Phase 1-4 ✅ Complete (Database & Models, Backend Services, Unit Tests, AI Integration), Phase 5 Next (Audio Generation) > **Priority**: High > **Complexity**: High > **Estimate**: 8-12 hours @@ -157,13 +157,13 @@ Order: 1 - [x] Write unit tests for StoryProgressRepository (Tests/Unit/Infrastructure/Data/Repositories/StoryProgressRepositoryTests.cs) - [x] All 324 tests pass -### Phase 4: AI Integration (2-3 hours) -- [ ] Configure Mistral-Medium API client -- [ ] Create prompt templates for each level -- [ ] Implement vocabulary extraction from lessons -- [ ] Implement story text segmentation -- [ ] Handle AI API errors gracefully -- [ ] Add retry logic for failed generations +### Phase 4: AI Integration (2-3 hours) - COMPLETE +- [x] Configure Mistral-Medium API client (MistralConfig with retry, rate limiting, circuit breaker) +- [x] Create prompt templates for each level (A1-C1 with CEFR-specific requirements) +- [x] Implement vocabulary extraction from lessons (ExtractVocabularyFromLessons) +- [x] Implement story text segmentation (SplitStoryIntoSegments, AdjustSegmentCount) +- [x] Handle AI API errors gracefully (AiServiceException with error codes) +- [x] Add retry logic for failed generations (ExecuteWithRetryAsync with exponential backoff) ### Phase 5: Audio Generation (2 hours) - [ ] Integrate with Coqui TTS service @@ -193,7 +193,7 @@ Order: 1 | Database & Models | June 13, 2025 | ✅ | | Backend Services | June 13, 2025 | ✅ | | Unit Tests | June 13, 2025 | ✅ | -| AI Integration | - | ⏳ | +| AI Integration | June 13, 2025 | ✅ | | Audio Generation | - | ⏳ | | Frontend Integration | - | ⏳ | | User Progress | - | ⏳ | @@ -416,6 +416,7 @@ Make the story engaging and suitable for adult learners. | May 31, 2025 | Created | Initial plan based on application-plan.md | | June 13, 2025 | Phase 1-2 Complete | Database & Models, Backend Services implemented | | June 13, 2025 | Phase 3 Complete | All unit tests written and passing (324 tests) | +| June 13, 2025 | Phase 4 Complete | Enhanced prompts with CEFR-level-specific requirements for story generation | ---