Location>code7788 >text

Hands-On Integration: Building an Intelligent Graph Search System

Popularity:717 ℃/2024-07-28 13:48:37

In the context of artificial intelligence and big data development, we often need to implement knowledge graphs in our projects to retrieve and use information quickly and accurately.

Today, I'm going to show you in detail how to integrate a new .NET project with theNET version of a reference GraphRag implementation that enables storage, retrieval, and Q&A of graph data.

Until then, if you're not familiar withYou can refer to my previous article for the basic principles and implementation. Next, let's start the hand-on integrationThe process of practicing it, I guess!

Step 1: Add the NuGet package

The easiest way to add a NuGet package is via the command line:

dotnet add package 

Of course, you can also add it via Visual Studio 2022 for graphical convenience.

 

Step 2: Configure the project

After successfully adding the NuGet package, we need to configure dependency injection in the program's startup file. First, add the following three configuration files to the project root directory:

  1. OpenAIConfiguration: used to set the API key and endpoints for the Big Model interface.

  2. TextChunkerConfiguration: used to set parameters for document slicing.

  3. GraphDBConnectionConfiguration: Used to set the database connection, SQLite and PostgreSQL are supported by default.

existAdd the following configuration to the

{
  "OpenAI": {
    "Key": "sk-xxx",
    "Endpoint": "/",
    "ChatModel": "gpt-4o-mini",
    "EmbeddingModel": "text-embedding-ada-002"
  },
  "TextChunker": {
    "LinesToken": 100,
    "ParagraphsToken": 1000
  },
  "GraphDBConnection": {
    "DbType": "Sqlite", // "PostgreSQL"
    "DBConnection": "Data Source=",
    "GraphDBConnection": "",
    "VectorSize": 1536 // Set only when using PostgreSQL.
  }
}

Next, in thein which dependency injection is performed:

// OpenAI configuration
("OpenAI").Get<OpenAIOption>();
// Document Slicing Configuration
("TextChunker").Get<TextChunkerOption>();
// Configure the database connection
("GraphDBConnection").Get<GraphDBConnectionOption>();

// Inject AddGraphRagNet, note that you need to inject the configuration file first and then inject AddGraphRagNet.
();

Note: The configuration file must be injected first, then theGraphRagNetto make sure everything is configured to work.

 

Step 3: Core functions used

After completing the configuration, we can start using the powerful mapping service features provided. Where it needs to be used, inject theIGraphService

public class YourService
{
    private readonly IGraphService _graphService;

    public YourService(IGraphService graphService)
    {
        _graphService = graphService;
    }

    // The following are examples of how the core methods are used
}

// The new syntax of .net8 can be annotated using the main constructor
public class YourService(IGraphService _graphService)
{
    // The following are examples of how the core methods are used
}

In the above example, we can call the following core methods:

Query all indexes

var graphModel = _graphService.GetAllIndex();

Querying the graphs under the index

var graphModel = _graphService.GetAllGraphs(index);

Inserting text data into a plot (not sliced)

await _graphService.InsertGraphDataAsync(, );

Insertion of text data into plots (slices)

await _graphService.InsertTextChunkAsync(index, txt);

Generate community summaries

await _graphService.GraphCommunitiesAsync(index);

Generate global summaries

await _graphService.GraphGlobalAsync(index);

Different query patterns

We provide two different query patterns, each with its advantages and disadvantages:

Search recursively obtains all edges and nodes associated with a node for graph conversation

var result = await _graphService.SearchGraphAsync(, );

Retrieve community nodes for dialog through community algorithms

var result = await _graphService.SearchGraphCommunityAsync(, );

summarize

With the introduction of this article, we understand how to integrate in a new project. According to different application scenarios, you can choose different query and summary generation methods. Since the current import only supports text data, in the actual project, you can combine Kernel Memory's custom Handler to import documents in different formats.

You are welcome to join our communication community, and if interested, pleaseFollow my public number "Xu Zeyu's technology sharing" and send "into the group".to get the means to join. I look forward to sharing ideas and making progress with you!