Skip to content

Commit

Permalink
支持基础的标记计数
Browse files Browse the repository at this point in the history
  • Loading branch information
Richasy committed Jul 11, 2024
1 parent 9cb7670 commit ab4bf18
Show file tree
Hide file tree
Showing 14 changed files with 281 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Desktop/RodelAgent.UI/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<converters:ObjectToVisibilityConverter x:Key="ObjectToVisibilityConverter" />
<converters:ObjectToVisibilityConverter x:Key="ObjectToVisibilityReverseConverter" IsReverse="True" />
<converters:SecondsToTimeConverter x:Key="SecondsToTimeConverter" />
<converters:TokenCountConverter x:Key="TokenCountConverter" />

<SolidColorBrush x:Key="AnthropicColor" Color="#F1F0E8" />
<SolidColorBrush x:Key="AzureOpenAIColor" Color="#FFFFFF" />
Expand Down
3 changes: 3 additions & 0 deletions src/Desktop/RodelAgent.UI/Controls/Chat/ChatSessionInput.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@
Duration="0:0:0.5" />
</animations:Implicit.HideAnimations>
</StackPanel>
<Grid Visibility="{x:Bind ViewModel.IsResponding, Mode=OneWay, Converter={StaticResource BoolToVisibilityReverseConverter}}">
<local:ChatSessionTokenUsageControl HorizontalAlignment="Left" ViewModel="{x:Bind ViewModel, Mode=OneWay}" />
</Grid>
<StackPanel
Grid.Column="1"
Orientation="Horizontal"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:ChatSessionControlBase
x:Class="RodelAgent.UI.Controls.Chat.ChatSessionTokenUsageControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ext="using:RodelAgent.UI.Extensions"
xmlns:local="using:RodelAgent.UI.Controls.Chat"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<UserControl.Resources>
<Style x:Key="LeftTipTextStyle" TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Foreground" Value="{ThemeResource TextFillColorSecondaryBrush}" />
</Style>
<Style x:Key="RightValueTextStyle" TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Right" />
</Style>
</UserControl.Resources>

<Grid>
<Button
Padding="8,4"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
Background="Transparent"
BorderThickness="0">
<TextBlock
HorizontalAlignment="Left"
VerticalAlignment="Center"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Style="{StaticResource CaptionTextBlockStyle}">
<Run Text="{ext:Locale Name=Token}" />
<Run Foreground="{ThemeResource AccentTextFillColorTertiaryBrush}" Text="{x:Bind ViewModel.TotalTokenUsage, Mode=OneWay}" />
</TextBlock>
<Button.Flyout>
<Flyout>
<Flyout.FlyoutPresenterStyle>
<Style BasedOn="{StaticResource DefaultFlyoutPresenterStyle}" TargetType="FlyoutPresenter">
<Setter Property="Padding" Value="20,16" />
</Style>
</Flyout.FlyoutPresenterStyle>
<Grid
MinWidth="180"
ColumnSpacing="12"
RowSpacing="12">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock
HorizontalAlignment="Left"
Style="{StaticResource BodyStrongTextBlockStyle}"
Text="{ext:Locale Name=TokenUsage}" />
<TextBlock
Grid.Row="1"
Style="{StaticResource LeftTipTextStyle}"
Text="{ext:Locale Name=SystemInstruction}" />
<TextBlock
Grid.Row="1"
Grid.Column="1"
Style="{StaticResource RightValueTextStyle}"
Text="{x:Bind ViewModel.SystemTokenCount, Mode=OneWay, Converter={StaticResource TokenCountConverter}}" />

<TextBlock
Grid.Row="2"
Style="{StaticResource LeftTipTextStyle}"
Text="{ext:Locale Name=InputToken}" />
<TextBlock
Grid.Row="2"
Grid.Column="1"
Style="{StaticResource RightValueTextStyle}"
Text="{x:Bind ViewModel.UserInputTokenCount, Mode=OneWay, Converter={StaticResource TokenCountConverter}}" />

<TextBlock
Grid.Row="3"
Style="{StaticResource LeftTipTextStyle}"
Text="{ext:Locale Name=InputWordCount}" />
<TextBlock
Grid.Row="3"
Grid.Column="1"
Style="{StaticResource RightValueTextStyle}"
Text="{x:Bind ViewModel.UserInputWordCount, Mode=OneWay, Converter={StaticResource TokenCountConverter}}" />

