Skip to content

Commit a035a8f

Browse files
committed
init 初始化框架
1 parent 6d9ff6d commit a035a8f

File tree

135 files changed

+828
-2855
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+828
-2855
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.34</Version>
4+
<Version>0.1.0</Version>
55
<SKVersion>1.17.1</SKVersion>
66
</PropertyGroup>
77
</Project>

README.md

Lines changed: 1 addition & 268 deletions
Original file line numberDiff line numberDiff line change
@@ -1,274 +1,7 @@
11
简体中文 | [English](./README.en.md)
22

3-
## 这是一个参考GraphRag的dotnet简易实现
3+
## 这是一个参考Text2Sql的dotnet简易实现
44

5-
基于微软在论文中提到的实现思路,执行过程GraphRAG主要实现了如下功能:
6-
- Source Documents → Text Chunks:将源文档分割成文本块。
7-
- Text Chunks → Element Instances:从每个文本块中提取图节点和边的实例。
8-
- Element Instances → Element Summaries:为每个图元素生成摘要。
9-
- Element Summaries → Graph Communities:使用社区检测算法将图划分为社区。
10-
- Graph Communities → Community Summaries:为每个社区生成摘要。
11-
- Community Summaries → Community Answers → Global Answer:使用社区摘要生成局部答案,然后汇总这些局部答案以生成全局答案。
12-
13-
本项目为demo示例,仅用于学习GraphRAG思路。
14-
15-
## 您可以直接在项目中引用NuGet包,或者直接使用本项目提供API服务。
16-
17-
出于方便,LLM接口目前只兼容了openai的规范,其他大模型可以考虑使用one-api类的集成产品
18-
19-
在appsettings.json配置
20-
21-
```
22-
"GraphOpenAI": {
23-
"Key": "sk-xxx",
24-
"EndPoint": "https://api.antsk.cn/",
25-
"ChatModel": "gpt-4o-mini",
26-
"EmbeddingModel": "text-embedding-ada-002"
27-
},
28-
"TextChunker": {
29-
"LinesToken": 100,
30-
"ParagraphsToken": 1000
31-
},
32-
"GraphDBConnection": {
33-
"DbType": "Sqlite", //PostgreSQL
34-
"DBConnection": "Data Source=graph.db",
35-
"VectorConnection": "graphmem.db", //如果用PostgreSQL,可以和DBConnection一致
36-
"VectorSize": 1536 //DbType=PostgreSQL时需要设置,sqlite可以不设置
37-
},
38-
"GraphSearch": {
39-
"SearchMinRelevance": 0.5, //搜索最小相关性
40-
"SearchLimit": 3, //向量搜索节点限制个数
41-
"NodeDepth": 3 ,//检索节点深度
42-
"MaxNodes": 100 //检索最大节点数
43-
},
44-
"GraphSys": {
45-
"RetryCounnt": 2 //重试次数,使用国产模型可能会出现json提取失败,增加重试次数可提高可用性
46-
}
47-
```
48-
## 启动项目
49-
```
50-
dotnet run --project GraphRag.Net.Web.csproj
51-
```
52-
53-
## 启动项目后可以通过
54-
```
55-
http://localhost:5000/swagger
56-
```
57-
## 打开swagger查看接口
58-
59-
![Graoh](https://github.com/xuzeyu91/GraphRag.Net/blob/main/doc/api.png)
60-
61-
### 也可以使用界面
62-
```
63-
http://localhost:5000/
64-
```
65-
66-
打开blazor的UI界面,页面提供了文本导入、文件导入,和问答对话,查看知识图谱等功能
67-
68-
![Graoh](https://github.com/xuzeyu91/GraphRag.Net/blob/main/doc/graph1.png)
69-
70-
71-
## Nuget包使用
72-
```
73-
dotnet add package GraphRag.Net
74-
```
75-
## 为了方便进行提示词调整与修改,SK Plugin我们剥离出了项目,您需要把GraphRag.Net.Web项目中的 graphPlugins目录拷贝到你的项目中,并设置:
76-
[graphPlugins](https://github.com/AIDotNet/GraphRag.Net/tree/main/src/GraphRag.Net.Web/graphPlugins)
77-
```
78-
<ItemGroup>
79-
<None Include="graphPlugins\**">
80-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
81-
</None>
82-
</ItemGroup>
83-
```
84-
85-
### 默认配置,使用OpenAI标准接口,在配置了OpenAI的appsettings后可以使用下面代码进行注入
86-
添加包以后,需要进行配置文件的设置以及依赖注入
87-
```
88-
//OpenAI配置
89-
builder.Configuration.GetSection("GraphOpenAI").Get<GraphOpenAIOption>();
90-
//文档切片配置
91-
builder.Configuration.GetSection("TextChunker").Get<TextChunkerOption>();
92-
//配置数据库链接
93-
builder.Configuration.GetSection("GraphDBConnection").Get<GraphDBConnectionOption>();
94-
//系统设置
95-
builder.Configuration.GetSection("GraphSys").Get<GraphSysOption>();
96-
97-
//注入AddGraphRagNet,注意,需要先注入配置文件,然后再注入GraphRagNet
98-
builder.Services.AddGraphRagNet();
99-
```
100-
101-
### 如果你想接入其他模型,可以参考以下代码,这里抽象了Kernel的实现,你可以自定义实现
102-
```
103-
var kernelBuild = Kernel.CreateBuilder();
104-
kernelBuild.Services.AddKeyedSingleton<ITextGenerationService>("mock-text", new MockTextCompletion());
105-
kernelBuild.Services.AddKeyedSingleton<IChatCompletionService>("mock-chat", new MockChatCompletion());
106-
kernelBuild.Services.AddSingleton((ITextEmbeddingGenerationService)new MockTextEmbeddingGeneratorService());
107-
kernelBuild.Services.AddKeyedSingleton("mock-embedding", new MockTextEmbeddingGeneratorService());
108-
109-
builder.Services.AddGraphRagNet(kernelBuild.Build());
110-
```
111-
112-
#### 此处需要注意,由于导入可能分多次导入,没有在导入时自动调用生成社区和全局信息,需要自己根据实际情况调用生成社区和全局信息
113-
114-
```
115-
await _graphService.GraphCommunitiesAsync(index);
116-
await _graphService.GraphGlobalAsync(index);
117-
```
118-
119-
120-
使用时注入 IGraphService 服务,以下为参考示例代码
121-
```
122-
namespace GraphRag.Net.Api.Controllers
123-
{
124-
[Route("api/[controller]/[action]")]
125-
[ApiController]
126-
public class GraphController(IGraphService _graphService) : ControllerBase
127-
{
128-
/// <summary>
129-
/// 获取所有的索引数据
130-
/// </summary>
131-
/// <returns></returns>
132-
[HttpGet]
133-
public async Task<IActionResult> GetAllIndex()
134-
{
135-
var graphModel = _graphService.GetAllIndex();
136-
return Ok(graphModel);
137-
}
138-
139-
140-
/// <summary>
141-
/// 获取所有的图谱数据
142-
/// </summary>
143-
/// <param name="index"></param>
144-
/// <returns></returns>
145-
[HttpGet]
146-
public async Task<IActionResult> GetAllGraphs(string index)
147-
{
148-
if (string.IsNullOrEmpty(index))
149-
{
150-
return Ok(new GraphViewModel());
151-
}
152-
var graphModel = _graphService.GetAllGraphs(index);
153-
return Ok(graphModel);
154-
}
155-
156-
157-
/// <summary>
158-
/// 插入文本数据
159-
/// </summary>
160-
/// <param name="model"></param>
161-
/// <returns></returns>
162-
[HttpPost]
163-
public async Task<IActionResult> InsertGraphData(InputModel model)
164-
{
165-
await _graphService.InsertGraphDataAsync(model.Index, model.Input);
166-
return Ok();
167-
}
168-
169-
/// <summary>
170-
/// 搜索递归获取节点相关的所有边和节点进行图谱对话
171-
/// </summary>
172-
/// <param name="model"></param>
173-
/// <returns></returns>
174-
[HttpPost]
175-
public async Task<IActionResult> SearchGraph(InputModel model)
176-
{
177-
var result = await _graphService.SearchGraphAsync(model.Index, model.Input);
178-
return Ok(result);
179-
}
180-
181-
/// <summary>
182-
/// 通过社区算法检索社区节点进行对话
183-
/// </summary>
184-
/// <param name="model"></param>
185-
/// <returns></returns>
186-
[HttpPost]
187-
public async Task<IActionResult> SearchGraphCommunity(InputModel model)
188-
{
189-
var result = await _graphService.SearchGraphCommunityAsync(model.Index, model.Input);
190-
return Ok(result);
191-
}
192-
193-
/// <summary>
194-
/// 导入txt文档
195-
/// </summary>
196-
/// <param name="index"></param>
197-
/// <param name="file"></param>
198-
/// <returns></returns>
199-
[HttpPost]
200-
public async Task<IActionResult> ImportTxt(string index,IFormFile file)
201-
{
202-
var forms = await Request.ReadFormAsync();
203-
using (var stream = new StreamReader(file.OpenReadStream()))
204-
{
205-
var txt = await stream.ReadToEndAsync();
206-
await _graphService.InsertTextChunkAsync(index,txt);
207-
return Ok();
208-
}
209-
}
210-
211-
/// <summary>
212-
/// 通过社区检测生成社区和摘要
213-
/// </summary>
214-
/// <param name="index"></param>
215-
/// <returns></returns>
216-
[HttpGet]
217-
public async Task<IActionResult> GraphCommunities(string index)
218-
{
219-
await _graphService.GraphCommunitiesAsync(index);
220-
return Ok();
221-
}
222-
223-
/// <summary>
224-
/// 通过社区摘要生成全局摘要
225-
/// </summary>
226-
/// <param name="index"></param>
227-
/// <returns></returns>
228-
[HttpGet]
229-
public async Task<IActionResult> GraphGlobal(string index)
230-
{
231-
await _graphService.GraphGlobalAsync(index);
232-
return Ok();
233-
}
234-
235-
/// <summary>
236-
/// 删除图谱数据
237-
/// </summary>
238-
/// <param name="index"></param>
239-
/// <returns></returns>
240-
[HttpGet]
241-
public async Task<IActionResult> DeleteGraph(string index)
242-
{
243-
await _graphService.DeleteGraph(index);
244-
return Ok();
245-
}
246-
}
247-
248-
public class InputModel
249-
{
250-
public string Index { get; set; }
251-
public string Input { get; set; }
252-
}
253-
}
254-
255-
```
256-
257-
## 测试DB,有社区朋友提前预训练了一些数据,链接如下,下载后直接放进项目目录替换即可测试体验
258-
```
259-
https://pan.quark.cn/s/bf2d21f29f85
260-
```
261-
262-
## 更多Rag场景可查看 AntSK
263-
项目地址:[AntSK](https://github.com/AIDotNet/AntSK)
264-
265-
体验环境:
266-
267-
[Demo地址](https://demo.antsk.cn)
268-
269-
账号:test
270-
271-
密码:test
2725

2736

2747
也欢迎大家加入我们的微信交流群,可以添加我的微信:**xuzeyu91** 发送进群

GraphRag.Net.sln renamed to Text2Sql.Net.sln

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,31 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.10.35027.167
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GraphRag.Net.Web", "src\GraphRag.Net.Web\GraphRag.Net.Web.csproj", "{8C4B0AA1-7083-4BEA-9F12-2C20CDDB8426}"
7-
EndProject
8-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GraphRag.Net", "src\GraphRag.Net\GraphRag.Net.csproj", "{214FF6B8-E291-4CC8-94BC-16C1CD9DB3B8}"
9-
EndProject
106
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "解决方案项", "解决方案项", "{A35F9835-DFCF-478A-9F24-E0B350161746}"
117
ProjectSection(SolutionItems) = preProject
128
Directory.Build.props = Directory.Build.props
139
.github\workflows\nuget-publish.yml = .github\workflows\nuget-publish.yml
1410
README.md = README.md
1511
EndProjectSection
1612
EndProject
13+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Text2Sql.Net.Web", "src\Text2Sql.Net.Web\Text2Sql.Net.Web.csproj", "{B6A2DDA6-7A99-382C-A36F-671178FE5732}"
14+
EndProject
15+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Text2Sql.Net", "src\Text2Sql.Net\Text2Sql.Net.csproj", "{05D5CB5E-E8C7-9F2B-8C3B-8B42A9C8C5D0}"
16+
EndProject
1717
Global
1818
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1919
Debug|Any CPU = Debug|Any CPU
2020
Release|Any CPU = Release|Any CPU
2121
EndGlobalSection
2222
GlobalSection(ProjectConfigurationPlatforms) = postSolution
23-
{8C4B0AA1-7083-4BEA-9F12-2C20CDDB8426}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24-
{8C4B0AA1-7083-4BEA-9F12-2C20CDDB8426}.Debug|Any CPU.Build.0 = Debug|Any CPU
25-
{8C4B0AA1-7083-4BEA-9F12-2C20CDDB8426}.Release|Any CPU.ActiveCfg = Release|Any CPU
26-
{8C4B0AA1-7083-4BEA-9F12-2C20CDDB8426}.Release|Any CPU.Build.0 = Release|Any CPU
27-
{214FF6B8-E291-4CC8-94BC-16C1CD9DB3B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28-
{214FF6B8-E291-4CC8-94BC-16C1CD9DB3B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
29-
{214FF6B8-E291-4CC8-94BC-16C1CD9DB3B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
30-
{214FF6B8-E291-4CC8-94BC-16C1CD9DB3B8}.Release|Any CPU.Build.0 = Release|Any CPU
23+
{B6A2DDA6-7A99-382C-A36F-671178FE5732}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{B6A2DDA6-7A99-382C-A36F-671178FE5732}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{B6A2DDA6-7A99-382C-A36F-671178FE5732}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{B6A2DDA6-7A99-382C-A36F-671178FE5732}.Release|Any CPU.Build.0 = Release|Any CPU
27+
{05D5CB5E-E8C7-9F2B-8C3B-8B42A9C8C5D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28+
{05D5CB5E-E8C7-9F2B-8C3B-8B42A9C8C5D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
29+
{05D5CB5E-E8C7-9F2B-8C3B-8B42A9C8C5D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
30+
{05D5CB5E-E8C7-9F2B-8C3B-8B42A9C8C5D0}.Release|Any CPU.Build.0 = Release|Any CPU
3131
EndGlobalSection
3232
GlobalSection(SolutionProperties) = preSolution
3333
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)