Skip to content

Commit

Permalink
Add chat history window
Browse files Browse the repository at this point in the history
  • Loading branch information
justinyoo committed Sep 12, 2024
1 parent f09ce1a commit 05ea86b
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

Welcome to your new app.

<ChatWindowComponent @rendermode="InteractiveServer"/>
<OldChatWindowComponent @rendermode="InteractiveServer"/>
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,52 @@
<FluentGridItem Class="chat-grid" xs="12" sm="12" md="8" lg="8" xl="8" xxl="8" Style="height: 900px;">
<FluentStack Style="width: 100%; height: 100%;" Orientation="Orientation.Vertical" VerticalAlignment="VerticalAlignment.Bottom">
<FluentStack Style="width: 100%;" Orientation="Orientation.Vertical" VerticalAlignment="VerticalAlignment.Top">
Chat History
@if (this.messages != null && this.messages.Any())
{
foreach (var message in this.messages)
{
<FluentStack Style="width: 70%;" Orientation="Orientation.Horizontal" HorizontalAlignment="@(message.Role == MessageRole.Assistant ? HorizontalAlignment.Start : HorizontalAlignment.End)">
<FluentCard MinimalStyle="true">
<p>@message.Message</p>
</FluentCard>
</FluentStack>
}
}
</FluentStack>
<FluentStack Style="width: 100%;" Orientation="Orientation.Vertical" VerticalAlignment="VerticalAlignment.Bottom">
<FluentTextArea Style="width: 100%;" Rows="6" Placeholder="Type user query here. (Shift + Enter for new line)" @bind-Value=value1></FluentTextArea>
<FluentStack Style="width: 100%;" Orientation="Orientation.Horizontal" HorizontalAlignment="HorizontalAlignment.Start">
<FluentStack Style="width: 50%;" Orientation="Orientation.Horizontal" HorizontalAlignment="HorizontalAlignment.Start">
<FluentButton IconStart="@(new Icons.Regular.Size20.Broom())" OnClick="ClearChat">Clear</FluentButton>
</FluentStack>
<FluentStack Style="width: 50%;" Orientation="Orientation.Horizontal" HorizontalAlignment="HorizontalAlignment.End">
<FluentButton IconStart="@(new Icons.Regular.Size20.Send())" OnClick="CompleteChat">Send</FluentButton>
</FluentStack>
</FluentStack>
<ChatWindowComponent Id="chat-window" OnPromptSent="SendPrompt" />
</FluentStack>
</FluentStack>
</FluentGridItem>
</FluentGrid>
</FluentLayout>

@code {
private string? value1;
private List<ChatMessage>? messages;

private async Task CompleteChat()
protected override async Task OnInitializedAsync()
{
this.messages = [];

await Task.CompletedTask;
}
private async Task ClearChat()

private async Task SendPrompt(string prompt)
{
this.messages!.Add(new ChatMessage() { Role = MessageRole.User, Message = prompt });

await Task.CompletedTask;
}

public class ChatMessage
{
public MessageRole? Role { get; set; }
public string? Message { get; set; }
}

public enum MessageRole
{
User,
Assistant
}
}
Original file line number Diff line number Diff line change
@@ -1,72 +1,39 @@
@using AzureOpenAIProxy.PlaygroundApp.Clients
@inject IOpenAIApiClient Api
<FluentLayout Id="@Id">
<FluentTextArea Style="width: 100%;" Rows="6" Placeholder="Type user query here. (Shift + Enter for new line)" @bind-Value="prompt" @onchange="UpdatePrompt"></FluentTextArea>
<FluentStack Style="width: 100%;" Orientation="Orientation.Horizontal" HorizontalAlignment="HorizontalAlignment.Start">
<FluentStack Style="width: 50%;" Orientation="Orientation.Horizontal" HorizontalAlignment="HorizontalAlignment.Start">
<FluentButton IconStart="@(new Icons.Regular.Size20.Broom())" OnClick="ClearChat">Clear</FluentButton>
</FluentStack>
<FluentStack Style="width: 50%;" Orientation="Orientation.Horizontal" HorizontalAlignment="HorizontalAlignment.End">
<FluentButton IconStart="@(new Icons.Regular.Size20.Send())" OnClick="CompleteChat">Send</FluentButton>
</FluentStack>
</FluentStack>
</FluentLayout>

