Skip to content

feat: use tokenizer from sdk #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions src/KernelMemory.DashScope/DashScopeEmbeddedResource.cs

This file was deleted.

11 changes: 7 additions & 4 deletions src/KernelMemory.DashScope/DashScopeTextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ public class DashScopeTextGenerator(
int maxToken = 6000) : ITextGenerator
{
private readonly ILogger<DashScopeTextGenerator> _logger = loggerFactory?.CreateLogger<DashScopeTextGenerator>()
?? DefaultLogger<DashScopeTextGenerator>.Instance;
?? DefaultLogger<DashScopeTextGenerator>.Instance;

/// <inheritdoc />
public int CountTokens(string text)
{
return tokenizer?.CountTokens(text) ?? QWenTokenizer.CountTokensStatic(text);
return tokenizer?.CountTokens(text) ?? QWenTokenizer.CountTokens(text);
}

/// <inheritdoc />
public IReadOnlyList<string> GetTokens(string text)
{
return tokenizer?.GetTokens(text) ?? QWenTokenizer.GetTokensStatic(text);
return tokenizer?.GetTokens(text)
?? QWenTokenizer.Tokenizer.EncodeToTokens(text, out _).Select(x => x.Value).ToList();
}

/// <inheritdoc />
Expand All @@ -47,7 +48,9 @@ public async IAsyncEnumerable<string> GenerateTextAsync(
TopP = options.NucleusSampling == 0 ? null : (float)options.NucleusSampling,
Temperature = options.Temperature == 0 ? null : (float)options.Temperature,
RepetitionPenalty =
options.FrequencyPenalty == 0 ? null : ((float)options.FrequencyPenalty + 1), // dashScope's default value is 1.0, kernel memory is 0.0
options.FrequencyPenalty == 0
? null
: ((float)options.FrequencyPenalty + 1), // dashScope's default value is 1.0, kernel memory is 0.0
MaxTokens = options.MaxTokens == 0 ? null : options.MaxTokens,
Stop = options.StopSequences.ToArray(),
IncrementalOutput = true,
Expand Down
10 changes: 5 additions & 5 deletions src/KernelMemory.DashScope/DependencyInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static IKernelMemoryBuilder WithDashScopeDefaults(
ITextTokenizer? textEmbeddingTokenizer = null,
bool onlyForRetrieval = false)
{
textGenerationTokenizer ??= new QWenTokenizer();
textGenerationTokenizer ??= new QWenTextTokenizer();
textEmbeddingTokenizer ??= new LengthTokenizer();

var config = new DashScopeConfig
Expand Down Expand Up @@ -97,7 +97,7 @@ public static IKernelMemoryBuilder WithDashScope(
{
config.EnsureValid();
embeddingTokenizer ??= new LengthTokenizer();
textTokenizer ??= new QWenTokenizer();
textTokenizer ??= new QWenTextTokenizer();
dashScopeClient ??= new DashScopeClient(config.ApiKey);
builder.WithDashScopeTextGeneration(config, textTokenizer, dashScopeClient);
builder.WithDashScopeTextEmbeddingGeneration(config, embeddingTokenizer, onlyForRetrieval, dashScopeClient);
Expand All @@ -119,7 +119,7 @@ public static IKernelMemoryBuilder WithDashScopeTextGeneration(
IDashScopeClient? dashScopeClient = null)
{
config.EnsureValid();
tokenizer ??= new QWenTokenizer();
tokenizer ??= new QWenTextTokenizer();
dashScopeClient ??= new DashScopeClient(config.ApiKey);
builder.Services.AddDashScopeTextGeneration(config, tokenizer, dashScopeClient);
return builder;
Expand Down Expand Up @@ -188,7 +188,7 @@ public static IServiceCollection AddDashScopeTextEmbeddingGeneration(
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="config">Settings for DashScope.</param>
/// <param name="tokenizer">The tokenizer to use, defaults to <see cref="QWenTokenizer"/>.</param>
/// <param name="tokenizer">The tokenizer to use, defaults to <see cref="QWenTextTokenizer"/>.</param>
/// <param name="dashScopeClient">The underlying <see cref="IDashScopeClient"/>.</param>
/// <returns></returns>
public static IServiceCollection AddDashScopeTextGeneration(
Expand All @@ -198,7 +198,7 @@ public static IServiceCollection AddDashScopeTextGeneration(
IDashScopeClient? dashScopeClient = null)
{
config.EnsureValid();
tokenizer ??= new QWenTokenizer();
tokenizer ??= new QWenTextTokenizer();

return services.AddSingleton<ITextGenerator>(
sp => new DashScopeTextGenerator(
Expand Down
7 changes: 1 addition & 6 deletions src/KernelMemory.DashScope/KernelMemory.DashScope.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.DeepDev.TokenizerLib" Version="1.3.3" />
<PackageReference Include="Microsoft.KernelMemory.Abstractions" Version="0.95.241216.2" />
<PackageReference Include="Cnblogs.DashScope.Core" Version="0.5.1" />
<PackageReference Include="Cnblogs.DashScope.Core" Version="0.5.2" />
</ItemGroup>

<ItemGroup>
Expand All @@ -31,8 +30,4 @@
<None Include="../../README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="qwen.tiktoken" />
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions src/KernelMemory.DashScope/QWenTextTokenizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Cnblogs.DashScope.Core;
using Microsoft.KernelMemory.AI;

namespace Cnblogs.KernelMemory.AI.DashScope;

/// <summary>
/// Tokenizer using QWen
/// </summary>
public class QWenTextTokenizer : ITextTokenizer
{
/// <inheritdoc />
public int CountTokens(string text)
{
return QWenTokenizer.CountTokens(text);
}

/// <inheritdoc />
public IReadOnlyList<string> GetTokens(string text)
{
return QWenTokenizer.Tokenizer.EncodeToTokens(text, out _).Select(x => x.Value).ToList();
}
}
72 changes: 0 additions & 72 deletions src/KernelMemory.DashScope/QWenTokenizer.cs

This file was deleted.

Loading
Loading