@@ -11,43 +11,43 @@ public static class DashScopeServiceCollectionExtensions
11
11
public static IKernelBuilder AddDashScopeChatCompletion (
12
12
this IKernelBuilder builder ,
13
13
string ? serviceId = null ,
14
+ Action < DashScopeClientOptions > ? configureOptions = null ,
14
15
Action < HttpClient > ? configureClient = null ,
15
16
string configSectionPath = "dashscope" )
16
17
{
17
18
Func < IServiceProvider , object ? , DashScopeChatCompletionService > factory = ( serviceProvider , _ ) =>
18
19
serviceProvider . GetRequiredService < DashScopeChatCompletionService > ( ) ;
19
20
20
- if ( configureClient == null )
21
- {
22
- builder . Services . AddHttpClient < DashScopeChatCompletionService > ( ) ;
23
- }
24
- else
25
- {
26
- builder . Services . AddHttpClient < DashScopeChatCompletionService > ( configureClient ) ;
27
- }
21
+ var optionsBuilder = builder . Services . AddOptions < DashScopeClientOptions > ( ) . BindConfiguration ( configSectionPath ) ;
22
+ if ( configureOptions != null ) optionsBuilder . PostConfigure ( configureOptions ) ;
23
+
24
+ var httpClientBuilder = configureClient == null
25
+ ? builder . Services . AddHttpClient < DashScopeChatCompletionService > ( )
26
+ : builder . Services . AddHttpClient < DashScopeChatCompletionService > ( configureClient ) ;
28
27
29
- builder . Services . AddOptions < DashScopeClientOptions > ( ) . BindConfiguration ( configSectionPath ) ;
30
28
builder . Services . AddKeyedSingleton < IChatCompletionService > ( serviceId , factory ) ;
31
29
return builder ;
32
30
}
33
31
34
32
public static IKernelBuilder AddDashScopeChatCompletion < T > (
35
33
this IKernelBuilder builder ,
34
+ string ? modelId = null ,
35
+ string ? apiKey = null ,
36
36
string ? serviceId = null ,
37
+ Action < DashScopeClientOptions > ? configureOptions = null ,
37
38
Action < HttpClient > ? configureClient = null ,
38
39
string configSectionPath = "dashscope" ) where T : class
39
40
{
40
- if ( ! builder . Services . Any ( s => s . ServiceType == typeof ( IConfiguration ) ) )
41
+ builder . Services . AddConfiguration < T > ( ) ;
42
+
43
+ void AggConfigureOptions ( DashScopeClientOptions options )
41
44
{
42
- IConfiguration config = new ConfigurationBuilder ( )
43
- . SetBasePath ( Directory . GetCurrentDirectory ( ) )
44
- . AddEnvironmentVariables ( )
45
- . AddJsonFile ( "appsettings.json" , true )
46
- . AddUserSecrets < T > ( )
47
- . Build ( ) ;
48
- builder . Services . TryAddSingleton ( config ) ;
45
+ if ( ! string . IsNullOrEmpty ( modelId ) ) options . ModelId = modelId ;
46
+ if ( ! string . IsNullOrEmpty ( apiKey ) ) options . ApiKey = apiKey ;
47
+ configureOptions ? . Invoke ( options ) ;
49
48
}
50
- return builder . AddDashScopeChatCompletion ( serviceId , configureClient , configSectionPath ) ;
49
+
50
+ return builder . AddDashScopeChatCompletion ( serviceId , AggConfigureOptions , configureClient , configSectionPath ) ;
51
51
}
52
52
53
53
public static IKernelBuilder AddDashScopeChatCompletion (
@@ -69,4 +69,20 @@ public static IKernelBuilder AddDashScopeChatCompletion(
69
69
builder . Services . AddKeyedSingleton < IChatCompletionService > ( serviceId , factory ) ;
70
70
return builder ;
71
71
}
72
+
73
+ private static IServiceCollection AddConfiguration < T > ( this IServiceCollection services ) where T : class
74
+ {
75
+ if ( ! services . Any ( s => s . ServiceType == typeof ( IConfiguration ) ) )
76
+ {
77
+ IConfiguration config = new ConfigurationBuilder ( )
78
+ . SetBasePath ( Directory . GetCurrentDirectory ( ) )
79
+ . AddEnvironmentVariables ( )
80
+ . AddJsonFile ( "appsettings.json" , true )
81
+ . AddUserSecrets < T > ( )
82
+ . Build ( ) ;
83
+ services . TryAddSingleton ( config ) ;
84
+ }
85
+
86
+ return services ;
87
+ }
72
88
}
0 commit comments