Skip to content

Commit

Permalink
🔧 Update ERNIE-Bot.SDK and sample projects (#98)
Browse files Browse the repository at this point in the history
- Update Microsoft.KernelMemory.Core to version 0.27.240205.2
- Update Microsoft.SemanticKernel.Abstractions and Microsoft.SemanticKernel.Core to version 1.3.0
- Update Microsoft.SemanticKernel.Plugins.Core and Microsoft.SemanticKernel.Plugins.Memory to version 1.3.0-alpha
- Update Microsoft.NET.Test.Sdk to version 17.10.0-preview-24080-01
- Update System.Text.Json to version 8.0.1
- Update xunit to version 2.6.6
- Update xunit.runner.visualstudio to version 2.5.6
- Update ERNIE-Bot-Kernel-Memory.Sample and ERNIE-Bot.Sample projects
- Add new endpoint for JSON format in ERNIE-Bot.Sample
- Add new endpoint for getting usage in SK-ERNIE-Bot.Sample
- Update Directory.Build.props with new version 0.14.0
- Update ModelEndpoints.cs with new version 1.3.0

🔧 Update ERNIE-Bot.SDK ModelEndpoints and ChatRequest

- Update the ERNIE_Bot_8K model endpoint in the ModelEndpoints class to have a space between the model name and the port number.
- Update the XuanYuan_70B_Chat_4bit model endpoint in the ModelEndpoints class to remove the space between the model name and the parentheses.
- Add a new embedding model endpoint called Tao_8k with a token size of 8192.
- Add a new property called ResponseFormat to the ChatRequest class to specify the format of the response content.
- Add a new property called MaxTokens to the ChatRequest class to specify the maximum number of output tokens.

Summary:
- Updated model endpoints in the ERNIE-Bot.SDK ModelEndpoints class.
- Added new properties to the ChatRequest class for specifying response format and maximum output tokens.

🔧 Fix system message handling

- Fix handling of system messages in ERNIEBotChatCompletion.cs
- Update logic to assign system message content to 'system' variable
- Exclude system messages from the returned chat history

📝 Update ERNIEBotChatMessage constructors

- Remove unused constructor in ERNIEBotChatMessage class
- Update existing constructor to include metadata parameter

📝 Update ERNIEBotStreamingChatMessage constructors

- Remove unused constructor in ERNIEBotStreamingChatMessage class
- Update existing constructor to include metadata parameter
  • Loading branch information
xbotter committed Feb 6, 2024
1 parent 65d9137 commit 689da95
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 30 deletions.
18 changes: 9 additions & 9 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
<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.23.231219.1" />
<PackageVersion Include="Microsoft.SemanticKernel.Abstractions" Version="1.0.1" />
<PackageVersion Include="Microsoft.SemanticKernel.Core" Version="1.0.1" />
<PackageVersion Include="Microsoft.KernelMemory.Core" Version="0.27.240205.2" />
<PackageVersion Include="Microsoft.SemanticKernel.Abstractions" Version="1.3.0" />
<PackageVersion Include="Microsoft.SemanticKernel.Core" Version="1.3.0" />
<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.1-alpha" />
<PackageVersion Include="Microsoft.SemanticKernel.Plugins.Memory" Version="1.0.1-alpha" />
<PackageVersion Include="Microsoft.SemanticKernel.Plugins.Core" Version="1.3.0-alpha" />
<PackageVersion Include="Microsoft.SemanticKernel.Plugins.Memory" Version="1.3.0-alpha" />
<PackageVersion Include="Microsoft.SemanticKernel.TemplateEngine.Basic" Version="1.0.0-beta8" />
<!-- Validation -->
<PackageVersion Include="Microsoft.VisualStudio.Validation" Version="17.8.8" />
<!-- Tests -->
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0-preview-24080-01" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="System.Text.Json" Version="8.0.0" />
<PackageVersion Include="System.Text.Json" Version="8.0.1" />
<PackageVersion Include="System.Threading.RateLimiting" Version="8.0.0" />
<PackageVersion Include="xunit" Version="2.6.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.5" />
<PackageVersion Include="xunit" Version="2.6.6" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.6" />
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
<!-- Misc -->
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
Expand Down
5 changes: 3 additions & 2 deletions samples/ERNIE-Bot-Kernel-Memory.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using ERNIE_Bot.SDK;
using Microsoft.Extensions.Configuration;
using Microsoft.KernelMemory;
using Microsoft.KernelMemory.Handlers;
using Microsoft.KernelMemory.Configuration;
using System.Threading.RateLimiting;

var config = new ConfigurationBuilder()
Expand All @@ -18,7 +18,8 @@
}));

