Skip to content

Commit

Permalink
🔧 Fix saving issue in ConfigViewModel
Browse files Browse the repository at this point in the history
- Fixed an issue in the `SaveConfig` method of the `ConfigViewModel` class where the `Path.Combine` method was incorrectly used to return the combined path of the `folder` and `configFile` variables. This was replaced with a call to the `_profile.Save` method to correctly save the configuration.

🔀 Refactor SemanticFunctionViewModel

- Moved the `Equals` method to the end of the class.
- Added a `DefaultConfig` method to generate default configuration.
- Added a `Create` method to create a new instance of `SemanticFunctionViewModel`.
- Updated the constructor to handle folder or name parameters.
- Updated properties to use the `ObservableProperty` attribute.
- Updated the `Prompt` and `Config` properties to use auto-implemented properties.
- Updated the `Folder` property to use an auto-implemented property.
- Removed unused methods and commands.
- Added missing using statements.
- Updated the `IsChanged` property to use an auto-implemented property.
- Updated the `IsGenerating` property to use an auto-implemented property.
- Updated the `Results` property to use an auto-implemented property.
  • Loading branch information
xbotter committed Feb 8, 2024
1 parent bfdee82 commit 9d52da6
Show file tree
Hide file tree
Showing 12 changed files with 367 additions and 283 deletions.
23 changes: 23 additions & 0 deletions PromptPlayground/Converters/NullToVisibilityConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Avalonia.Data.Converters;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PromptPlayground.Converters
{
public class NullToVisibilityConverter : IValueConverter
{
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)
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

namespace PromptPlayground.Messages
{
public abstract class FileOrFolderOpenMessage
public abstract class FileOrFolderPathMessage
{
public string? Path { get; set; }
public FileOrFolderOpenMessage(string? path)
public FileOrFolderPathMessage(string? path)
{
Path = path;
}
Expand Down
10 changes: 9 additions & 1 deletion PromptPlayground/Messages/FunctionOpenMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

namespace PromptPlayground.ViewModels
{
public class FunctionOpenMessage : FileOrFolderOpenMessage
public class FunctionOpenMessage : FileOrFolderPathMessage
{
public FunctionOpenMessage(string folder) : base(folder)
{

}
}

public class FunctionSavedMessage : FileOrFolderPathMessage
{
public FunctionSavedMessage(string folder) : base(folder)
{

}
}
}
4 changes: 2 additions & 2 deletions PromptPlayground/Messages/PluginOpenMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace PromptPlayground.ViewModels
{
public class PluginOpenMessage : FileOrFolderOpenMessage
public class PluginOpenMessage : FileOrFolderPathMessage
{
public PluginOpenMessage(string path) : base(path) { }
}

public class PluginCloseMessage : FileOrFolderOpenMessage
public class PluginCloseMessage : FileOrFolderPathMessage
{
public PluginCloseMessage(string path) : base(path) { }
}
Expand Down
4 changes: 2 additions & 2 deletions PromptPlayground/Services/ProfileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class ProfileService<T>
{
private readonly string _profile;

public ProfileService(string profile = nameof(T))
public ProfileService(string profile)
{
this._profile = profile;
}
Expand All @@ -24,7 +24,7 @@ public string ProfilePath()
{
Directory.CreateDirectory(profileFolder);
}
return Path.Combine(profileFolder, $"{_profile}.json");
return Path.Combine(profileFolder, $"{_profile}");
}


Expand Down
8 changes: 4 additions & 4 deletions PromptPlayground/Services/PromptService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ private Kernel Build()
return _kernel;
}

public async Task<GenerateResult> RunAsync(string prompt, PromptExecutionSettings? config, KernelArguments arguments, CancellationToken cancellationToken = default)
public async Task<GenerateResult> RunAsync(string prompt,
PromptExecutionSettings? config,
KernelArguments arguments,
CancellationToken cancellationToken = default)
{
var _kernel = Build();
var promptFilter = new KernelFilter();
Expand Down Expand Up @@ -74,12 +77,10 @@ public class KernelFilter : IPromptFilter, IFunctionFilter
public string? PromptRendered { get; set; }
public void OnFunctionInvoked(FunctionInvokedContext context)
{

}

public void OnFunctionInvoking(FunctionInvokingContext context)
{

}

public void OnPromptRendered(PromptRenderedContext context)
Expand All @@ -89,7 +90,6 @@ public void OnPromptRendered(PromptRenderedContext context)

public void OnPromptRendering(PromptRenderingContext context)
{

}
}

Expand Down
33 changes: 10 additions & 23 deletions PromptPlayground/ViewModels/ConfigViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using PromptPlayground.Messages;
using PromptPlayground.Services;
using PromptPlayground.ViewModels.ConfigViewModels;
using PromptPlayground.ViewModels.ConfigViewModels.LLM;
using System;
Expand Down Expand Up @@ -42,6 +43,7 @@ public partial class ConfigViewModel : ViewModelBase, IConfigAttributesProvider,
#region Model
private int modelSelectedIndex = 0;
private ProfileService<ConfigViewModel> _profile;
public int ModelSelectedIndex
{
Expand Down Expand Up @@ -93,22 +95,19 @@ public ConfigViewModel()
LLMs.Add(new BaiduConfigViewModel(this));
LLMs.Add(new OpenAIConfigViewModel(this));
LLMs.Add(new DashScopeConfigViewModel(this));

this._profile = new ProfileService<ConfigViewModel>("user.config");
}

private void LoadConfigFromUserProfile()
{
var profile = GetConfigFilePath();
if (File.Exists(profile))
var vm = this._profile.Get();
if (vm != null)
{
var vm = JsonSerializer.Deserialize<ConfigViewModel>(File.ReadAllText(profile));
if (vm != null)
{
this.AllAttributes = CheckAttributes(vm.AllAttributes);
OnPropertyChanged(nameof(AllAttributes));
this.AllAttributes = CheckAttributes(vm.AllAttributes);

this.MaxCount = vm.MaxCount;
this.ModelSelectedIndex = vm.ModelSelectedIndex;
}
this.MaxCount = vm.MaxCount;
this.ModelSelectedIndex = vm.ModelSelectedIndex;
}
}
private List<ConfigAttribute> CheckAttributes(List<ConfigAttribute> list)
Expand All @@ -125,19 +124,7 @@ private List<ConfigAttribute> CheckAttributes(List<ConfigAttribute> list)

private void SaveConfigToUserProfile()
{
var profile = GetConfigFilePath();
File.WriteAllText(profile, JsonSerializer.Serialize(this));
}

private string GetConfigFilePath(string configFile = "user.config")
{
var profile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var folder = Path.Combine(profile, ".prompt_playground");
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
return Path.Combine(folder, configFile);
this._profile.Save(this);
}

public void SaveConfig()
Expand Down
35 changes: 29 additions & 6 deletions PromptPlayground/ViewModels/GenerateResult.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using PromptPlayground.ViewModels;
using System;

namespace PromptPlayground.Services
{
public partial class AverageResult : ViewModelBase
{
[ObservableProperty]
private bool hasResults = false;

[ObservableProperty]
private TimeSpan elapsed;

[ObservableProperty]
private ResultTokenUsage? tokenUsage;

}

public partial class GenerateResult : ViewModelBase
{
public string Text { get; set; } = string.Empty;
public TimeSpan Elapsed { get; set; }
public string? Error { get; set; } = string.Empty;
public string? PromptRendered { get; set; }
public ResultTokenUsage? TokenUsage { get; set; }
[ObservableProperty]
private string text = string.Empty;

[ObservableProperty]
private TimeSpan elapsed;

[ObservableProperty]
private string? error;

[ObservableProperty]
private string? promptRendered;

[ObservableProperty]
private ResultTokenUsage? tokenUsage;

public bool HasError => !string.IsNullOrWhiteSpace(Error);

Expand Down
25 changes: 23 additions & 2 deletions PromptPlayground/ViewModels/PluginsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ public partial class PluginsViewModel : ObservableRecipient,
IRecipient<PluginOpenMessage>,
IRecipient<PluginCloseMessage>,
IRecipient<FunctionOpenMessage>,
IRecipient<FunctionCreateMessage>
IRecipient<FunctionCreateMessage>,
IRecipient<FunctionSavedMessage>
{
private readonly ProfileService<List<string>> profile;
const string DefaultPlugin = "·______·";

public PluginsViewModel()
{
this.profile = new ProfileService<List<string>>("openedPlugins");
this.profile = new ProfileService<List<string>>("openedPlugins.json");
Plugins = new ObservableCollection<PluginViewModel>();
OpenedPlugin = new PluginViewModel(DefaultPlugin);
Plugins.Add(OpenedPlugin);
Expand Down Expand Up @@ -106,6 +107,26 @@ public void Receive(FunctionCreateMessage message)
FunctionSelected(function);
}

public void Receive(FunctionSavedMessage message)
{
var defaultPlugin = Plugins.FirstOrDefault(_ => _.Folder is null);
if (defaultPlugin != null)
{
var function = defaultPlugin.Functions.FirstOrDefault(_ => _.Folder == message.Path);

var parent = Path.GetDirectoryName(message.Path);

var plugin = Plugins.FirstOrDefault(_ => _.Folder == parent);

if (plugin != null && function != null)
{
defaultPlugin.Functions.Remove(function);
plugin.Functions.Add(function);
WeakReferenceMessenger.Default.Send(new FunctionSelectedMessage(function));
}
}
}

public PluginViewModel OpenedPlugin { get; set; }

public ObservableCollection<PluginViewModel> Plugins { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions PromptPlayground/ViewModels/ResultsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class ResultsViewModel : ObservableRecipient, IRecipient<FunctionSelected
{
private SemanticFunctionViewModel function;
public ObservableCollection<GenerateResult> Results => function.Results;
public AverageResult AverageResult => function.Average;

public ResultsViewModel(SemanticFunctionViewModel function)
{
this.function = function;
Expand Down
Loading

0 comments on commit 9d52da6

Please sign in to comment.