Skip to content

Commit

Permalink
Merge pull request #16 from UiT-ITA/rag
Browse files Browse the repository at this point in the history
Rework of chatservice and added tools
  • Loading branch information
tveito authored Jan 24, 2025
2 parents 13eaf3e + abf02f0 commit 439bd67
Show file tree
Hide file tree
Showing 20 changed files with 1,290 additions and 679 deletions.
3 changes: 2 additions & 1 deletion ChatUiT2/ChatUiT2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="2.0.0" />
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0" />
<PackageReference Include="Azure.Identity" Version="1.11.4" />
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.7.0" />
<PackageReference Include="DocumentFormat.OpenXml" Version="3.1.1" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.72" />
<PackageReference Include="itext" Version="8.0.5" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.22.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
Expand Down
4 changes: 2 additions & 2 deletions ChatUiT2/Components/Layout/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@
</MudButton>
}
*@
@* @if (!Nav.Uri.EndsWith("/test"))
@*@if (!Nav.Uri.EndsWith("/test"))
{
<MudButton FullWidth Class="pa-2" Style="display:flex; justify-content:flex-start" OnClick="@(() => Nav.NavigateTo("/test"))" Color="Color.Tertiary">
<MudStack Row>
<MudIcon Icon="@Icons.Material.Filled.Science" Size="Size.Medium" Class="mt-0" Style="height:28px; width: 28px;" />
<MudText Typo="Typo.h5" Class="pt-1">Test area</MudText>
</MudStack>
</MudButton>
} *@
} *@



Expand Down
14 changes: 7 additions & 7 deletions ChatUiT2/Components/Pages/Test.razor
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@



<span>@debug_text</span>
<MudTextField AutoGrow="true" Value="debug_text" />

