Skip to content

Commit db6de0a

Browse files
committed
Merge branch 'main' of github.com:xuzeyu91/GraphRag.Net
2 parents 722bcd7 + 5e18e3b commit db6de0a

File tree

2 files changed

+275
-0
lines changed

2 files changed

+275
-0
lines changed

README.en.md

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
[简体中文](./README.md) | English
2+
3+
## This is a simple implementation of dotnet referring to GraphRag
4+
5+
Based on the implementation idea mentioned by Microsoft in the paper, GraphRAG mainly implements the following functions in the execution process:
6+
- Source Documents → Text Chunks: divide the source document into text blocks.
7+
- Text Chunks → Element Instances: Extract instances of graph nodes and edges from each text block.
8+
- Element Instances → Element Summaries: generate summaries for each diagram element.
9+
- Element Summaries → Graph Communities: use the community detection algorithm to divide the graph into communities.
10+
- Graph Communities → Community Summaries: generate summaries for each community.
11+
- Community Summaries → Community Answers → Global Answer: use community summaries to generate local answers, and then summarize these local answers to generate global answers.
12+
13+
This project is a demo example, which is only used to learn GraphRAG ideas.
14+
15+
## You can directly reference the NuGet package in the project, or directly use the project to provide API services.
16+
For convenience, the LLM interface is currently only compatible with the openai specification, and other large models can consider using one api class integration products
17+
18+
Configure in appsettings.json
19+
20+
```
21+
"GraphOpenAI": {
22+
"Key": "sk-xxx",
23+
"EndPoint": "https://api.antsk.cn/",
24+
"ChatModel": "gpt-4o-mini",
25+
"EmbeddingModel": "text-embedding-ada-002"
26+
},
27+
"TextChunker": {
28+
"LinesToken": 100,
29+
"ParagraphsToken": 1000
30+
},
31+
"GraphDBConnection": {
32+
"DbType": "Sqlite", //PostgreSQL
33+
"DBConnection": "Data Source=graph.db",
34+
"VectorConnection": "graphmem.db", //If PostgreSQL is used, it can be consistent with DBConnection
35+
"VectorSize": 1536 //DbType=PostgreSQL needs to be set, sqlite can not be set
36+
},
37+
"GraphSearch": {
38+
"SearchMinRelevance": 0.5, //Search for minimum relevance
39+
"SearchLimit": 3, //Limit the number of vector search nodes
40+
"NodeDepth": 3 ,//Retrieve node depth
41+
"MaxNodes": 100 //Retrieve the maximum number of nodes
42+
},
43+
"GraphSys": {
44+
"RetryCounnt": 2 //Number of retries. Using the domestic model may cause json extraction failure. Increasing the number of retries can improve availability
45+
}
46+
```
47+
## Startup project
48+
```
49+
dotnet run --project GraphRag.Net.Web.csproj
50+
```
51+
52+
## After starting the project, you can use the
53+
```
54+
http://localhost:5000/swagger
55+
```
56+
## Open the swagger view interface
57+
58+
![Graoh](https://github.com/xuzeyu91/GraphRag.Net/blob/main/doc/api.png)
59+
60+
### You can also use the interface
61+
```
62+
http://localhost:5000/
63+
```
64+
65+
Open the UI interface of blazer. The page provides functions such as text import, file import, question and answer dialogue, and view knowledge map
66+
67+
![Graoh](https://github.com/xuzeyu91/GraphRag.Net/blob/main/doc/graph1.png)
68+
69+
70+
## Nuget Package Usage
71+
```
72+
dotnet add package GraphRag.Net
73+
```
74+
## In order to facilitate the adjustment and modification of prompt words, SK Plugin has separated the project. You need to put GraphRag Copy the graphPlugins directory in the Net. Web project to your project, and set:
75+
[graphPlugins](https://github.com/AIDotNet/GraphRag.Net/tree/main/src/GraphRag.Net.Web/graphPlugins)
76+
```
77+
<ItemGroup>
78+
<None Include="graphPlugins\**">
79+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
80+
</None>
81+
</ItemGroup>
82+
```
83+
84+
### The default configuration uses the OpenAI standard interface. After configuring the OpenAI app settings, you can use the following code to inject
85+
After adding a package, you need to set the configuration file and dependency injection
86+
```
87+
//OpenAI configuration
88+
builder.Configuration.GetSection("GraphOpenAI").Get<GraphOpenAIOption>();
89+
//Document Slicing Configuration
90+
builder.Configuration.GetSection("TextChunker").Get<TextChunkerOption>();
91+
//Configure Database Links
92+
builder.Configuration.GetSection("GraphDBConnection").Get<GraphDBConnectionOption>();
93+
//System settings
94+
builder.Configuration.GetSection("GraphSys").Get<GraphSysOption>();
95+
96+
//Inject AddGraphRagNet. Note that you need to inject the configuration file first, and then inject GraphRagNet
97+
builder.Services.AddGraphRagNet();
98+
```
99+
100+
### If you want to access other models, you can refer to the following code, which abstracts the implementation of Kernel. You can customize the implementation
101+
```
102+
var kernelBuild = Kernel.CreateBuilder();
103+
kernelBuild.Services.AddKeyedSingleton<ITextGenerationService>("mock-text", new MockTextCompletion());
104+
kernelBuild.Services.AddKeyedSingleton<IChatCompletionService>("mock-chat", new MockChatCompletion());
105+
kernelBuild.Services.AddSingleton((ITextEmbeddingGenerationService)new MockTextEmbeddingGeneratorService());
106+
kernelBuild.Services.AddKeyedSingleton("mock-embedding", new MockTextEmbeddingGeneratorService());
107+
108+
builder.Services.AddGraphRagNet(kernelBuild.Build());
109+
```
110+
111+
#### It should be noted here that since the import may be carried out several times, the generated community and global information is not automatically called during import, so you need to call the generated community and global information according to the actual situation
112+
113+
```
114+
await _graphService.GraphCommunitiesAsync(index);
115+
await _graphService.GraphGlobalAsync(index);
116+
```
117+
118+
119+
Inject IGraphService service when using. The following is the reference sample code
120+
```
121+
namespace GraphRag.Net.Api.Controllers
122+
{
123+
[Route("api/[controller]/[action]")]
124+
[ApiController]
125+
public class GraphDemoController(IGraphService _graphService) : ControllerBase
126+
{
127+
/// <summary>
128+
/// 获取所有的索引数据
129+
/// </summary>
130+
/// <returns></returns>
131+
[HttpGet]
132+
public async Task<IActionResult> GetAllIndex()
133+
{
134+
var graphModel = _graphService.GetAllIndex();
135+
return Ok(graphModel);
136+
}
137+
138+
139+
/// <summary>
140+
/// 获取所有的图谱数据
141+
/// </summary>
142+
/// <param name="index"></param>
143+
/// <returns></returns>
144+
[HttpGet]
145+
public async Task<IActionResult> GetAllGraphs(string index)
146+
{
147+
if (string.IsNullOrEmpty(index))
148+
{
149+
return Ok(new GraphViewModel());
150+
}
151+
var graphModel = _graphService.GetAllGraphs(index);
152+
return Ok(graphModel);
153+
}
154+
155+
156+
/// <summary>
157+
/// 插入文本数据
158+
/// </summary>
159+
/// <param name="model"></param>
160+
/// <returns></returns>
161+
[HttpPost]
162+
public async Task<IActionResult> InsertGraphData(InputModel model)
163+
{
164+
await _graphService.InsertGraphDataAsync(model.Index, model.Input);
165+
return Ok();
166+
}
167+
168+
/// <summary>
169+
/// 搜索递归获取节点相关的所有边和节点进行图谱对话
170+
/// </summary>
171+
/// <param name="model"></param>
172+
/// <returns></returns>
173+
[HttpPost]
174+
public async Task<IActionResult> SearchGraph(InputModel model)
175+
{
176+
var result = await _graphService.SearchGraphAsync(model.Index, model.Input);
177+
return Ok(result);
178+
}
179+
180+
/// <summary>
181+
/// 通过社区算法检索社区节点进行对话
182+
/// </summary>
183+
/// <param name="model"></param>
184+
/// <returns></returns>
185+
[HttpPost]
186+
public async Task<IActionResult> SearchGraphCommunity(InputModel model)
187+
{
188+
var result = await _graphService.SearchGraphCommunityAsync(model.Index, model.Input);
189+
return Ok(result);
190+
}
191+
192+
/// <summary>
193+
/// 导入txt文档
194+
/// </summary>
195+
/// <param name="index"></param>
196+
/// <param name="file"></param>
197+
/// <returns></returns>
198+
[HttpPost]
199+
public async Task<IActionResult> ImportTxt(string index,IFormFile file)
200+
{
201+
var forms = await Request.ReadFormAsync();
202+
using (var stream = new StreamReader(file.OpenReadStream()))
203+
{
204+
var txt = await stream.ReadToEndAsync();
205+
await _graphService.InsertTextChunkAsync(index,txt);
206+
return Ok();
207+
}
208+
}
209+
210+
/// <summary>
211+
/// 通过社区检测生成社区和摘要
212+
/// </summary>
213+
/// <param name="index"></param>
214+
/// <returns></returns>
215+
[HttpGet]
216+
public async Task<IActionResult> GraphCommunities(string index)
217+
{
218+
await _graphService.GraphCommunitiesAsync(index);
219+
return Ok();
220+
}
221+
222+
/// <summary>
223+
/// 通过社区摘要生成全局摘要
224+
/// </summary>
225+
/// <param name="index"></param>
226+
/// <returns></returns>
227+
[HttpGet]
228+
public async Task<IActionResult> GraphGlobal(string index)
229+
{
230+
await _graphService.GraphGlobalAsync(index);
231+
return Ok();
232+
}
233+
234+
/// <summary>
235+
/// 删除图谱数据
236+
/// </summary>
237+
/// <param name="index"></param>
238+
/// <returns></returns>
239+
[HttpGet]
240+
public async Task<IActionResult> DeleteGraph(string index)
241+
{
242+
await _graphService.DeleteGraph(index);
243+
return Ok();
244+
}
245+
}
246+
247+
public class InputModel
248+
{
249+
public string Index { get; set; }
250+
public string Input { get; set; }
251+
}
252+
}
253+
254+
```
255+
256+
## Test DB. Some community friends pre trained some data in advance. The link is as follows. After downloading, it can be directly put into the project directory for replacement to test the experience
257+
```
258+
https://pan.quark.cn/s/bf2d21f29f85
259+
```
260+
261+
## See AntSK for more Rag scenarios
262+
Project address:[AntSK](https://github.com/AIDotNet/AntSK)
263+
264+
Experience environment:
265+
266+
[Demo地址](https://demo.antsk.cn)
267+
268+
User:test
269+
270+
Pwd:test
271+
272+
273+
You are also welcome to join our WeChat communication group. You can add my WeChat: **xuzeyu91** and send it to the group

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
简体中文 | [English](./README.en.md)
2+
13
## 这是一个参考GraphRag的dotnet简易实现
24

35
基于微软在论文中提到的实现思路,执行过程GraphRAG主要实现了如下功能:

0 commit comments

Comments
 (0)