Skip to content

Commit

Permalink
Merge pull request #20 from cnblogs/support-null-logger
Browse files Browse the repository at this point in the history
refactor: support null logger
  • Loading branch information
cnblogs-dudu authored Mar 20, 2024
2 parents 960c161 + b47dfe4 commit 499401d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 33 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
Make DashScope work with Semantic Kernel and Kernel Memory.

## Get started with SemanticKernel

Add the NuGet package to your project.

```shell
dotnet add package Cnblogs.SemanticKernel.Connectors.DashScope
```
Expand All @@ -12,7 +14,7 @@ dotnet add package Cnblogs.SemanticKernel.Connectors.DashScope
using Microsoft.SemanticKernel;

var builder = Kernel.CreateBuilder();
builder.Services.AddLogging().AddDashScopeChatCompletion("your-api-key", "qwen-max");
builder.Services.AddDashScopeChatCompletion("your-api-key", "qwen-max");
var kernel = builder.Build();

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

```json
{
"dashScope": {
"apiKey": "your-key",
"chatCompletionModelId": "qwen-max",
"textEmbeddingModelId": "text-embedding-v2"
}
"dashScope": {
"apiKey": "your-key",
"chatCompletionModelId": "qwen-max",
"textEmbeddingModelId": "text-embedding-v2"
}
}
```

`Program.cs`

