mirror of
https://github.com/tuxdotrs/highminded.git
synced 2025-08-23 08:01:03 +05:30
feat: initial commit
This commit is contained in:
14
ui/controls/ChatUserControl.axaml
Normal file
14
ui/controls/ChatUserControl.axaml
Normal file
@@ -0,0 +1,14 @@
|
||||
<UserControl xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="highminded.ui.controls.ChatUserControl">
|
||||
|
||||
<Grid RowDefinitions="*,Auto" Margin="15" RowSpacing="10">
|
||||
<HtmlPanel Grid.Row="0" Name="ResultBlock" BaseStylesheet="* { font: Inter; color: white; }" />
|
||||
<TextBox Grid.Row="1" Name="PromptBox" CornerRadius="5" TextWrapping="Wrap"
|
||||
KeyDown="PromptBox_OnKeyDown" />
|
||||
</Grid>
|
||||
|
||||
</UserControl>
|
67
ui/controls/ChatUserControl.axaml.cs
Normal file
67
ui/controls/ChatUserControl.axaml.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using Avalonia.Controls;
|
||||
using System;
|
||||
using System.ClientModel;
|
||||
using System.Text;
|
||||
using Avalonia.Input;
|
||||
using highminded.utils;
|
||||
using OpenAI.Chat;
|
||||
using OpenAI;
|
||||
using Markdig;
|
||||
using Markdown.ColorCode;
|
||||
|
||||
namespace highminded.ui.controls;
|
||||
|
||||
public partial class ChatUserControl : UserControl
|
||||
{
|
||||
|
||||
// OpenAI
|
||||
private readonly OpenAI.Chat.ChatClient _client = null!;
|
||||
private readonly MarkdownPipeline _pipeline = null!;
|
||||
|
||||
public ChatUserControl()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
_client = new ChatClient(
|
||||
model: InMemoryDb.Obj.settingsManager.Settings.Model,
|
||||
credential: new ApiKeyCredential(InMemoryDb.Obj.settingsManager.Settings.ApiKey),
|
||||
options: new OpenAIClientOptions
|
||||
{
|
||||
Endpoint = new Uri(InMemoryDb.Obj.settingsManager.Settings.ApiURL)
|
||||
});
|
||||
|
||||
_pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().UseColorCode().Build();
|
||||
}
|
||||
|
||||
private async void PromptBox_OnKeyDown(object? sender, KeyEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (e.Key != Key.Enter) return;
|
||||
|
||||
var prompt = PromptBox.Text;
|
||||
if (prompt is null) return;
|
||||
PromptBox.Clear();
|
||||
|
||||
AsyncCollectionResult<StreamingChatCompletionUpdate> completionUpdates =
|
||||
_client.CompleteChatStreamingAsync(prompt);
|
||||
|
||||
var responseBuilder = new StringBuilder();
|
||||
|
||||
await foreach (var completionUpdate in completionUpdates)
|
||||
{
|
||||
if (completionUpdate.ContentUpdate.Count <= 0) continue;
|
||||
|
||||
var token = completionUpdate.ContentUpdate[0].Text;
|
||||
responseBuilder.Append(token);
|
||||
|
||||
var html = Markdig.Markdown.ToHtml(responseBuilder.ToString(), _pipeline);
|
||||
ResultBlock.Text = html;
|
||||
}
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
ResultBlock.Text = err.Message;
|
||||
}
|
||||
}
|
||||
}
|
30
ui/controls/SettingsUserControl.axaml
Normal file
30
ui/controls/SettingsUserControl.axaml
Normal file
@@ -0,0 +1,30 @@
|
||||
<UserControl xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="highminded.ui.controls.SettingsUserControl">
|
||||
|
||||
<Grid Margin="15" RowSpacing="10" ColumnSpacing="10" RowDefinitions="Auto,Auto,Auto" ColumnDefinitions="*,*">
|
||||
|
||||
<StackPanel Grid.Row="0" Grid.Column="0">
|
||||
<TextBlock Text="Model" />
|
||||
<TextBox Name="ModelTextBox" />
|
||||
</StackPanel>
|
||||
|
||||
<StackPanel Grid.Row="0" Grid.Column="1">
|
||||
<TextBlock Text="API URL" />
|
||||
<TextBox Name="ApiUrlTextBox" />
|
||||
</StackPanel>
|
||||
|
||||
<StackPanel Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2">
|
||||
<TextBlock Text="API Key" />
|
||||
<TextBox Name="ApiKeyTextBox" PasswordChar="*" />
|
||||
</StackPanel>
|
||||
|
||||
<StackPanel Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2">
|
||||
<Button Name="SaveSettingsBtn" Content="Save" Click="SaveSettingsBtn_OnClick"/>
|
||||
</StackPanel>
|
||||
|
||||
</Grid>
|
||||
</UserControl>
|
30
ui/controls/SettingsUserControl.axaml.cs
Normal file
30
ui/controls/SettingsUserControl.axaml.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using highminded.utils;
|
||||
|
||||
namespace highminded.ui.controls;
|
||||
|
||||
public partial class SettingsUserControl : UserControl
|
||||
{
|
||||
public SettingsUserControl()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
ModelTextBox.Text = InMemoryDb.Obj.settingsManager.Settings.Model;
|
||||
ApiUrlTextBox.Text = InMemoryDb.Obj.settingsManager.Settings.ApiURL;
|
||||
ApiKeyTextBox.Text = InMemoryDb.Obj.settingsManager.Settings.ApiKey;
|
||||
}
|
||||
|
||||
private void SaveSettingsBtn_OnClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
InMemoryDb.Obj.settingsManager.Settings.Model = ModelTextBox.Text;
|
||||
InMemoryDb.Obj.settingsManager.Settings.ApiURL= ApiUrlTextBox.Text;
|
||||
InMemoryDb.Obj.settingsManager.Settings.ApiKey = ApiKeyTextBox.Text;
|
||||
InMemoryDb.Obj.settingsManager.Save();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user