Skip to content

Commit

Permalink
add mock类型便于测试
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzeyu91 committed Mar 12, 2024
1 parent 2f9e2fb commit 4f89d54
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/AntSK.Domain/AntSK.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AntSk.LLM.SparkDesk\AntSk.LLM.SparkDesk.csproj" />
<ProjectReference Include="..\AntSk.LLM\AntSK.LLM.csproj" />
<ProjectReference Include="..\MiddleWare\AntSK.BackgroundTask\AntSK.BackgroundTask.csproj" />
</ItemGroup>

Expand Down
6 changes: 5 additions & 1 deletion src/AntSK.Domain/Domain/Service/KernelService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AntSk.LLM.SparkDesk;
using AntSK.LLM.SparkDesk;
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Domain.Other;
Expand All @@ -14,6 +14,7 @@
using RestSharp;
using System;
using ServiceLifetime = AntSK.Domain.Common.DependencyInjection.ServiceLifetime;
using AntSK.LLM.Mock;

namespace AntSK.Domain.Domain.Service
{
Expand Down Expand Up @@ -93,6 +94,9 @@ private void WithTextGenerationByAIType(IKernelBuilder builder, Apps app, AIMode
var options = new SparkDeskOptions { AppId = chatModel.EndPoint, ApiSecret = chatModel.ModelKey, ApiKey = chatModel.ModelName, ModelVersion = Sdcb.SparkDesk.ModelVersion.V3_5 };
builder.Services.AddKeyedSingleton<ITextGenerationService>("spark-desk", new SparkDeskTextCompletion(options, app.Id));
break;
case Model.Enum.AIType.Mock:
builder.Services.AddKeyedSingleton<ITextGenerationService>("mock", new MockTextCompletion());
break;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/AntSK.Domain/Model/Enum/AIModelType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum AIType
AzureOpenAI = 2,
LLamaSharp=3,
SparkDesk=4,
Mock=5,
}

/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions src/AntSK.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "MiddleWare", "MiddleWare",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AntSK.BackgroundTask", "MiddleWare\AntSK.BackgroundTask\AntSK.BackgroundTask.csproj", "{DF87E829-99C5-44A7-9718-B3E67DC801F6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AntSk.LLM.SparkDesk", "AntSk.LLM.SparkDesk\AntSk.LLM.SparkDesk.csproj", "{1B4313B3-EF8E-4ADC-982A-C3F608BDEFDB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AntSK.LLM", "AntSk.LLM\AntSK.LLM.csproj", "{19529BFA-152F-4A8C-8254-F2D4896AB739}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -38,10 +38,10 @@ Global
{DF87E829-99C5-44A7-9718-B3E67DC801F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF87E829-99C5-44A7-9718-B3E67DC801F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF87E829-99C5-44A7-9718-B3E67DC801F6}.Release|Any CPU.Build.0 = Release|Any CPU
{1B4313B3-EF8E-4ADC-982A-C3F608BDEFDB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1B4313B3-EF8E-4ADC-982A-C3F608BDEFDB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1B4313B3-EF8E-4ADC-982A-C3F608BDEFDB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1B4313B3-EF8E-4ADC-982A-C3F608BDEFDB}.Release|Any CPU.Build.0 = Release|Any CPU
{19529BFA-152F-4A8C-8254-F2D4896AB739}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{19529BFA-152F-4A8C-8254-F2D4896AB739}.Debug|Any CPU.Build.0 = Debug|Any CPU
{19529BFA-152F-4A8C-8254-F2D4896AB739}.Release|Any CPU.ActiveCfg = Release|Any CPU
{19529BFA-152F-4A8C-8254-F2D4896AB739}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
7 changes: 4 additions & 3 deletions src/AntSK/Pages/KmsPage/AddKms.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ public partial class AddKms
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
//星火没实现KM先隐藏
_chatList = _aimodels_Repositories.GetList(p => p.AIModelType == AIModelType.Chat&&p.AIType!=AIType.SparkDesk);
_embeddingList = _aimodels_Repositories.GetList(p => p.AIModelType == AIModelType.Embedding && p.AIType != AIType.SparkDesk);
//星火 Mock没实现KM先隐藏
List<AIType> ignores = new List<AIType>() { AIType.SparkDesk, AIType.Mock };
_chatList = _aimodels_Repositories.GetList(p => p.AIModelType == AIModelType.Chat&& !ignores.Contains(p.AIType));
_embeddingList = _aimodels_Repositories.GetList(p => p.AIModelType == AIModelType.Embedding && !ignores.Contains(p.AIType));
if (!string.IsNullOrEmpty(KmsId))
{
//查看
Expand Down
5 changes: 5 additions & 0 deletions src/AntSK/Pages/Setting/AIModel/AddModel.razor
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@
<FormItem Label="模型路径" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
<Input Placeholder="请输入模型路径" @bind-Value="@context.ModelName" />
</FormItem>
}
@if (context.AIType == AIType.Mock)
{


}
<FormItem Label=" " Style="margin-top:32px" WrapperCol="LayoutModel._submitFormLayout.WrapperCol">
<Button Type="primary" OnClick="HandleSubmit">
Expand Down
4 changes: 4 additions & 0 deletions src/AntSK/Pages/Setting/AIModel/ModelList.razor
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
{
<Tag Color="@PresetColor.Orange.ToString()">SparkDesk</Tag>
}
else if (context.AIType == AIType.Mock)
{
<Tag Color="@PresetColor.Cyan.ToString()">Mock</Tag>
}
</p>
</div>

Expand Down
File renamed without changes.
56 changes: 56 additions & 0 deletions src/AntSk.LLM/Mock/MockTextCompletion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using AntSK.LLM.SparkDesk;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Services;
using Microsoft.SemanticKernel.TextGeneration;
using Sdcb.SparkDesk;
using System;
using System.ComponentModel;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Unicode;

namespace AntSK.LLM.Mock
{
public class MockTextCompletion : ITextGenerationService, IAIService
{
private readonly Dictionary<string, object?> _attributes = new();
private readonly SparkDeskClient _client;
private string _chatId;
private readonly SparkDeskOptions _options;

private static readonly JsonSerializerOptions _jsonSerializerOptions = new()
{
NumberHandling = JsonNumberHandling.AllowReadingFromString,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
};

public IReadOnlyDictionary<string, object?> Attributes => _attributes;

public MockTextCompletion()
{

}

public async Task<IReadOnlyList<TextContent>> GetTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, CancellationToken cancellationToken = default)
{
StringBuilder sb = new();
string result = $"这是一条Mock数据,便于聊天测试,你的消息是:{prompt}";
return [new(result.ToString())];
}

public async IAsyncEnumerable<StreamingTextContent> GetStreamingTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, CancellationToken cancellationToken = default)
{
StringBuilder sb = new();
string result = $"这是一条Mock数据,便于聊天测试,你的消息是:{prompt}";
foreach (var c in result)
{
var streamingTextContent = new StreamingTextContent(c.ToString(), modelId: "mock");

yield return streamingTextContent;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Text;
using System.Threading.Tasks;

namespace AntSk.LLM.SparkDesk
namespace AntSK.LLM.SparkDesk
{
public class SparkDeskOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using System.Text.Json.Serialization;
using System.Text.Unicode;

namespace AntSk.LLM.SparkDesk
namespace AntSK.LLM.SparkDesk
{
public class SparkDeskTextCompletion : ITextGenerationService, IAIService
{
Expand Down

0 comments on commit 4f89d54

Please sign in to comment.