Skip to content

Commit

Permalink
Remove usage of the Ioc.Default
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Apr 26, 2024
1 parent c0309df commit e607d1c
Show file tree
Hide file tree
Showing 47 changed files with 437 additions and 792 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>preview.22</VersionSuffix>
<VersionSuffix>preview.23</VersionSuffix>
<Authors>Wiesław Šoltés</Authors>
<Company>Wiesław Šoltés</Company>
<Copyright>Copyright © Wiesław Šoltés 2024</Copyright>
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ You can import [OpenAI ChatGPT web version](https://chat.openai.com/chat) chats
- [Markdown.Avalonia](https://github.com/whistyun/Markdown.Avalonia)
- [Avalonia.HtmlRenderer](https://github.com/AvaloniaUI/Avalonia.HtmlRenderer)
- [CommunityToolkit.Mvvm](https://github.com/CommunityToolkit/dotnet)
- [Microsoft.Extensions.DependencyInjection](https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection/)

# .NET tool

Expand Down
7 changes: 4 additions & 3 deletions samples/ChatGPT.CLI.Chaining/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using ChatGPT;
using AI.Services;
using ChatGPT.ViewModels.Chat;

Defaults.ConfigureDefaultServices();
var chatSerializer = new SystemTextJsonChatSerializer();
var chatService = new ChatService(chatSerializer);

using var cts = new CancellationTokenSource();
var input1 = "Some random test string.";
Expand All @@ -17,7 +18,7 @@

async Task<string?> SendAsync(string directions, string input, CancellationToken token)
{
var chat = new ChatViewModel(directions);
var chat = new ChatViewModel(chatService, chatSerializer,directions);
chat.AddSystemMessage(directions);
chat.AddUserMessage(input);
var result = await chat.SendAsync(chat.CreateChatMessages(), token);
Expand Down
26 changes: 15 additions & 11 deletions samples/ChatGPT.CLI.FunctionCalling/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using ChatGPT;
using AI.Services;
using ChatGPT.ViewModels.Chat;

Defaults.ConfigureDefaultServices();
var chatSerializer = new SystemTextJsonChatSerializer();
var chatService = new ChatService(chatSerializer);

var directions =
"""
Expand All @@ -20,15 +21,18 @@ Only use the functions you have been provided with.

var functions = GetFunctions();

var chat = new ChatViewModel(new ChatSettingsViewModel
{
MaxTokens = 2000,
Model = "gpt-3.5-turbo-0613",
Functions = functions,
FunctionCall = "auto"
// Force function call by setting FunctionCall property.
// FunctionCall = new { name = "GetCurrentWeather" }
});
var chat = new ChatViewModel(
chatService,
chatSerializer,
new ChatSettingsViewModel
{
MaxTokens = 2000,
Model = "gpt-3.5-turbo-0613",
Functions = functions,
FunctionCall = "auto"
// Force function call by setting FunctionCall property.
// FunctionCall = new { name = "GetCurrentWeather" }
});

// Enable to debug json requests and responses.
// chat.Debug = true;
Expand Down
18 changes: 11 additions & 7 deletions samples/ChatGPT.CLI.Repl/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using ChatGPT;
using AI.Services;
using ChatGPT.ViewModels.Chat;

Defaults.ConfigureDefaultServices();
var chatSerializer = new SystemTextJsonChatSerializer();
var chatService = new ChatService(chatSerializer);

var directions =
"""
Expand All @@ -17,11 +18,14 @@ Do not use markdown.

using var cts = new CancellationTokenSource();

var chat = new ChatViewModel(new ChatSettingsViewModel
{
MaxTokens = 2000,
Model = "gpt-3.5-turbo"
});
var chat = new ChatViewModel(
chatService,
chatSerializer,
new ChatSettingsViewModel
{
MaxTokens = 2000,
Model = "gpt-3.5-turbo"
});

chat.AddSystemMessage(directions);

Expand Down
9 changes: 7 additions & 2 deletions samples/ChatGPT.UI.Designer/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading;
using AI.Services;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
Expand All @@ -10,13 +11,17 @@ namespace ChatGPT;

public partial class MainWindow : Window
{
private readonly SystemTextJsonChatSerializer _chatSerializer;
private readonly ChatService _chatService;

public MainWindow()
{
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
Defaults.ConfigureDefaultServices();
_chatSerializer = new SystemTextJsonChatSerializer();
_chatService = new ChatService(_chatSerializer);
}

private async void SendButton_OnClick(object? sender, RoutedEventArgs e)
Expand Down Expand Up @@ -53,7 +58,7 @@ Write XAML as plain text.
};

var cts = new CancellationTokenSource();
var chat = new ChatViewModel(chatSettings);
var chat = new ChatViewModel(_chatService, _chatSerializer, chatSettings);
chat.AddSystemMessage(chatSettings.Directions);
chat.AddUserMessage(prompt);
var result = await chat.SendAsync(chat.CreateChatMessages(), cts.Token);
Expand Down
34 changes: 23 additions & 11 deletions samples/ChatGPT.UI.Game/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using AI.Services;
using ChatGPT.ViewModels.Chat;

namespace ChatGPT;
Expand All @@ -11,7 +12,15 @@ public class Game
private string? _directions;
private CancellationTokenSource? _cts;
private ChatViewModel? _chat;
private readonly SystemTextJsonChatSerializer _chatSerializer;
private readonly ChatService _chatService;

public Game()
{
_chatSerializer = new SystemTextJsonChatSerializer();
_chatService = new ChatService(_chatSerializer);
}

public void New()
{
_directions =
Expand All @@ -35,17 +44,20 @@ Player has always three options to choose.
}
""";

_chat = new ChatViewModel(new ChatSettingsViewModel
{
Temperature = 0.7m,
TopP = 1m,
PresencePenalty = 0m,
FrequencyPenalty = 0m,
MaxTokens = 3000,
ApiKey = null,
// Model = "gpt-3.5-turbo",
Model = "gpt-4",
});
_chat = new ChatViewModel(
_chatService,
_chatSerializer,
new ChatSettingsViewModel
{
Temperature = 0.7m,
TopP = 1m,
PresencePenalty = 0m,
FrequencyPenalty = 0m,
MaxTokens = 3000,
ApiKey = null,
// Model = "gpt-3.5-turbo",
Model = "gpt-4",
});

_chat.AddSystemMessage(_directions);
}
Expand Down
2 changes: 0 additions & 2 deletions samples/ChatGPT.UI.Game/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public MainWindow()
#if DEBUG
this.AttachDevTools();
#endif
Defaults.ConfigureDefaultServices();

_game = new Game();
_game.New();

Expand Down
16 changes: 14 additions & 2 deletions src/ChatGPT.CLI/Chat.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System.Diagnostics;
using System.Text.Json;
using AI.Model.Services;
using AI.Services;
using ChatGPT.Model.Services;
using ChatGPT.Services;
using ChatGPT.ViewModels;
using ChatGPT.ViewModels.Chat;

Expand All @@ -8,6 +12,14 @@ namespace ChatGPT.CLI;
internal static class Chat
{
private static readonly Action<object>? s_log = Console.WriteLine;
private static readonly IChatSerializer s_chatSerializer;
private static readonly IChatService s_chatService;

static Chat()
{
s_chatSerializer = new SystemTextJsonChatSerializer();
s_chatService = new ChatService(s_chatSerializer);
}

private static async Task RunJob(ChatJob job, CancellationToken token)
{
Expand All @@ -19,7 +31,7 @@ private static async Task RunJob(ChatJob job, CancellationToken token)
}

var input = await File.ReadAllTextAsync(job.InputPath, token);
var chat = new ChatViewModel(job.Settings);
var chat = new ChatViewModel(s_chatService, s_chatSerializer, job.Settings);
chat.AddSystemMessage(job.Settings.Directions);
chat.AddUserMessage(input);
var result = await chat.SendAsync(chat.CreateChatMessages(), token);
Expand Down Expand Up @@ -129,7 +141,7 @@ private static List<ChatJob> GetJobs(CliSettings cliSettings, List<FileInfo> pat
using var stream = File.OpenRead(cliSettings.SettingsFile.FullName);
fileSettings = JsonSerializer.Deserialize(
stream,
MainViewModelJsonContext.s_instance.ChatSettingsViewModel);
CoreJsonContext.s_instance.ChatSettingsViewModel);
}

var chatSettings = fileSettings ?? new ChatSettingsViewModel
Expand Down
2 changes: 0 additions & 2 deletions src/ChatGPT.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using ChatGPT;
using ChatGPT.CLI;

Defaults.ConfigureDefaultServices();

await CreateRootCommand().InvokeAsync(args);

RootCommand CreateRootCommand()
Expand Down
1 change: 0 additions & 1 deletion src/ChatGPT.Core/ChatGPT.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="$(CommunityToolkitMvvmVersion)" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(MicrosoftExtensionsDependencyInjectionVersion)" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<PackageReference Include="Microsoft.VisualBasic" Version="10.3.0" />
Expand Down
33 changes: 0 additions & 33 deletions src/ChatGPT.Core/Defaults.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
using System;
using AI.Model.Services;
using AI.Services;
using ChatGPT.Model.Services;
using ChatGPT.Services;
using ChatGPT.ViewModels.Chat;
using ChatGPT.ViewModels.Settings;
using CommunityToolkit.Mvvm.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

namespace ChatGPT;

public static class Defaults
Expand Down Expand Up @@ -35,27 +25,4 @@ public static class Defaults
public const string MarkdownMessageFormat = "Markdown";

public const string HtmlMessageTextFormat = "Html";

public static Ioc Locator = Ioc.Default;

public static IServiceProvider ConfigureDefaultServices()
{
IServiceCollection serviceCollection = new ServiceCollection();

serviceCollection.AddSingleton<IChatSerializer, SystemTextJsonChatSerializer>();
serviceCollection.AddSingleton<IStorageFactory, IsolatedStorageFactory>();
serviceCollection.AddSingleton<IChatService, ChatService>();

serviceCollection.AddTransient<ChatMessageViewModel>();
serviceCollection.AddTransient<ChatSettingsViewModel>();
serviceCollection.AddTransient<ChatResultViewModel>();
serviceCollection.AddTransient<ChatViewModel>();
serviceCollection.AddTransient<PromptViewModel>();

IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

Locator.ConfigureServices(serviceProvider);

return serviceProvider;
}
}
13 changes: 0 additions & 13 deletions src/ChatGPT.Core/Model/Plugins/IChatPlugin.cs

This file was deleted.

10 changes: 0 additions & 10 deletions src/ChatGPT.Core/Model/Plugins/IPluginContext.cs

This file was deleted.

9 changes: 0 additions & 9 deletions src/ChatGPT.Core/Model/Services/IPluginsService.cs

This file was deleted.

31 changes: 0 additions & 31 deletions src/ChatGPT.Core/Plugins/DummyChatPlugin.cs

This file was deleted.

Loading

0 comments on commit e607d1c

Please sign in to comment.