Skip to content

Commit

Permalink
Improve support for unsorted models and fix model handling issues
Browse files Browse the repository at this point in the history
  • Loading branch information
lcarrere committed Nov 26, 2024
1 parent 68895e0 commit 330a267
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 44 deletions.
33 changes: 0 additions & 33 deletions LM-Kit-Maestro/Helpers/FileHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,39 +63,6 @@ public static bool GetModelInfoFromFileUri(Uri fileUri, string modelsRootFolder,
return false;
}

public static bool GetModelInfoFromPath(string filePath, out string publisher, out string repository, out string fileName)
{
repository = string.Empty;
publisher = string.Empty;
fileName = string.Empty;

Uri fileUri;

try
{
fileUri = new Uri(filePath);
}
catch (Exception)
{
return false;
}

if (fileUri.IsFile && fileUri.Segments.Length > 4)
{
publisher = SanitizeUriSegment(fileUri.Segments[fileUri.Segments.Length - 3]);
repository = SanitizeUriSegment(fileUri.Segments[fileUri.Segments.Length - 2]);
fileName = SanitizeUriSegment(fileUri.Segments[fileUri.Segments.Length - 1]);

return true;
}

repository = string.Empty;
publisher = string.Empty;
fileName = string.Empty;

return false;
}

public static bool TryCreateFileUri(string filePath, out Uri? uri)
{
try
Expand Down
19 changes: 16 additions & 3 deletions LM-Kit-Maestro/Helpers/MaestroHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ internal static class MaestroHelpers
return null;
}

public static ModelInfoViewModel? TryGetExistingModelInfoViewModel(ICollection<ModelInfoViewModel> modelInfoViewModels, Uri modelFileUri)
public static ModelInfoViewModel? TryGetExistingModelInfoViewModel(string modelsFolderPath, ICollection<ModelInfoViewModel> modelInfoViewModels, Uri modelFileUri)
{
if (FileHelpers.GetModelInfoFromPath(modelFileUri.LocalPath, out string publisher, out string repository, out string fileName))
if (FileHelpers.GetModelInfoFromPath(modelFileUri.LocalPath, modelsFolderPath, out string publisher, out string repository, out string fileName))
{
foreach (var modelInfoViewModel in modelInfoViewModels)
{
Expand All @@ -34,8 +34,21 @@ internal static class MaestroHelpers
}
}
}
else
{
//handling unsorted models.
foreach (var modelInfoViewModel in modelInfoViewModels)
{
if (modelInfoViewModel.ModelInfo.FileUri == modelFileUri)
{
return modelInfoViewModel;
}
}
}

return null;
//Loïc: we have an architecture defect. We can reach this stage, especially at startup, while modelInfoViewModels is not completely loaded.
//todo Evan: fix.
return new ModelInfoViewModel(new ModelInfo(publisher, repository, fileName, modelFileUri));
}
}
}
3 changes: 1 addition & 2 deletions LM-Kit-Maestro/Services/Interfaces/ILLMFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ public interface ILLMFileManager
ObservableCollection<ModelInfo> UserModels { get; }
ObservableCollection<Uri> UnsortedModels { get; }
bool FileCollectingInProgress { get; }

string ModelsFolderPath { get; set; }
event EventHandler? FileCollectingCompleted;

void Initialize();
void DeleteModel(ModelInfo modelInfo);
}
1 change: 0 additions & 1 deletion LM-Kit-Maestro/Services/LLMFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ private async Task CollectModelFilesAsync()
await CancelOngoingFileCollecting();
}


_cancellationTokenSource = new CancellationTokenSource();

Exception? exception = null;
Expand Down
1 change: 1 addition & 0 deletions LM-Kit-Maestro/Services/LMKitService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public void LoadModel(Uri fileUri, string? localFilePath = null)
if (modelLoadingSuccess)
{
LMKitConfig.LoadedModelUri = fileUri!;
ModelLoadingCompleted?.Invoke(this, new NotifyModelStateChangedEventArgs(LMKitConfig.LoadedModelUri));
ModelLoadingState = LMKitModelLoadingState.Loaded;
}
Expand Down
4 changes: 2 additions & 2 deletions LM-Kit-Maestro/ViewModels/ModelListViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void Initialize()
{
if (LMKitService.LMKitConfig.LoadedModelUri != null)
{
SelectedModel = MaestroHelpers.TryGetExistingModelInfoViewModel(UserModels, LMKitService.LMKitConfig.LoadedModelUri);
SelectedModel = MaestroHelpers.TryGetExistingModelInfoViewModel(_fileManager.ModelsFolderPath, UserModels, LMKitService.LMKitConfig.LoadedModelUri);
}
}

Expand Down Expand Up @@ -208,7 +208,7 @@ private void ClearUserModelList()

private void OnModelLoadingCompleted(object? sender, EventArgs e)
{
SelectedModel = MaestroHelpers.TryGetExistingModelInfoViewModel(UserModels, LMKitService.LMKitConfig.LoadedModelUri!);
SelectedModel = MaestroHelpers.TryGetExistingModelInfoViewModel(_fileManager.ModelsFolderPath, UserModels, LMKitService.LMKitConfig.LoadedModelUri!);
LoadingProgress = 0;
ModelLoadingIsFinishingUp = false;
}
Expand Down
6 changes: 3 additions & 3 deletions LM-Kit-Maestro/ViewModels/Pages/AppShellViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ public async Task Init()

await _conversationListViewModel.LoadConversationLogs();

_llmFileManager.FileCollectingCompleted += OnFileManagerFileCollectingCompleted;
_llmFileManager.Initialize();

_lmKitService.ModelLoadingFailed += OnModelLoadingFailed;

if (_appSettingsService.LastLoadedModel != null)
{
TryLoadLastUsedModel();
}

_llmFileManager.FileCollectingCompleted += OnFileManagerFileCollectingCompleted;
_llmFileManager.Initialize();

// todo: we should ensure UI is loaded before starting loading a model with this call.
_modelListViewModel.Initialize();

Expand Down

0 comments on commit 330a267

Please sign in to comment.