Skip to content

Commit 7dd3169

Browse files
authored
Merge pull request #21 from itchangc/main
fix: 生成图谱数据根据index索引检索
2 parents d917c20 + d812041 commit 7dd3169

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/GraphRag.Net/Domain/Service/GraphService.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ public async Task InsertGraphDataAsync(string index, string input)
131131
bool isContinue = false;
132132

133133
//判断是否存在相同节点
134-
var oldNode = _nodes_Repositories.GetFirst(p =>p.Index==index&& p.Name == n.Name);
135-
if (oldNode.IsNotNull())
134+
var oldNode = _nodes_Repositories.GetFirst(p => p.Index == index && p.Name == n.Name);
135+
if (oldNode.IsNotNull() && !string.IsNullOrWhiteSpace(n.Desc))
136136
{
137137
//相同节点关联edge关系
138138
var newDesc = await _semanticService.MergeDesc(oldNode.Desc.ConvertToString(), n.Desc.ConvertToString());
@@ -188,7 +188,7 @@ public async Task InsertGraphDataAsync(string index, string input)
188188
}
189189
if (!_edges_Repositories.IsAny(p => p.Target == relationShip.Edge.Target && p.Source == relationShip.Edge.Source))
190190
{
191-
relationShip.Edge.Id=Guid.NewGuid().ToString();
191+
relationShip.Edge.Id = Guid.NewGuid().ToString();
192192
relationShip.Edge.Index = index;
193193
_edges_Repositories.Insert(relationShip.Edge);
194194
}
@@ -223,7 +223,7 @@ public async Task InsertGraphDataAsync(string index, string input)
223223
{
224224
Edges edge = new Edges()
225225
{
226-
Id=Guid.NewGuid().ToString(),
226+
Id = Guid.NewGuid().ToString(),
227227
Index = index,
228228
Source = nodeDic[e.Source],
229229
Target = nodeDic[e.Target],
@@ -239,9 +239,9 @@ public async Task InsertGraphDataAsync(string index, string input)
239239
.ToList().Where(p => p.Count > 1).ToList();
240240
//合并查询Edges 的Source和Target 重复数据
241241
foreach (var edge in repeatEdges)
242-
{
242+
{
243243
var edges = _edges_Repositories.GetList(p => p.Source == edge.Source && p.Target == edge.Target);
244-
var firstEdge=edges.First();
244+
var firstEdge = edges.First();
245245

246246
for (int i = 1; i < edges.Count(); i++)
247247
{
@@ -251,11 +251,11 @@ public async Task InsertGraphDataAsync(string index, string input)
251251
_edges_Repositories.Delete(edges[i]);
252252
continue;
253253
}
254-
var newDesc=await _semanticService.MergeDesc(firstEdge.Relationship, edges[i].Relationship);
255-
firstEdge.Relationship= newDesc;
254+
var newDesc = await _semanticService.MergeDesc(firstEdge.Relationship, edges[i].Relationship);
255+
firstEdge.Relationship = newDesc;
256256
_edges_Repositories.Update(firstEdge);
257257
_edges_Repositories.Delete(edges[i]);
258-
}
258+
}
259259
}
260260
}
261261
catch (Exception ex)
@@ -271,7 +271,8 @@ public async Task InsertGraphDataAsync(string index, string input)
271271
/// <param name="input"></param>
272272
/// <returns></returns>
273273
/// <exception cref="ArgumentException"></exception>
274-
public async Task<GraphModel> SearchGraphModel(string index, string input) {
274+
public async Task<GraphModel> SearchGraphModel(string index, string input)
275+
{
275276
if (string.IsNullOrWhiteSpace(index) || string.IsNullOrWhiteSpace(input))
276277
{
277278
throw new ArgumentException("Values required for index and input cannot be null.");
@@ -327,7 +328,7 @@ public async Task<GraphModel> SearchGraphCommunityModel(string index, string inp
327328
/// <returns></returns>
328329
public async Task<string> SearchGraphAsync(string index, string input)
329330
{
330-
var graphModel = await SearchGraphModel(index, input);
331+
var graphModel = await SearchGraphModel(index, input);
331332
string answer = await _semanticService.GetGraphAnswerAsync(JsonConvert.SerializeObject(graphModel), input);
332333
return answer;
333334
}
@@ -362,8 +363,8 @@ public async Task<string> SearchGraphCommunityAsync(string index, string input)
362363
string answer = "";
363364
var graphModel = await SearchGraphCommunityModel(index, input);
364365
var global = _globals_Repositories.GetFirst(p => p.Index == index)?.Summaries;
365-
if (graphModel.Nodes.Count()>0)
366-
{
366+
if (graphModel.Nodes.Count() > 0)
367+
{
367368
var community = string.Join(Environment.NewLine, _communities_Repositories.GetDB().Queryable<Communities>().Where(p => p.Index == index).Select(p => p.Summaries).ToList());
368369

369370
//这里数据有点多,要通过语义进行一次过滤
@@ -388,7 +389,7 @@ public async IAsyncEnumerable<StreamingKernelContent> SearchGraphCommunityStream
388389
var textMemModelList = await RetrieveTextMemModelList(index, input);
389390
var global = _globals_Repositories.GetFirst(p => p.Index == index)?.Summaries;
390391
IAsyncEnumerable<StreamingKernelContent> answer;
391-
392+
392393
//匹配到节点信息
393394
var graphModel = await SearchGraphCommunityModel(index, input);
394395
if (graphModel.Nodes.Count() > 0)
@@ -475,8 +476,8 @@ public async Task GraphCommunitiesAsync(string index)
475476
public async Task GraphGlobalAsync(string index)
476477
{
477478
_globals_Repositories.Delete(p => p.Index == index);
478-
var communitieSummariesList = _communities_Repositories.GetDB().Queryable<Communities>().Where(p => p.Index == index).Select(p=>p.Summaries).ToList();
479-
var communitieSummaries =string.Join(Environment.NewLine, communitieSummariesList);
479+
var communitieSummariesList = _communities_Repositories.GetDB().Queryable<Communities>().Where(p => p.Index == index).Select(p => p.Summaries).ToList();
480+
var communitieSummaries = string.Join(Environment.NewLine, communitieSummariesList);
480481
var globalSummaries = await _semanticService.GlobalSummaries(communitieSummaries);
481482

482483
Globals globals = new Globals()
@@ -578,7 +579,7 @@ private GraphModel GetGraphAllRecursion(string index, List<Nodes> initialNodes)
578579
// 如果节点数超过最大限制,进行截断
579580
if (allNodes.Count > GraphSearchOption.MaxNodes)
580581
{
581-
allNodes = allNodes.Take(GraphSearchOption.MaxNodes).ToList();
582+
allNodes = allNodes.Take(GraphSearchOption.MaxNodes).ToList();
582583
}
583584
// 需要相应地处理 allEdges,确保边的节点在 allNodes 中
584585
allEdges = allEdges.Where(e => allNodes.Any(p => p.Id == e.Source) && allNodes.Any(p => p.Id == e.Target)).ToList();

0 commit comments

Comments
 (0)