Skip to content

Commit

Permalink
added sample generation context (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
NicklausBrain authored Dec 5, 2024
1 parent fcea7aa commit 92b3dbe
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
6 changes: 6 additions & 0 deletions My1kWordsEe/Models/SampleWord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public string[] EnWords
/// </summary>
public required string EnExplanation { get; init; }


/// <summary>
/// Explaining the word in Estonian
/// </summary>
public string? EeExplanation { get; init; }

/// <summary>
/// Sample pronunciation of the word
/// </summary>
Expand Down
17 changes: 11 additions & 6 deletions My1kWordsEe/Services/Ai/OpenAiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@ public static async Task<Result<SampleWord>> GetWordMetadata(this OpenAiClient o
"Teie väljund on sõna metaandmed JSON-is vastavalt antud lepingule:\n" +
"```\n{\n" +
"\"ee_word\": \"<antud sõna>\",\n" +
"\"en_word\": \"<english translation>\"\n" +
"\"en_words\": [<array of alternative english translations if applicable>]\n" +
"\"en_explanation\": \"<explanation of the word meaning in english>\"\n" +
"\"en_word\": \"<english translation>\",\n" +
"\"en_words\": [<array of alternative english translations if applicable>],\n" +
"\"en_explanation\": \"<explanation of the word meaning in english>\",\n" +
"\"ee_explanation\": \"<sõna tähenduse seletus eesti keeles>\"\n" +
"}\n```\n";

var response = await openAiClient.CompleteAsync(prompt, eeWord, new ChatCompletionOptions
Expand Down Expand Up @@ -158,14 +159,15 @@ public static async Task<Result<SampleWord>> GetWordMetadata(this OpenAiClient o
EnWord = wordMetadata.EnWord,
EnWords = wordMetadata.EnWords,
EnExplanation = wordMetadata.EnExplanation,
EeExplanation = wordMetadata.EeExplanation,
});
}

public static async Task<Result<Sentence>> GetSampleSentence(this OpenAiClient openAiClient, string eeWord, string[]? existingSamples = null)
public static async Task<Result<Sentence>> GetSampleSentence(this OpenAiClient openAiClient, string eeWord, string explanation, string[]? existingSamples = null)
{
var prompt =
"Sa oled keeleõppe süsteemi abiline, mis aitab õppida enim levinud eesti keele sõnu.\n" +
"Sinu sisend on üks sõna eesti keeles.\n" +
"Sinu sisend on üks eestikeelne sõna ja selle rakenduse kontekst: <sõna> (<kontekst>).\n" +
"Sinu ülesanne on kirjutada selle kasutamise kohta lihtne lühike näitelause, kasutades seda sõna.\n" +
"Lauses kasuta kõige levinuimaid ja lihtsamaid sõnu eesti keeles et toetada keeleõpet.\n" +
"Eelistan SVO-lausete sõnajärge, kus esikohal on subjekt (S), seejärel tegusõna (V) ja objekt (O)\n" +
Expand All @@ -178,7 +180,7 @@ public static async Task<Result<Sentence>> GetSampleSentence(this OpenAiClient o
? "PS: Ärge korrake järgmisi näidiseid, olge erinevad:\n" + string.Join(",", existingSamples.Select(s => $"'{s}'"))
: string.Empty);

return await openAiClient.CompleteJsonAsync<Sentence>(prompt, eeWord);
return await openAiClient.CompleteJsonAsync<Sentence>(prompt, $"{eeWord} (${explanation})");
}

private class WordMetadata
Expand All @@ -192,6 +194,9 @@ private class WordMetadata
[JsonPropertyName("en_explanation")]
public required string EnExplanation { get; set; }

[JsonPropertyName("ee_explanation")]
public required string EeExplanation { get; set; }

[JsonPropertyName("en_words")]
public required string[] EnWords { get; set; } = Array.Empty<string>();
}
Expand Down
1 change: 1 addition & 0 deletions My1kWordsEe/Services/Cqs/AddSampleSentenceCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public async Task<Result<SampleWord>> Invoke(SampleWord word)

var sentence = await this.openAiService.GetSampleSentence(
eeWord: word.EeWord,
explanation: word.EeExplanation ?? word.EnExplanation,
existingSamples: word.Samples.Select(s => s.EeSentence).ToArray());
if (sentence.IsFailure)
{
Expand Down

0 comments on commit 92b3dbe

Please sign in to comment.