Skip to content

Commit

Permalink
Bump SK to 1.0.0-rc3 , KM to 0.18 (#79)
Browse files Browse the repository at this point in the history
* 🔄 Rename ERNIEBotTextGeneration to ERNIEBotTextGenerator

- Updated package versions for Microsoft.KernelMemory.Core, Microsoft.SemanticKernel.Abstractions, Microsoft.SemanticKernel.Core, Microsoft.SemanticKernel.Plugins.Core, and Microsoft.SemanticKernel.Plugins.Memory.
- Fixed code in ERNIE-Bot-Kernel-Memory.Sample/Program.cs and SK-ERNIE-Bot.Sample/Controllers/ApiController.cs.
- Renamed ERNIE-Bot.KernelMemory/ERNIEBotTextEmbeddingGeneration.cs to ERNIE-Bot.KernelMemory/ERNIEBotTextEmbeddingGenerator.cs.
- Updated ERNIEBotTextEmbeddingGenerator class to implement ITextEmbeddingGenerator interface.
- Added MaxTokens property and GenerateEmbeddingAsync method to ERNIEBotTextEmbeddingGenerator class.
- Added CountTokens method to ERNIEBotTextEmbeddingGenerator class.
- Updated summary formatting in ERNIEBotTextEmbeddingGenerator class.
- Added two new model endpoints: XuanYuan_70B_Chat_4bit and ChatLaw.
- Updated the bge-large-zh and bge-large-en embedding models to include a size of 512.
- Refactored ERNIEBotKernelBuilderExtensions to simplify code and improve readability.

🔧 Update ModelEndpoints and ERNIEBotKernelBuilderExtensions

- Added two new model endpoints: XuanYuan_70B_Chat_4bit and ChatLaw.
- Updated the bge-large-zh and bge-large-en embedding models to include a size of 512.
- Refactored ERNIEBotKernelBuilderExtensions to simplify code and improve readability.

* bump version 0.12
  • Loading branch information
xbotter authored Dec 11, 2023
1 parent 2ce743b commit 09c74b8
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 51 deletions.
10 changes: 5 additions & 5 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.KernelMemory.Abstractions" Version="0.11.231121.2-preview+ea157ef" />
<PackageVersion Include="Microsoft.KernelMemory.Core" Version="0.15.231203.1-preview" />
<PackageVersion Include="Microsoft.SemanticKernel.Abstractions" Version="1.0.0-rc1" />
<PackageVersion Include="Microsoft.SemanticKernel.Core" Version="1.0.0-rc1" />
<PackageVersion Include="Microsoft.KernelMemory.Core" Version="0.18.231209.1-preview" />
<PackageVersion Include="Microsoft.SemanticKernel.Abstractions" Version="1.0.0-rc3" />
<PackageVersion Include="Microsoft.SemanticKernel.Core" Version="1.0.0-rc3" />
<PackageVersion Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.SemanticKernel.Plugins.Core" Version="1.0.0-beta8" />
<PackageVersion Include="Microsoft.SemanticKernel.Plugins.Memory" Version="1.0.0-rc1" />
<PackageVersion Include="Microsoft.SemanticKernel.Plugins.Core" Version="1.0.0-rc3" />
<PackageVersion Include="Microsoft.SemanticKernel.Plugins.Memory" Version="1.0.0-rc3" />
<PackageVersion Include="Microsoft.SemanticKernel.TemplateEngine.Basic" Version="1.0.0-beta8" />
<!-- Validation -->
<PackageVersion Include="Microsoft.VisualStudio.Validation" Version="17.8.8" />
Expand Down
2 changes: 1 addition & 1 deletion samples/ERNIE-Bot-Kernel-Memory.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

await memory.ImportDocumentAsync("sample-SK-Readme.pdf");

var question = "What's Semantic Kernel?";
var question = "什么是 Semantic Kernel?";

Console.WriteLine($"\n\nQuestion: {question}");

Expand Down
12 changes: 6 additions & 6 deletions samples/SK-ERNIE-Bot.Sample/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task<IActionResult> ChatAsync([FromBody] UserInput input, Cancellat
return NoContent();
}

var chat = _kernel.GetService<IChatCompletionService>();
var chat = _kernel.GetRequiredService<IChatCompletionService>();
var history = new ChatHistory();
history.AddUserMessage(input.Text);

Expand All @@ -45,7 +45,7 @@ public async Task<IActionResult> TextAsync([FromBody] UserInput input, Cancellat
return NoContent();
}

var completion = _kernel.GetService<ITextGenerationService>();
var completion = _kernel.GetRequiredService<ITextGenerationService>();

var result = await completion.GetTextContentAsync(input.Text, null, cancellationToken: cancellationToken);

Expand All @@ -61,7 +61,7 @@ public async Task ChatStreamAsync([FromBody] UserInput input, CancellationToken
await Response.CompleteAsync();
}

var chat = _kernel.GetService<IChatCompletionService>();
var chat = _kernel.GetRequiredService<IChatCompletionService>();
var history = new ChatHistory();
history.AddUserMessage(input.Text);
var results = chat.GetStreamingChatMessageContentsAsync(history, cancellationToken: cancellationToken);
Expand Down Expand Up @@ -104,7 +104,7 @@ public async Task<IActionResult> FuncAsync([FromBody] UserInput input, Cancellat
""";
var func = _kernel.CreateFunctionFromPrompt(prompt);
var result = await _kernel.InvokeAsync(func, input.Text);
var result = await _kernel.InvokeAsync(func,new KernelArguments(input.Text));
return Ok(result.GetValue<string>());
}

Expand All @@ -115,7 +115,7 @@ public async Task<IActionResult> SemanticPlugin([FromBody] UserInput input)

var translateFunc = plugin["Translate"];

var result = await _kernel.InvokeAsync(translateFunc, input.Text);
var result = await _kernel.InvokeAsync(translateFunc, new KernelArguments(input.Text));
return Ok(result.GetValue<string>());
}

Expand All @@ -127,7 +127,7 @@ public async Task<IActionResult> ChatWithSystemAsync([FromBody] UserInput input,
return NoContent();
}

var chat = _kernel.GetService<IChatCompletionService>();
var chat = _kernel.GetRequiredService<IChatCompletionService>();

var history = new ChatHistory($"你是一个友善的AI助手。你的名字叫做Alice,今天是{DateTime.Today}.");

Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageProjectUrl>https://github.com/custouch/semantic-kernel-ERNIE-Bot</PackageProjectUrl>
<RepositoryUrl>https://github.com/custouch/semantic-kernel-ERNIE-Bot</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>0.11.0-preview</Version>
<Version>0.12.0-preview</Version>
<PackageOutputPath>..\..\nupkgs</PackageOutputPath>
<PackageReadmeFile>readme.md</PackageReadmeFile>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
using ERNIE_Bot.SDK;
using Microsoft.KernelMemory;
using Microsoft.KernelMemory.AI;
using Microsoft.SemanticKernel.AI.Embeddings;

namespace ERNIE_Bot.KernelMemory
{
/// <summary>
/// Generating text embeddings using ERNIEBotClient.
/// </summary>
public class ERNIEBotTextEmbeddingGeneration : ITextEmbeddingGeneration
public class ERNIEBotTextEmbeddingGenerator : ITextEmbeddingGenerator
{
private readonly ERNIEBotClient _client;
private readonly EmbeddingModelEndpoint _endpoint;

/// <summary>
/// Initializes a new instance of the <see cref="ERNIEBotTextEmbeddingGeneration"/> class.
/// Initializes a new instance of the <see cref="ERNIEBotTextEmbeddingGenerator"/> class.
/// </summary>
/// <param name="client">The ERNIEBotClient instance to use for generating embeddings.</param>
/// <param name="endpoint">The endpoint to use for the embedding model. Defaults to ModelEndpoints.Embedding_v1.</param>
public ERNIEBotTextEmbeddingGeneration(ERNIEBotClient client, EmbeddingModelEndpoint? endpoint = null)
public ERNIEBotTextEmbeddingGenerator(ERNIEBotClient client, EmbeddingModelEndpoint? endpoint = null)
{
this._client = client;
_endpoint = endpoint ?? ModelEndpoints.Embedding_v1;
}

private readonly Dictionary<string, string> _attributes = new();
public IReadOnlyDictionary<string, string> Attributes => _attributes;
public int MaxTokens => _endpoint.MaxTokens;

/// <inheritdoc/>
public async Task<IList<ReadOnlyMemory<float>>> GenerateEmbeddingsAsync(IList<string> data, CancellationToken cancellationToken = default)
public async Task<Embedding> GenerateEmbeddingAsync(string text, CancellationToken cancellationToken = default)
{
var embeddings = await _client.EmbeddingsAsync(new SDK.Models.EmbeddingsRequest()
{
Input = data.ToList()
Input = [text]
}, _endpoint, cancellationToken);

return embeddings.Data.Select(d => new ReadOnlyMemory<float>(d.Embedding.Select(e => (float)e).ToArray())).ToList();
return new Embedding(embeddings.Data[0].Embedding.Select(e => (float)e).ToArray());
}

public int CountTokens(string text)
{
return Tokenizer.ApproxNumTokens(text);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@ namespace ERNIE_Bot.KernelMemory
/// <summary>
/// Generating text using ERNIEBotClient.
/// </summary>
public class ERNIEBotTextGeneration : ITextGeneration
public class ERNIEBotTextGenerator : ITextGenerator
{
private readonly ERNIEBotClient _client;
private readonly ModelEndpoint _endpoint;

/// <summary>
/// Initializes a new instance of the <see cref="ERNIEBotTextGeneration"/> class.
/// Initializes a new instance of the <see cref="ERNIEBotTextGenerator"/> class.
/// </summary>
/// <param name="client">The ERNIEBotClient instance to use for generating text.</param>
/// <param name="endpoint">The endpoint to use for the model. Defaults to ModelEndpoints.ERNIE_Bot_Turbo.</param>
public ERNIEBotTextGeneration(ERNIEBotClient client, ModelEndpoint? endpoint = null)
public ERNIEBotTextGenerator(ERNIEBotClient client, ModelEndpoint? endpoint = null)
{
this._client = client;
_endpoint = endpoint ?? ModelEndpoints.ERNIE_Bot_Turbo;
}

public int MaxTokenTotal => _endpoint.MaxTokens;

public int CountTokens(string text) => Tokenizer.ApproxNumTokens(text);

/// <inheritdoc/>
public async IAsyncEnumerable<string> GenerateTextAsync(string prompt, TextGenerationOptions options, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
Expand Down
12 changes: 6 additions & 6 deletions src/ERNIE-Bot.KernelMemory/KernelMemoryBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public static class KernelMemoryBuilderExtensions
/// <param name="client">The ERNIEBotClient instance to use for text generation.</param>
/// <param name="endpoint">The endpoint to use for the text generation model. Defaults to ModelEndpoints.ERNIE_Bot_Turbo.</param>
/// <returns>The configured KernelMemoryBuilder instance.</returns>
public static KernelMemoryBuilder WithERNIEBotTextGeneration(this KernelMemoryBuilder builder, ERNIEBotClient client, ModelEndpoint? endpoint = null)
public static KernelMemoryBuilder WithERNIEBotTextGenerator(this KernelMemoryBuilder builder, ERNIEBotClient client, ModelEndpoint? endpoint = null)
{
builder.WithCustomTextGeneration(new ERNIEBotTextGeneration(client, endpoint));
builder.WithCustomTextGenerator(new ERNIEBotTextGenerator(client, endpoint));
return builder;
}

Expand All @@ -28,9 +28,9 @@ public static KernelMemoryBuilder WithERNIEBotTextGeneration(this KernelMemoryBu
/// <param name="client">The ERNIEBotClient instance to use for embedding generation.</param>
/// <param name="endpoint">The endpoint to use for the embedding generation model. Defaults to ModelEndpoints.Embedding_v1.</param>
/// <returns>The configured KernelMemoryBuilder instance.</returns>
public static KernelMemoryBuilder WithERNIEBotEmbeddingGeneration(this KernelMemoryBuilder builder, ERNIEBotClient client, EmbeddingModelEndpoint? endpoint = null)
public static KernelMemoryBuilder WithERNIEBotEmbeddingGenerator(this KernelMemoryBuilder builder, ERNIEBotClient client, EmbeddingModelEndpoint? endpoint = null)
{
builder.WithCustomEmbeddingGeneration(new ERNIEBotTextEmbeddingGeneration(client, endpoint));
builder.WithCustomEmbeddingGenerator(new ERNIEBotTextEmbeddingGenerator(client, endpoint));
return builder;
}

Expand All @@ -55,8 +55,8 @@ public static KernelMemoryBuilder WithERNIEBotDefaults(this KernelMemoryBuilder
/// <returns>The configured KernelMemoryBuilder instance.</returns>
public static KernelMemoryBuilder WithERNIEBotDefaults(this KernelMemoryBuilder builder, ERNIEBotClient client)
{
builder.WithERNIEBotTextGeneration(client);
builder.WithERNIEBotEmbeddingGeneration(client);
builder.WithERNIEBotTextGenerator(client);
builder.WithERNIEBotEmbeddingGenerator(client);
return builder;
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/ERNIE-Bot.SDK/ModelEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ public abstract class Endpoint
{
public string Model { get; set; }
public string Task { get; set; }
public int MaxTokens { get; set; }

internal Endpoint(string model, string task)
internal Endpoint(string model, string task, int maxTokens)
{
Model = model;
Task = task;
MaxTokens = maxTokens;
}

public static implicit operator string(Endpoint endpoint)
Expand All @@ -23,15 +25,15 @@ public static implicit operator string(Endpoint endpoint)
/// <inheritdoc/>
public class ModelEndpoint : Endpoint
{
public ModelEndpoint(string model) : base(model, "chat")
public ModelEndpoint(string model, int maxTokens = 2000) : base(model, "chat", maxTokens)
{
}
}

/// <inheritdoc/>
public class EmbeddingModelEndpoint : Endpoint
{
public EmbeddingModelEndpoint(string endpoint) : base(endpoint, "embeddings")
public EmbeddingModelEndpoint(string endpoint, int maxTokens = 384) : base(endpoint, "embeddings", maxTokens)
{
}
}
Expand Down
23 changes: 19 additions & 4 deletions src/ERNIE-Bot.SDK/ModelEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public static class ModelEndpoints
/// <summary>
/// <see href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/6lp69is2a">ERNIE Bot 8k Chat Model</see>
/// </summary>
public static readonly ModelEndpoint ERNIE_Bot_8K = new("ernie_bot_8k");
public static readonly ModelEndpoint ERNIE_Bot_8K = new("ernie_bot_8k",6000);

/// <summary>
/// <see href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/clntwmv7t">ERNIE Bot 4 Chat Model</see>
Expand All @@ -20,7 +20,7 @@ public static class ModelEndpoints
/// <summary>
/// <see href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/4lilb2lpf"> ERNIE Bot Turbo Chat Model</see>
/// </summary>
public static readonly ModelEndpoint ERNIE_Bot_Turbo = new("eb-instant");
public static readonly ModelEndpoint ERNIE_Bot_Turbo = new("eb-instant", 7000);

/// <summary>
/// <see href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Jljcadglj">BLOOMZ 7B Chat Model</see>
Expand Down Expand Up @@ -52,11 +52,26 @@ public static class ModelEndpoints
/// </summary>
public static readonly ModelEndpoint Qianfan_Chinese_Llama_2_7b = new("qianfan_chinese_llama_2_7b");

/// <summary>
/// <see href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/8lo479b4b">Qianfan Chinese Llama2 13B Chat Model</see>
/// </summary>
public static readonly ModelEndpoint Qianfan_Chinese_Llama_2_13b = new("qianfan_chinese_llama_2_13b");

/// <summary>
/// <see href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Bllz001ff">ChatGLM2 6b 32k Chat Model</see>
/// </summary>
public static readonly ModelEndpoint ChatGLM2_6b_32k = new("chatglm2_6b_32k");

/// <summary>
/// <see href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Ylp88e5jc">XuanYuan 70B Chat 4bit</see>
/// </summary>
public static readonly ModelEndpoint XuanYuan_70B_Chat_4bit = new ("xuanyuan_70b_chat");

/// <summary>
/// <see href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Qlphtigbf">ChatLaw</see>
/// </summary>
public static readonly ModelEndpoint ChatLaw = new("chatlaw");

/// <summary>
/// <see href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/ollz02e7i">AquilaChat-7B Chat Model</see>
/// </summary>
Expand All @@ -70,11 +85,11 @@ public static class ModelEndpoints
/// <summary>
/// <see href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/dllz04sro">bge-large-zh Embedding Model</see>
/// </summary>
public static readonly EmbeddingModelEndpoint bge_large_zh = new("bge_large_zh");
public static readonly EmbeddingModelEndpoint bge_large_zh = new("bge_large_zh", 512);

/// <summary>
/// <see href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/mllz05nzk">bge-large-en Embedding Model</see>
/// </summary>
public static readonly EmbeddingModelEndpoint bge_large_en = new("bge_large_en");
public static readonly EmbeddingModelEndpoint bge_large_en = new("bge_large_en", 512);
}
}
22 changes: 8 additions & 14 deletions src/ERNIE-Bot.SemanticKernel/ERNIEBotKernelBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ public static KernelBuilder WithERNIEBotChatCompletionService(this KernelBuilder
string? serviceId = null,
ModelEndpoint? modelEndpoint = null)
{
builder.WithServices(c =>
{
var client = CreateERNIEBotClient(service, configuration);
var generation = new ERNIEBotChatCompletion(client, modelEndpoint);
c.AddKeyedSingleton<IChatCompletionService>(serviceId, generation);
c.AddKeyedSingleton<ITextGenerationService>(serviceId, generation);
});
var client = CreateERNIEBotClient(service, configuration);
var generation = new ERNIEBotChatCompletion(client, modelEndpoint);
builder.Services.AddKeyedSingleton<IChatCompletionService>(serviceId, generation);
builder.Services.AddKeyedSingleton<ITextGenerationService>(serviceId, generation);

return builder;
}
Expand All @@ -54,13 +51,10 @@ public static KernelBuilder WithERNIEBotChatCompletionService(this KernelBuilder
ModelEndpoint? modelEndpoint = null)
{

builder.WithServices(c =>
{
var client = CreateERNIEBotClient(clientId, secret);
var generation = new ERNIEBotChatCompletion(client, modelEndpoint);
c.AddKeyedSingleton<IChatCompletionService>(serviceId, generation);
c.AddKeyedSingleton<ITextGenerationService>(serviceId, generation);
});
var client = CreateERNIEBotClient(clientId, secret);
var generation = new ERNIEBotChatCompletion(client, modelEndpoint);
builder.Services.AddKeyedSingleton<IChatCompletionService>(serviceId, generation);
builder.Services.AddKeyedSingleton<ITextGenerationService>(serviceId, generation);

return builder;
}
Expand Down

0 comments on commit 09c74b8

Please sign in to comment.