Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: AIDotNet/GraphRag.Net
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0.1.0
Choose a base ref
...
head repository: AIDotNet/GraphRag.Net
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
Loading
Showing with 1,614 additions and 1,311 deletions.
  1. +20 −0 .github/workflows/nuget-publish.yml
  2. +2 −2 Directory.Build.props
  3. +1 −0 GraphRag.Net.sln
  4. +273 −0 README.en.md
  5. +239 −8 README.md
  6. +13 −0 src/GraphRag.Net.Web/.config/dotnet-tools.json
  7. +15 −0 src/GraphRag.Net.Web/Components/GlobalHeader/RightContent.razor
  8. +21 −9 src/GraphRag.Net.Web/Controllers/{GraphDemoController.cs → GraphController.cs}
  9. +62 −4 src/GraphRag.Net.Web/GraphRag.Net.Web.csproj
  10. +1 −0 src/GraphRag.Net.Web/GraphRag.Net.Web.csproj.user
  11. +15 −8 src/GraphRag.Net.Web/GraphRag.Net.Web.xml
  12. +3 −3 src/GraphRag.Net.Web/Layouts/BasicLayout.razor
  13. +3 −3 src/GraphRag.Net.Web/Layouts/UserLayout.razor
  14. +48 −0 src/GraphRag.Net.Web/Mock/MockChatCompletion.cs
  15. +49 −0 src/GraphRag.Net.Web/Mock/MockTextCompletion.cs
  16. +39 −0 src/GraphRag.Net.Web/Mock/MockTextEmbeddingGeneratorService.cs
  17. +3 −2 src/GraphRag.Net.Web/Pages/Graph/Chat.razor
  18. +26 −2 src/GraphRag.Net.Web/Pages/Graph/Chat.razor.cs
  19. +116 −47 src/GraphRag.Net.Web/Pages/Graph/Graph.razor
  20. +1 −0 src/GraphRag.Net.Web/Pages/Index.razor
  21. +22 −4 src/GraphRag.Net.Web/Program.cs
  22. +20 −5 src/GraphRag.Net.Web/appsettings.json
  23. 0 src/{GraphRag.Net/plugins → GraphRag.Net.Web/graphPlugins}/graph/community_search/config.json
  24. 0 src/{GraphRag.Net/plugins → GraphRag.Net.Web/graphPlugins}/graph/community_search/skprompt.txt
  25. 0 src/{GraphRag.Net/plugins → GraphRag.Net.Web/graphPlugins}/graph/community_summaries/config.json
  26. 0 src/{GraphRag.Net/plugins → GraphRag.Net.Web/graphPlugins}/graph/community_summaries/skprompt.txt
  27. 0 src/{GraphRag.Net/plugins → GraphRag.Net.Web/graphPlugins}/graph/create/config.json
  28. +71 −0 src/GraphRag.Net.Web/graphPlugins/graph/create/skprompt.txt
  29. 0 src/{GraphRag.Net/plugins → GraphRag.Net.Web/graphPlugins}/graph/global_summaries/config.json
  30. 0 src/{GraphRag.Net/plugins → GraphRag.Net.Web/graphPlugins}/graph/global_summaries/skprompt.txt
  31. 0 src/{GraphRag.Net/plugins → GraphRag.Net.Web/graphPlugins}/graph/mergedesc/config.json
  32. +24 −0 src/GraphRag.Net.Web/graphPlugins/graph/mergedesc/skprompt.txt
  33. 0 src/{GraphRag.Net/plugins → GraphRag.Net.Web/graphPlugins}/graph/relationship/config.json
  34. +2 −2 src/{GraphRag.Net/plugins → GraphRag.Net.Web/graphPlugins}/graph/relationship/skprompt.txt
  35. 0 src/{GraphRag.Net/plugins → GraphRag.Net.Web/graphPlugins}/graph/search/config.json
  36. 0 src/{GraphRag.Net/plugins → GraphRag.Net.Web/graphPlugins}/graph/search/skprompt.txt
  37. +0 −12 src/GraphRag.Net/Common/Map/AutoMapProfile.cs
  38. +0 −32 src/GraphRag.Net/Common/Map/MapperExtend.cs
  39. +0 −28 src/GraphRag.Net/Common/Map/MapperRegister.cs
  40. +10 −3 src/GraphRag.Net/Common/Options/GraphDBConnectionOption.cs
  41. +3 −3 src/GraphRag.Net/Common/Options/{OpenAIOption.cs → GraphOpenAIOption.cs}
  42. +32 −0 src/GraphRag.Net/Common/Options/GraphSearchOption.cs
  43. +16 −0 src/GraphRag.Net/Common/Options/GraphSysOption.cs
  44. +1 −1 src/GraphRag.Net/Common/Options/TextChunkerOption.cs
  45. +3 −16 src/GraphRag.Net/Common/{DependencyInjection → }/ServiceDescriptionAttribute.cs
  46. +14 −0 src/GraphRag.Net/Domain/Interface/ICommunityDetectionService.cs
  47. +38 −0 src/GraphRag.Net/Domain/Interface/IGraphService.cs
  48. +4 −3 src/GraphRag.Net/Domain/Interface/ISemanticService.cs
  49. +0 −7 src/GraphRag.Net/Domain/Model/Constant/SystemConstant.cs
  50. +0 −44 src/GraphRag.Net/Domain/Model/Graph/Graph.cs
  51. +1 −2 src/GraphRag.Net/Domain/Model/Graph/GraphModel.cs
  52. +7 −0 src/GraphRag.Net/Domain/Model/Graph/GraphViewModel.cs
  53. +1 −1 src/GraphRag.Net/Domain/Model/Graph/RelationShipModel.cs
  54. +61 −0 src/GraphRag.Net/Domain/Service/CommunityDetectionService.cs
  55. +147 −92 src/GraphRag.Net/Domain/Service/GraphService.cs
  56. +84 −33 src/GraphRag.Net/Domain/Service/SemanticService.cs
  57. +32 −32 src/GraphRag.Net/{Common/DependencyInjection → Extensions}/ServiceCollectionExtensions.cs
  58. +21 −13 src/GraphRag.Net/GraphRag.Net.csproj
  59. +2 −4 src/GraphRag.Net/Repositories/Base/IRepository.cs
  60. +1 −56 src/GraphRag.Net/Repositories/Base/Repository.cs
  61. +1 −1 src/GraphRag.Net/Repositories/Base/SqlSugarHelper.cs
  62. +1 −1 src/GraphRag.Net/Repositories/Graph/CommunitieNodes/CommunitieNodes_Repositories.cs
  63. +1 −1 src/GraphRag.Net/Repositories/Graph/CommunitieNodes/ICommunitieNodes_Repositories.cs
  64. +1 −1 src/GraphRag.Net/Repositories/Graph/Communities/Communities_Repositories.cs
  65. +1 −1 src/GraphRag.Net/Repositories/Graph/Communities/ICommunities_Repositories.cs
  66. +6 −1 src/GraphRag.Net/Repositories/Graph/Edges/Edges.cs
  67. +2 −1 src/GraphRag.Net/Repositories/Graph/Edges/Edges_Repositories.cs
  68. +1 −1 src/GraphRag.Net/Repositories/Graph/Edges/IEdges_Repositories.cs
  69. +1 −1 src/GraphRag.Net/Repositories/Graph/Global/Globals_Repositories.cs
  70. +1 −1 src/GraphRag.Net/Repositories/Graph/Global/IGlobals_Repositories.cs
  71. +1 −1 src/GraphRag.Net/Repositories/Graph/Nodes/INodes_Repositories.cs
  72. +1 −1 src/GraphRag.Net/Repositories/Graph/Nodes/Nodes.cs
  73. +2 −1 src/GraphRag.Net/Repositories/Graph/Nodes/Nodes_Repositories.cs
  74. +1 −1 src/GraphRag.Net/Utils/ConvertUtils.cs
  75. +3 −3 src/GraphRag.Net/Utils/OpenAIHttpClientHandler.cs
  76. +1 −1 src/GraphRag.Net/Utils/RepoUtils/AppException.cs
  77. +1 −1 src/GraphRag.Net/Utils/RepoUtils/ObjectExtensions.cs
  78. +21 −14 src/GraphRag.Net/Utils/RepoUtils/RepoFiles.cs
  79. +0 −23 src/GraphRag.Net/plugins/generator/community_report_rating/config.json
  80. +0 −122 src/GraphRag.Net/plugins/generator/community_report_rating/skprompt.txt
  81. +0 −23 src/GraphRag.Net/plugins/generator/community_report_summarization/config.json
  82. +0 −89 src/GraphRag.Net/plugins/generator/community_report_summarization/skprompt.txt
  83. +0 −23 src/GraphRag.Net/plugins/generator/community_reporter_role/config.json
  84. +0 −14 src/GraphRag.Net/plugins/generator/community_reporter_role/skprompt.txt
  85. +0 −15 src/GraphRag.Net/plugins/generator/defaults/config.json
  86. +0 −1 src/GraphRag.Net/plugins/generator/defaults/skprompt.txt
  87. +0 −15 src/GraphRag.Net/plugins/generator/domain/config.json
  88. +0 −6 src/GraphRag.Net/plugins/generator/domain/skprompt.txt
  89. +0 −23 src/GraphRag.Net/plugins/generator/entity_extraction_json_prompt/config.json
  90. +0 −34 src/GraphRag.Net/plugins/generator/entity_extraction_json_prompt/skprompt.txt
  91. +0 −23 src/GraphRag.Net/plugins/generator/entity_extraction_prompt/config.json
  92. +0 −33 src/GraphRag.Net/plugins/generator/entity_extraction_prompt/skprompt.txt
  93. +0 −23 src/GraphRag.Net/plugins/generator/entity_relationship/config.json
  94. +0 −30 src/GraphRag.Net/plugins/generator/entity_relationship/skprompt.txt
  95. +0 −23 src/GraphRag.Net/plugins/generator/entity_relationship_json/config.json
  96. +0 −32 src/GraphRag.Net/plugins/generator/entity_relationship_json/skprompt.txt
  97. +0 −19 src/GraphRag.Net/plugins/generator/entity_summarization_prompt/config.json
  98. +0 −16 src/GraphRag.Net/plugins/generator/entity_summarization_prompt/skprompt.txt
  99. +0 −19 src/GraphRag.Net/plugins/generator/entity_types/config.json
  100. +0 −40 src/GraphRag.Net/plugins/generator/entity_types/skprompt.txt
  101. +0 −19 src/GraphRag.Net/plugins/generator/entity_types_json/config.json
  102. +0 −39 src/GraphRag.Net/plugins/generator/entity_types_json/skprompt.txt
  103. +0 −15 src/GraphRag.Net/plugins/generator/language/config.json
  104. +0 −6 src/GraphRag.Net/plugins/generator/language/skprompt.txt
  105. +0 −15 src/GraphRag.Net/plugins/generator/persona/config.json
  106. +0 −7 src/GraphRag.Net/plugins/generator/persona/skprompt.txt
  107. +0 −37 src/GraphRag.Net/plugins/graph/create/skprompt.txt
  108. +0 −13 src/GraphRag.Net/plugins/graph/mergedesc/skprompt.txt
