简体中文 | English
基于微软在论文中提到的实现思路,执行过程GraphRAG主要实现了如下功能:
本项目为demo示例,仅用于学习GraphRAG思路。
出于方便,LLM接口目前只兼容了openai的规范,其他大模型可以考虑使用one-api类的集成产品
在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", //如果用PostgreSQL,可以和DBConnection一致
"VectorSize": 1536 //DbType=PostgreSQL时需要设置,sqlite可以不设置
},
"GraphSearch": {
"SearchMinRelevance": 0.5, //搜索最小相关性
"SearchLimit": 3, //向量搜索节点限制个数
"NodeDepth": 3 ,//检索节点深度
"MaxNodes": 100 //检索最大节点数
},
"GraphSys": {
"RetryCounnt": 2 //重试次数,使用国产模型可能会出现json提取失败,增加重试次数可提高可用性
}
dotnet run --project GraphRag.Net.Web.csproj
http://localhost:5000/swagger
http://localhost:5000/
打开blazor的UI界面,页面提供了文本导入、文件导入,和问答对话,查看知识图谱等功能
dotnet add package GraphRag.Net
<ItemGroup>
<None Include="graphPlugins\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
添加包以后,需要进行配置文件的设置以及依赖注入
//OpenAI配置
builder.Configuration.GetSection("GraphOpenAI").Get<GraphOpenAIOption>();
//文档切片配置
builder.Configuration.GetSection("TextChunker").Get<TextChunkerOption>();
//配置数据库链接
builder.Configuration.GetSection("GraphDBConnection").Get<GraphDBConnectionOption>();
//系统设置
builder.Configuration.GetSection("GraphSys").Get<GraphSysOption>();
//注入AddGraphRagNet,注意,需要先注入配置文件,然后再注入GraphRagNet
builder.Services.AddGraphRagNet();
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());
await _graphService.GraphCommunitiesAsync(index);
await _graphService.GraphGlobalAsync(index);
使用时注入 IGraphService 服务,以下为参考示例代码
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; }
}
}
https://pan.quark.cn/s/bf2d21f29f85
项目地址:AntSK
体验环境:
账号:test
密码:test
也欢迎大家加入我们的微信交流群,可以添加我的微信:xuzeyu91 发送进群