Browse Source

Add model name suggestions on validation errors

pull/165/head
Ionite 1 year ago
parent
commit
39d89b94b8
No known key found for this signature in database
  1. 48
      StabilityMatrix.Avalonia/DialogHelper.cs
  2. 1
      StabilityMatrix.Avalonia/Models/Inference/Prompt.cs
  3. 8
      StabilityMatrix.Avalonia/ViewModels/Inference/PromptCardViewModel.cs
  4. 24
      StabilityMatrix.Core/Exceptions/PromptUnknownModelError.cs
  5. 18
      StabilityMatrix.Core/Exceptions/PromptValidationError.cs

48
StabilityMatrix.Avalonia/DialogHelper.cs

@ -23,7 +23,11 @@ using StabilityMatrix.Avalonia.Controls;
using StabilityMatrix.Avalonia.Helpers;
using StabilityMatrix.Core.Exceptions;
using StabilityMatrix.Core.Extensions;
using StabilityMatrix.Core.Models;
using StabilityMatrix.Core.Models.Database;
using StabilityMatrix.Core.Services;
using TextMateSharp.Grammars;
using Process = FuzzySharp.Process;
namespace StabilityMatrix.Avalonia;
@ -298,9 +302,16 @@ public static class DialogHelper
return dialog;
}
/// <summary>
/// Create a dialog for displaying a prompt error
/// </summary>
/// <param name="exception">Target exception to display</param>
/// <param name="sourceText">Full text of the target Document</param>
/// <param name="modelIndexService">Optional model index service to look for similar names</param>
public static BetterContentDialog CreatePromptErrorDialog(
PromptError exception,
string sourceText
string sourceText,
IModelIndexService? modelIndexService = null
)
{
Dispatcher.UIThread.VerifyAccess();
@ -383,6 +394,41 @@ public static class DialogHelper
}
};
// Check model typos
if (modelIndexService is not null && exception is PromptUnknownModelError unknownModelError)
{
var sharedFolderType = unknownModelError.ModelType.ConvertTo<SharedFolderType>();
if (modelIndexService.ModelIndex.TryGetValue(sharedFolderType, out var models))
{
var result = Process.ExtractOne(
unknownModelError.ModelName,
models.Select(m => m.FileNameWithoutExtension)
);
if (result.Score > 40)
{
/*mainGrid.Children.Add(
new TextBlock
{
Text = $"Did you mean: {result.Value}?",
FontSize = 18,
FontWeight = FontWeight.Medium,
Margin = new Thickness(0, 8),
}
);*/
mainGrid.Children.Add(
new InfoBar
{
Title = $"Did you mean: {result.Value}?",
IsClosable = false,
IsOpen = true
}
);
}
}
}
textEditor.ScrollToHorizontalOffset(errorLineEndOffset - errorLineOffset);
var dialog = new BetterContentDialog

1
StabilityMatrix.Avalonia/Models/Inference/Prompt.cs

@ -239,6 +239,7 @@ public record Prompt
{
throw PromptValidationError.Network_UnknownModel(
modelName,
parsedNetworkType,
currentToken.StartIndex,
GetSafeEndIndex(currentToken.EndIndex)
);

8
StabilityMatrix.Avalonia/ViewModels/Inference/PromptCardViewModel.cs

@ -108,7 +108,7 @@ public partial class PromptCardViewModel : LoadableViewModelBase
}
catch (PromptError e)
{
var dialog = DialogHelper.CreatePromptErrorDialog(e, promptText);
var dialog = DialogHelper.CreatePromptErrorDialog(e, promptText, modelIndexService);
await dialog.ShowAsync();
return false;
}
@ -120,7 +120,7 @@ public partial class PromptCardViewModel : LoadableViewModelBase
}
catch (PromptError e)
{
var dialog = DialogHelper.CreatePromptErrorDialog(e, negPromptText);
var dialog = DialogHelper.CreatePromptErrorDialog(e, negPromptText, modelIndexService);
await dialog.ShowAsync();
return false;
}
@ -190,7 +190,9 @@ public partial class PromptCardViewModel : LoadableViewModelBase
}
catch (PromptError e)
{
await DialogHelper.CreatePromptErrorDialog(e, prompt.RawText).ShowAsync();
await DialogHelper
.CreatePromptErrorDialog(e, prompt.RawText, modelIndexService)
.ShowAsync();
return;
}

24
StabilityMatrix.Core/Exceptions/PromptUnknownModelError.cs

@ -0,0 +1,24 @@
using StabilityMatrix.Core.Models.Tokens;
namespace StabilityMatrix.Core.Exceptions;
public class PromptUnknownModelError : PromptValidationError
{
public string ModelName { get; }
public PromptExtraNetworkType ModelType { get; }
/// <inheritdoc />
public PromptUnknownModelError(
string message,
int textOffset,
int textEndOffset,
string modelName,
PromptExtraNetworkType modelType
)
: base(message, textOffset, textEndOffset)
{
ModelName = modelName;
ModelType = modelType;
}
}

18
StabilityMatrix.Core/Exceptions/PromptValidationError.cs

@ -1,4 +1,6 @@
namespace StabilityMatrix.Core.Exceptions;
using StabilityMatrix.Core.Models.Tokens;
namespace StabilityMatrix.Core.Exceptions;
public class PromptValidationError : PromptError
{
@ -9,11 +11,19 @@ public class PromptValidationError : PromptError
public static PromptValidationError Network_UnknownType(int textOffset, int textEndOffset) =>
new("Unknown network type", textOffset, textEndOffset);
public static PromptValidationError Network_UnknownModel(
string model,
public static PromptUnknownModelError Network_UnknownModel(
string modelName,
PromptExtraNetworkType modelType,
int textOffset,
int textEndOffset
) => new($"Model '{model}' was not found locally", textOffset, textEndOffset);
) =>
new(
$"Model '{modelName}' was not found locally",
textOffset,
textEndOffset,
modelName,
modelType
);
public static PromptSyntaxError Network_InvalidWeight(int textOffset, int textEndOffset) =>
new("Invalid network weight, could not be parsed as double", textOffset, textEndOffset);

Loading…
Cancel
Save