Skip to content

Commit

Permalink
Run Stream Support (#35)
Browse files Browse the repository at this point in the history
* 🎨 Update ConfigWindow.axaml and ResultsView.axaml

- Added a checkbox for the "RunStream" configuration option in ConfigWindow.axaml.
- Updated the visibility of the TokenUsage and Elapsed labels in ResultsView.axaml.

🔧 Refactor converters and messages, add new service method

Refactor converters and messages to use nullable reference types. Add new method `RunStreamAsync` to the `PromptService` class for running prompts asynchronously.

🔧 Update ConfigAttribute and BaiduConfigViewModel

- Added new options to the ConfigType attribute in ConfigAttribute.cs.
- Added new model endpoints in BaiduConfigViewModel.cs.

🐛 Fix GenerateResult and SemanticFunctionViewModel

- Changed the elapsed property in GenerateResult.cs to be nullable.
- Added a new configuration option for running the stream in SemanticFunctionViewModel.cs.

🔧 Fix binding issue in ResultsView.axaml

- Update binding expression for Elapsed property in ResultsView.axaml to use Elapsed.Value.TotalSeconds instead of Elapsed.TotalSeconds.

This change fixes a bug where the Elapsed property was not correctly displayed in the ResultsView. The binding expression has been updated to use the Value property of the Elapsed object, ensuring that the correct value is displayed.

* 🔀 Update version to 0.6.0

Update the version in Directory.Build.props from 0.5.3 to 0.6.0.

* Due to issue SciSharp/LLamaSharp#382, support for llamaSharp has been temporarily removed.
  • Loading branch information
xbotter authored Feb 18, 2024
1 parent fa98603 commit 69fc300
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 152 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<Nullable>enable</Nullable>
<Version>0.5.3</Version>
<Version>0.6.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down
4 changes: 2 additions & 2 deletions PromptPlayground/Converters/NullToVisibilityConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ namespace PromptPlayground.Converters
{
public class NullToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
return value != null;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
Expand Down
4 changes: 2 additions & 2 deletions PromptPlayground/Converters/StringToBooleanConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace PromptPlayground.Converters
{
public class StringToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value == null || parameter == null || !(value is string) || !(parameter is string))
{
Expand All @@ -23,7 +23,7 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
return strValue.Equals(strParameter, StringComparison.OrdinalIgnoreCase);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@

namespace PromptPlayground.Messages
{
public class ResultCountRequestMessage : RequestMessage<int>
public class ConfigurationRequestMessage : RequestMessage<string>
{
public ConfigurationRequestMessage(string config)
{
Config = config;
}

public string Config { get; }
}
}
14 changes: 7 additions & 7 deletions PromptPlayground/PromptPlayground.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<Platforms>AnyCPU;x64</Platforms>
<NoWarn>SKEXP0001;SKEXP0002;SKEXP0004;SKEXP0050</NoWarn>
</PropertyGroup>
<PropertyGroup Condition="'$(OS)' == 'Windows_NT'">
<DefineConstants>$(DefineConstants);WINDOWS</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(OS)' == 'Windows_NT'">
<DefineConstants>$(DefineConstants);WINDOWS</DefineConstants>
</PropertyGroup>


<ItemGroup>
Expand All @@ -33,7 +33,7 @@
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="DashScope.SemanticKernel" Version="0.8.1" />
<PackageReference Include="Humanizer" Version="2.14.1" />
<PackageReference Include="LLamaSharp.Backend.Cpu" Version="0.10.0" />
<!--<PackageReference Include="LLamaSharp.Backend.Cpu" Version="0.10.0" />-->
<PackageReference Include="LLamaSharp.semantic-kernel" Version="0.10.0" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Core" Version="1.4.0-alpha" />
<PackageReference Include="Projektanker.Icons.Avalonia.MaterialDesign" Version="9.0.1" />
Expand All @@ -47,8 +47,8 @@
</ItemGroup>

<ItemGroup>
<Compile Update="Views\PluginsView.axaml.cs">
<DependentUpon>PluginsView.axaml</DependentUpon>
</Compile>
<Compile Update="Views\PluginsView.axaml.cs">
<DependentUpon>PluginsView.axaml</DependentUpon>
</Compile>
</ItemGroup>
</Project>
32 changes: 32 additions & 0 deletions PromptPlayground/Services/PromptService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
using PromptPlayground.ViewModels.ConfigViewModels;
using PromptPlayground.ViewModels.ConfigViewModels.LLM;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -32,6 +35,35 @@ private Kernel Build()
return _kernel;
}

public async IAsyncEnumerable<GenerateResult> RunStreamAsync(string prompt, PromptExecutionSettings? config,
KernelArguments arguments,
[EnumeratorCancellation]
CancellationToken cancellationToken = default)
{
var _kernel = Build();
var promptFilter = new KernelFilter();
_kernel.PromptFilters.Add(promptFilter);
_kernel.FunctionFilters.Add(promptFilter);

var sw = Stopwatch.StartNew();
var func = _kernel.CreateFunctionFromPrompt(prompt, config);

var results = func.InvokeStreamingAsync(_kernel, arguments, cancellationToken);

var sb = new StringBuilder();
await foreach (var result in results)
{
sb.Append(result.ToString());
yield return new GenerateResult()
{
Text = sb.ToString(),
Elapsed = sw.Elapsed,
PromptRendered = promptFilter.PromptRendered
};
}
sw.Stop();
}

public async Task<GenerateResult> RunAsync(string prompt,
PromptExecutionSettings? config,
KernelArguments arguments,
Expand Down
Loading

0 comments on commit 69fc300

Please sign in to comment.