Skip to content

Commit

Permalink
clean code & bump 0.9.0 (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
xbotter authored Nov 23, 2023
1 parent 27f362b commit 17070d3
Show file tree
Hide file tree
Showing 44 changed files with 89 additions and 200 deletions.
25 changes: 13 additions & 12 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@
</PropertyGroup>
<ItemGroup>
<!-- Microsoft -->
<PackageVersion Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageVersion Include="Microsoft.KernelMemory.Core" Version="0.10.231119.1-preview" />
<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.11.231121.2-preview" />
<PackageVersion Include="Microsoft.SemanticKernel.Abstractions" Version="1.0.0-beta8" />
<PackageVersion Include="Microsoft.SemanticKernel.Core" Version="1.0.0-beta8" />
<PackageVersion Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageVersion Include="Microsoft.SemanticKernel.Plugins.Core" Version="1.0.0-beta6" />
<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-beta8" />
<PackageVersion Include="Microsoft.SemanticKernel.TemplateEngine.Basic" Version="1.0.0-beta6" />
<PackageVersion Include="Microsoft.SemanticKernel.TemplateEngine.Basic" Version="1.0.0-beta8" />
<!-- Validation -->
<PackageVersion Include="Microsoft.VisualStudio.Validation" Version="17.6.11" />
<PackageVersion Include="Microsoft.VisualStudio.Validation" Version="17.8.8" />
<!-- Tests -->
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="System.Text.Json" Version="7.0.3" />
<PackageVersion Include="System.Text.Json" Version="8.0.0" />
<PackageVersion Include="System.Threading.RateLimiting" Version="8.0.0" />
<PackageVersion Include="xunit" Version="2.6.1" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.3" />
<PackageVersion Include="xunit" Version="2.6.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.4" />
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
<!-- Misc -->
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
Expand Down
7 changes: 2 additions & 5 deletions samples/ERNIE-Bot-Kernel-Memory.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -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<Program>()
.Build();


var client = new ERNIEBotClient(config["ClientId"]!, config["ClientSecret"]!,
HttpClientProvider.CreateFixedWindowRateLimitedClient(new FixedWindowRateLimiterOptions()
{
Expand Down Expand Up @@ -45,4 +42,4 @@
foreach (var x in answer.RelevantSources)
{
Console.WriteLine($" - {x.SourceName} - {x.Link} [{x.Partitions.First().LastUpdate:D}]");
}
}
2 changes: 0 additions & 2 deletions samples/ERNIE-Bot.Sample/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -161,7 +160,6 @@ public async Task<IActionResult> EmbeddingAsync([FromBody] UserInput input)
[HttpPost("History")]
public async Task<IActionResult> HistoryAsync([FromBody] UserHistoryInput input)
{

if (!input.Messages.Any())
{
return NoContent();
Expand Down
1 change: 1 addition & 0 deletions samples/ERNIE-Bot.Sample/Controllers/Models/UserInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserInput> Messages { get; set; } = new List<UserInput> { };
Expand Down
1 change: 0 additions & 1 deletion samples/ERNIE-Bot.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

builder.Services.AddScoped(svc =>
{

var logger = svc.GetRequiredService<ILogger<ERNIEBotClient>>();

var clientId = builder.Configuration["ClientId"];
Expand Down
5 changes: 1 addition & 4 deletions samples/SK-ERNIE-Bot.Sample/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -35,7 +34,6 @@ public async Task<IActionResult> ChatAsync([FromBody] UserInput input, Cancellat
var result = await chat.GenerateMessageAsync(history, cancellationToken: cancellationToken);

return Ok(result);

}

[HttpPost("text")]
Expand All @@ -52,7 +50,6 @@ public async Task<IActionResult> TextAsync([FromBody] UserInput input, Cancellat

var text = await result.First().GetCompletionAsync();
return Ok(text);

}

[HttpPost("stream")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
"type": "completion",
"description": "",
"completion": {

},
"default_services": [],
"input": {
"parameters": []
}
}
}
1 change: 0 additions & 1 deletion samples/SK-ERNIE-Bot.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
return memory;
});


var app = builder.Build();

// Configure the HTTP request pipeline.
Expand Down
4 changes: 2 additions & 2 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
<PackageId>$(AssemblyName)</PackageId>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
<LangVersion>12</LangVersion>
<Authors>Custouch</Authors>
<PackageLicenseFile>LICENSE</PackageLicenseFile>

<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.8.0-preview</Version>
<Version>0.9.0-preview</Version>
<PackageOutputPath>..\..\nupkgs</PackageOutputPath>
<PackageReadmeFile>readme.md</PackageReadmeFile>

Expand Down
2 changes: 1 addition & 1 deletion src/ERNIE-Bot.KernelMemory/ERNIE-Bot.KernelMemory.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.KernelMemory.Core" />
<PackageReference Include="Microsoft.KernelMemory.Core" />
</ItemGroup>

<ItemGroup>
Expand Down
8 changes: 0 additions & 8 deletions src/ERNIE-Bot.KernelMemory/ERNIEBotTextEmbeddingGeneration.cs
Original file line number Diff line number Diff line change
@@ -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
{

/// <summary>
/// Generating text embeddings using ERNIEBotClient.
/// </summary>
Expand All @@ -17,8 +11,6 @@ public class ERNIEBotTextEmbeddingGeneration : ITextEmbeddingGeneration
private readonly ERNIEBotClient _client;
private readonly EmbeddingModelEndpoint _endpoint;



/// <summary>
/// Initializes a new instance of the <see cref="ERNIEBotTextEmbeddingGeneration"/> class.
/// </summary>
Expand Down
5 changes: 0 additions & 5 deletions src/ERNIE-Bot.KernelMemory/ERNIEBotTextGeneration.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
5 changes: 0 additions & 5 deletions src/ERNIE-Bot.KernelMemory/KernelMemoryBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
8 changes: 1 addition & 7 deletions src/ERNIE-Bot.SDK/Defaults.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -24,6 +19,5 @@ public static class Defaults
/// Use <see cref="ModelEndpoints">ModelEndpoints</see> to get the Model Endpoint
/// </summary>
public static string Endpoint(Endpoint endpoint) => $"https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/{endpoint.Task}/{endpoint.Model}";

}
}
1 change: 1 addition & 0 deletions src/ERNIE-Bot.SDK/DistributedTokenStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public DistributedTokenStore(IDistributedCache cache)
{
this._cache = cache;
}

public async Task<string?> GetTokenAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
Expand Down
12 changes: 2 additions & 10 deletions src/ERNIE-Bot.SDK/ERNIEBotClient.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -45,8 +38,8 @@ public ERNIEBotClient(string clientId, string clientSecret, int rateLimit, IToke
Window = TimeSpan.FromSeconds(1),
}), tokenStore, logger)
{

}

/// <summary>
/// API for Chat Completion
/// </summary>
Expand Down Expand Up @@ -175,7 +168,6 @@ public async Task<string> GetAccessTokenAsync(CancellationToken cancellationToke

#region ===== private methods =====


private async Task<HttpRequestMessage> CreateRequestAsync<TRequest>(HttpMethod method, string path, TRequest? body = null, CancellationToken cancellationToken = default) where TRequest : class
{
var accessToken = await GetAccessTokenAsync(cancellationToken);
Expand Down Expand Up @@ -290,7 +282,7 @@ private void OrganizeChatMessages(List<Message> messages)
}
}
}
#endregion

