Skip to content

Commit 2efe6aa

Browse files
committed
add 处理maxNodes 防止超出token
1 parent 12a2eba commit 2efe6aa

File tree

6 files changed

+34
-9
lines changed

6 files changed

+34
-9
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.24</Version>
4+
<Version>0.1.25</Version>
55
<SKVersion>1.17.1</SKVersion>
66
</PropertyGroup>
77
</Project>

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"GraphSearch": {
3737
"SearchMinRelevance": 0.5, //搜索最小相关性
3838
"SearchLimit": 3, //搜索限制
39-
"NodeDepth": 3 //节点深度
39+
"NodeDepth": 3 ,//检索节点深度
40+
"MaxNodes": 100 //检索最大节点数
4041
},
4142
"GraphSys": {
4243
"RetryCounnt": 2 //重试次数,使用国产模型可能会出现json提取失败,增加重试次数可提高可用性

src/GraphRag.Net.Web/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
builder.Configuration.GetSection("GraphOpenAI").Get<GraphOpenAIOption>();
3737
builder.Configuration.GetSection("TextChunker").Get<TextChunkerOption>();
38+
builder.Configuration.GetSection("GraphSearch").Get<GraphSearchOption>();
3839
//builder.Configuration.GetSection("GraphDBConnection").Get<GraphDBConnectionOption>();
3940

4041
builder.Services.AddGraphRagNet();

src/GraphRag.Net.Web/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"GraphSearch": {
4343
"SearchMinRelevance": 0.5,
4444
"SearchLimit": 3,
45-
"NodeDepth": 3
45+
"NodeDepth": 3,
46+
"MaxNodes":10
4647
},
4748
"GraphSys": {
4849
"RetryCounnt": 2 //重试次数,使用国产模型可能会出现json提取失败,增加重试次数可提高可用性

src/GraphRag.Net/Common/Options/GraphSearchOption.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,10 @@ public class GraphSearchOption
2323
/// 节点关系检索深度
2424
/// </summary>
2525
public static int NodeDepth { get; set; } = 3;
26+
27+
/// <summary>
28+
/// 节点检索最多节点数量
29+
/// </summary>
30+
public static int MaxNodes { get; set; } = 300;
2631
}
2732
}

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,10 @@ private GraphModel GetGraphAllRecursion(string index, List<Nodes> initialNodes)
533533
var allEdges = new List<Edges>();
534534
var nodesToExplore = new List<Nodes>(initialNodes);
535535
int i = 0;
536+
536537
while (nodesToExplore.Count > 0)
537538
{
538-
//线的深度,暂时先不处理太远距离的关联
539-
if (i > GraphSearchOption.NodeDepth)
539+
if (i > GraphSearchOption.NodeDepth || allNodes.Count >= GraphSearchOption.MaxNodes)
540540
{
541541
break;
542542
}
@@ -557,22 +557,30 @@ private GraphModel GetGraphAllRecursion(string index, List<Nodes> initialNodes)
557557

558558
// 获取新的节点
559559
var newNodes = GetNodes(index, newEdges);
560-
561560
// 找到新获取的节点,并更新 nodesToExplore
562561
nodesToExplore = newNodes.Where(n => !allNodes.Any(existingNode => existingNode.Id == n.Id)).ToList();
563-
564562
// 将新节点加入到 allNodes 中
565563
allNodes.AddRange(nodesToExplore);
566564

567565
i++;
568566
}
567+
568+
// 如果节点数超过最大限制,进行截断
569+
if (allNodes.Count > GraphSearchOption.MaxNodes)
570+
{
571+
allNodes = allNodes.Take(GraphSearchOption.MaxNodes).ToList();
572+
}
573+
// 需要相应地处理 allEdges,确保边的节点在 allNodes 中
574+
allEdges = allEdges.Where(e => allNodes.Any(p => p.Id == e.Source) && allNodes.Any(p => p.Id == e.Target)).ToList();
575+
569576
return new GraphModel
570577
{
571578
Nodes = allNodes,
572579
Edges = allEdges
573580
};
574581
}
575582

583+
576584
/// <summary>
577585
/// 通过社区算法检索社区节点
578586
/// </summary>
@@ -600,6 +608,15 @@ private GraphModel GetGraphAllCommunitiesRecursion(string index, List<Nodes> ini
600608
allEdges.Add(edge);
601609
}
602610
}
611+
612+
// 如果节点数超过最大限制,进行截断
613+
if (allNodes.Count > GraphSearchOption.MaxNodes)
614+
{
615+
allNodes = allNodes.Take(GraphSearchOption.MaxNodes).ToList();
616+
}
617+
// 需要相应地处理 allEdges,确保边的节点在 allNodes 中
618+
allEdges = allEdges.Where(e => allNodes.Any(p => p.Id == e.Source) && allNodes.Any(p => p.Id == e.Target)).ToList();
619+
603620
return new GraphModel
604621
{
605622
Nodes = allNodes,
@@ -616,7 +633,7 @@ private List<Edges> GetEdges(string index, List<Nodes> nodes)
616633
{
617634
var nodeIds = nodes.Select(x => x.Id).ToList();
618635
var edges = new List<Edges>();
619-
edges = _edges_Repositories.GetList(x => x.Index == index && nodeIds.Contains(x.Source) || nodeIds.Contains(x.Target));
636+
edges = _edges_Repositories.GetList(x => x.Index == index && nodeIds.Contains(x.Source) && nodeIds.Contains(x.Target));
620637
return edges;
621638
}
622639

@@ -633,7 +650,7 @@ private List<Nodes> GetNodes(string index, List<Edges> edges)
633650
nodeIds.AddRange(targets);
634651
nodeIds.AddRange(sources);
635652

636-
var nodes = _nodes_Repositories.GetList(p => p.Index == index && nodeIds.Contains(p.Id));
653+
var nodes = _nodes_Repositories.GetList(p => p.Index == index || nodeIds.Contains(p.Id));
637654
return nodes;
638655
}
639656

0 commit comments

Comments
 (0)