Skip to content

Commit 67433df

Browse files
authored
Merge pull request #13 from AIDotNet/feature_kernel
add 0.1.6 抽象接口实现
2 parents c7160bf + a20c465 commit 67433df

File tree

8 files changed

+168
-3
lines changed

8 files changed

+168
-3
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<!-- See https://aka.ms/dotnet/msbuild/customize for more details on customizing your build -->
33
<PropertyGroup>
4-
<Version>0.1.5</Version>
4+
<Version>0.1.6</Version>
55
<SKVersion>1.16.2</SKVersion>
66
</PropertyGroup>
77
</Project>

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ builder.Configuration.GetSection("GraphDBConnection").Get<GraphDBConnectionOptio
7575
builder.Services.AddGraphRagNet();
7676
```
7777

78+
## 如果你想接入其他模型,可以参考以下代码,这里抽象了Kernel的实现,你可以自定义实现
79+
```
80+
var kernelBuild = Kernel.CreateBuilder();
81+
kernelBuild.Services.AddKeyedSingleton<ITextGenerationService>("mock-text", new MockTextCompletion());
82+
kernelBuild.Services.AddKeyedSingleton<IChatCompletionService>("mock-chat", new MockChatCompletion());
83+
kernelBuild.Services.AddSingleton((ITextEmbeddingGenerationService)new MockTextEmbeddingGeneratorService());
84+
kernelBuild.Services.AddKeyedSingleton("mock-embedding", new MockTextEmbeddingGeneratorService());
85+
86+
builder.Services.AddGraphRagNet(kernelBuild.Build());
87+
```
88+
89+
7890
使用时注入 IGraphService 服务,以下为参考示例代码
7991
```
8092
namespace GraphRag.Net.Api.Controllers

src/GraphRag.Net.Web/GraphRag.Net.Web.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<Description>商务需求联系微信xuzeyu91</Description>
1111
<GenerateDocumentationFile>True</GenerateDocumentationFile>
1212
<DocumentationFile>GraphRag.Net.Web.xml</DocumentationFile>
13+
<NoWarn>CA1050,CA1707,CA2007,VSTHRD111,CS1591,RCS1110,CA5394,SKEXP0001,SKEXP0002,SKEXP0003,SKEXP0004,SKEXP0010,SKEXP0011,,SKEXP0012,SKEXP0020,SKEXP0021,SKEXP0022,SKEXP0023,SKEXP0024,SKEXP0025,SKEXP0026,SKEXP0027,SKEXP0028,SKEXP0029,SKEXP0030,SKEXP0031,SKEXP0032,SKEXP0040,SKEXP0041,SKEXP0042,SKEXP0050,SKEXP0051,SKEXP0052,SKEXP0053,SKEXP0054,SKEXP0055,SKEXP0060,SKEXP0061,SKEXP0101,SKEXP0102,KMEXP01</NoWarn>
1314
</PropertyGroup>
1415