<div class="container">
<h3>Chat Window</h3>

<div class="row">
<div class="col-md-6">
<textarea class="form-control" readonly>@userPrompt</textarea>
</div>
<div class="col-md-6">
<textarea class="form-control" readonly>@assistantMessage</textarea>
</div>
</div>
@code {
private string? prompt;

<div class="row mt-3">
<div class="col-md-6">
<textarea class="form-control" @bind="@userInput"></textarea>
</div>
<div class="col-md-6">
<button class="btn btn-primary" @onclick="SendAsync">Send</button>
<button class="btn btn-secondary" @onclick="ClearAsync">Clear</button>
</div>
</div>
</div>
[Parameter]
public string? Id { get; set; }

@code {
private string? endpoint = "https://localhost:7001/";
private string? apiKey = "abcdef";
private string? deploymentName = "model-gpt35turbo16k-0613";
private int? maxTokens = 4096;
private float? temperature = 0.7f;
private string? systemPrompt = "You are an AI assistant that helps people find information.";
private string? userPrompt;
private string? assistantMessage;
private string? userInput;
[Parameter]
public EventCallback<string?> OnPromptSent { get; set; }

private async Task SendAsync()
private async Task UpdatePrompt(ChangeEventArgs e)
{
userPrompt = userInput;

try
{
var options = new OpenAIApiClientOptions()
{
Endpoint = endpoint,
ApiKey = apiKey,
DeploymentName = deploymentName,
MaxTokens = maxTokens,
Temperature = temperature,
SystemPrompt = systemPrompt,
UserPrompt = userInput,
};
var result = await Api.CompleteChatAsync(options);
assistantMessage = result;
this.prompt = e.Value!.ToString();

}
catch (Exception ex)
{
assistantMessage = ex.Message;
}
await Task.CompletedTask;
}

private async Task ClearAsync()
private async Task CompleteChat()
{
userPrompt = string.Empty;
assistantMessage = string.Empty;
userInput = string.Empty;
await this.OnPromptSent.InvokeAsync(this.prompt);

await Task.CompletedTask;
}
}
private async Task ClearChat()
{
await Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
@using AzureOpenAIProxy.PlaygroundApp.Clients
@inject IOpenAIApiClient Api

<div class="container">
<h3>Chat Window</h3>

<div class="row">
<div class="col-md-6">
<textarea class="form-control" readonly>@userPrompt</textarea>
</div>
<div class="col-md-6">
<textarea class="form-control" readonly>@assistantMessage</textarea>
</div>
</div>

<div class="row mt-3">
<div class="col-md-6">
<textarea class="form-control" @bind="@userInput"></textarea>
</div>
<div class="col-md-6">
<button class="btn btn-primary" @onclick="SendAsync">Send</button>
<button class="btn btn-secondary" @onclick="ClearAsync">Clear</button>
</div>
</div>
</div>

@code {
private string? endpoint = "https://localhost:7001/";
private string? apiKey = "abcdef";
private string? deploymentName = "model-gpt35turbo16k-0613";
private int? maxTokens = 4096;
private float? temperature = 0.7f;
private string? systemPrompt = "You are an AI assistant that helps people find information.";
private string? userPrompt;
private string? assistantMessage;
private string? userInput;

private async Task SendAsync()
{
userPrompt = userInput;

try
{
var options = new OpenAIApiClientOptions()
{
Endpoint = endpoint,
ApiKey = apiKey,
DeploymentName = deploymentName,
MaxTokens = maxTokens,
Temperature = temperature,
SystemPrompt = systemPrompt,
UserPrompt = userInput,
};
var result = await Api.CompleteChatAsync(options);
assistantMessage = result;

}
catch (Exception ex)
{
assistantMessage = ex.Message;
}
}

private async Task ClearAsync()
{
userPrompt = string.Empty;
assistantMessage = string.Empty;
userInput = string.Empty;

await Task.CompletedTask;
}
}

0 comments on commit 05ea86b

Please sign in to comment.