Skip to content

Commit

Permalink
fix 插件抽离出来
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzeyu91 committed Aug 15, 2024
1 parent 5759b42 commit d26d3d7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<!-- See https://aka.ms/dotnet/msbuild/customize for more details on customizing your build -->
<PropertyGroup>
<Version>0.1.21</Version>
<Version>0.1.22</Version>
<SKVersion>1.17.1</SKVersion>
</PropertyGroup>
</Project>
46 changes: 15 additions & 31 deletions src/GraphRag.Net/Domain/Service/SemanticService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,16 @@
namespace GraphRag.Net.Domain.Service
{
[ServiceDescription(typeof(ISemanticService), ServiceLifetime.Scoped)]
public class SemanticService: ISemanticService
public class SemanticService(Kernel _kernel) : ISemanticService
{
private readonly Kernel _kernel;

private KernelPlugin _plugin;
public SemanticService(Kernel kernel)
{
_kernel = kernel;
//导入插件
if (!_kernel.Plugins.Any(p => p.Name == "graph"))
{
var basePath = AppDomain.CurrentDomain.BaseDirectory; // 或使用其他方式获取根路径
var pluginPath = Path.Combine(basePath, RepoFiles.SamplePluginsPath(), "graph");
Console.WriteLine($"pluginPatth:{pluginPath}");
_plugin=_kernel.ImportPluginFromPromptDirectory(pluginPath);
Console.WriteLine($"FunCount:{_plugin.Count()}");
}
}
public async Task<string> CreateGraphAsync(string input)
{
OpenAIPromptExecutionSettings settings = new()
{
Temperature = 0,
ResponseFormat = ChatCompletionsResponseFormat.JsonObject
};
KernelFunction createFun = _plugin["create"];
KernelFunction createFun = _kernel.Plugins.GetFunction("graph", "create");
var args = new KernelArguments(settings)
{
["input"] = input,
Expand All @@ -51,7 +35,7 @@ public async Task<string> CreateGraphAsync(string input)
public async Task<string> GetGraphAnswerAsync(string graph, string input)
{

KernelFunction createFun = _plugin["search"];
KernelFunction createFun = _kernel.Plugins.GetFunction("graph", "search");
var args = new KernelArguments()
{
["graph"] = graph,
Expand All @@ -64,7 +48,7 @@ public async Task<string> GetGraphAnswerAsync(string graph, string input)
}
public async IAsyncEnumerable<StreamingKernelContent> GetGraphAnswerStreamAsync(string graph, string input)
{
KernelFunction createFun = _plugin["search"];
KernelFunction createFun = _kernel.Plugins.GetFunction("graph", "search");
var args = new KernelArguments()
{
["graph"] = graph,
Expand All @@ -78,10 +62,10 @@ public async IAsyncEnumerable<StreamingKernelContent> GetGraphAnswerStreamAsync(
}


public async Task<string> GetGraphCommunityAnswerAsync(string graph,string community,string global,string input)
public async Task<string> GetGraphCommunityAnswerAsync(string graph, string community, string global, string input)
{

KernelFunction createFun = _plugin["community_search"];
KernelFunction createFun = _kernel.Plugins.GetFunction("graph", "community_search");
var args = new KernelArguments()
{
["graph"] = graph,
Expand All @@ -98,15 +82,15 @@ public async Task<string> GetGraphCommunityAnswerAsync(string graph,string commu
public async IAsyncEnumerable<StreamingKernelContent> GetGraphCommunityAnswerStreamAsync(string graph, string community, string global, string input)
{

KernelFunction createFun = _plugin["community_search"];
KernelFunction createFun = _kernel.Plugins.GetFunction("graph", "community_search");
var args = new KernelArguments()
{
["graph"] = graph,
["community"] = community,
["global"] = global,
["input"] = input,
};
var skresult = _kernel.InvokeStreamingAsync(createFun, args);
var skresult = _kernel.InvokeStreamingAsync(createFun, args);
await foreach (var content in skresult)
{
yield return content;
Expand All @@ -117,7 +101,7 @@ public async IAsyncEnumerable<StreamingKernelContent> GetGraphCommunityAnswerStr

public async Task<string> GetRelationship(string node1, string node2)
{
KernelFunction createFun = _plugin["relationship"];
KernelFunction createFun = _kernel.Plugins.GetFunction("graph", "relationship");
var args = new KernelArguments()
{
["node1"] = node1,
Expand All @@ -131,7 +115,7 @@ public async Task<string> GetRelationship(string node1, string node2)

public async Task<string> MergeDesc(string desc1, string desc2)
{
KernelFunction createFun = _plugin["mergedesc"];
KernelFunction createFun = _kernel.Plugins.GetFunction("graph", "mergedesc");
var args = new KernelArguments()
{
["desc1"] = desc1,
Expand All @@ -141,10 +125,10 @@ public async Task<string> MergeDesc(string desc1, string desc2)

string result = skresult.GetValue<string>()?.Trim() ?? "";
return result;
}
}
public async Task<string> CommunitySummaries(string nodes)
{
KernelFunction createFun = _plugin["community_summaries"];
KernelFunction createFun = _kernel.Plugins.GetFunction("graph", "community_summaries");
var args = new KernelArguments()
{
["nodes"] = nodes
Expand All @@ -153,10 +137,10 @@ public async Task<string> CommunitySummaries(string nodes)

string result = skresult.GetValue<string>()?.Trim() ?? "";
return result;
}
}
public async Task<string> GlobalSummaries(string community)
{
KernelFunction createFun = _plugin["global_summaries"];
KernelFunction createFun = _kernel.Plugins.GetFunction("graph", "global_summaries");
var args = new KernelArguments()
{
["community"] = community
Expand All @@ -174,7 +158,7 @@ public async Task<string> GlobalSummaries(string community)
/// <exception cref="InvalidOperationException"></exception>
public async Task<SemanticTextMemory> GetTextMemory()
{
IMemoryStore memoryStore=null ;
IMemoryStore memoryStore = null;
switch (GraphDBConnectionOption.DbType)
{
case "Sqlite":
Expand Down
8 changes: 7 additions & 1 deletion src/GraphRag.Net/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ static void InitSK(IServiceCollection services,Kernel _kernel = null)
)
.Build();
}

//导入插件
if (!_kernel.Plugins.Any(p => p.Name == "graph"))
{
var pluginPatth = Path.Combine(RepoFiles.SamplePluginsPath(), "graph");
Console.WriteLine($"pluginPatth:{pluginPatth}");
_kernel.ImportPluginFromPromptDirectory(pluginPatth);
}
return _kernel;
});
}
Expand Down

0 comments on commit d26d3d7

Please sign in to comment.