@code {



[Inject] private LocalStorageService LocalStorageService { get; set; } = null!;
[Inject] private IUserService UserService { get; set; } = null!;

private string debug_text = "";

public async void Testing()
{
var chats = await LocalStorageService.GetLocalConversations();

Console.WriteLine(chats[0].Messages[0].Content);
// debug_text = await ChatToolService.GetWikipediaEntry("Elvis_Presley");
foreach(var chat in chats)
{
UserService.AddChat(chat);
}
await Task.Delay(1);
StateHasChanged();
}
}
16 changes: 8 additions & 8 deletions ChatUiT2/Components/Shared/ModelSelect.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
{
<MudMenuItem OnClick="@(() => SelectModel(model))">
<div class="d-flex align-items-center" style="width: 300px;">
<MudIcon Icon="@GetIcon(model.Name)" Size="Size.Large" Class="me-2" Style="margin-top: auto; margin-bottom: auto;" />
<MudIcon Icon="@model.Icon" Size="Size.Large" Class="me-2" Style="margin-top: auto; margin-bottom: auto;" />
<div class="flex-grow-1 d-flex flex-column justify-content-center">
<MudText Typo="Typo.h4" Class="m-0">@model.Name</MudText>
<MudText Typo="Typo.h4" Class="m-0">@model.DisplayName</MudText>
<MudText Typo="Typo.body2" Class="m-0">@model.Description</MudText>
</div>
@if (UserService.CurrentChat.Settings.Model == model.Name)
@if (UserService.CurrentChat.Settings.Model == model.DisplayName)
{
<MudIcon Icon="@Icons.Material.Filled.Check" Class="ms-auto" Style="margin-top: auto; margin-bottom: auto;" />
}
Expand All @@ -29,15 +29,15 @@
@code {

[Inject] private IUserService UserService { get; set; } = null!;
[Inject] private IConfigService ConfigService { get; set; } = null!;
[Inject] private ISettingsService SettingsService { get; set; } = null!;
[Inject] private IUpdateService UpdateService { get; set; } = null!;

[Parameter] public Color Color { get; set; } = Color.Tertiary;
[Parameter] public string Class { get; set; } = "";
[Parameter] public bool AnchorRight { get; set; } = false;


private List<Model> Models { get; set; } = new List<Model>();
private List<AiModel> Models { get; set; } = new List<AiModel>();

private Origin AnchorOrigin => AnchorRight ? Origin.BottomRight : Origin.BottomLeft;
private Origin TransformOrigin => AnchorRight ? Origin.TopRight : Origin.TopLeft;
Expand All @@ -46,7 +46,7 @@
{
if (Models.Count == 0)
{
Models = ConfigService.GetModels();
Models = SettingsService.Models;
StateHasChanged();
}
}
Expand Down Expand Up @@ -80,9 +80,9 @@
}


private void SelectModel(Model model)
private void SelectModel(AiModel model)
{
UserService.CurrentChat.Settings.Model = model.Name;
UserService.CurrentChat.Settings.Model = model.DisplayName;

if (UserService.CurrentChat.Settings.MaxTokens > model.MaxTokens)
{
Expand Down
14 changes: 0 additions & 14 deletions ChatUiT2/Interfaces/IConfigService.cs

This file was deleted.

15 changes: 15 additions & 0 deletions ChatUiT2/Interfaces/ISettingsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using ChatUiT2.Models;
using ChatUiT2.Services;

namespace ChatUiT2.Interfaces;

public interface ISettingsService
{
List<AiModel> Models { get; set; }
AiModel DefaultModel { get; set; }
AiModel NamingModel { get; set; }

AiModel GetModel(string name);
//ModelEndpoint GetEndpoint(string name);
//ModelEndpoint GetEndpoint(AiModel model);
}
4 changes: 2 additions & 2 deletions ChatUiT2/Interfaces/IUserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface IUserService
bool IsAdmin { get; set; }
bool IsTester { get; set; }
bool EnableFileUpload { get; set; }
IConfigService _configService { get; set; }
ISettingsService _settingsService { get; set; }
WorkItemChat CurrentChat { get; }
ChatWidth ChatWidth { get; set; }

Expand All @@ -25,7 +25,7 @@ public interface IUserService
bool GetSaveHistory();
Task SetSaveHistory(IWorkItem workItem, bool value);
Task SetDefaultChatSettings();
List<Model> GetModelList();
List<AiModel> GetModelList();
void SetPreferredModel(string model);
int GetMaxTokens();
int GetMaxTokens(WorkItemChat chat);
Expand Down
144 changes: 144 additions & 0 deletions ChatUiT2/Models/AiModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
using MudBlazor;

namespace ChatUiT2.Models;

public class OldModel
{
public string Name { get; set; } = null!;
public string Description { get; set; } = null!;
public string DeploymentName { get; set; } = null!;
public string DeploymentType { get; set; } = null!;
public string Deployment { get; set; } = null!;
public int MaxContext { get; set; }
public int MaxTokens { get; set; }
}

public class ModelEndpoint
{
public string Name { get; set; } = null!;
public string Url { get; set; } = null!;
public string Key { get; set; } = null!;
}

public class AzureOpenaiEndpoint : ModelEndpoint
{
public string DeploymentName { get; set; } = null!;

}
public class AiModel
{
// Displayed to user
public string DisplayName { get; set; } = null!;
public string Description { get; set; } = null!;
public string Icon { get; set; } = Icons.Material.Filled.Build;
public ModelName ModelName { get; set; }


// Used internally
public DeploymentType DeploymentType { get; set; }
public string Endpoint { get; set; } = null!;
public string ApiKey { get; set; } = null!;
public string DeploymentName { get; set; } = null!;

// Capabilities
public ModelCapabilities Capabilities { get; set; } = new ModelCapabilities();
public int MaxContext { get; set; }
public int MaxTokens { get; set; }

// Settings

public List<ChatToolDescription> OptionalTools { get; set; } = new List<ChatToolDescription>();
public List<ChatToolDescription> RequiredTools { get; set; } = new List<ChatToolDescription>();

// Restrictions
public List<string> AllowedRoles { get; set; } = new List<string>(); // If none set, all roles are allowed
public bool AllowCustomPrompt { get; set; } = true;

}

public class ModelCapabilities
{
public int MaxContext { get; set; } = 0;
public int MaxTokens { get; set; } = 0;

public bool Chat = false;
public bool Vision = false;
public bool ImageGeneration = false;
public bool VoiceInput = false;
public bool VoiceOutput = false;
public bool VideoInput = false;
public bool VideoOutput = false;
}


public enum DeploymentType
{
AzureOpenAI,
}

public enum ModelName
{
gpt35,
gpt35turbo,
gpt4,
gpt4turbo,
gpt4o,
gpt4omini,
o1,
o1mini,
dalle2,
dalle3,
}

public static class ModelServiceExtensions
{
public static string GetDisplayName(this DeploymentType service)
{
return service switch
{
DeploymentType.AzureOpenAI => "Azure OpenAI",
_ => throw new NotImplementedException(),
};
}

public static ModelCapabilities GetCapabilities(this ModelName name)
{
return name switch
{
// OpenAI LLMs
ModelName.gpt35 => new ModelCapabilities { MaxContext = 4096, MaxTokens = 4096, Chat = true },
ModelName.gpt35turbo => new ModelCapabilities { MaxContext = 4096, MaxTokens = 4096, Chat = true },
ModelName.gpt4 => new ModelCapabilities { MaxContext = 8138, MaxTokens = 4096, Chat = true },
ModelName.gpt4turbo => new ModelCapabilities { MaxContext = 16_276, MaxTokens = 4096, Chat = true },
ModelName.gpt4o => new ModelCapabilities { MaxContext = 128_000, MaxTokens = 4096, Chat = true, Vision = true },
ModelName.gpt4omini => new ModelCapabilities { MaxContext = 128_000, MaxTokens = 16_276, Chat = true, Vision = true },
ModelName.o1 => new ModelCapabilities { MaxContext = 128_000, MaxTokens = 4096, Chat = true, Vision = true },
ModelName.o1mini => new ModelCapabilities { MaxContext = 128_000, MaxTokens = 4096, Chat = true, Vision = true },

// OpenAI DALL-E
ModelName.dalle2 => new ModelCapabilities { Vision = true, ImageGeneration = true },
ModelName.dalle3 => new ModelCapabilities { Vision = false, ImageGeneration = true },

_ => throw new NotImplementedException(),
};
}
}

public class ModelConfig
{
public string DisplayName { get; set; } = null!;
public string Description { get; set; } = null!;
public string? Icon { get; set; }
public string DeploymentName { get; set; } = null!;
public string DeploymentType { get; set; } = null!;
public string ModelName { get; set; } = null!;
public string DeploymentEndpoint { get; set; } = null!;
public List<string> OptionalTools { get; set; } = new List<string>();
public List<string> RequiredTools { get; set; } = new List<string>();
public List<string> AllowedRoles { get; set; } = new List<string>();
public int MaxContext { get; set; } = 0;
public int MaxTokens { get; set; } = 0;
public bool AllowCustomPrompt { get; set; } = true;

// Add other properties as needed
}
13 changes: 13 additions & 0 deletions ChatUiT2/Models/ChatToolDescription.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using MudBlazor;
using OpenAI.Chat;

namespace ChatUiT2.Models;

public class ChatToolDescription
{
public string DisplayName { get; set; } = null!;
public string Description { get; set; } = null!;
public string Icon { get; set; } = Icons.Material.Filled.Build;
public ChatTool Tool { get; set; } = null!;
public bool Selected = false;
}
33 changes: 0 additions & 33 deletions ChatUiT2/Models/Model.cs

This file was deleted.

2 changes: 1 addition & 1 deletion ChatUiT2/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
builder.Services.AddMudMarkdownServices();

// Singleton services
builder.Services.AddSingleton<IConfigService, ConfigService>();
builder.Services.AddSingleton<ISettingsService, SettingsService>();
builder.Services.AddSingleton<IDatabaseService, DatabaseService>();
builder.Services.AddSingleton<IKeyVaultService, KeyVaultService>();
builder.Services.AddSingleton<IEncryptionService, EncryptionService>();
Expand Down
Loading

0 comments on commit 439bd67

Please sign in to comment.