Skip to content

Commit 499401d

Browse files
authored
Merge pull request #20 from cnblogs/support-null-logger
refactor: support null logger
2 parents 960c161 + b47dfe4 commit 499401d

File tree

5 files changed

+38
-33
lines changed

5 files changed

+38
-33
lines changed

README.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
Make DashScope work with Semantic Kernel and Kernel Memory.
44

55
## Get started with SemanticKernel
6+
67
Add the NuGet package to your project.
8+
79
```shell
810
dotnet add package Cnblogs.SemanticKernel.Connectors.DashScope
911
```
@@ -12,7 +14,7 @@ dotnet add package Cnblogs.SemanticKernel.Connectors.DashScope
1214
using Microsoft.SemanticKernel;
1315

1416
var builder = Kernel.CreateBuilder();
15-
builder.Services.AddLogging().AddDashScopeChatCompletion("your-api-key", "qwen-max");
17+
builder.Services.AddDashScopeChatCompletion("your-api-key", "qwen-max");
1618
var kernel = builder.Build();
1719

1820
var prompt = "<message role=\"user\">Tell me about the Cnblogs</message>";
@@ -32,15 +34,16 @@ Install Nuget package `Microsoft.KernelMemory.SemanticKernelPlugin`
3234

3335
```json
3436
{
35-
"dashScope": {
36-
"apiKey": "your-key",
37-
"chatCompletionModelId": "qwen-max",
38-
"textEmbeddingModelId": "text-embedding-v2"
39-
}
37+
"dashScope": {
38+
"apiKey": "your-key",
39+
"chatCompletionModelId": "qwen-max",
40+
"textEmbeddingModelId": "text-embedding-v2"
41+
}
4042
}
4143
```
4244