<Border
Grid.Row="4"
Grid.ColumnSpan="2"
Height="1"
HorizontalAlignment="Stretch"
Background="{ThemeResource DividerStrokeColorDefaultBrush}" />

<TextBlock
Grid.Row="5"
Style="{StaticResource LeftTipTextStyle}"
Text="{ext:Locale Name=TotalUsage}" />
<TextBlock
Grid.Row="5"
Grid.Column="1"
Style="{StaticResource RightValueTextStyle}"
Text="{x:Bind ViewModel.TotalTokenUsage, Mode=OneWay, Converter={StaticResource TokenCountConverter}}" />

<TextBlock
Grid.Row="6"
Style="{StaticResource LeftTipTextStyle}"
Text="{ext:Locale Name=RemainderUsage}" />
<TextBlock
Grid.Row="6"
Grid.Column="1"
Style="{StaticResource RightValueTextStyle}"
Text="{x:Bind ViewModel.RemainderTokenCount, Mode=OneWay, Converter={StaticResource TokenCountConverter}}" />
</Grid>
</Flyout>
</Button.Flyout>
</Button>
</Grid>
</local:ChatSessionControlBase>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Rodel. All rights reserved.

namespace RodelAgent.UI.Controls.Chat;

/// <summary>
/// 对话会话令牌使用控件.
/// </summary>
public sealed partial class ChatSessionTokenUsageControl : ChatSessionControlBase
{
/// <summary>
/// Initializes a new instance of the <see cref="ChatSessionTokenUsageControl"/> class.
/// </summary>
public ChatSessionTokenUsageControl()
{
InitializeComponent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ private void OnSystemBoxTextChanged(object sender, TextChangedEventArgs e)
}

ViewModel.Data.SystemInstruction = SystemBox.Text;
ViewModel.CalcTotalTokenCountCommand.Execute(default);
_textChanged = true;
}

Expand Down
14 changes: 14 additions & 0 deletions src/Desktop/RodelAgent.UI/Converters/TokenCountConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Rodel. All rights reserved.

namespace RodelAgent.UI.Converters;

internal sealed class TokenCountConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var count = System.Convert.ToInt32(value);
return count < 0 ? "--" : (object)count.ToString("N0");
}

public object ConvertBack(object value, Type targetType, object parameter, string language) => throw new NotImplementedException();
}
1 change: 1 addition & 0 deletions src/Desktop/RodelAgent.UI/Pages/ChatServicePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -281,5 +281,6 @@
IsHide="{x:Bind ViewModel.IsExtraColumnManualHide, Mode=OneWay}" />
</Grid>
</Grid>
<ProgressRing x:Name="InitialRing" Style="{StaticResource PageProgressRingStyle}" />
</Grid>
</local:ChatServicePageBase>
8 changes: 5 additions & 3 deletions src/Desktop/RodelAgent.UI/Pages/ChatServicePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ public ChatServicePage()
/// <inheritdoc/>
protected override async void OnPageLoaded()
{
InitialRing.IsActive = true;
if (ViewModel.IsAvailableServicesEmpty)
{
ViewModel.ResetAvailableChatServicesCommand.Execute(default);
await ViewModel.ResetAvailableChatServicesCommand.ExecuteAsync(default);
}

if (ViewModel.IsAgentsEmpty)
Expand All @@ -37,17 +38,18 @@ protected override async void OnPageLoaded()

if (ViewModel.IsSessionPresetsEmpty)
{
ViewModel.ResetSessionPresetsCommand.Execute(default);
await ViewModel.ResetSessionPresetsCommand.ExecuteAsync(default);
}

if (ViewModel.IsGroupsEmpty)
{
ViewModel.ResetGroupsCommand.Execute(default);
await ViewModel.ResetGroupsCommand.ExecuteAsync(default);
}

InitializeSessionPanelType();
InitializeGroupPanelType();
UpdateExtraSizer();
InitialRing.IsActive = false;
}

