|
1 | 1 | 简体中文 | [English](./README.en.md)
|
2 | 2 |
|
3 |
| -## 这是一个参考GraphRag的dotnet简易实现 |
| 3 | +## 这是一个参考Text2Sql的dotnet简易实现 |
4 | 4 |
|
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 |
| - |
60 |
| - |
61 |
| -### 也可以使用界面 |
62 |
| -``` |
63 |
| -http://localhost:5000/ |
64 |
| -``` |
65 |
| - |
66 |
| -打开blazor的UI界面,页面提供了文本导入、文件导入,和问答对话,查看知识图谱等功能 |
67 |
| - |
68 |
| - |
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 |
272 | 5 |
|
273 | 6 |
|
274 | 7 | 也欢迎大家加入我们的微信交流群,可以添加我的微信:**xuzeyu91** 发送进群
|
0 commit comments