Skip to content

Commit

Permalink
Code(WEB::ModelsCatalog): Adapt to backend changes after renaming fea…
Browse files Browse the repository at this point in the history
…ture from BrowseAvailableModels to BrowseModelsCatalog and improving directory structure
  • Loading branch information
ktutak1337 committed Jul 22, 2024
1 parent 6b5cca3 commit 3ef8f34
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@inject IActionService _actionService
@inject IAvailableModelsService _availableModelsService
@inject IModelCatalogService _modelCatalogService
@inject ChatState _chatState
@inject ISnackbar Snackbar

Expand Down Expand Up @@ -39,7 +39,7 @@
For="@(() => Action!.Model)"
HelperText="The model that will be used when executing actions.">

@foreach (var model in AvailableModels)
@foreach (var model in ModelCatalog)
{
<MudSelectItem Value="@model.Name" />
}
Expand Down Expand Up @@ -112,7 +112,7 @@
[Parameter] public Guid ActiontId { get; set; }
[Parameter] public EventCallback OnActionSubmit { get; set; }
private NativeActionResponse Action { get; set; } = new();
private List<AvailableModelsResponse> AvailableModels { get; set; } = new();
private List<ModelCatalogResponse> ModelCatalog { get; set; } = new();

public List<string> Categories { get; set; } = new()
{
Expand Down Expand Up @@ -187,27 +187,27 @@ Some actions require just the latest message, while others need the full convers

protected override async Task OnInitializedAsync()
{
await LoadModels();
await LoadModelCatalog();
}

protected override async Task OnParametersSetAsync()
{
await PopulateForm();
}

private async Task LoadModels()
private async Task LoadModelCatalog()
{
var response = await _availableModelsService.BrowseAvailableModels();
var models = response.Value;
var response = await _modelCatalogService.BrowseModelsCatalog();
var modelCatalog = response.Value;

if (!response.Succeeded)
{
ShowSnackbar("Failed to load available models. Please try again.", Severity.Error);
ShowSnackbar("Failed to load model catalog. Please try again.", Severity.Error);
}

if (models is not null)
if (modelCatalog is not null)
{
AvailableModels = models.ToList();
ModelCatalog = modelCatalog.ToList();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@inject NavigationManager _navigationManager
@inject IAssistantService _assistantService
@inject IStorageService _storageService
@inject IAvailableModelsService _availableModelsService
@inject IModelCatalogService _modelCatalogService
@inject ChatState _chatState
@inject ISnackbar Snackbar

Expand Down Expand Up @@ -52,7 +52,7 @@
[Parameter] public Guid AssistantId { get; set; }
[Parameter] public EventCallback OnAssistantSubmit { get; set; }
private AssistantResponse Assistant { get; set; } = new();
private List<AvailableModelsResponse> AvailableModels { get; set; } = new();
private List<ModelCatalogResponse> AvailableModels { get; set; } = new();

private const string assistantBehaviorGuidelines = @"
Enter the Metaprompt here. This set of instructions will define your assistant's behavior
Expand Down Expand Up @@ -107,7 +107,7 @@ reflective of the assistant's intended personality and capabilities.<br /><br />

private async Task LoadModels()
{
var response = await _availableModelsService.BrowseAvailableModels();
var response = await _modelCatalogService.BrowseModelsCatalog();
var models = response.Value;

if (!response.Succeeded)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@inject IAvailableModelsService _availableModelsService
@inject IModelCatalogService _modelCatalogService
@inject ChatState _chatState
@inject ISnackbar Snackbar

Expand All @@ -23,7 +23,7 @@

@code
{
public List<AvailableModelsResponse> Models { get; set; } = new();
public List<ModelCatalogResponse> ModelCatalog { get; set; } = new();
private string CurrentValue { get; set; } = string.Empty;
private string ServiceId { get; set; } = string.Empty;

Expand All @@ -36,23 +36,23 @@
_chatState.ServiceIdChanged += StateHasChanged;
_chatState.SelectedAssistantChanged += SetDefaultModel;

await LoadModels();
await LoadModelCatalog();
SetDefaultModel();
}

private async Task LoadModels()
private async Task LoadModelCatalog()
{
var response = await _availableModelsService.BrowseAvailableModels();
var models = response.Value;
var response = await _modelCatalogService.BrowseModelsCatalog();
var modelCatalog = response.Value;

if(!response.Succeeded)
{
ShowSnackbar("Failed to load available models. Please try again.", Severity.Error);
ShowSnackbar("Failed to load model catalog. Please try again.", Severity.Error);
}

if(models is not null)
if(modelCatalog is not null)
{
Models = models.ToList();
ModelCatalog = modelCatalog.ToList();
}
}

Expand All @@ -65,7 +65,7 @@
CurrentValue = selectedModel!;
_chatState.SetSelectedModel(selectedModel!);

var serviceId = Models.SingleOrDefault(x => x.Name == CurrentValue)?.Provider ?? string.Empty;
var serviceId = ModelCatalog.SingleOrDefault(x => x.Name == CurrentValue)?.Provider ?? string.Empty;
_chatState.SetServiceId(serviceId);
}
}
Expand All @@ -74,10 +74,10 @@
{
if (string.IsNullOrEmpty(value))
{
return Models.Select(model => model.Name);
return ModelCatalog.Select(model => model.Name);
}

var result = Models
var result = ModelCatalog
.Where(x => x.Name.Contains(value, StringComparison.InvariantCultureIgnoreCase))
.Select(model => model.Name);

Expand All @@ -87,7 +87,7 @@
private void OnValueChanged(string value)
{
CurrentValue = value.IsEmpty() ? string.Empty : value;
var serviceId = Models.SingleOrDefault(x => x.Name == value)?.Provider ?? string.Empty;
var serviceId = ModelCatalog.SingleOrDefault(x => x.Name == value)?.Provider ?? string.Empty;

_chatState.SetServiceId(serviceId);
_chatState.SetSelectedModel(CurrentValue);
Expand All @@ -100,7 +100,7 @@
=> SetDefaultModel();

private bool IsValidModel(string value)
=> value.IsNotEmpty() && Models.Any(model => model.Name == value);
=> value.IsNotEmpty() && ModelCatalog.Any(model => model.Name == value);

private void ShowSnackbar(string message, Severity severity)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
For="@(() => AppSettings.Key)"
HelperText="Select the model to be pulled from the Ollama server to your local disk.">

@foreach (var model in AvailableModels)
@foreach (var model in ModelCatalog)
{
<MudSelectItem Value="@model.Name" />
}
Expand All @@ -54,7 +54,7 @@
{
private AppSettingsResponse AppSettings { get; set; } = new();

private List<AvailableModelsResponse> AvailableModels { get; set; } = new();
private List<ModelCatalogResponse> ModelCatalog { get; set; } = new();

private const string OllamaProvider = "ollama";
private const string OpenAiProvider = "openai";
Expand Down
2 changes: 1 addition & 1 deletion src/Client/StellarChat.Client.Web/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static WebAssemblyHostBuilder AddServices(this WebAssemblyHostBuilder bui
builder.Services.AddScoped<IAssistantService, AssistantService>();
builder.Services.AddScoped<ISettingsService, SettingsService>();
builder.Services.AddScoped<IStorageService, StorageService>();
builder.Services.AddScoped<IAvailableModelsService, AvailableModelsService>();
builder.Services.AddScoped<IModelCatalogService, ModelCatalogService>();

builder.Services.AddMudServices();

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace StellarChat.Client.Web.Services.Models;

public interface IAvailableModelsService
public interface IModelCatalogService
{
ValueTask<ApiResponse<IEnumerable<AvailableModelsResponse>>> BrowseAvailableModels();
ValueTask<ApiResponse<IEnumerable<ModelCatalogResponse>>> BrowseModelsCatalog();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using StellarChat.Client.Web.Shared.Http;
using StellarChat.Shared.Contracts.Models;

namespace StellarChat.Client.Web.Services.Models;

public class ModelCatalogService(IRestHttpClient httpClient) : IModelCatalogService
{
private readonly IRestHttpClient _httpClient = httpClient;

public async ValueTask<ApiResponse<IEnumerable<ModelCatalogResponse>>> BrowseModelsCatalog()
=> await _httpClient.GetAsync<IEnumerable<ModelCatalogResponse>>($"/models?Provider=openai&Filter=completions");
}

0 comments on commit 3ef8f34

Please sign in to comment.