4345
`Program.cs`
46+
4447
```csharp
4548
// Kernel Memory stuff
4649
var memory = new KernelMemoryBuilder(builder.Services).WithDashScope(builder.Configuration).Build();

src/SemanticKernel.DashScope/DashScopeChatCompletionService.cs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using System.Runtime.CompilerServices;
1+
using System.Runtime.CompilerServices;
22
using System.Text.Json;
33
using Cnblogs.DashScope.Core;
44
using Microsoft.Extensions.Logging;
5+
using Microsoft.Extensions.Logging.Abstractions;
56
using Microsoft.SemanticKernel;
67
using Microsoft.SemanticKernel.ChatCompletion;
78
using Microsoft.SemanticKernel.Services;
@@ -17,22 +18,24 @@ public sealed class DashScopeChatCompletionService : IChatCompletionService, ITe
1718
private readonly IDashScopeClient _dashScopeClient;
1819
private readonly Dictionary<string, object?> _attributes = new();
1920
private readonly string _modelId;
20-
private readonly ILogger<DashScopeChatCompletionService> _logger;
21+
private readonly ILogger _logger;
2122

2223
/// <summary>
2324
/// Creates a new DashScope chat completion service.
2425
/// </summary>
2526
/// <param name="modelId"></param>
2627
/// <param name="dashScopeClient"></param>
27-
/// <param name="logger"></param>
28+
/// <param name="loggerFactory"></param>
2829
public DashScopeChatCompletionService(
2930
string modelId,
3031
IDashScopeClient dashScopeClient,
31-
ILogger<DashScopeChatCompletionService> logger)
32+
ILoggerFactory? loggerFactory = null)
3233
{
3334
_dashScopeClient = dashScopeClient;
3435
_modelId = modelId;
35-
_logger = logger;
36+
_logger = loggerFactory != null
37+
? loggerFactory.CreateLogger<DashScopeChatCompletionService>()
38+
: NullLogger.Instance;
3639
_attributes.Add(AIServiceExtensions.ModelIdKey, _modelId);
3740
}
3841

@@ -50,7 +53,7 @@ public async Task<IReadOnlyList<ChatMessageContent>> GetChatMessageContentsAsync
5053
chatParameters.ToolCallBehavior?.ConfigureOptions(kernel, chatParameters);
5154

5255
var autoInvoke = kernel is not null && chatParameters.ToolCallBehavior?.MaximumAutoInvokeAttempts > 0;
53-
for (var it = 1;; it++)
56+
for (var it = 1; ; it++)
5457
{
5558
var response = await _dashScopeClient.GetTextCompletionAsync(
5659
new ModelRequest<TextGenerationInput, ITextGenerationParameters>

src/SemanticKernel.DashScope/DashScopeServiceCollectionExtensions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Cnblogs.DashScope.Core;
1+
using Cnblogs.DashScope.Core;
22
using Cnblogs.SemanticKernel.Connectors.DashScope;
33
using Microsoft.Extensions.Configuration;
44
using Microsoft.Extensions.DependencyInjection;
@@ -95,13 +95,13 @@ public static IServiceCollection AddDashScopeChatCompletion(
9595
(sp, _) => new DashScopeChatCompletionService(
9696
modelId,
9797
new DashScopeClient(apiKey),
98-
sp.GetRequiredService<ILogger<DashScopeChatCompletionService>>()));
98+
sp.GetService<ILoggerFactory>()));
9999
return services.AddKeyedSingleton<IChatCompletionService, DashScopeChatCompletionService>(
100100
serviceId,
101101
(sp, _) => new DashScopeChatCompletionService(
102102
modelId,
103103
new DashScopeClient(apiKey),
104-
sp.GetRequiredService<ILogger<DashScopeChatCompletionService>>()));
104+
sp.GetService<ILoggerFactory>()));
105105
}
106106

107107
#endregion

test/SemanticKernel.DashScope.UnitTest/ChatCompletionTests.cs

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using Cnblogs.DashScope.Core;
1+
using Cnblogs.DashScope.Core;
22
using Cnblogs.SemanticKernel.Connectors.DashScope;
33
using FluentAssertions;
44
using Microsoft.Extensions.Logging.Abstractions;
55
using Microsoft.SemanticKernel;
66
using Microsoft.SemanticKernel.ChatCompletion;
77
using NSubstitute;
8-
using NSubstitute.Core;
98
using NSubstitute.Extensions;
109

1110
namespace SemanticKernel.DashScope.UnitTest;
@@ -24,7 +23,7 @@ public async Task ChatCompletion_Normal_SuccessAsync(PromptExecutionSettings? se
2423
var service = new DashScopeChatCompletionService(
2524
Cases.ModelId,
2625
dashScopeClient,
27-
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
26+
NullLoggerFactory.Instance);
2827

2928
// Act
3029
var response = await service.GetChatMessageContentsAsync(Cases.ChatHistory, settings);
@@ -60,7 +59,7 @@ public async Task ChatCompletion_ToolCalling_SuccessAsync()
6059
var service = new DashScopeChatCompletionService(
6160
Cases.ModelId,
6261
dashScopeClient,
63-
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
62+
NullLoggerFactory.Instance);
6463
var settings =
6564
new DashScopePromptExecutionSettings { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
6665
var history = new ChatHistory();
@@ -94,7 +93,7 @@ public async Task ChatCompletion_MaximumToolCallingCount_SuccessAsync()
9493
var service = new DashScopeChatCompletionService(
9594
Cases.ModelId,
9695
dashScopeClient,
97-
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
96+
NullLoggerFactory.Instance);
9897
var settings =
9998
new DashScopePromptExecutionSettings { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
10099
var history = new ChatHistory();
@@ -124,7 +123,7 @@ public async Task ChatCompletion_ToolTypeIsNotFunction_SkipAsync()
124123
var service = new DashScopeChatCompletionService(
125124
Cases.ModelId,
126125
dashScopeClient,
127-
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
126+
NullLoggerFactory.Instance);
128127
var settings =
129128
new DashScopePromptExecutionSettings { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
130129
var history = new ChatHistory();
@@ -154,7 +153,7 @@ public async Task ChatCompletion_FunctionCallWithMalformedJson_SkipAsync()
154153
var service = new DashScopeChatCompletionService(
155154
Cases.ModelId,
156155
dashScopeClient,
157-
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
156+
NullLoggerFactory.Instance);
158157
var settings =
159158
new DashScopePromptExecutionSettings { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
160159
var history = new ChatHistory();
@@ -185,7 +184,7 @@ public async Task ChatCompletion_FunctionThrowException_SkipAsync()
185184
var service = new DashScopeChatCompletionService(
186185
Cases.ModelId,
187186
dashScopeClient,
188-
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
187+
NullLoggerFactory.Instance);
189188
var settings =
190189
new DashScopePromptExecutionSettings { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
191190
var history = new ChatHistory();
@@ -218,7 +217,7 @@ public async Task ChatCompletion_FunctionDoesNotExists_SkipAsync()
218217
var service = new DashScopeChatCompletionService(
219218
Cases.ModelId,
220219
dashScopeClient,
221-
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
220+
NullLoggerFactory.Instance);
222221
var settings =
223222
new DashScopePromptExecutionSettings
224223
{
@@ -252,7 +251,7 @@ public async Task ChatCompletion_CallingNotProvidedFunction_SkipAsync()
252251
var service = new DashScopeChatCompletionService(
253252
Cases.ModelId,
254253
dashScopeClient,
255-
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
254+
NullLoggerFactory.Instance);
256255
var settings =
257256
new DashScopePromptExecutionSettings
258257
{
@@ -279,7 +278,7 @@ public async Task ChatCompletion_CustomModel_SuccessAsync()
279278
var service = new DashScopeChatCompletionService(
280279
Cases.ModelId,
281280
dashScopeClient,
282-
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
281+
NullLoggerFactory.Instance);
283282
var settings = new DashScopePromptExecutionSettings { ModelId = Cases.ModelIdAlter };
284283

285284
// Act
@@ -303,7 +302,7 @@ public async Task ChatCompletionStream_Normal_SuccessAsync(PromptExecutionSettin
303302
var service = new DashScopeChatCompletionService(
304303
Cases.ModelId,
305304
dashScopeClient,
306-
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
305+
NullLoggerFactory.Instance);
307306

308307
// Act
309308
var response = await service.GetStreamingChatMessageContentsAsync(Cases.ChatHistory, settings).ToListAsync();
@@ -336,7 +335,7 @@ public async Task ChatCompletionStream_CustomModel_SuccessAsync()
336335
var service = new DashScopeChatCompletionService(
337336
Cases.ModelId,
338337
dashScopeClient,
339-
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
338+
NullLoggerFactory.Instance);
340339
var settings = new DashScopePromptExecutionSettings { ModelId = Cases.ModelIdAlter };
341340

342341
// Act

test/SemanticKernel.DashScope.UnitTest/TextCompletionTests.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Cnblogs.DashScope.Core;
1+
using Cnblogs.DashScope.Core;
22
using Cnblogs.SemanticKernel.Connectors.DashScope;
33
using FluentAssertions;
44
using Microsoft.Extensions.Logging.Abstractions;
@@ -22,7 +22,7 @@ public async Task GetTextContent_Normal_SuccessAsync(PromptExecutionSettings? se
2222
var service = new DashScopeChatCompletionService(
2323
Cases.ModelId,
2424
dashScopeClient,
25-
NullLogger<DashScopeChatCompletionService>.Instance);
25+
NullLoggerFactory.Instance);
2626

2727
// Act
2828
var response = await service.GetTextContentsAsync(Cases.Prompt, settings);
@@ -54,7 +54,7 @@ public async Task GetTextContent_OverrideModelId_SuccessAsync()
5454
var service = new DashScopeChatCompletionService(
5555
Cases.ModelId,
5656
dashScopeClient,
57-
NullLogger<DashScopeChatCompletionService>.Instance);
57+
NullLoggerFactory.Instance);
5858
var settings = new DashScopePromptExecutionSettings { ModelId = Cases.ModelIdAlter };
5959

6060
// Act
@@ -78,7 +78,7 @@ public async Task GetTextContentStream_Normal_SuccessAsync(PromptExecutionSettin
7878
var service = new DashScopeChatCompletionService(
7979
Cases.ModelId,
8080
dashScopeClient,
81-
NullLogger<DashScopeChatCompletionService>.Instance);
81+
NullLoggerFactory.Instance);
8282

8383
// Act
8484
var response = await service.GetStreamingTextContentsAsync(Cases.Prompt, settings).ToListAsync();
@@ -111,7 +111,7 @@ public async Task GetTextContentStream_OverrideModelId_SuccessAsync()
111111
var service = new DashScopeChatCompletionService(
112112
Cases.ModelId,
113113
dashScopeClient,
114-
NullLogger<DashScopeChatCompletionService>.Instance);
114+
NullLoggerFactory.Instance);
115115
var settings = new PromptExecutionSettings { ModelId = Cases.ModelIdAlter };
116116

117117
// Act

0 commit comments

Comments
 (0)