Skip to content

Commit

Permalink
+ getting ai sample
Browse files Browse the repository at this point in the history
  • Loading branch information
NicklausBrain committed Jul 31, 2024
1 parent b8a827a commit 850de06
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 32 deletions.
46 changes: 33 additions & 13 deletions My1kWordsEe/Components/Layout/Sample.razor
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
@code {
[Parameter]
public string Ee { get; set; }
@using My1kWordsEe.Services;
@using My1kWordsEe.Models;
@using CSharpFunctionalExtensions;

@inject OpenAiService OpenAiService

@code {
[Parameter]
public string En { get; set; }
public string EeWord { get; set; }

private Result<Sentence>? sentence;

protected override async Task OnInitializedAsync()
{
sentence = await OpenAiService.GetSampleSentence(EeWord);
}
}

<div>
<div class="row">

<ul class="list-group">
<li class="list-group-item">
@Ee
</li>
<li class="list-group-item">
@En
</li>
</ul>
@if (sentence == null)
{
<p><em>Loading...</em></p>
}
else if (sentence.Value.IsSuccess)
{
<ul class="list-group">
<li class="list-group-item">
@sentence.Value.Value.Ee
</li>
<li class="list-group-item">
@sentence.Value.Value.En
</li>
</ul>
}
else
{
<p><em>@sentence.Value.Error</em></p>
}

</div>
</div>
8 changes: 7 additions & 1 deletion My1kWordsEe/Components/Pages/Words.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

@using My1kWordsEe.Services;
@using My1kWordsEe.Models;
@inject OpenAiService OpenAiService
@using My1kWordsEe.Components.Layout;

@inject OpenAiService OpenAiService

@code {
public Ee1kWords Ee1KWords { get; private set; } = new Ee1kWords();
Expand Down Expand Up @@ -44,6 +45,11 @@
@onclick="@(e => SelectWord(e, word))">@word</li>
</div>
<div class="col">
@if (word == Ee1KWords.SelectedWord)
{
<Sample EeWord="@word">
</Sample>
}
</div>
</div>
}
Expand Down
3 changes: 2 additions & 1 deletion My1kWordsEe/My1kWordsEe.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CSharpFunctionalExtensions" Version="2.42.5" />
<PackageReference Include="OpenAI" Version="2.0.0-beta.1" />
</ItemGroup>
</Project>
35 changes: 18 additions & 17 deletions My1kWordsEe/Services/OpenAiService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using OpenAI.Chat;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using CSharpFunctionalExtensions;
using OpenAI.Chat;

namespace My1kWordsEe.Services
{
Expand All @@ -16,7 +14,7 @@ public OpenAiService(string apiKey)

private string ApiKey { get; }

public async Task<SentenceResult> GetSampleSentence(string word)
public async Task<Result<Sentence>> GetSampleSentence(string word)
{
ChatClient client = new(model: "gpt-4o-mini", ApiKey);

Expand All @@ -30,28 +28,31 @@ public async Task<SentenceResult> GetSampleSentence(string word)
"Your task is to provide a sample sentence using this word, " +
"preferably in combination with some of the other 1000 most common Estonian words. " +
"Your output is JSON object with the sample sentence in Estonian and its respective English translation:\n" +
"```\n{\n \"ee_sentence\": \"<sample in Estonian>\",\n \"en_sentence\": \"<sample in English>\"\n}\n```"),
"```\n" +
"{\"ee_sentence\": \"<sample in Estonian>\", \"en_sentence\": \"<sample in English>\" }" +
"\n```"),
new UserChatMessage(word),
]);

foreach (var c in chatCompletion.Content)
{
var sentence = JsonSerializer.Deserialize<Sentence>(c.Text);
return new SentenceResult { Sentence = sentence };
var jsonStr = c.Text.Trim('`', ' ', '\'', '"');
var sentence = JsonSerializer.Deserialize<Sentence>(jsonStr);
if (sentence == null)
{
break;
}
else
{
return Result.Success(sentence);
}
}

return new SentenceResult { Error = "No response found" };
return Result.Failure<Sentence>("Empty response");
}
}
}

public class SentenceResult
{
public Sentence? Sentence { get; set; }

public string Error { get; set; }
}

public class Sentence
{
[JsonPropertyName("ee_sentence")]
Expand Down

0 comments on commit 850de06

Please sign in to comment.