private void OnSizeChanged(object sender, SizeChangedEventArgs e)
Expand Down
18 changes: 18 additions & 0 deletions src/Desktop/RodelAgent.UI/Resources/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,12 @@
<data name="ImportPlugin" xml:space="preserve">
<value>Import plugin</value>
</data>
<data name="InputToken" xml:space="preserve">
<value>Input tokens</value>
</data>
<data name="InputWordCount" xml:space="preserve">
<value>Input words</value>
</data>
<data name="InsertImage" xml:space="preserve">
<value>Insert image</value>
</data>
Expand Down Expand Up @@ -874,6 +880,9 @@ You can manually adjust the limit, and the application will capture the specifie
<data name="Region" xml:space="preserve">
<value>Region</value>
</data>
<data name="RemainderUsage" xml:space="preserve">
<value>Remainder</value>
</data>
<data name="RemoveAllSessions" xml:space="preserve">
<value>Remove all sessions</value>
</data>
Expand Down Expand Up @@ -1069,6 +1078,12 @@ You can manually adjust the limit, and the application will capture the specifie
<data name="Tip" xml:space="preserve">
<value>Tip</value>
</data>
<data name="Token" xml:space="preserve">
<value>TOKEN</value>
</data>
<data name="TokenUsage" xml:space="preserve">
<value>Token usage</value>
</data>
<data name="Tools" xml:space="preserve">
<value>Tools</value>
</data>
Expand All @@ -1084,6 +1099,9 @@ You can manually adjust the limit, and the application will capture the specifie
<data name="TopPDescription" xml:space="preserve">
<value>Only consider higher quality token results, for example, at 0.1, the model only considers tokens within the top 10% of probability mass (usually not mixed with sampling temperature)</value>
</data>
<data name="TotalUsage" xml:space="preserve">
<value>Total usage</value>
</data>
<data name="Translate" xml:space="preserve">
<value>Translate</value>
</data>
Expand Down
18 changes: 18 additions & 0 deletions src/Desktop/RodelAgent.UI/Resources/zh-Hans-CN/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,12 @@
<data name="ImportPlugin" xml:space="preserve">
<value>导入插件</value>
</data>
<data name="InputToken" xml:space="preserve">
<value>输入标记</value>
</data>
<data name="InputWordCount" xml:space="preserve">
<value>输入字数</value>
</data>
<data name="InsertImage" xml:space="preserve">
<value>插入图片</value>
</data>
Expand Down Expand Up @@ -874,6 +880,9 @@
<data name="Region" xml:space="preserve">
<value>位置/区域</value>
</data>
<data name="RemainderUsage" xml:space="preserve">
<value>剩余可用</value>
</data>
<data name="RemoveAllSessions" xml:space="preserve">
<value>清除所有会话</value>
</data>
Expand Down Expand Up @@ -1069,6 +1078,12 @@
<data name="Tip" xml:space="preserve">
<value>提醒</value>
</data>
<data name="Token" xml:space="preserve">
<value>标记</value>
</data>
<data name="TokenUsage" xml:space="preserve">
<value>标记记录</value>
</data>
<data name="Tools" xml:space="preserve">
<value>工具</value>
</data>
Expand All @@ -1084,6 +1099,9 @@
<data name="TopPDescription" xml:space="preserve">
<value>只考虑较高质量的标记结果,例如 0.1 时,模型仅考虑包含概率质量前 10% 的标记(通常不与采样温度混用)</value>
</data>
<data name="TotalUsage" xml:space="preserve">
<value>总计使用</value>
</data>
<data name="Translate" xml:space="preserve">
<value>翻译</value>
</data>
Expand Down
5 changes: 5 additions & 0 deletions src/Desktop/RodelAgent.UI/RodelAgent.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<None Remove="Controls\Chat\ChatGroupPanel.xaml" />
<None Remove="Controls\Chat\ChatPluginSection.xaml" />
<None Remove="Controls\Chat\ChatPluginSelection.xaml" />
<None Remove="Controls\Chat\ChatSessionTokenUsageControl.xaml" />
<None Remove="Controls\Chat\GroupAgentsPanel.xaml" />
<None Remove="Controls\Chat\GroupOptionsPanel.xaml" />
<None Remove="Controls\Chat\GroupPresetSettingsDialog.xaml" />
Expand Down Expand Up @@ -234,6 +235,7 @@
<PackageReference Include="NAudio" Version="2.2.1" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.11" />
<PackageReference Include="Roman-Numerals" Version="2.0.0" />
<PackageReference Include="Tiktoken" Version="2.0.3" />
<PackageReference Include="WinUIEx" Version="2.3.4" />
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>
Expand Down Expand Up @@ -627,6 +629,9 @@
<Page Update="Controls\Startup\Chat\DouBaoChatConfigSection.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Update="Controls\Chat\ChatSessionTokenUsageControl.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<None Update="NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Loading

0 comments on commit ab4bf18

Please sign in to comment.