#endregion ===== private methods =====
}
}
6 changes: 2 additions & 4 deletions src/ERNIE-Bot.SDK/HttpClientProvider.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -24,6 +21,7 @@ public static HttpClient CreateFixedWindowRateLimitedClient(FixedWindowRateLimit
return CreateRateLimitedClient(new FixedWindowRateLimiter(fixedWindowRateLimiterOptions));
}
}

/// <summary>
/// ref: https://learn.microsoft.com/dotnet/core/extensions/http-ratelimiter
/// </summary>
Expand Down
1 change: 0 additions & 1 deletion src/ERNIE-Bot.SDK/ModelEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public static implicit operator string(Endpoint endpoint)
}
}


/// <inheritdoc/>
public class ModelEndpoint : Endpoint
{
Expand Down
2 changes: 1 addition & 1 deletion src/ERNIE-Bot.SDK/ModelEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static class ModelEndpoints
public static readonly ModelEndpoint Llama_2_7b_chat = new("llama_2_7b");

/// <summary>
/// <see href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/2lki2us1e">Llama2 13b Chat Model</see>
/// <see href="https://cloud.baidu.com/doc/WENXINWORKSHOP/s/2lki2us1e">Llama2 13b Chat Model</see>
/// </summary>
public static readonly ModelEndpoint Llama_2_13b_chat = new("llama_2_13b");

Expand Down
10 changes: 6 additions & 4 deletions src/ERNIE-Bot.SDK/Models/ChatRequest.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
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 ChatRequest
{
[JsonPropertyName("messages")]
public List<Message> Messages { get; set; } = new List<Message>();

public bool? Stream { get; set; }

[JsonPropertyName("user_id")]
public string? UserId { get; set; }
}
Expand All @@ -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; }
}
Expand All @@ -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";
Expand Down
13 changes: 9 additions & 4 deletions src/ERNIE-Bot.SDK/Models/ChatResponse.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
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 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();
}
Expand Down
Loading

0 comments on commit 17070d3

Please sign in to comment.