var memory = new KernelMemoryBuilder()
.WithERNIEBotDefaults(client)
.WithERNIEBotTextGenerator(client)
.WithERNIEBotEmbeddingGenerator(client, ModelEndpoints.bge_large_en)
.With(new TextPartitioningOptions
{
MaxTokensPerParagraph = 300,
Expand Down
24 changes: 24 additions & 0 deletions samples/ERNIE-Bot.Sample/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,29 @@ public async Task<IActionResult> HistoryAsync([FromBody] UserHistoryInput input)

return Ok(result.Result);
}

[HttpPost("JsonFormat")]
public async Task<IActionResult> JsonFormatAsync([FromBody] UserInput input)
{
if (string.IsNullOrWhiteSpace(input.Text))
{
return NoContent();
}

var result = await _client.ChatAsync(new ChatCompletionsRequest()
{
Messages = new List<Message>
{
new Message()
{
Content = input.Text,
Role = MessageRole.User
}
},
ResponseFormat = "json_object"
}, ModelEndpoints.ERNIE_Bot_4);

return Ok(result.Result);
}
}
}
22 changes: 20 additions & 2 deletions samples/SK-ERNIE-Bot.Sample/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ public async Task ChatStreamAsync([FromBody] UserInput input, CancellationToken
}

[HttpPost("embedding")]
#pragma warning disable SKEXP0003 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
public async Task<IActionResult> EmbeddingAsync([FromBody] UserInput input, [FromServices] ISemanticTextMemory memory, CancellationToken cancellationToken)
#pragma warning restore SKEXP0003 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
{
const string collection = "demo";
if (string.IsNullOrWhiteSpace(input.Text))
Expand Down Expand Up @@ -139,5 +137,25 @@ public async Task<IActionResult> ChatWithSystemAsync([FromBody] UserInput input,
var text = result[0].Content;
return Ok(text);
}

[HttpPost("get_usage")]
public async Task<IActionResult> GetUsageAsync([FromBody] UserInput input, CancellationToken cancellationToken)
{
var plugin = _kernel.ImportPluginFromPromptDirectory("Plugins/Demo", "Demo");

var translateFunc = plugin["Translate"];

var result = await _kernel.InvokeAsync(translateFunc, new() { ["input"] = input.Text }, cancellationToken: cancellationToken);

var value = result.GetValue<string>();

var usage = result.Metadata?["Usage"];

return Ok(new
{
value,
usage
});
}
}
}
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.13.1</Version>
<Version>0.14.0</Version>
<PackageOutputPath>..\..\nupkgs</PackageOutputPath>
<PackageReadmeFile>readme.md</PackageReadmeFile>
<NoWarn>SKEXP0001;SKEXP0002;SKEXP0052;SKEXP0003</NoWarn>
Expand Down
9 changes: 7 additions & 2 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",6000);
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 Down Expand Up @@ -65,7 +65,7 @@ public static class ModelEndpoints
/// <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");
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>
Expand All @@ -91,5 +91,10 @@ public static class ModelEndpoints
/// <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", 512);

/// <summary>
/// <see cref="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/7lq0buxys"/>
/// </summary>
public static readonly EmbeddingModelEndpoint Tao_8k = new("tao_8k", 8192);
}
}
17 changes: 17 additions & 0 deletions src/ERNIE-Bot.SDK/Models/ChatRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ public class ChatCompletionsRequest : ChatRequest

[JsonPropertyName("penalty_score")]
public float? PenaltyScore { get; set; }

/// <summary>
/// 指定响应内容的格式,说明:<br/>
///(1)可选值:<br/>
///· json_object:以json格式返回,可能出现不满足效果情况 <br/>
///· text:以文本格式返回 <br/>
///(2)如果不填写参数response_format值,默认为text <br/>
/// </summary>
[JsonPropertyName("response_format")]
public string? ResponseFormat { get; set; }

