1
- using Microsoft . Extensions . DependencyInjection ;
2
- using Microsoft . Extensions . Options ;
1
+ using Microsoft . Extensions . Configuration ;
2
+ using Microsoft . Extensions . DependencyInjection ;
3
3
4
4
namespace OpenAI . ChatGpt . AspNetCore . Extensions ;
5
5
@@ -14,12 +14,16 @@ public static class ServiceCollectionExtensions
14
14
15
15
public static IServiceCollection AddChatGptInMemoryIntegration (
16
16
this IServiceCollection services ,
17
+ IConfiguration configuration ,
17
18
bool injectInMemoryChatService = true ,
18
- string credentialsConfigSectionPath = OpenAiCredentialsConfigSectionPathDefault ,
19
19
string completionsConfigSectionPath = ChatGPTConfigSectionPathDefault ,
20
+ string credentialsConfigSectionPath = OpenAiCredentialsConfigSectionPathDefault ,
21
+ string azureOpenAiCredentialsConfigSectionPath = AzureOpenAiCredentialsConfigSectionPathDefault ,
22
+ string openRouterCredentialsConfigSectionPath = OpenRouterCredentialsConfigSectionPathDefault ,
20
23
bool validateAiClientProviderOnStart = true )
21
24
{
22
25
ArgumentNullException . ThrowIfNull ( services ) ;
26
+ ArgumentNullException . ThrowIfNull ( configuration ) ;
23
27
if ( string . IsNullOrWhiteSpace ( credentialsConfigSectionPath ) )
24
28
{
25
29
throw new ArgumentException ( "Value cannot be null or whitespace." ,
@@ -32,15 +36,29 @@ public static IServiceCollection AddChatGptInMemoryIntegration(
32
36
nameof ( completionsConfigSectionPath ) ) ;
33
37
}
34
38
39
+ if ( string . IsNullOrWhiteSpace ( azureOpenAiCredentialsConfigSectionPath ) )
40
+ {
41
+ throw new ArgumentException ( "Value cannot be null or whitespace." ,
42
+ nameof ( azureOpenAiCredentialsConfigSectionPath ) ) ;
43
+ }
44
+ if ( string . IsNullOrWhiteSpace ( openRouterCredentialsConfigSectionPath ) )
45
+ {
46
+ throw new ArgumentException ( "Value cannot be null or whitespace." ,
47
+ nameof ( openRouterCredentialsConfigSectionPath ) ) ;
48
+ }
49
+
35
50
services . AddSingleton < IChatHistoryStorage , InMemoryChatHistoryStorage > ( ) ;
36
51
if ( injectInMemoryChatService )
37
52
{
38
53
services . AddScoped < ChatService > ( CreateChatService ) ;
39
54
}
40
55
41
56
return services . AddChatGptIntegrationCore (
42
- credentialsConfigSectionPath : credentialsConfigSectionPath ,
57
+ configuration ,
43
58
completionsConfigSectionPath : completionsConfigSectionPath ,
59
+ credentialsConfigSectionPath : credentialsConfigSectionPath ,
60
+ azureOpenAiCredentialsConfigSectionPath ,
61
+ openRouterCredentialsConfigSectionPath ,
44
62
validateAiClientProviderOnStart : validateAiClientProviderOnStart
45
63
) ;
46
64
}
@@ -69,14 +87,16 @@ private static ChatService CreateChatService(IServiceProvider provider)
69
87
}
70
88
71
89
public static IServiceCollection AddChatGptIntegrationCore ( this IServiceCollection services ,
72
- string credentialsConfigSectionPath = OpenAiCredentialsConfigSectionPathDefault ,
90
+ IConfiguration configuration ,
73
91
string completionsConfigSectionPath = ChatGPTConfigSectionPathDefault ,
92
+ string credentialsConfigSectionPath = OpenAiCredentialsConfigSectionPathDefault ,
74
93
string azureOpenAiCredentialsConfigSectionPath = AzureOpenAiCredentialsConfigSectionPathDefault ,
75
94
string openRouterCredentialsConfigSectionPath = OpenRouterCredentialsConfigSectionPathDefault ,
76
95
ServiceLifetime gptFactoryLifetime = ServiceLifetime . Scoped ,
77
96
bool validateAiClientProviderOnStart = true )
78
97
{
79
98
ArgumentNullException . ThrowIfNull ( services ) ;
99
+ ArgumentNullException . ThrowIfNull ( configuration ) ;
80
100
if ( string . IsNullOrWhiteSpace ( credentialsConfigSectionPath ) )
81
101
{
82
102
throw new ArgumentException ( "Value cannot be null or whitespace." ,
@@ -89,12 +109,55 @@ public static IServiceCollection AddChatGptIntegrationCore(this IServiceCollecti
89
109
nameof ( completionsConfigSectionPath ) ) ;
90
110
}
91
111
112
+ if ( string . IsNullOrWhiteSpace ( azureOpenAiCredentialsConfigSectionPath ) )
113
+ {
114
+ throw new ArgumentException ( "Value cannot be null or whitespace." ,
115
+ nameof ( azureOpenAiCredentialsConfigSectionPath ) ) ;
116
+ }
117
+ if ( string . IsNullOrWhiteSpace ( openRouterCredentialsConfigSectionPath ) )
118
+ {
119
+ throw new ArgumentException ( "Value cannot be null or whitespace." ,
120
+ nameof ( openRouterCredentialsConfigSectionPath ) ) ;
121
+ }
92
122
123
+ services . AddOptions < ChatGPTConfig > ( )
124
+ . BindConfiguration ( completionsConfigSectionPath )
125
+ . Configure ( _ => { } ) //make optional
126
+ . ValidateDataAnnotations ( )
127
+ . ValidateOnStart ( ) ;
128
+
129
+ services . AddSingleton < ITimeProvider , TimeProviderUtc > ( ) ;
130
+ services . Add ( new ServiceDescriptor ( typeof ( ChatGPTFactory ) , typeof ( ChatGPTFactory ) , gptFactoryLifetime ) ) ;
131
+
132
+ services . AddAiClient ( configuration , credentialsConfigSectionPath , azureOpenAiCredentialsConfigSectionPath , openRouterCredentialsConfigSectionPath , validateAiClientProviderOnStart ) ;
133
+
134
+ return services ;
135
+ }
136
+
137
+ internal static void AddAiClient (
138
+ this IServiceCollection services ,
139
+ IConfiguration configuration ,
140
+ string credentialsConfigSectionPath ,
141
+ string azureOpenAiCredentialsConfigSectionPath ,
142
+ string openRouterCredentialsConfigSectionPath ,
143
+ bool validateAiClientProviderOnStart )
144
+ {
145
+ ArgumentNullException . ThrowIfNull ( services ) ;
146
+ ArgumentNullException . ThrowIfNull ( configuration ) ;
147
+ if ( string . IsNullOrWhiteSpace ( credentialsConfigSectionPath ) )
148
+ throw new ArgumentException ( "Value cannot be null or whitespace." , nameof ( credentialsConfigSectionPath ) ) ;
149
+ if ( string . IsNullOrWhiteSpace ( azureOpenAiCredentialsConfigSectionPath ) )
150
+ throw new ArgumentException ( "Value cannot be null or whitespace." ,
151
+ nameof ( azureOpenAiCredentialsConfigSectionPath ) ) ;
152
+ if ( string . IsNullOrWhiteSpace ( openRouterCredentialsConfigSectionPath ) )
153
+ throw new ArgumentException ( "Value cannot be null or whitespace." ,
154
+ nameof ( openRouterCredentialsConfigSectionPath ) ) ;
155
+
93
156
services . AddOptions < OpenAICredentials > ( )
94
157
. BindConfiguration ( credentialsConfigSectionPath )
95
158
. Configure ( _ => { } ) //make optional
96
159
. ValidateDataAnnotations ( )
97
- . ValidateOnStart ( ) ;
160
+ . ValidateOnStart ( ) ;
98
161
services . AddOptions < AzureOpenAICredentials > ( )
99
162
. BindConfiguration ( azureOpenAiCredentialsConfigSectionPath )
100
163
. Configure ( _ => { } ) //make optional
@@ -105,30 +168,21 @@ public static IServiceCollection AddChatGptIntegrationCore(this IServiceCollecti
105
168
. Configure ( _ => { } ) //make optional
106
169
. ValidateDataAnnotations ( )
107
170
. ValidateOnStart ( ) ;
108
-
109
- services . AddOptions < ChatGPTConfig > ( )
110
- . BindConfiguration ( completionsConfigSectionPath )
111
- . Configure ( _ => { } ) //make optional
112
- . ValidateDataAnnotations ( )
113
- . ValidateOnStart ( ) ;
114
-
115
- services . AddSingleton < ITimeProvider , TimeProviderUtc > ( ) ;
116
- services . Add ( new ServiceDescriptor ( typeof ( ChatGPTFactory ) , typeof ( ChatGPTFactory ) , gptFactoryLifetime ) ) ;
117
171
118
172
services . AddHttpClient ( nameof ( OpenAiClient ) ) ;
119
173
services . AddHttpClient ( nameof ( AzureOpenAiClient ) ) ;
120
174
services . AddHttpClient ( nameof ( OpenRouterClient ) ) ;
121
175
122
176
services . AddSingleton < IAiClient , AiClientFromConfiguration > ( ) ;
177
+ services . AddSingleton < AiClientFactory > ( ) ;
123
178
#pragma warning disable CS0618 // Type or member is obsolete
179
+ // will be removed in 5.0
124
180
services . AddSingleton < IOpenAiClient , AiClientFromConfiguration > ( ) ;
125
181
#pragma warning restore CS0618 // Type or member is obsolete
126
182
127
183
if ( validateAiClientProviderOnStart )
128
184
{
129
185
services . AddHostedService < AiClientStartupValidationBackgroundService > ( ) ;
130
186
}
131
-
132
- return services ;
133
187
}
134
188
}
0 commit comments