Skip to content

Commit

Permalink
#25 ExtractCharacterListの修正
Browse files Browse the repository at this point in the history
  • Loading branch information
aiueo-1234 committed Apr 28, 2024
1 parent e0804df commit ee25951
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
35 changes: 17 additions & 18 deletions KoeBook.Core/Services/ClaudeAnalyzerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ public async ValueTask<BookScripts> LlmAnalyzeScriptLinesAsync(BookProperties bo
{
var message1 = await _claudeService.Messages.CreateAsync(new()
{
Model = "claude-3-opus-20240229", // you can use Claudia.Models.Claude3Opus string constant
Model = Claudia.Models.Claude3Opus,
MaxTokens = 4000,
Messages = [new()
{
Role = "user",
Content = CreateCharaterGuessPrompt(lineNumberingText)
Content = CreateCharacterGuessPrompt(lineNumberingText)
}]
},
cancellationToken: cancellationToken
);
var characterList = ExtractCharacterList(message1.ToString());
var characterList = ExtractCharacterList(message1.ToString(), scriptLines);

var message2 = await _claudeService.Messages.CreateAsync(new()
{
Model = "claude-3-opus-20240229", // you can use Claudia.Models.Claude3Opus string constant
Model = Claudia.Models.Claude3Opus,
MaxTokens = 4000,
Messages = [new()
{
Expand All @@ -51,9 +51,9 @@ public async ValueTask<BookScripts> LlmAnalyzeScriptLinesAsync(BookProperties bo
return new(bookProperties, new(characterVoiceMapping)) { ScriptLines = scriptLines };
}
catch (OperationCanceledException) { throw; }
catch
catch (Exception e)
{
throw new EbookException(ExceptionType.ClaudeTalkerAndStyleSettingFailed);
throw new EbookException(ExceptionType.ClaudeTalkerAndStyleSettingFailed, innerException: e);
}
}

Expand All @@ -73,7 +73,7 @@ private Dictionary<string, string> ExtractCharacterVoiceMapping(string response,
}).ToDictionary();
}

private static string CreateCharaterGuessPrompt(string lineNumberingText)
private static string CreateCharacterGuessPrompt(string lineNumberingText)
{
return $$"""
{{lineNumberingText}}
Expand Down Expand Up @@ -138,7 +138,7 @@ private static string LineNumbering(List<ScriptLine> scriptLines)
return sb.ToString();
}

private static List<Character> ExtractCharacterList(string response)
private static List<Character> ExtractCharacterList(string response, List<ScriptLine> scriptLines)
{
var characterList = new List<Character>();
var lines = response.Split("\n");
Expand Down Expand Up @@ -169,18 +169,17 @@ private static List<Character> ExtractCharacterList(string response)
}
}

var dic = characterList.Select(x => KeyValuePair.Create(x.Id, x.Name)).ToDictionary();
var lines2 = lines.AsSpan()[(characterListEndIndex + 1)..];

for (var i = 0; i < lines2.Length; i++)
{
var dest = (stackalloc Range[4]);
for (var i = characterListEndIndex + 1; i < lines.Length; i++)
var line = lines2[i].AsSpan().TrimEnd();
line = line[(line.IndexOf(' ') + 2)..];//cまで無視
line = line[..line.IndexOf(' ')];
if (dic.TryGetValue(line.ToString(), out var characterName))
{
var line = lines[i].AsSpan();
if (line.Length > 0 && line.Contains('c'))
{
if (line.Split(dest, ' ') > 3)
throw new EbookException(ExceptionType.ClaudeTalkerAndStyleSettingFailed);
var characterId = line[dest[1]];
var narrationOrDialogue = line[dest[2]];
}
scriptLines[i].Character = characterName;
}
}
return characterList;
Expand Down
2 changes: 2 additions & 0 deletions KoeBook.Core/Services/MyOpenAiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class MyOpenAiService(ISecretSettingsService secretSettingsService, IHttp

public IAudioService Audio => GetOpenAiService()?.Audio!;

public IBatchService Batch => GetOpenAiService()?.Batch!;

public void SetDefaultModelId(string modelId)
{
GetOpenAiService()?.SetDefaultModelId(modelId);
Expand Down

0 comments on commit ee25951

Please sign in to comment.