/// <summary>
/// 指定模型最大输出token数,范围[2, 2048]
/// </summary>
[JsonPropertyName("max_output_tokens")]
public int? MaxTokens { get; set; }

}

public class Message
Expand Down
6 changes: 6 additions & 0 deletions src/ERNIE-Bot.SemanticKernel/ERNIEBotAIRequestSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ public class ERNIEBotAIRequestSettings : PromptExecutionSettings
[JsonPropertyName("penalty_score")]
public float? PenaltyScore { get; set; }

[JsonPropertyName("response_format")]
public string? ResponseFormat { get; set; }

[JsonPropertyName("max_output_tokens")]
public int? MaxTokens { get; set; }

public static ERNIEBotAIRequestSettings FromRequestSettings(PromptExecutionSettings? requestSettings, int? defaultMaxTokens = null)
{
if (requestSettings is null)
Expand Down
23 changes: 18 additions & 5 deletions src/ERNIE-Bot.SemanticKernel/ERNIEBotChatCompletion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public async Task<IReadOnlyList<ChatMessageContent>> GetChatMessageContentsAsync
system,
cancellationToken
);

return new List<ChatMessageContent>() { new ERNIEBotChatMessage(result) };
var metadata = GetResponseMetadata(result);
return new List<ChatMessageContent>() { new ERNIEBotChatMessage(result, metadata) };
}

public async IAsyncEnumerable<StreamingChatMessageContent> GetStreamingChatMessageContentsAsync(ChatHistory chatHistory, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
Expand All @@ -58,7 +58,8 @@ public async IAsyncEnumerable<StreamingChatMessageContent> GetStreamingChatMessa
);
await foreach (var result in results)
{
yield return new ERNIEBotStreamingChatMessage(result);
var metadata = GetResponseMetadata(result);
yield return new ERNIEBotStreamingChatMessage(result, metadata);
}
}

Expand All @@ -77,7 +78,7 @@ public async Task<IReadOnlyList<TextContent>> GetTextContentsAsync(string prompt
cancellationToken
);

return new List<TextContent>() { new(result.Result) }.AsReadOnly();
return new List<TextContent>() { new(result.Result, metadata: GetResponseMetadata(result)) }.AsReadOnly();
}

public async IAsyncEnumerable<StreamingTextContent> GetStreamingTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
Expand All @@ -96,7 +97,7 @@ public async IAsyncEnumerable<StreamingTextContent> GetStreamingTextContentsAsyn
);
await foreach (var result in results)
{
yield return new StreamingTextContent(result.Result);
yield return new StreamingTextContent(result.Result, metadata: GetResponseMetadata(result));
}
}

Expand All @@ -112,6 +113,18 @@ public ChatHistory CreateNewChat(string? instructions = null)

return history;
}

private static Dictionary<string, object?> GetResponseMetadata(ChatResponse result)
{
return new Dictionary<string, object?>()
{
{nameof(result.Id), result.Id},
{nameof(result.Created), result.Created},
{nameof(result.NeedClearHistory), result.NeedClearHistory},
{nameof(result.Usage), result.Usage }
};
}

private List<Message> StringToMessages(string text)
{
return
Expand Down
13 changes: 4 additions & 9 deletions src/ERNIE-Bot.SemanticKernel/ERNIEBotChatResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,17 @@ namespace Connectors.AI.ERNIEBot
{
internal class ERNIEBotChatMessage : ChatMessageContent
{
public ERNIEBotChatMessage(ChatResponse response)
: base(AuthorRole.Assistant, response.Result)
public ERNIEBotChatMessage(ChatResponse response, IReadOnlyDictionary<string, object?>? metadata = null)
: base(AuthorRole.Assistant, response.Result, metadata: metadata)
{

}

public ERNIEBotChatMessage(string content)
: base(AuthorRole.Assistant, content)
{
}
}

internal class ERNIEBotStreamingChatMessage : StreamingChatMessageContent
{
public ERNIEBotStreamingChatMessage(ChatResponse response)
: base(AuthorRole.Assistant, response.Result, response)
public ERNIEBotStreamingChatMessage(ChatResponse response, IReadOnlyDictionary<string, object?>? metadata = null)
: base(AuthorRole.Assistant, response.Result, response, metadata: metadata)
{

}
Expand Down

0 comments on commit 689da95

Please sign in to comment.