Skip to content

Commit

Permalink
Merge pull request #21 from itchangc/main
Browse files Browse the repository at this point in the history
fix: 生成图谱数据根据index索引检索
  • Loading branch information
xuzeyu91 authored Oct 20, 2024
2 parents d917c20 + d812041 commit 7dd3169
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/GraphRag.Net/Domain/Service/GraphService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ public async Task InsertGraphDataAsync(string index, string input)
bool isContinue = false;

//判断是否存在相同节点
var oldNode = _nodes_Repositories.GetFirst(p =>p.Index==index&& p.Name == n.Name);
if (oldNode.IsNotNull())
var oldNode = _nodes_Repositories.GetFirst(p => p.Index == index && p.Name == n.Name);
if (oldNode.IsNotNull() && !string.IsNullOrWhiteSpace(n.Desc))
{
//相同节点关联edge关系
var newDesc = await _semanticService.MergeDesc(oldNode.Desc.ConvertToString(), n.Desc.ConvertToString());
Expand Down Expand Up @@ -188,7 +188,7 @@ public async Task InsertGraphDataAsync(string index, string input)
}
if (!_edges_Repositories.IsAny(p => p.Target == relationShip.Edge.Target && p.Source == relationShip.Edge.Source))
{
relationShip.Edge.Id=Guid.NewGuid().ToString();
relationShip.Edge.Id = Guid.NewGuid().ToString();
relationShip.Edge.Index = index;
_edges_Repositories.Insert(relationShip.Edge);
}
Expand Down Expand Up @@ -223,7 +223,7 @@ public async Task InsertGraphDataAsync(string index, string input)
{
Edges edge = new Edges()
{
Id=Guid.NewGuid().ToString(),
Id = Guid.NewGuid().ToString(),
Index = index,
Source = nodeDic[e.Source],
Target = nodeDic[e.Target],
Expand All @@ -239,9 +239,9 @@ public async Task InsertGraphDataAsync(string index, string input)
.ToList().Where(p => p.Count > 1).ToList();
//合并查询Edges 的Source和Target 重复数据
foreach (var edge in repeatEdges)
{
{
var edges = _edges_Repositories.GetList(p => p.Source == edge.Source && p.Target == edge.Target);
var firstEdge=edges.First();
var firstEdge = edges.First();

for (int i = 1; i < edges.Count(); i++)
{
Expand All @@ -251,11 +251,11 @@ public async Task InsertGraphDataAsync(string index, string input)
_edges_Repositories.Delete(edges[i]);
continue;
}
var newDesc=await _semanticService.MergeDesc(firstEdge.Relationship, edges[i].Relationship);
firstEdge.Relationship= newDesc;
var newDesc = await _semanticService.MergeDesc(firstEdge.Relationship, edges[i].Relationship);
firstEdge.Relationship = newDesc;
_edges_Repositories.Update(firstEdge);
_edges_Repositories.Delete(edges[i]);
}
}
}
}
catch (Exception ex)
Expand All @@ -271,7 +271,8 @@ public async Task InsertGraphDataAsync(string index, string input)
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public async Task<GraphModel> SearchGraphModel(string index, string input) {
public async Task<GraphModel> SearchGraphModel(string index, string input)
{
if (string.IsNullOrWhiteSpace(index) || string.IsNullOrWhiteSpace(input))
{
throw new ArgumentException("Values required for index and input cannot be null.");
Expand Down Expand Up @@ -327,7 +328,7 @@ public async Task<GraphModel> SearchGraphCommunityModel(string index, string inp
/// <returns></returns>
public async Task<string> SearchGraphAsync(string index, string input)
{
var graphModel = await SearchGraphModel(index, input);
var graphModel = await SearchGraphModel(index, input);
string answer = await _semanticService.GetGraphAnswerAsync(JsonConvert.SerializeObject(graphModel), input);
return answer;
}
Expand Down Expand Up @@ -362,8 +363,8 @@ public async Task<string> SearchGraphCommunityAsync(string index, string input)
string answer = "";
var graphModel = await SearchGraphCommunityModel(index, input);
var global = _globals_Repositories.GetFirst(p => p.Index == index)?.Summaries;
if (graphModel.Nodes.Count()>0)
{
if (graphModel.Nodes.Count() > 0)
{
var community = string.Join(Environment.NewLine, _communities_Repositories.GetDB().Queryable<Communities>().Where(p => p.Index == index).Select(p => p.Summaries).ToList());

//这里数据有点多,要通过语义进行一次过滤
Expand All @@ -388,7 +389,7 @@ public async IAsyncEnumerable<StreamingKernelContent> SearchGraphCommunityStream
var textMemModelList = await RetrieveTextMemModelList(index, input);
var global = _globals_Repositories.GetFirst(p => p.Index == index)?.Summaries;
IAsyncEnumerable<StreamingKernelContent> answer;

//匹配到节点信息
var graphModel = await SearchGraphCommunityModel(index, input);
if (graphModel.Nodes.Count() > 0)
Expand Down Expand Up @@ -475,8 +476,8 @@ public async Task GraphCommunitiesAsync(string index)
public async Task GraphGlobalAsync(string index)
{
_globals_Repositories.Delete(p => p.Index == index);
var communitieSummariesList = _communities_Repositories.GetDB().Queryable<Communities>().Where(p => p.Index == index).Select(p=>p.Summaries).ToList();
var communitieSummaries =string.Join(Environment.NewLine, communitieSummariesList);
var communitieSummariesList = _communities_Repositories.GetDB().Queryable<Communities>().Where(p => p.Index == index).Select(p => p.Summaries).ToList();
var communitieSummaries = string.Join(Environment.NewLine, communitieSummariesList);
var globalSummaries = await _semanticService.GlobalSummaries(communitieSummaries);

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

0 comments on commit 7dd3169

Please sign in to comment.