1516
<ItemGroup>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Microsoft.SemanticKernel;
2+
using Microsoft.SemanticKernel.ChatCompletion;
3+
using System.Runtime.CompilerServices;
4+
using System.Text;
5+
using System.Text.Encodings.Web;
6+
using System.Text.Json;
7+
using System.Text.Json.Serialization;
8+
using System.Text.Unicode;
9+
10+
namespace GraphRag.Net.Web.Mock
11+
{
12+
public class MockChatCompletion : IChatCompletionService
13+
{
14+
private readonly Dictionary<string, object?> _attributes = new();
15+
private string _chatId;
16+
17+
18+
private static readonly JsonSerializerOptions _jsonSerializerOptions = new()
19+
{
20+
NumberHandling = JsonNumberHandling.AllowReadingFromString,
21+
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
22+
};
23+
24+
public IReadOnlyDictionary<string, object?> Attributes => _attributes;
25+
26+
public MockChatCompletion()
27+
{
28+
29+
}
30+
31+
public async Task<IReadOnlyList<ChatMessageContent>> GetChatMessageContentsAsync(ChatHistory chatHistory, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
32+
{
33+
StringBuilder sb = new();
34+
string result = $"这是一条Mock数据,便于聊天测试,你的消息是:{chatHistory.LastOrDefault().ToString()}";
35+
return [new(AuthorRole.Assistant, result.ToString())];
36+
}
37+
38+
public async IAsyncEnumerable<StreamingChatMessageContent> GetStreamingChatMessageContentsAsync(ChatHistory chatHistory, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
39+
{
40+
StringBuilder sb = new();
41+
string result = $"这是一条Mock数据,便于聊天测试,你的消息是:{chatHistory.LastOrDefault().ToString()}";
42+
foreach (var c in result)
43+
{
44+
yield return new StreamingChatMessageContent(AuthorRole.Assistant, c.ToString());
45+
}
46+
}
47+
}
48+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Microsoft.SemanticKernel;
2+
using Microsoft.SemanticKernel.Services;
3+
using Microsoft.SemanticKernel.TextGeneration;
4+
using System.Text;
5+
using System.Text.Encodings.Web;
6+
using System.Text.Json;
7+
using System.Text.Json.Serialization;
8+
using System.Text.Unicode;
9+
10+
namespace GraphRag.Net.Web.Mock
11+
{
12+
public class MockTextCompletion : ITextGenerationService, IAIService
13+
{
14+
private readonly Dictionary<string, object?> _attributes = new();
15+
private string _chatId;
16+
17+
private static readonly JsonSerializerOptions _jsonSerializerOptions = new()
18+
{
19+
NumberHandling = JsonNumberHandling.AllowReadingFromString,
20+
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
21+
};
22+
23+
public IReadOnlyDictionary<string, object?> Attributes => _attributes;
24+
25+
public MockTextCompletion()
26+
{
27+
28+
}
29+
30+
public async Task<IReadOnlyList<TextContent>> GetTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, CancellationToken cancellationToken = default)
31+
{
32+
StringBuilder sb = new();
33+
string result = $"这是一条Mock数据,便于聊天测试,你的消息是:{prompt}";
34+
return [new(result.ToString())];
35+
}
36+
37+
public async IAsyncEnumerable<StreamingTextContent> GetStreamingTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, CancellationToken cancellationToken = default)
38+
{
39+
StringBuilder sb = new();
40+
string result = $"这是一条Mock数据,便于聊天测试,你的消息是:{prompt}";
41+
foreach (var c in result)
42+
{
43+
var streamingTextContent = new StreamingTextContent(c.ToString(), modelId: "mock");
44+
45+
yield return streamingTextContent;
46+
}
47+
}
48+
}
49+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Microsoft.SemanticKernel;
2+
using Microsoft.SemanticKernel.Embeddings;
3+
using Microsoft.SemanticKernel.Services;
4+
5+
namespace GraphRag.Net.Web.Mock
6+
{
7+
public sealed class MockTextEmbeddingGeneratorService : ITextEmbeddingGenerationService
8+
{
9+
private Dictionary<string, object?> AttributesInternal { get; } = [];
10+
public IReadOnlyDictionary<string, object?> Attributes => this.AttributesInternal;
11+
public MockTextEmbeddingGeneratorService()
12+
{
13+
14+
}
15+
public async Task<IList<ReadOnlyMemory<float>>> GenerateEmbeddingsAsync(
16+
IList<string> data,
17+
Kernel? kernel = null,
18+
CancellationToken cancellationToken = default)
19+
{
20+
IList<ReadOnlyMemory<float>> results = new List<ReadOnlyMemory<float>>();
21+
22+
float[] array1 = { 1.0f, 2.0f, 3.0f };
23+
float[] array2 = { 4.0f, 5.0f, 6.0f };
24+
float[] array3 = { 7.0f, 8.0f, 9.0f };
25+
26+
// 将数组包装为ReadOnlyMemory<float>并添加到列表中
27+
results.Add(new ReadOnlyMemory<float>(array1));
28+
results.Add(new ReadOnlyMemory<float>(array2));
29+
results.Add(new ReadOnlyMemory<float>(array3));
30+
31+
return results;
32+
}
33+
34+
public void Dispose()
35+
{
36+
37+
}
38+
}
39+
}

src/GraphRag.Net.Web/Program.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
using GraphRag.Net;
33
using GraphRag.Net.Common.Options;
44
using GraphRag.Net.Options;
5+
using GraphRag.Net.Web.Mock;
56
using Microsoft.AspNetCore.Components;
7+
using Microsoft.Extensions.Options;
8+
using Microsoft.SemanticKernel;
9+
using Microsoft.SemanticKernel.ChatCompletion;
10+
using Microsoft.SemanticKernel.Embeddings;
11+
using Microsoft.SemanticKernel.TextGeneration;
612
using System.Reflection;
713

814
var builder = WebApplication.CreateBuilder(args);
@@ -35,6 +41,16 @@
3541

3642
builder.Services.AddGraphRagNet();
3743

44+
////自定义Kernel 可以实现其他模型的对接实现
45+
//var kernelBuild = Kernel.CreateBuilder();
46+
//kernelBuild.Services.AddKeyedSingleton<ITextGenerationService>("mock-text", new MockTextCompletion());
47+
//kernelBuild.Services.AddKeyedSingleton<IChatCompletionService>("mock-chat", new MockChatCompletion());
48+
//kernelBuild.Services.AddSingleton((ITextEmbeddingGenerationService)new MockTextEmbeddingGeneratorService());
49+
//kernelBuild.Services.AddKeyedSingleton("mock-embedding", new MockTextEmbeddingGeneratorService());
50+
51+
//builder.Services.AddGraphRagNet(kernelBuild.Build());
52+
53+
3854
var app = builder.Build();
3955

4056
// Configure the HTTP request pipeline.

src/GraphRag.Net/Extensions/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static class ServiceCollectionExtensions
1818
/// </summary>
1919
/// <param name="services">容器</param>
2020
/// <returns></returns>
21-
public static IServiceCollection AddGraphRagNet(this IServiceCollection services)
21+
public static IServiceCollection AddGraphRagNet(this IServiceCollection services, Kernel _kernel = null)
2222
{
2323
Type attributeType = typeof(ServiceDescriptionAttribute);
2424
//var refAssembyNames = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
@@ -50,7 +50,7 @@ public static IServiceCollection AddGraphRagNet(this IServiceCollection services
5050
}
5151

5252
CodeFirst();
53-
InitSK(services);
53+
InitSK(services, _kernel);
5454

5555
return services;
5656
}

0 commit comments

Comments
 (0)