```csharp
// Kernel Memory stuff
var memory = new KernelMemoryBuilder(builder.Services).WithDashScope(builder.Configuration).Build();
Expand Down
15 changes: 9 additions & 6 deletions src/SemanticKernel.DashScope/DashScopeChatCompletionService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
using System.Text.Json;
using Cnblogs.DashScope.Core;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Services;
Expand All @@ -17,22 +18,24 @@ public sealed class DashScopeChatCompletionService : IChatCompletionService, ITe
private readonly IDashScopeClient _dashScopeClient;
private readonly Dictionary<string, object?> _attributes = new();
private readonly string _modelId;
private readonly ILogger<DashScopeChatCompletionService> _logger;
private readonly ILogger _logger;

/// <summary>
/// Creates a new DashScope chat completion service.
/// </summary>
/// <param name="modelId"></param>
/// <param name="dashScopeClient"></param>
/// <param name="logger"></param>
/// <param name="loggerFactory"></param>
public DashScopeChatCompletionService(
string modelId,
IDashScopeClient dashScopeClient,
ILogger<DashScopeChatCompletionService> logger)
ILoggerFactory? loggerFactory = null)
{
_dashScopeClient = dashScopeClient;
_modelId = modelId;
_logger = logger;
_logger = loggerFactory != null
? loggerFactory.CreateLogger<DashScopeChatCompletionService>()
: NullLogger.Instance;
_attributes.Add(AIServiceExtensions.ModelIdKey, _modelId);
}

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

var autoInvoke = kernel is not null && chatParameters.ToolCallBehavior?.MaximumAutoInvokeAttempts > 0;
for (var it = 1;; it++)
for (var it = 1; ; it++)
{
var response = await _dashScopeClient.GetTextCompletionAsync(
new ModelRequest<TextGenerationInput, ITextGenerationParameters>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Cnblogs.DashScope.Core;
using Cnblogs.DashScope.Core;
using Cnblogs.SemanticKernel.Connectors.DashScope;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -95,13 +95,13 @@ public static IServiceCollection AddDashScopeChatCompletion(
(sp, _) => new DashScopeChatCompletionService(
modelId,
new DashScopeClient(apiKey),
sp.GetRequiredService<ILogger<DashScopeChatCompletionService>>()));
sp.GetService<ILoggerFactory>()));
return services.AddKeyedSingleton<IChatCompletionService, DashScopeChatCompletionService>(
serviceId,
(sp, _) => new DashScopeChatCompletionService(
modelId,
new DashScopeClient(apiKey),
sp.GetRequiredService<ILogger<DashScopeChatCompletionService>>()));
sp.GetService<ILoggerFactory>()));
}

#endregion
Expand Down
25 changes: 12 additions & 13 deletions test/SemanticKernel.DashScope.UnitTest/ChatCompletionTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Cnblogs.DashScope.Core;
using Cnblogs.DashScope.Core;
using Cnblogs.SemanticKernel.Connectors.DashScope;
using FluentAssertions;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using NSubstitute;
using NSubstitute.Core;
using NSubstitute.Extensions;

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

// Act
var response = await service.GetChatMessageContentsAsync(Cases.ChatHistory, settings);
Expand Down Expand Up @@ -60,7 +59,7 @@ public async Task ChatCompletion_ToolCalling_SuccessAsync()
var service = new DashScopeChatCompletionService(
Cases.ModelId,
dashScopeClient,
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
NullLoggerFactory.Instance);
var settings =
new DashScopePromptExecutionSettings { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
var history = new ChatHistory();
Expand Down Expand Up @@ -94,7 +93,7 @@ public async Task ChatCompletion_MaximumToolCallingCount_SuccessAsync()
var service = new DashScopeChatCompletionService(
Cases.ModelId,
dashScopeClient,
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
NullLoggerFactory.Instance);
var settings =
new DashScopePromptExecutionSettings { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
var history = new ChatHistory();
Expand Down Expand Up @@ -124,7 +123,7 @@ public async Task ChatCompletion_ToolTypeIsNotFunction_SkipAsync()
var service = new DashScopeChatCompletionService(
Cases.ModelId,
dashScopeClient,
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
NullLoggerFactory.Instance);
var settings =
new DashScopePromptExecutionSettings { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
var history = new ChatHistory();
Expand Down Expand Up @@ -154,7 +153,7 @@ public async Task ChatCompletion_FunctionCallWithMalformedJson_SkipAsync()
var service = new DashScopeChatCompletionService(
Cases.ModelId,
dashScopeClient,
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
NullLoggerFactory.Instance);
var settings =
new DashScopePromptExecutionSettings { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
var history = new ChatHistory();
Expand Down Expand Up @@ -185,7 +184,7 @@ public async Task ChatCompletion_FunctionThrowException_SkipAsync()
var service = new DashScopeChatCompletionService(
Cases.ModelId,
dashScopeClient,
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
NullLoggerFactory.Instance);
var settings =
new DashScopePromptExecutionSettings { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
var history = new ChatHistory();
Expand Down Expand Up @@ -218,7 +217,7 @@ public async Task ChatCompletion_FunctionDoesNotExists_SkipAsync()
var service = new DashScopeChatCompletionService(
Cases.ModelId,
dashScopeClient,
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
NullLoggerFactory.Instance);
var settings =
new DashScopePromptExecutionSettings
{
Expand Down Expand Up @@ -252,7 +251,7 @@ public async Task ChatCompletion_CallingNotProvidedFunction_SkipAsync()
var service = new DashScopeChatCompletionService(
Cases.ModelId,
dashScopeClient,
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
NullLoggerFactory.Instance);
var settings =
new DashScopePromptExecutionSettings
{
Expand All @@ -279,7 +278,7 @@ public async Task ChatCompletion_CustomModel_SuccessAsync()
var service = new DashScopeChatCompletionService(
Cases.ModelId,
dashScopeClient,
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
NullLoggerFactory.Instance);
var settings = new DashScopePromptExecutionSettings { ModelId = Cases.ModelIdAlter };

// Act
Expand All @@ -303,7 +302,7 @@ public async Task ChatCompletionStream_Normal_SuccessAsync(PromptExecutionSettin
var service = new DashScopeChatCompletionService(
Cases.ModelId,
dashScopeClient,
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
NullLoggerFactory.Instance);

// Act
var response = await service.GetStreamingChatMessageContentsAsync(Cases.ChatHistory, settings).ToListAsync();
Expand Down Expand Up @@ -336,7 +335,7 @@ public async Task ChatCompletionStream_CustomModel_SuccessAsync()
var service = new DashScopeChatCompletionService(
Cases.ModelId,
dashScopeClient,
MockLoggerFactory.MockLogger<DashScopeChatCompletionService>());
NullLoggerFactory.Instance);
var settings = new DashScopePromptExecutionSettings { ModelId = Cases.ModelIdAlter };

// Act
Expand Down
10 changes: 5 additions & 5 deletions test/SemanticKernel.DashScope.UnitTest/TextCompletionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Cnblogs.DashScope.Core;
using Cnblogs.DashScope.Core;
using Cnblogs.SemanticKernel.Connectors.DashScope;
using FluentAssertions;
using Microsoft.Extensions.Logging.Abstractions;
Expand All @@ -22,7 +22,7 @@ public async Task GetTextContent_Normal_SuccessAsync(PromptExecutionSettings? se
var service = new DashScopeChatCompletionService(
Cases.ModelId,
dashScopeClient,
NullLogger<DashScopeChatCompletionService>.Instance);
NullLoggerFactory.Instance);

// Act
var response = await service.GetTextContentsAsync(Cases.Prompt, settings);
Expand Down Expand Up @@ -54,7 +54,7 @@ public async Task GetTextContent_OverrideModelId_SuccessAsync()
var service = new DashScopeChatCompletionService(
Cases.ModelId,
dashScopeClient,
NullLogger<DashScopeChatCompletionService>.Instance);
NullLoggerFactory.Instance);
var settings = new DashScopePromptExecutionSettings { ModelId = Cases.ModelIdAlter };

// Act
Expand All @@ -78,7 +78,7 @@ public async Task GetTextContentStream_Normal_SuccessAsync(PromptExecutionSettin
var service = new DashScopeChatCompletionService(
Cases.ModelId,
dashScopeClient,
NullLogger<DashScopeChatCompletionService>.Instance);
NullLoggerFactory.Instance);

// Act
var response = await service.GetStreamingTextContentsAsync(Cases.Prompt, settings).ToListAsync();
Expand Down Expand Up @@ -111,7 +111,7 @@ public async Task GetTextContentStream_OverrideModelId_SuccessAsync()
var service = new DashScopeChatCompletionService(
Cases.ModelId,
dashScopeClient,
NullLogger<DashScopeChatCompletionService>.Instance);
NullLoggerFactory.Instance);
var settings = new PromptExecutionSettings { ModelId = Cases.ModelIdAlter };

// Act
Expand Down

0 comments on commit 499401d

Please sign in to comment.