Skip to content

Commit e607d1c

Browse files
committed
Remove usage of the Ioc.Default
1 parent c0309df commit e607d1c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+437
-792
lines changed

Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<PropertyGroup>
33
<VersionPrefix>1.0.0</VersionPrefix>
4-
<VersionSuffix>preview.22</VersionSuffix>
4+
<VersionSuffix>preview.23</VersionSuffix>
55
<Authors>Wiesław Šoltés</Authors>
66
<Company>Wiesław Šoltés</Company>
77
<Copyright>Copyright © Wiesław Šoltés 2024</Copyright>

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ You can import [OpenAI ChatGPT web version](https://chat.openai.com/chat) chats
5454
- [Markdown.Avalonia](https://github.com/whistyun/Markdown.Avalonia)
5555
- [Avalonia.HtmlRenderer](https://github.com/AvaloniaUI/Avalonia.HtmlRenderer)
5656
- [CommunityToolkit.Mvvm](https://github.com/CommunityToolkit/dotnet)
57-
- [Microsoft.Extensions.DependencyInjection](https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection/)
5857

5958
# .NET tool
6059

samples/ChatGPT.CLI.Chaining/Program.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using ChatGPT;
1+
using AI.Services;
22
using ChatGPT.ViewModels.Chat;
33

4-
Defaults.ConfigureDefaultServices();
4+
var chatSerializer = new SystemTextJsonChatSerializer();
5+
var chatService = new ChatService(chatSerializer);
56

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

1819
async Task<string?> SendAsync(string directions, string input, CancellationToken token)
1920
{
20-
var chat = new ChatViewModel(directions);
21+
var chat = new ChatViewModel(chatService, chatSerializer,directions);
2122
chat.AddSystemMessage(directions);
2223
chat.AddUserMessage(input);
2324
var result = await chat.SendAsync(chat.CreateChatMessages(), token);

samples/ChatGPT.CLI.FunctionCalling/Program.cs

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using ChatGPT;
1+
using AI.Services;
22
using ChatGPT.ViewModels.Chat;
33

4-
Defaults.ConfigureDefaultServices();
4+
var chatSerializer = new SystemTextJsonChatSerializer();
5+
var chatService = new ChatService(chatSerializer);
56

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

2122
var functions = GetFunctions();
2223

23-
var chat = new ChatViewModel(new ChatSettingsViewModel
24-
{
25-
MaxTokens = 2000,
26-
Model = "gpt-3.5-turbo-0613",
27-
Functions = functions,
28-
FunctionCall = "auto"
29-
// Force function call by setting FunctionCall property.
30-
// FunctionCall = new { name = "GetCurrentWeather" }
31-
});
24+
var chat = new ChatViewModel(
25+
chatService,
26+
chatSerializer,
27+
new ChatSettingsViewModel
28+
{
29+
MaxTokens = 2000,
30+
Model = "gpt-3.5-turbo-0613",
31+
Functions = functions,
32+
FunctionCall = "auto"
33+
// Force function call by setting FunctionCall property.
34+
// FunctionCall = new { name = "GetCurrentWeather" }
35+
});
3236

3337
// Enable to debug json requests and responses.
3438
// chat.Debug = true;

samples/ChatGPT.CLI.Repl/Program.cs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using ChatGPT;
1+
using AI.Services;
22
using ChatGPT.ViewModels.Chat;
33

4-
Defaults.ConfigureDefaultServices();
4+
var chatSerializer = new SystemTextJsonChatSerializer();
5+
var chatService = new ChatService(chatSerializer);
56

67
var directions =
78
"""
@@ -17,11 +18,14 @@ Do not use markdown.
1718

1819
using var cts = new CancellationTokenSource();
1920

20-
var chat = new ChatViewModel(new ChatSettingsViewModel
21-
{
22-
MaxTokens = 2000,
23-
Model = "gpt-3.5-turbo"
24-
});
21+
var chat = new ChatViewModel(
22+
chatService,
23+
chatSerializer,
24+
new ChatSettingsViewModel
25+
{
26+
MaxTokens = 2000,
27+
Model = "gpt-3.5-turbo"
28+
});
2529

2630
chat.AddSystemMessage(directions);
2731

samples/ChatGPT.UI.Designer/MainWindow.axaml.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Threading;
3+
using AI.Services;
34
using Avalonia;
45
using Avalonia.Controls;
56
using Avalonia.Interactivity;
@@ -10,13 +11,17 @@ namespace ChatGPT;
1011

1112
public partial class MainWindow : Window
1213
{
14+
private readonly SystemTextJsonChatSerializer _chatSerializer;
15+
private readonly ChatService _chatService;
16+
1317
public MainWindow()
1418
{
1519
InitializeComponent();
1620
#if DEBUG
1721
this.AttachDevTools();
1822
#endif
19-
Defaults.ConfigureDefaultServices();
23+
_chatSerializer = new SystemTextJsonChatSerializer();
24+
_chatService = new ChatService(_chatSerializer);
2025
}
2126

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

5560
var cts = new CancellationTokenSource();
56-
var chat = new ChatViewModel(chatSettings);
61+
var chat = new ChatViewModel(_chatService, _chatSerializer, chatSettings);
5762
chat.AddSystemMessage(chatSettings.Directions);
5863
chat.AddUserMessage(prompt);
5964
var result = await chat.SendAsync(chat.CreateChatMessages(), cts.Token);

samples/ChatGPT.UI.Game/Game.cs

+23-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Text.Json;
33
using System.Threading;
44
using System.Threading.Tasks;
5+
using AI.Services;
56
using ChatGPT.ViewModels.Chat;
67

78
namespace ChatGPT;
@@ -11,7 +12,15 @@ public class Game
1112
private string? _directions;
1213
private CancellationTokenSource? _cts;
1314
private ChatViewModel? _chat;
15+
private readonly SystemTextJsonChatSerializer _chatSerializer;
16+
private readonly ChatService _chatService;
1417

18+
public Game()
19+
{
20+
_chatSerializer = new SystemTextJsonChatSerializer();
21+
_chatService = new ChatService(_chatSerializer);
22+
}
23+
1524
public void New()
1625
{
1726
_directions =
@@ -35,17 +44,20 @@ Player has always three options to choose.
3544
}
3645
""";
3746

38-
_chat = new ChatViewModel(new ChatSettingsViewModel
39-
{
40-
Temperature = 0.7m,
41-
TopP = 1m,
42-
PresencePenalty = 0m,
43-
FrequencyPenalty = 0m,
44-
MaxTokens = 3000,
45-
ApiKey = null,
46-
// Model = "gpt-3.5-turbo",
47-
Model = "gpt-4",
48-
});
47+
_chat = new ChatViewModel(
48+
_chatService,
49+
_chatSerializer,
50+
new ChatSettingsViewModel
51+
{
52+
Temperature = 0.7m,
53+
TopP = 1m,
54+
PresencePenalty = 0m,
55+
FrequencyPenalty = 0m,
56+
MaxTokens = 3000,
57+
ApiKey = null,
58+
// Model = "gpt-3.5-turbo",
59+
Model = "gpt-4",
60+
});
4961

5062
_chat.AddSystemMessage(_directions);
5163
}

samples/ChatGPT.UI.Game/MainWindow.axaml.cs

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ public MainWindow()
1515
#if DEBUG
1616
this.AttachDevTools();
1717
#endif
18-
Defaults.ConfigureDefaultServices();
19-
2018
_game = new Game();
2119
_game.New();
2220

src/ChatGPT.CLI/Chat.cs

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
using System.Diagnostics;
22
using System.Text.Json;
3+
using AI.Model.Services;
4+
using AI.Services;
5+
using ChatGPT.Model.Services;
6+
using ChatGPT.Services;
37
using ChatGPT.ViewModels;
48
using ChatGPT.ViewModels.Chat;
59

@@ -8,6 +12,14 @@ namespace ChatGPT.CLI;
812
internal static class Chat
913
{
1014
private static readonly Action<object>? s_log = Console.WriteLine;
15+
private static readonly IChatSerializer s_chatSerializer;
16+
private static readonly IChatService s_chatService;
17+
18+
static Chat()
19+
{
20+
s_chatSerializer = new SystemTextJsonChatSerializer();
21+
s_chatService = new ChatService(s_chatSerializer);
22+
}
1123

1224
private static async Task RunJob(ChatJob job, CancellationToken token)
1325
{
@@ -19,7 +31,7 @@ private static async Task RunJob(ChatJob job, CancellationToken token)
1931
}
2032

2133
var input = await File.ReadAllTextAsync(job.InputPath, token);
22-
var chat = new ChatViewModel(job.Settings);
34+
var chat = new ChatViewModel(s_chatService, s_chatSerializer, job.Settings);
2335
chat.AddSystemMessage(job.Settings.Directions);
2436
chat.AddUserMessage(input);
2537
var result = await chat.SendAsync(chat.CreateChatMessages(), token);
@@ -129,7 +141,7 @@ private static List<ChatJob> GetJobs(CliSettings cliSettings, List<FileInfo> pat
129141
using var stream = File.OpenRead(cliSettings.SettingsFile.FullName);
130142
fileSettings = JsonSerializer.Deserialize(
131143
stream,
132-
MainViewModelJsonContext.s_instance.ChatSettingsViewModel);
144+
CoreJsonContext.s_instance.ChatSettingsViewModel);
133145
}
134146

135147
var chatSettings = fileSettings ?? new ChatSettingsViewModel

src/ChatGPT.CLI/Program.cs

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
using ChatGPT;
44
using ChatGPT.CLI;
55

6-
Defaults.ConfigureDefaultServices();
7-
86
await CreateRootCommand().InvokeAsync(args);
97

108
RootCommand CreateRootCommand()

src/ChatGPT.Core/ChatGPT.Core.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
<ItemGroup>
1919
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
2020
<PackageReference Include="CommunityToolkit.Mvvm" Version="$(CommunityToolkitMvvmVersion)" />
21-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(MicrosoftExtensionsDependencyInjectionVersion)" />
2221
</ItemGroup>
2322
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
2423
<PackageReference Include="Microsoft.VisualBasic" Version="10.3.0" />

src/ChatGPT.Core/Defaults.cs

-33
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
using System;
2-
using AI.Model.Services;
3-
using AI.Services;
4-
using ChatGPT.Model.Services;
5-
using ChatGPT.Services;
6-
using ChatGPT.ViewModels.Chat;
7-
using ChatGPT.ViewModels.Settings;
8-
using CommunityToolkit.Mvvm.DependencyInjection;
9-
using Microsoft.Extensions.DependencyInjection;
10-
111
namespace ChatGPT;
122

133
public static class Defaults
@@ -35,27 +25,4 @@ public static class Defaults
3525
public const string MarkdownMessageFormat = "Markdown";
3626

3727
public const string HtmlMessageTextFormat = "Html";
38-
39-
public static Ioc Locator = Ioc.Default;
40-
41-
public static IServiceProvider ConfigureDefaultServices()
42-
{
43-
IServiceCollection serviceCollection = new ServiceCollection();
44-
45-
serviceCollection.AddSingleton<IChatSerializer, SystemTextJsonChatSerializer>();
46-
serviceCollection.AddSingleton<IStorageFactory, IsolatedStorageFactory>();
47-
serviceCollection.AddSingleton<IChatService, ChatService>();
48-
49-
serviceCollection.AddTransient<ChatMessageViewModel>();
50-
serviceCollection.AddTransient<ChatSettingsViewModel>();
51-
serviceCollection.AddTransient<ChatResultViewModel>();
52-
serviceCollection.AddTransient<ChatViewModel>();
53-
serviceCollection.AddTransient<PromptViewModel>();
54-
55-
IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
56-
57-
Locator.ConfigureServices(serviceProvider);
58-
59-
return serviceProvider;
60-
}
6128
}

src/ChatGPT.Core/Model/Plugins/IChatPlugin.cs

-13
This file was deleted.

src/ChatGPT.Core/Model/Plugins/IPluginContext.cs

-10
This file was deleted.

src/ChatGPT.Core/Model/Services/IPluginsService.cs

-9
This file was deleted.

src/ChatGPT.Core/Plugins/DummyChatPlugin.cs

-31
This file was deleted.

0 commit comments

Comments
 (0)