20 changes: 20 additions & 0 deletions .github/workflows/nuget-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: main
on:
push:
branches:
- main
paths:
- 'Directory.Build.props'

jobs:
deploy-nuget:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build
run: dotnet build --configuration Release
- name: Pack
run: dotnet pack --no-build --configuration Release
- name: Push NuGet package
run: |
dotnet nuget push **/*.nupkg --skip-duplicate --source https://api.nuget.org/v3/index.json --api-key ${{ vars.XUZEYU }}
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<!-- See https://aka.ms/dotnet/msbuild/customize for more details on customizing your build -->
<PropertyGroup>
<SKVersion>1.16.0</SKVersion>
<NewtonsoftVersion>13.0.3</NewtonsoftVersion>
<Version>0.1.32</Version>
<SKVersion>1.17.1</SKVersion>
</PropertyGroup>
</Project>
1 change: 1 addition & 0 deletions GraphRag.Net.sln
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "解决方案项", "解决方案项", "{A35F9835-DFCF-478A-9F24-E0B350161746}"
ProjectSection(SolutionItems) = preProject
Directory.Build.props = Directory.Build.props
.github\workflows\nuget-publish.yml = .github\workflows\nuget-publish.yml
README.md = README.md
EndProjectSection
EndProject
273 changes: 273 additions & 0 deletions README.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
[简体中文](./README.md) | English

## This is a simple implementation of dotnet referring to GraphRag

Based on the implementation idea mentioned by Microsoft in the paper, GraphRAG mainly implements the following functions in the execution process:
- Source Documents → Text Chunks: divide the source document into text blocks.
- Text Chunks → Element Instances: Extract instances of graph nodes and edges from each text block.
- Element Instances → Element Summaries: generate summaries for each diagram element.
- Element Summaries → Graph Communities: use the community detection algorithm to divide the graph into communities.
- Graph Communities → Community Summaries: generate summaries for each community.
- Community Summaries → Community Answers → Global Answer: use community summaries to generate local answers, and then summarize these local answers to generate global answers.

This project is a demo example, which is only used to learn GraphRAG ideas.

## You can directly reference the NuGet package in the project, or directly use the project to provide API services.
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

Configure in appsettings.json

```
"GraphOpenAI": {
"Key": "sk-xxx",
"EndPoint": "https://api.antsk.cn/",
"ChatModel": "gpt-4o-mini",
"EmbeddingModel": "text-embedding-ada-002"
},
"TextChunker": {
"LinesToken": 100,
"ParagraphsToken": 1000
},
"GraphDBConnection": {
"DbType": "Sqlite", //PostgreSQL
"DBConnection": "Data Source=graph.db",
"VectorConnection": "graphmem.db", //If PostgreSQL is used, it can be consistent with DBConnection
"VectorSize": 1536 //DbType=PostgreSQL needs to be set, sqlite can not be set
},
"GraphSearch": {
"SearchMinRelevance": 0.5, //Search for minimum relevance
"SearchLimit": 3, //Limit the number of vector search nodes
"NodeDepth": 3 ,//Retrieve node depth
"MaxNodes": 100 //Retrieve the maximum number of nodes
},
"GraphSys": {
"RetryCounnt": 2 //Number of retries. Using the domestic model may cause json extraction failure. Increasing the number of retries can improve availability
}
```
## Startup project
```
dotnet run --project GraphRag.Net.Web.csproj
```

## After starting the project, you can use the
```
http://localhost:5000/swagger
```
## Open the swagger view interface

![Graoh](https://github.com/xuzeyu91/GraphRag.Net/blob/main/doc/api.png)

### You can also use the interface
```
http://localhost:5000/
```

Open the UI interface of blazer. The page provides functions such as text import, file import, question and answer dialogue, and view knowledge map

![Graoh](https://github.com/xuzeyu91/GraphRag.Net/blob/main/doc/graph1.png)


## Nuget Package Usage
```
dotnet add package GraphRag.Net
```
## 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:
[graphPlugins](https://github.com/AIDotNet/GraphRag.Net/tree/main/src/GraphRag.Net.Web/graphPlugins)
```
<ItemGroup>
<None Include="graphPlugins\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
```

### The default configuration uses the OpenAI standard interface. After configuring the OpenAI app settings, you can use the following code to inject
After adding a package, you need to set the configuration file and dependency injection
```
//OpenAI configuration
builder.Configuration.GetSection("GraphOpenAI").Get<GraphOpenAIOption>();
//Document Slicing Configuration
builder.Configuration.GetSection("TextChunker").Get<TextChunkerOption>();
//Configure Database Links
builder.Configuration.GetSection("GraphDBConnection").Get<GraphDBConnectionOption>();
//System settings
builder.Configuration.GetSection("GraphSys").Get<GraphSysOption>();
//Inject AddGraphRagNet. Note that you need to inject the configuration file first, and then inject GraphRagNet
builder.Services.AddGraphRagNet();
```

### 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
```
var kernelBuild = Kernel.CreateBuilder();
kernelBuild.Services.AddKeyedSingleton<ITextGenerationService>("mock-text", new MockTextCompletion());
kernelBuild.Services.AddKeyedSingleton<IChatCompletionService>("mock-chat", new MockChatCompletion());
kernelBuild.Services.AddSingleton((ITextEmbeddingGenerationService)new MockTextEmbeddingGeneratorService());
kernelBuild.Services.AddKeyedSingleton("mock-embedding", new MockTextEmbeddingGeneratorService());
builder.Services.AddGraphRagNet(kernelBuild.Build());
```

#### 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

```
await _graphService.GraphCommunitiesAsync(index);
await _graphService.GraphGlobalAsync(index);
```


Inject IGraphService service when using. The following is the reference sample code
```
namespace GraphRag.Net.Api.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class GraphDemoController(IGraphService _graphService) : ControllerBase
{
/// <summary>
/// 获取所有的索引数据
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<IActionResult> GetAllIndex()
{
var graphModel = _graphService.GetAllIndex();
return Ok(graphModel);
}
/// <summary>
/// 获取所有的图谱数据
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
[HttpGet]
public async Task<IActionResult> GetAllGraphs(string index)
{
if (string.IsNullOrEmpty(index))
{
return Ok(new GraphViewModel());
}
var graphModel = _graphService.GetAllGraphs(index);
return Ok(graphModel);
}
/// <summary>
/// 插入文本数据
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public async Task<IActionResult> InsertGraphData(InputModel model)
{
await _graphService.InsertGraphDataAsync(model.Index, model.Input);
return Ok();
}
/// <summary>
/// 搜索递归获取节点相关的所有边和节点进行图谱对话
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public async Task<IActionResult> SearchGraph(InputModel model)
{
var result = await _graphService.SearchGraphAsync(model.Index, model.Input);
return Ok(result);
}
/// <summary>
/// 通过社区算法检索社区节点进行对话
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public async Task<IActionResult> SearchGraphCommunity(InputModel model)
{
var result = await _graphService.SearchGraphCommunityAsync(model.Index, model.Input);
return Ok(result);
}
/// <summary>
/// 导入txt文档
/// </summary>
/// <param name="index"></param>
/// <param name="file"></param>
/// <returns></returns>
[HttpPost]
public async Task<IActionResult> ImportTxt(string index,IFormFile file)
{
var forms = await Request.ReadFormAsync();
using (var stream = new StreamReader(file.OpenReadStream()))
{
var txt = await stream.ReadToEndAsync();
await _graphService.InsertTextChunkAsync(index,txt);
return Ok();
}
}
/// <summary>
/// 通过社区检测生成社区和摘要
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
[HttpGet]
public async Task<IActionResult> GraphCommunities(string index)
{
await _graphService.GraphCommunitiesAsync(index);
return Ok();
}
/// <summary>
/// 通过社区摘要生成全局摘要
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
[HttpGet]
public async Task<IActionResult> GraphGlobal(string index)
{
await _graphService.GraphGlobalAsync(index);
return Ok();
}
/// <summary>
/// 删除图谱数据
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
[HttpGet]
public async Task<IActionResult> DeleteGraph(string index)
{
await _graphService.DeleteGraph(index);
return Ok();
}
}
public class InputModel
{
public string Index { get; set; }
public string Input { get; set; }
}
}
```

## 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
```
https://pan.quark.cn/s/bf2d21f29f85
```

## See AntSK for more Rag scenarios
Project address:[AntSK](https://github.com/AIDotNet/AntSK)

Experience environment:

[Demo地址](https://demo.antsk.cn)

User:test

Pwd:test


You are also welcome to join our WeChat communication group. You can add my WeChat: **xuzeyu91** and send it to the group
Loading