diff --git a/Directory.Packages.props b/Directory.Packages.props index 42b9b06..815aeb3 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -4,26 +4,27 @@ - + - - + + + - - - + + + - + - + - + - + - - + + diff --git a/samples/ERNIE-Bot-Kernel-Memory.Sample/Program.cs b/samples/ERNIE-Bot-Kernel-Memory.Sample/Program.cs index 5d43063..08602a1 100644 --- a/samples/ERNIE-Bot-Kernel-Memory.Sample/Program.cs +++ b/samples/ERNIE-Bot-Kernel-Memory.Sample/Program.cs @@ -1,17 +1,14 @@ - -using ERNIE_Bot.KernelMemory; +using ERNIE_Bot.KernelMemory; using ERNIE_Bot.SDK; using Microsoft.Extensions.Configuration; using Microsoft.KernelMemory; using Microsoft.KernelMemory.Handlers; -using System; using System.Threading.RateLimiting; var config = new ConfigurationBuilder() .AddUserSecrets() .Build(); - var client = new ERNIEBotClient(config["ClientId"]!, config["ClientSecret"]!, HttpClientProvider.CreateFixedWindowRateLimitedClient(new FixedWindowRateLimiterOptions() { @@ -45,4 +42,4 @@ foreach (var x in answer.RelevantSources) { Console.WriteLine($" - {x.SourceName} - {x.Link} [{x.Partitions.First().LastUpdate:D}]"); -} \ No newline at end of file +} diff --git a/samples/ERNIE-Bot.Sample/Controllers/ApiController.cs b/samples/ERNIE-Bot.Sample/Controllers/ApiController.cs index c0bf43b..4688395 100644 --- a/samples/ERNIE-Bot.Sample/Controllers/ApiController.cs +++ b/samples/ERNIE-Bot.Sample/Controllers/ApiController.cs @@ -1,7 +1,6 @@ using ERNIE_Bot.Sample.Controllers.Models; using ERNIE_Bot.SDK; using ERNIE_Bot.SDK.Models; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Text; @@ -161,7 +160,6 @@ public async Task EmbeddingAsync([FromBody] UserInput input) [HttpPost("History")] public async Task HistoryAsync([FromBody] UserHistoryInput input) { - if (!input.Messages.Any()) { return NoContent(); diff --git a/samples/ERNIE-Bot.Sample/Controllers/Models/UserInput.cs b/samples/ERNIE-Bot.Sample/Controllers/Models/UserInput.cs index 7b42568..8d4c4c3 100644 --- a/samples/ERNIE-Bot.Sample/Controllers/Models/UserInput.cs +++ b/samples/ERNIE-Bot.Sample/Controllers/Models/UserInput.cs @@ -7,6 +7,7 @@ public class UserInput public string Role { get; set; } = MessageRole.User; public string Text { get; set; } = string.Empty; } + public class UserHistoryInput { public List Messages { get; set; } = new List { }; diff --git a/samples/ERNIE-Bot.Sample/Program.cs b/samples/ERNIE-Bot.Sample/Program.cs index be7041a..f2f59ac 100644 --- a/samples/ERNIE-Bot.Sample/Program.cs +++ b/samples/ERNIE-Bot.Sample/Program.cs @@ -11,7 +11,6 @@ builder.Services.AddScoped(svc => { - var logger = svc.GetRequiredService>(); var clientId = builder.Configuration["ClientId"]; diff --git a/samples/SK-ERNIE-Bot.Sample/Controllers/ApiController.cs b/samples/SK-ERNIE-Bot.Sample/Controllers/ApiController.cs index 36022df..1166e3a 100644 --- a/samples/SK-ERNIE-Bot.Sample/Controllers/ApiController.cs +++ b/samples/SK-ERNIE-Bot.Sample/Controllers/ApiController.cs @@ -1,5 +1,4 @@ -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.AI.ChatCompletion; using Microsoft.SemanticKernel.AI.Embeddings; @@ -35,7 +34,6 @@ public async Task ChatAsync([FromBody] UserInput input, Cancellat var result = await chat.GenerateMessageAsync(history, cancellationToken: cancellationToken); return Ok(result); - } [HttpPost("text")] @@ -52,7 +50,6 @@ public async Task TextAsync([FromBody] UserInput input, Cancellat var text = await result.First().GetCompletionAsync(); return Ok(text); - } [HttpPost("stream")] diff --git a/samples/SK-ERNIE-Bot.Sample/Plugins/Demo/Translate/config.json b/samples/SK-ERNIE-Bot.Sample/Plugins/Demo/Translate/config.json index 91ed9bd..c62e4e6 100644 --- a/samples/SK-ERNIE-Bot.Sample/Plugins/Demo/Translate/config.json +++ b/samples/SK-ERNIE-Bot.Sample/Plugins/Demo/Translate/config.json @@ -3,10 +3,9 @@ "type": "completion", "description": "", "completion": { - }, "default_services": [], "input": { "parameters": [] } -} \ No newline at end of file +} diff --git a/samples/SK-ERNIE-Bot.Sample/Program.cs b/samples/SK-ERNIE-Bot.Sample/Program.cs index 1e7b5fe..10364f9 100644 --- a/samples/SK-ERNIE-Bot.Sample/Program.cs +++ b/samples/SK-ERNIE-Bot.Sample/Program.cs @@ -28,7 +28,6 @@ return memory; }); - var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/src/Directory.Build.props b/src/Directory.Build.props index b790e04..281aadd 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -5,14 +5,14 @@ $(AssemblyName) enable enable - 10 + 12 Custouch LICENSE https://github.com/custouch/semantic-kernel-ERNIE-Bot https://github.com/custouch/semantic-kernel-ERNIE-Bot git - 0.8.0-preview + 0.9.0-preview ..\..\nupkgs readme.md diff --git a/src/ERNIE-Bot.KernelMemory/ERNIE-Bot.KernelMemory.csproj b/src/ERNIE-Bot.KernelMemory/ERNIE-Bot.KernelMemory.csproj index c398365..773d4ac 100644 --- a/src/ERNIE-Bot.KernelMemory/ERNIE-Bot.KernelMemory.csproj +++ b/src/ERNIE-Bot.KernelMemory/ERNIE-Bot.KernelMemory.csproj @@ -7,7 +7,7 @@ - + diff --git a/src/ERNIE-Bot.KernelMemory/ERNIEBotTextEmbeddingGeneration.cs b/src/ERNIE-Bot.KernelMemory/ERNIEBotTextEmbeddingGeneration.cs index ec1e41c..e5a8453 100644 --- a/src/ERNIE-Bot.KernelMemory/ERNIEBotTextEmbeddingGeneration.cs +++ b/src/ERNIE-Bot.KernelMemory/ERNIEBotTextEmbeddingGeneration.cs @@ -1,14 +1,8 @@ using ERNIE_Bot.SDK; using Microsoft.SemanticKernel.AI.Embeddings; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace ERNIE_Bot.KernelMemory { - /// /// Generating text embeddings using ERNIEBotClient. /// @@ -17,8 +11,6 @@ public class ERNIEBotTextEmbeddingGeneration : ITextEmbeddingGeneration private readonly ERNIEBotClient _client; private readonly EmbeddingModelEndpoint _endpoint; - - /// /// Initializes a new instance of the class. /// diff --git a/src/ERNIE-Bot.KernelMemory/ERNIEBotTextGeneration.cs b/src/ERNIE-Bot.KernelMemory/ERNIEBotTextGeneration.cs index cc10b43..e51132e 100644 --- a/src/ERNIE-Bot.KernelMemory/ERNIEBotTextGeneration.cs +++ b/src/ERNIE-Bot.KernelMemory/ERNIEBotTextGeneration.cs @@ -1,11 +1,6 @@ using ERNIE_Bot.SDK; using Microsoft.KernelMemory.AI; -using System; -using System.Collections.Generic; -using System.Linq; using System.Runtime.CompilerServices; -using System.Text; -using System.Threading.Tasks; namespace ERNIE_Bot.KernelMemory { diff --git a/src/ERNIE-Bot.KernelMemory/KernelMemoryBuilderExtensions.cs b/src/ERNIE-Bot.KernelMemory/KernelMemoryBuilderExtensions.cs index 7da8804..67ad6e5 100644 --- a/src/ERNIE-Bot.KernelMemory/KernelMemoryBuilderExtensions.cs +++ b/src/ERNIE-Bot.KernelMemory/KernelMemoryBuilderExtensions.cs @@ -1,10 +1,5 @@ using ERNIE_Bot.SDK; using Microsoft.KernelMemory; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace ERNIE_Bot.KernelMemory { diff --git a/src/ERNIE-Bot.SDK/Defaults.cs b/src/ERNIE-Bot.SDK/Defaults.cs index ae5818b..12ffbb2 100644 --- a/src/ERNIE-Bot.SDK/Defaults.cs +++ b/src/ERNIE-Bot.SDK/Defaults.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Reflection; -using System.Text; - -namespace ERNIE_Bot.SDK +namespace ERNIE_Bot.SDK { public static class Defaults { @@ -24,6 +19,5 @@ public static class Defaults /// Use ModelEndpoints to get the Model Endpoint /// public static string Endpoint(Endpoint endpoint) => $"https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/{endpoint.Task}/{endpoint.Model}"; - } } diff --git a/src/ERNIE-Bot.SDK/DistributedTokenStore.cs b/src/ERNIE-Bot.SDK/DistributedTokenStore.cs index 960ddbb..9693490 100644 --- a/src/ERNIE-Bot.SDK/DistributedTokenStore.cs +++ b/src/ERNIE-Bot.SDK/DistributedTokenStore.cs @@ -10,6 +10,7 @@ public DistributedTokenStore(IDistributedCache cache) { this._cache = cache; } + public async Task GetTokenAsync(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); diff --git a/src/ERNIE-Bot.SDK/ERNIEBotClient.cs b/src/ERNIE-Bot.SDK/ERNIEBotClient.cs index c8b88ef..2e75896 100644 --- a/src/ERNIE-Bot.SDK/ERNIEBotClient.cs +++ b/src/ERNIE-Bot.SDK/ERNIEBotClient.cs @@ -1,18 +1,11 @@ using ERNIE_Bot.SDK.Models; using Microsoft; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; -using System; -using System.Collections.Generic; -using System.IO; -using System.Net.Http; using System.Runtime.CompilerServices; using System.Text; using System.Text.Json; -using System.Threading; using System.Threading.RateLimiting; -using System.Threading.Tasks; namespace ERNIE_Bot.SDK { @@ -45,8 +38,8 @@ public ERNIEBotClient(string clientId, string clientSecret, int rateLimit, IToke Window = TimeSpan.FromSeconds(1), }), tokenStore, logger) { - } + /// /// API for Chat Completion /// @@ -175,7 +168,6 @@ public async Task GetAccessTokenAsync(CancellationToken cancellationToke #region ===== private methods ===== - private async Task CreateRequestAsync(HttpMethod method, string path, TRequest? body = null, CancellationToken cancellationToken = default) where TRequest : class { var accessToken = await GetAccessTokenAsync(cancellationToken); @@ -290,7 +282,7 @@ private void OrganizeChatMessages(List messages) } } } - #endregion + #endregion ===== private methods ===== } } diff --git a/src/ERNIE-Bot.SDK/HttpClientProvider.cs b/src/ERNIE-Bot.SDK/HttpClientProvider.cs index a160ee8..2fefaf5 100644 --- a/src/ERNIE-Bot.SDK/HttpClientProvider.cs +++ b/src/ERNIE-Bot.SDK/HttpClientProvider.cs @@ -1,8 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Globalization; +using System.Globalization; using System.Net; -using System.Text; using System.Threading.RateLimiting; namespace ERNIE_Bot.SDK @@ -24,6 +21,7 @@ public static HttpClient CreateFixedWindowRateLimitedClient(FixedWindowRateLimit return CreateRateLimitedClient(new FixedWindowRateLimiter(fixedWindowRateLimiterOptions)); } } + /// /// ref: https://learn.microsoft.com/dotnet/core/extensions/http-ratelimiter /// diff --git a/src/ERNIE-Bot.SDK/ModelEndpoint.cs b/src/ERNIE-Bot.SDK/ModelEndpoint.cs index 466fae8..b1c6910 100644 --- a/src/ERNIE-Bot.SDK/ModelEndpoint.cs +++ b/src/ERNIE-Bot.SDK/ModelEndpoint.cs @@ -20,7 +20,6 @@ public static implicit operator string(Endpoint endpoint) } } - /// public class ModelEndpoint : Endpoint { diff --git a/src/ERNIE-Bot.SDK/ModelEndpoints.cs b/src/ERNIE-Bot.SDK/ModelEndpoints.cs index a1e250d..7ecb075 100644 --- a/src/ERNIE-Bot.SDK/ModelEndpoints.cs +++ b/src/ERNIE-Bot.SDK/ModelEndpoints.cs @@ -28,7 +28,7 @@ public static class ModelEndpoints public static readonly ModelEndpoint Llama_2_7b_chat = new("llama_2_7b"); /// - /// Llama2 13b Chat Model + /// Llama2 13b Chat Model /// public static readonly ModelEndpoint Llama_2_13b_chat = new("llama_2_13b"); diff --git a/src/ERNIE-Bot.SDK/Models/ChatRequest.cs b/src/ERNIE-Bot.SDK/Models/ChatRequest.cs index b6ba9b8..ffa4229 100644 --- a/src/ERNIE-Bot.SDK/Models/ChatRequest.cs +++ b/src/ERNIE-Bot.SDK/Models/ChatRequest.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Text.Json.Serialization; +using System.Text.Json.Serialization; namespace ERNIE_Bot.SDK.Models { @@ -9,7 +6,9 @@ public class ChatRequest { [JsonPropertyName("messages")] public List Messages { get; set; } = new List(); + public bool? Stream { get; set; } + [JsonPropertyName("user_id")] public string? UserId { get; set; } } @@ -18,8 +17,10 @@ public class ChatCompletionsRequest : ChatRequest { [JsonPropertyName("temperature")] public float? Temperature { get; set; } + [JsonPropertyName("top_p")] public float? TopP { get; set; } + [JsonPropertyName("penalty_score")] public float? PenaltyScore { get; set; } } @@ -32,6 +33,7 @@ public class Message [JsonPropertyName("content")] public string Content { get; set; } = string.Empty; } + public static class MessageRole { public const string User = "user"; diff --git a/src/ERNIE-Bot.SDK/Models/ChatResponse.cs b/src/ERNIE-Bot.SDK/Models/ChatResponse.cs index 6a76094..d82f334 100644 --- a/src/ERNIE-Bot.SDK/Models/ChatResponse.cs +++ b/src/ERNIE-Bot.SDK/Models/ChatResponse.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Text.Json.Serialization; +using System.Text.Json.Serialization; namespace ERNIE_Bot.SDK.Models { @@ -9,20 +6,28 @@ public class ChatResponse { [JsonPropertyName("id")] public string Id { get; set; } = string.Empty; + [JsonPropertyName("object")] public string Object { get; set; } = "chat.completion"; + [JsonPropertyName("created")] public int Created { get; set; } + [JsonPropertyName("sentence_id")] public int? SentenceId { get; set; } + [JsonPropertyName("is_truncated")] public bool? IsTruncated { get; set; } + [JsonPropertyName("is_end")] public bool? IsEnd { get; set; } + [JsonPropertyName("result")] public string Result { get; set; } = string.Empty; + [JsonPropertyName("need_clear_history")] public bool NeedClearHistory { get; set; } + [JsonPropertyName("usage")] public UsageData Usage { get; set; } = new UsageData(); } diff --git a/src/ERNIE-Bot.SDK/Models/ERNIEBotError.cs b/src/ERNIE-Bot.SDK/Models/ERNIEBotError.cs index 8f4d489..fa856a0 100644 --- a/src/ERNIE-Bot.SDK/Models/ERNIEBotError.cs +++ b/src/ERNIE-Bot.SDK/Models/ERNIEBotError.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Text.Json.Serialization; +using System.Text.Json.Serialization; namespace ERNIE_Bot.SDK.Models { @@ -17,10 +14,12 @@ public class ERNIEBotError public class ERNIEBotException : Exception { public ERNIEBotError Error { get; } + public ERNIEBotException(ERNIEBotError? error) : base(error?.Message) { Error = error ?? new ERNIEBotError(); } + public ERNIEBotException(int code, string message) : this(new ERNIEBotError() { @@ -29,5 +28,4 @@ public ERNIEBotException(int code, string message) }) { } } - } diff --git a/src/ERNIE-Bot.SDK/Models/EmbeddingRequest.cs b/src/ERNIE-Bot.SDK/Models/EmbeddingRequest.cs index 0bcc13e..88e0488 100644 --- a/src/ERNIE-Bot.SDK/Models/EmbeddingRequest.cs +++ b/src/ERNIE-Bot.SDK/Models/EmbeddingRequest.cs @@ -1,11 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Text.Json.Serialization; +using System.Text.Json.Serialization; namespace ERNIE_Bot.SDK.Models { - public class EmbeddingsRequest { [JsonPropertyName("input")] diff --git a/src/ERNIE-Bot.SDK/Models/EmbeddingsResponse.cs b/src/ERNIE-Bot.SDK/Models/EmbeddingsResponse.cs index 5ba1e72..3827529 100644 --- a/src/ERNIE-Bot.SDK/Models/EmbeddingsResponse.cs +++ b/src/ERNIE-Bot.SDK/Models/EmbeddingsResponse.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Text.Json.Serialization; +using System.Text.Json.Serialization; namespace ERNIE_Bot.SDK.Models { diff --git a/src/ERNIE-Bot.SDK/Models/TokenResponse.cs b/src/ERNIE-Bot.SDK/Models/TokenResponse.cs index 333c002..9b4cae2 100644 --- a/src/ERNIE-Bot.SDK/Models/TokenResponse.cs +++ b/src/ERNIE-Bot.SDK/Models/TokenResponse.cs @@ -1,12 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Text.Json.Serialization; +using System.Text.Json.Serialization; namespace ERNIE_Bot.SDK.Models { /// - /// Access Token Response + /// Access Token Response /// public class TokenResponse { diff --git a/src/ERNIE-Bot.SDK/Models/UsageData.cs b/src/ERNIE-Bot.SDK/Models/UsageData.cs index f743919..8769aea 100644 --- a/src/ERNIE-Bot.SDK/Models/UsageData.cs +++ b/src/ERNIE-Bot.SDK/Models/UsageData.cs @@ -6,8 +6,10 @@ public class UsageData { [JsonPropertyName("prompt_tokens")] public int PromptTokens { get; set; } + [JsonPropertyName("completion_tokens")] public int CompletionTokens { get; set; } + [JsonPropertyName("total_tokens")] public int TotalTokens { get; set; } } diff --git a/src/ERNIE-Bot.SDK/TokenStore/DefaultTokenStore.cs b/src/ERNIE-Bot.SDK/TokenStore/DefaultTokenStore.cs index 37c34e5..9168fab 100644 --- a/src/ERNIE-Bot.SDK/TokenStore/DefaultTokenStore.cs +++ b/src/ERNIE-Bot.SDK/TokenStore/DefaultTokenStore.cs @@ -1,13 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace ERNIE_Bot.SDK +namespace ERNIE_Bot.SDK { public class DefaultTokenStore : ITokenStore { - static string? _access_token = null; - static DateTime? _expires_at = null; + private static string? _access_token = null; + private static DateTime? _expires_at = null; + public Task GetTokenAsync(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); diff --git a/src/ERNIE-Bot.SDK/TokenStore/ITokenStore.cs b/src/ERNIE-Bot.SDK/TokenStore/ITokenStore.cs index 81531ae..c9f421b 100644 --- a/src/ERNIE-Bot.SDK/TokenStore/ITokenStore.cs +++ b/src/ERNIE-Bot.SDK/TokenStore/ITokenStore.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading; -using System.Threading.Tasks; - -namespace ERNIE_Bot.SDK +namespace ERNIE_Bot.SDK { public interface ITokenStore { diff --git a/src/ERNIE-Bot.SDK/TokenStore/MemoryTokenStore.cs b/src/ERNIE-Bot.SDK/TokenStore/MemoryTokenStore.cs index c1f4399..b000f99 100644 --- a/src/ERNIE-Bot.SDK/TokenStore/MemoryTokenStore.cs +++ b/src/ERNIE-Bot.SDK/TokenStore/MemoryTokenStore.cs @@ -1,10 +1,5 @@ using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Caching.Memory; -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading; -using System.Threading.Tasks; namespace ERNIE_Bot.SDK { @@ -16,13 +11,13 @@ public MemoryTokenStore(IMemoryCache cache) { this._cache = cache; } + public Task GetTokenAsync(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); _cache.TryGetValue(Defaults.TokenCacheName, out string? accessToken); return Task.FromResult(accessToken); - } public Task SaveTokenAsync(string accessToken, TimeSpan expiration, CancellationToken cancellationToken) diff --git a/src/ERNIE-Bot.SDK/Tokenizer.cs b/src/ERNIE-Bot.SDK/Tokenizer.cs index e0ab4bf..20316a0 100644 --- a/src/ERNIE-Bot.SDK/Tokenizer.cs +++ b/src/ERNIE-Bot.SDK/Tokenizer.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Text.RegularExpressions; +using System.Text.RegularExpressions; namespace ERNIE_Bot.SDK { diff --git a/src/ERNIE-Bot.SemanticKernel/ERNIEBotAIRequestSettings.cs b/src/ERNIE-Bot.SemanticKernel/ERNIEBotAIRequestSettings.cs index ff3d1aa..864a235 100644 --- a/src/ERNIE-Bot.SemanticKernel/ERNIEBotAIRequestSettings.cs +++ b/src/ERNIE-Bot.SemanticKernel/ERNIEBotAIRequestSettings.cs @@ -13,7 +13,6 @@ public class ERNIEBotAIRequestSettings : AIRequestSettings [JsonPropertyName("penalty_score")] public float? PenaltyScore { get; set; } - public static ERNIEBotAIRequestSettings FromRequestSettings(AIRequestSettings? requestSettings, int? defaultMaxTokens = null) { if (requestSettings is null) @@ -52,4 +51,4 @@ private static JsonSerializerOptions CreateOptions() return options; } -} \ No newline at end of file +} diff --git a/src/ERNIE-Bot.SemanticKernel/ERNIEBotChatCompletion.cs b/src/ERNIE-Bot.SemanticKernel/ERNIEBotChatCompletion.cs index b74d451..57db27d 100644 --- a/src/ERNIE-Bot.SemanticKernel/ERNIEBotChatCompletion.cs +++ b/src/ERNIE-Bot.SemanticKernel/ERNIEBotChatCompletion.cs @@ -5,10 +5,7 @@ using Microsoft.SemanticKernel.AI.ChatCompletion; using Microsoft.SemanticKernel.AI.TextCompletion; using Microsoft.SemanticKernel.Diagnostics; -using System; -using System.Collections.Generic; using System.Runtime.CompilerServices; -using System.Text; public class ERNIEBotChatCompletion : IChatCompletion, ITextCompletion { @@ -24,6 +21,7 @@ public ERNIEBotChatCompletion(ERNIEBotClient client, ModelEndpoint? modelEndpoin this._modelEndpoint = modelEndpoint ?? ModelEndpoints.ERNIE_Bot; } + public ChatHistory CreateNewChat(string? instructions = null) { var history = new ChatHistory(); @@ -36,7 +34,6 @@ public ChatHistory CreateNewChat(string? instructions = null) return history; } - public async Task> GetChatCompletionsAsync(ChatHistory chat, AIRequestSettings? requestSettings = null, CancellationToken cancellationToken = default) { var messages = ChatHistoryToMessages(chat); @@ -53,8 +50,6 @@ public async Task> GetChatCompletionsAsync(ChatHistor return new List() { new ERNIEBotChatResult(result) }; } - - public async Task> GetCompletionsAsync(string text, AIRequestSettings? requestSettings, CancellationToken cancellationToken = default) { requestSettings ??= new AIRequestSettings(); diff --git a/src/ERNIE-Bot.SemanticKernel/ERNIEBotChatResult.cs b/src/ERNIE-Bot.SemanticKernel/ERNIEBotChatResult.cs index 953ff75..06dd464 100644 --- a/src/ERNIE-Bot.SemanticKernel/ERNIEBotChatResult.cs +++ b/src/ERNIE-Bot.SemanticKernel/ERNIEBotChatResult.cs @@ -2,15 +2,12 @@ using Microsoft.SemanticKernel.AI.ChatCompletion; using Microsoft.SemanticKernel.AI.TextCompletion; using Microsoft.SemanticKernel.Orchestration; -using System; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Text; namespace Connectors.AI.ERNIEBot { - class ERNIEBotChatMessage : ChatMessage + internal class ERNIEBotChatMessage : ChatMessage { public ERNIEBotChatMessage(string content) : base(AuthorRole.Assistant, content) @@ -22,11 +19,13 @@ internal class ERNIEBotChatResult : IChatResult, ITextResult, IChatStreamingResu { private readonly ChatResponse? _response; private readonly IAsyncEnumerable? _responses; + public ERNIEBotChatResult(IAsyncEnumerable responses) { this.ModelResult = new ModelResult(responses); this._responses = responses; } + public ERNIEBotChatResult(ChatResponse response) { this.ModelResult = new ModelResult(response); @@ -60,6 +59,7 @@ public async Task GetCompletionAsync(CancellationToken cancellationToken } #region Streaming + public async IAsyncEnumerable GetCompletionStreamingAsync([EnumeratorCancellation] CancellationToken cancellationToken = default) { await foreach (var response in _responses.WithCancellation(cancellationToken).ConfigureAwait(false)) @@ -75,6 +75,7 @@ public async IAsyncEnumerable GetStreamingChatMessageAsync([Enumera yield return new ERNIEBotChatMessage(response.Result); } } - #endregion + + #endregion Streaming } } diff --git a/src/ERNIE-Bot.SemanticKernel/ERNIEBotEmbeddingGeneration.cs b/src/ERNIE-Bot.SemanticKernel/ERNIEBotEmbeddingGeneration.cs index 356b203..b5c4ad7 100644 --- a/src/ERNIE-Bot.SemanticKernel/ERNIEBotEmbeddingGeneration.cs +++ b/src/ERNIE-Bot.SemanticKernel/ERNIEBotEmbeddingGeneration.cs @@ -1,13 +1,7 @@ using ERNIE_Bot.SDK; using ERNIE_Bot.SDK.Models; -using Microsoft.SemanticKernel.AI; using Microsoft.SemanticKernel.AI.Embeddings; using Microsoft.SemanticKernel.Diagnostics; -using Microsoft.SemanticKernel.Services; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; public class ERNIEBotEmbeddingGeneration : ITextEmbeddingGeneration { @@ -29,7 +23,7 @@ public async Task>> GenerateEmbeddingsAsync(IList + // TODO: ITextEmbeddingGeneration not support ReadOnlyMemory return embeddings.Data.Select(d => new ReadOnlyMemory(d.Embedding.Select(e => (float)e).ToArray())).ToList(); } catch (ERNIEBotException ex) diff --git a/src/ERNIE-Bot.SemanticKernel/ERNIEBotKernelBuilderExtensions.cs b/src/ERNIE-Bot.SemanticKernel/ERNIEBotKernelBuilderExtensions.cs index d5158fb..2138d22 100644 --- a/src/ERNIE-Bot.SemanticKernel/ERNIEBotKernelBuilderExtensions.cs +++ b/src/ERNIE-Bot.SemanticKernel/ERNIEBotKernelBuilderExtensions.cs @@ -3,7 +3,6 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.SemanticKernel.AI.ChatCompletion; -using Microsoft.SemanticKernel.AI.Embeddings; using Microsoft.SemanticKernel.AI.TextCompletion; using Microsoft.SemanticKernel.Plugins.Memory; @@ -43,7 +42,7 @@ public static KernelBuilder WithERNIEBotChatCompletionService(this KernelBuilder } /// - /// + /// /// /// /// @@ -73,7 +72,7 @@ public static KernelBuilder WithERNIEBotChatCompletionService(this KernelBuilder } /// - /// + /// /// /// /// @@ -89,7 +88,7 @@ public static MemoryBuilder WithERNIEBotEmbeddingGenerationService(this MemoryBu } /// - /// + /// /// /// /// diff --git a/tests/ERNIE-Bot.SDK.Tests/ERNIEBotClientTests.cs b/tests/ERNIE-Bot.SDK.Tests/ERNIEBotClientTests.cs index 16d8f6d..f0ca62e 100644 --- a/tests/ERNIE-Bot.SDK.Tests/ERNIEBotClientTests.cs +++ b/tests/ERNIE-Bot.SDK.Tests/ERNIEBotClientTests.cs @@ -1,12 +1,5 @@ -using Xunit; -using ERNIE_Bot.SDK; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ERNIE_Bot.SDK.Models; using ERNIE_BOT.SDK.Tests; -using ERNIE_Bot.SDK.Models; namespace ERNIE_Bot.SDK.Tests { @@ -157,4 +150,4 @@ await Assert.ThrowsAsync(async () => }); } } -} \ No newline at end of file +} diff --git a/tests/ERNIE-Bot.SDK.Tests/TestHelper.cs b/tests/ERNIE-Bot.SDK.Tests/TestHelper.cs index eebedfd..6f60360 100644 --- a/tests/ERNIE-Bot.SDK.Tests/TestHelper.cs +++ b/tests/ERNIE-Bot.SDK.Tests/TestHelper.cs @@ -1,10 +1,4 @@ -using NSubstitute; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading.Tasks; +using System.Net; namespace ERNIE_BOT.SDK.Tests { @@ -45,9 +39,6 @@ internal static async Task GetHttpResponseFormFileAsync(str Content = streamContent }; } - - - } internal static async Task FakeHttpClient(string fileName, bool isStream = false) @@ -58,6 +49,7 @@ internal static async Task FakeHttpClient(string fileName, bool isSt return client; } } + public class MockHttpMessageHandler : HttpMessageHandler { private readonly HttpResponseMessage _response; @@ -66,6 +58,7 @@ public MockHttpMessageHandler(HttpResponseMessage response) { this._response = response; } + protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { return Task.FromResult(_response); diff --git a/tests/ERNIE-Bot.SDK.Tests/TokenStoreHelper.cs b/tests/ERNIE-Bot.SDK.Tests/TokenStoreHelper.cs index f6b1a5d..bdf697d 100644 --- a/tests/ERNIE-Bot.SDK.Tests/TokenStoreHelper.cs +++ b/tests/ERNIE-Bot.SDK.Tests/TokenStoreHelper.cs @@ -1,9 +1,4 @@ using ERNIE_Bot.SDK; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace ERNIE_BOT.SDK.Tests { diff --git a/tests/ERNIE-Bot.SDK.Tests/TokenizerTests.cs b/tests/ERNIE-Bot.SDK.Tests/TokenizerTests.cs index f044d80..3481765 100644 --- a/tests/ERNIE-Bot.SDK.Tests/TokenizerTests.cs +++ b/tests/ERNIE-Bot.SDK.Tests/TokenizerTests.cs @@ -1,6 +1,4 @@ -using ERNIE_Bot.SDK; - -namespace ERNIE_Bot.SDK.Tests +namespace ERNIE_Bot.SDK.Tests { public class TokenizerTests { diff --git a/tests/ERNIE-Bot.SDK.Tests/Usings.cs b/tests/ERNIE-Bot.SDK.Tests/Usings.cs index 8c927eb..c802f44 100644 --- a/tests/ERNIE-Bot.SDK.Tests/Usings.cs +++ b/tests/ERNIE-Bot.SDK.Tests/Usings.cs @@ -1 +1 @@ -global using Xunit; \ No newline at end of file +global using Xunit; diff --git a/tests/IntegrationTests/SDK/ChatCompletionStreamTest.cs b/tests/IntegrationTests/SDK/ChatCompletionStreamTest.cs index 5ffaeba..24051cd 100644 --- a/tests/IntegrationTests/SDK/ChatCompletionStreamTest.cs +++ b/tests/IntegrationTests/SDK/ChatCompletionStreamTest.cs @@ -1,11 +1,6 @@ using ERNIE_Bot.SDK; -using Microsoft.Extensions.Configuration; using Microsoft; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; namespace IntegrationTests.SDK { @@ -27,7 +22,9 @@ public ChatCompletionStreamTest() _client = new ERNIEBotClient(clientId, clientSecret); } + #region StreamChatCompletion + private async IAsyncEnumerable InternalStreamChatAsync(ModelEndpoint endpoint) { var response = _client.ChatStreamAsync(new ERNIE_Bot.SDK.Models.ChatRequest() @@ -48,7 +45,6 @@ private async IAsyncEnumerable InternalStreamChatAsync(ModelEndpoint end } } - [Fact] public async Task ERNIEBotStreamChatAsync() { @@ -148,6 +144,7 @@ public async Task AquilaChat_7bStreamAsync() break; } } - #endregion + + #endregion StreamChatCompletion } } diff --git a/tests/IntegrationTests/SDK/ChatCompletionTest.cs b/tests/IntegrationTests/SDK/ChatCompletionTest.cs index fd2e5e0..bebe352 100644 --- a/tests/IntegrationTests/SDK/ChatCompletionTest.cs +++ b/tests/IntegrationTests/SDK/ChatCompletionTest.cs @@ -1,11 +1,6 @@ using ERNIE_Bot.SDK; using Microsoft; using Microsoft.Extensions.Configuration; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace IntegrationTests.SDK { @@ -28,7 +23,6 @@ public ChatCompletionTest() _client = new ERNIEBotClient(clientId, clientSecret); } - #region ChatCompletion private async Task InternalChatAsync(ModelEndpoint endpoint) @@ -47,7 +41,6 @@ private async Task InternalChatAsync(ModelEndpoint endpoint) return response.Result; } - [Fact] public async Task ERNIEBotChatAsync() { @@ -127,9 +120,7 @@ public async Task AquilaChat_7bAsync() Assert.NotNull(result); } - #endregion - - + #endregion ChatCompletion } } diff --git a/tests/IntegrationTests/SDK/EmbeddingTest.cs b/tests/IntegrationTests/SDK/EmbeddingTest.cs index f121336..717c8b1 100644 --- a/tests/IntegrationTests/SDK/EmbeddingTest.cs +++ b/tests/IntegrationTests/SDK/EmbeddingTest.cs @@ -1,11 +1,6 @@ using ERNIE_Bot.SDK; -using Microsoft.Extensions.Configuration; using Microsoft; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; namespace IntegrationTests.SDK { @@ -29,6 +24,7 @@ public EmbeddingTest() } #region Embedding + private async Task> InternalEmbeddingAsync(EmbeddingModelEndpoint endpoint) { var response = await _client.EmbeddingsAsync(new ERNIE_Bot.SDK.Models.EmbeddingsRequest() @@ -61,6 +57,7 @@ public async Task bge_large_enAsync() Assert.NotEmpty(result); } - #endregion + + #endregion Embedding } } diff --git a/tests/IntegrationTests/Usings.cs b/tests/IntegrationTests/Usings.cs index 8c927eb..c802f44 100644 --- a/tests/IntegrationTests/Usings.cs +++ b/tests/IntegrationTests/Usings.cs @@ -1 +1 @@ -global using Xunit; \ No newline at end of file +global using Xunit;