-
Notifications
You must be signed in to change notification settings - Fork 581
/
Copy pathHome.razor
46 lines (37 loc) · 1.41 KB
/
Home.razor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@page "/"
@using OpenAI
@using OpenAI.Chat
@inject OpenAIClient aiClient
@inject ILogger<Home> logger
@inject IConfiguration configuration
<div class="storybox" style="margin: 25%">
@foreach (var message in chatMessages.OfType<AssistantChatMessage>())
{
<p style="font-size: 3em;">@message.Content[0].Text</p>
}
<button @onclick="GenerateNextParagraph" autofocus>Generate</button>
</div>
@code {
private ChatClient? chatClient = null;
private List<ChatMessage> chatMessages = new List<ChatMessage>();
private async Task GenerateNextParagraph()
{
if (chatClient == null)
{
chatClient = aiClient.GetChatClient(configuration["OpenAI:DeploymentName"] ?? throw new ApplicationException("No OpenAI:DeploymentName."));
chatMessages.Add(new SystemChatMessage("Pick a random topic and write a sentence of a fictional story about it."));
}
if (chatMessages.Count > 1)
{
chatMessages.Add(new UserChatMessage("Write the next sentence in the story."));
}
var result = await chatClient.CompleteChatAsync(chatMessages);
chatMessages.Add(new AssistantChatMessage(result.Value.Content[0].Text));
this.StateHasChanged();
}
protected override async Task OnInitializedAsync()
{
await GenerateNextParagraph();
await base.OnInitializedAsync();
}
}