From 7f2e7754fcd4f49ed575c802e9398cc242316742 Mon Sep 17 00:00:00 2001 From: herring101 <81513223+herring101@users.noreply.github.com> Date: Wed, 1 May 2024 22:45:27 +0900 Subject: [PATCH 1/3] story generator --- .../Services/ILlmStoryGeneratorService.cs | 8 +++ .../Services/ClaudeStoryGeneratorService.cs | 68 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 KoeBook.Core/Contracts/Services/ILlmStoryGeneratorService.cs create mode 100644 KoeBook.Core/Services/ClaudeStoryGeneratorService.cs diff --git a/KoeBook.Core/Contracts/Services/ILlmStoryGeneratorService.cs b/KoeBook.Core/Contracts/Services/ILlmStoryGeneratorService.cs new file mode 100644 index 0000000..ea1ea9d --- /dev/null +++ b/KoeBook.Core/Contracts/Services/ILlmStoryGeneratorService.cs @@ -0,0 +1,8 @@ +using KoeBook.Core.Models; + +namespace KoeBook.Core.Contracts.Services; + +internal interface ILlmStoryGeneratorService +{ + ValueTask GenerateStoryAsync(StoryGenre storyGenre, string premise, CancellationToken cancellationToken); +} diff --git a/KoeBook.Core/Services/ClaudeStoryGeneratorService.cs b/KoeBook.Core/Services/ClaudeStoryGeneratorService.cs new file mode 100644 index 0000000..b35525b --- /dev/null +++ b/KoeBook.Core/Services/ClaudeStoryGeneratorService.cs @@ -0,0 +1,68 @@ +using KoeBook.Core.Contracts.Services; +using KoeBook.Core.Models; + +namespace KoeBook.Core.Services; + +internal class ClaudeStoryGeneratorService(IClaudeService claudeService) : ILlmStoryGeneratorService +{ + private readonly IClaudeService _claudeService = claudeService; + + public async ValueTask GenerateStoryAsync(StoryGenre storyGenre, string premise, CancellationToken cancellationToken) + { + if (_claudeService.Messages is null) + { + throw new EbookException(ExceptionType.ApiKeyNotSet); + } + try + { + var storyXml = await _claudeService.Messages.CreateAsync(new() + { + Model = Claudia.Models.Claude3Opus, + MaxTokens = 4000, + Messages = [new() + { + Role = "user", + Content = CreateStoryPrompt(storyGenre, premise) + }] + }, + cancellationToken: cancellationToken + ); + return storyXml.ToString(); + } + catch (OperationCanceledException) { throw; } + catch (Exception e) + { + throw new EbookException(ExceptionType.ClaudeTalkerAndStyleSettingFailed, innerException: e); + } + } + + private string CreateStoryPrompt(StoryGenre storyGenre, string premise) + { + return $""" + You are a highly capable AI novelist that can write compelling 2500-character short stories and novellas in fluent, natural Japanese based on a given theme or plot points. + + When crafting the story, please focus on the following: + - Use dialogue extensively to advance the plot while revealing characters' personalities, motivations and relationships + - Write witty, revealing conversations authentic to each character's voice + - Vary dialogue tags and phrases to convey nuanced speech + - Aim to create a polished story where characters' distinct voices come through in the dialogue, within the 2500-character limit + - Words beyond the general vocabulary and proper nouns that are easily misread are given ruby. Not necessary for simple vocabulary. + + Please generate the full story in Japanese in a single output follow this example: + + + 境界線の向こう側 + +
+ 高校2年の夏、バレー部のエースで端正たんせいな顔立ちの山田祐樹は、バスケ部のキャプテンで凛とした佇まいの田中麻衣に密かに想いを寄せていた。しかし、両者の部活仲間たちの目をはばかり、互いに素振りも見せずにいた。 +
+
+
+ + Based on this prompt, please generate a ~2500-character Japanese story from the specified theme or plot points provided. + + theme: {storyGenre.Genre} + premise: {premise} + """; + } +} From ce2f066f1e710b64cbdb35fdf5c157496482120a Mon Sep 17 00:00:00 2001 From: miyaji255 <84168445+miyaji255@users.noreply.github.com> Date: Wed, 1 May 2024 22:53:10 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=E3=82=A4=E3=83=B3=E3=83=87=E3=83=B3?= =?UTF-8?q?=E3=83=88=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- KoeBook.Core/Services/ClaudeStoryGeneratorService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/KoeBook.Core/Services/ClaudeStoryGeneratorService.cs b/KoeBook.Core/Services/ClaudeStoryGeneratorService.cs index b35525b..4cc1f71 100644 --- a/KoeBook.Core/Services/ClaudeStoryGeneratorService.cs +++ b/KoeBook.Core/Services/ClaudeStoryGeneratorService.cs @@ -39,7 +39,7 @@ public async ValueTask GenerateStoryAsync(StoryGenre storyGenre, string private string CreateStoryPrompt(StoryGenre storyGenre, string premise) { return $""" - You are a highly capable AI novelist that can write compelling 2500-character short stories and novellas in fluent, natural Japanese based on a given theme or plot points. + You are a highly capable AI novelist that can write compelling 2500-character short stories and novellas in fluent, natural Japanese based on a given theme or plot points. When crafting the story, please focus on the following: - Use dialogue extensively to advance the plot while revealing characters' personalities, motivations and relationships From a38fb79fe8c864e161298b5e0e486cf3cabca380 Mon Sep 17 00:00:00 2001 From: herring101 <81513223+herring101@users.noreply.github.com> Date: Wed, 1 May 2024 22:54:10 +0900 Subject: [PATCH 3/3] ChangeTemeprature --- KoeBook.Core/Services/ClaudeStoryGeneratorService.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/KoeBook.Core/Services/ClaudeStoryGeneratorService.cs b/KoeBook.Core/Services/ClaudeStoryGeneratorService.cs index b35525b..57eea94 100644 --- a/KoeBook.Core/Services/ClaudeStoryGeneratorService.cs +++ b/KoeBook.Core/Services/ClaudeStoryGeneratorService.cs @@ -19,6 +19,7 @@ public async ValueTask GenerateStoryAsync(StoryGenre storyGenre, string { Model = Claudia.Models.Claude3Opus, MaxTokens = 4000, + Temperature = 0.4, Messages = [new() { Role = "user",