13
13
namespace GraphRag . Net . Domain . Service
14
14
{
15
15
[ ServiceDescription ( typeof ( ISemanticService ) , ServiceLifetime . Scoped ) ]
16
- public class SemanticService : ISemanticService
16
+ public class SemanticService ( Kernel _kernel ) : ISemanticService
17
17
{
18
- private readonly Kernel _kernel ;
19
-
20
- private KernelPlugin _plugin ;
21
- public SemanticService ( Kernel kernel )
22
- {
23
- _kernel = kernel ;
24
- //导入插件
25
- if ( ! _kernel . Plugins . Any ( p => p . Name == "graph" ) )
26
- {
27
- var basePath = AppDomain . CurrentDomain . BaseDirectory ; // 或使用其他方式获取根路径
28
- var pluginPath = Path . Combine ( basePath , RepoFiles . SamplePluginsPath ( ) , "graph" ) ;
29
- Console . WriteLine ( $ "pluginPatth:{ pluginPath } ") ;
30
- _plugin = _kernel . ImportPluginFromPromptDirectory ( pluginPath ) ;
31
- Console . WriteLine ( $ "FunCount:{ _plugin . Count ( ) } ") ;
32
- }
33
- }
34
18
public async Task < string > CreateGraphAsync ( string input )
35
19
{
36
20
OpenAIPromptExecutionSettings settings = new ( )
37
21
{
38
22
Temperature = 0 ,
39
23
ResponseFormat = ChatCompletionsResponseFormat . JsonObject
40
24
} ;
41
- KernelFunction createFun = _plugin [ " create"] ;
25
+ KernelFunction createFun = _kernel . Plugins . GetFunction ( "graph" , " create") ;
42
26
var args = new KernelArguments ( settings )
43
27
{
44
28
[ "input" ] = input ,
@@ -51,7 +35,7 @@ public async Task<string> CreateGraphAsync(string input)
51
35
public async Task < string > GetGraphAnswerAsync ( string graph , string input )
52
36
{
53
37
54
- KernelFunction createFun = _plugin [ " search"] ;
38
+ KernelFunction createFun = _kernel . Plugins . GetFunction ( "graph" , " search") ;
55
39
var args = new KernelArguments ( )
56
40
{
57
41
[ "graph" ] = graph ,
@@ -64,7 +48,7 @@ public async Task<string> GetGraphAnswerAsync(string graph, string input)
64
48
}
65
49
public async IAsyncEnumerable < StreamingKernelContent > GetGraphAnswerStreamAsync ( string graph , string input )
66
50
{
67
- KernelFunction createFun = _plugin [ " search"] ;
51
+ KernelFunction createFun = _kernel . Plugins . GetFunction ( "graph" , " search") ;
68
52
var args = new KernelArguments ( )
69
53
{
70
54
[ "graph" ] = graph ,
@@ -78,10 +62,10 @@ public async IAsyncEnumerable<StreamingKernelContent> GetGraphAnswerStreamAsync(
78
62
}
79
63
80
64
81
- public async Task < string > GetGraphCommunityAnswerAsync ( string graph , string community , string global , string input )
65
+ public async Task < string > GetGraphCommunityAnswerAsync ( string graph , string community , string global , string input )
82
66
{
83
67
84
- KernelFunction createFun = _plugin [ " community_search"] ;
68
+ KernelFunction createFun = _kernel . Plugins . GetFunction ( "graph" , " community_search") ;
85
69
var args = new KernelArguments ( )
86
70
{
87
71
[ "graph" ] = graph ,
@@ -98,15 +82,15 @@ public async Task<string> GetGraphCommunityAnswerAsync(string graph,string commu
98
82
public async IAsyncEnumerable < StreamingKernelContent > GetGraphCommunityAnswerStreamAsync ( string graph , string community , string global , string input )
99
83
{
100
84
101
- KernelFunction createFun = _plugin [ " community_search"] ;
85
+ KernelFunction createFun = _kernel . Plugins . GetFunction ( "graph" , " community_search") ;
102
86
var args = new KernelArguments ( )
103
87
{
104
88
[ "graph" ] = graph ,
105
89
[ "community" ] = community ,
106
90
[ "global" ] = global ,
107
91
[ "input" ] = input ,
108
92
} ;
109
- var skresult = _kernel . InvokeStreamingAsync ( createFun , args ) ;
93
+ var skresult = _kernel . InvokeStreamingAsync ( createFun , args ) ;
110
94
await foreach ( var content in skresult )
111
95
{
112
96
yield return content ;
@@ -117,7 +101,7 @@ public async IAsyncEnumerable<StreamingKernelContent> GetGraphCommunityAnswerStr
117
101
118
102
public async Task < string > GetRelationship ( string node1 , string node2 )
119
103
{
120
- KernelFunction createFun = _plugin [ " relationship"] ;
104
+ KernelFunction createFun = _kernel . Plugins . GetFunction ( "graph" , " relationship") ;
121
105
var args = new KernelArguments ( )
122
106
{
123
107
[ "node1" ] = node1 ,
@@ -131,7 +115,7 @@ public async Task<string> GetRelationship(string node1, string node2)
131
115
132
116
public async Task < string > MergeDesc ( string desc1 , string desc2 )
133
117
{
134
- KernelFunction createFun = _plugin [ " mergedesc"] ;
118
+ KernelFunction createFun = _kernel . Plugins . GetFunction ( "graph" , " mergedesc") ;
135
119
var args = new KernelArguments ( )
136
120
{
137
121
[ "desc1" ] = desc1 ,
@@ -141,10 +125,10 @@ public async Task<string> MergeDesc(string desc1, string desc2)
141
125
142
126
string result = skresult . GetValue < string > ( ) ? . Trim ( ) ?? "" ;
143
127
return result ;
144
- }
128
+ }
145
129
public async Task < string > CommunitySummaries ( string nodes )
146
130
{
147
- KernelFunction createFun = _plugin [ " community_summaries"] ;
131
+ KernelFunction createFun = _kernel . Plugins . GetFunction ( "graph" , " community_summaries") ;
148
132
var args = new KernelArguments ( )
149
133
{
150
134
[ "nodes" ] = nodes
@@ -153,10 +137,10 @@ public async Task<string> CommunitySummaries(string nodes)
153
137
154
138
string result = skresult . GetValue < string > ( ) ? . Trim ( ) ?? "" ;
155
139
return result ;
156
- }
140
+ }
157
141
public async Task < string > GlobalSummaries ( string community )
158
142
{
159
- KernelFunction createFun = _plugin [ " global_summaries"] ;
143
+ KernelFunction createFun = _kernel . Plugins . GetFunction ( "graph" , " global_summaries") ;
160
144
var args = new KernelArguments ( )
161
145
{
162
146
[ "community" ] = community
@@ -174,7 +158,7 @@ public async Task<string> GlobalSummaries(string community)
174
158
/// <exception cref="InvalidOperationException"></exception>
175
159
public async Task < SemanticTextMemory > GetTextMemory ( )
176
160
{
177
- IMemoryStore memoryStore = null ;
161
+ IMemoryStore memoryStore = null ;
178
162
switch ( GraphDBConnectionOption . DbType )
179
163
{
180
164
case "Sqlite" :
0 commit comments