Grand Overhaul: Refactored Core Structure, Introduced New Features, E… #1295
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Refactoring of
graphrag/query
ModuleDescription
This pull request introduces a significant refactoring of the
graphrag/query
module within the GraphRAG project. Theprimary objectives of this refactoring are:
query
component into an independent package, fully decoupled fromother modules.
query pipeline, promoting loose coupling and facilitating future maintenance and extension.
management of GraphRAG clients.
question_gen
module), streamliningthe codebase.
ensuring code reliability and passing
mypy
checks.GUI built with PyQt6.
outputs within a single method.
Related Issues
N/A
Proposed Changes
1. Project Layout
The codebase has been reorganized, promoting separation of concerns and ease of navigation. The new structure is as
follows:
query
module is now fully decoupled from other modules, making it usable as a standalone package.2. Enhanced Python API
2.1 Initialize
Users can easily create a
GraphRAGClient
instance using configuration file, dictionary, environment variables orconfiguration object.
a) From Configuration File
e.g.,
graphrag.example.yaml
file for an example.b) From Configuration Dictionary
e.g.,
c) From Configuration Object
If you prefer to use a configuration object and an optional logger, you can pass them directly to the constructor:
d) From Environment Variables
You can also initialize a client using environment variables:
Or create
.env
file in the project root directory:Then initialize the client:
2.2 Chatting with GraphRAG
a) Simple Chat
You can chat with GraphRAG using the
chat
method:Or, in streaming mode:
c) Using
with
StatementYou can also use the
with
statement to manage the client's lifecycle:d) Verbose Search Results
If you want to collect verbose search results, you can set the
verbose
parameter toTrue
:Or, in streaming mode:
e) Async Client
AsyncGraphRAGClient
provides an asynchronous version of theGraphRAGClient
:3. Streamlined CLI and GUI Tools
3.1 CLI Parameters
Execute the following command:
To see the available options:
3.2 Usage Examples
We can get started with the CLI from the corpus used in the GraphRAG official tutorial:
Then running the indexing pipeline. Ommited for brevity.
a) Console Mode
python -m graphrag.query --engine local \ --chat-api-key API_KEY \ --chat-model MODEL \ --embedding-api-key API_KEY \ --embedding-model MODEL \ --context-dir ./output \ --mode console \ --stream
Or, more concisely:
python -m graphrag.query -e local \ -k API_KEY \ -m MODEL \ -K API_KEY \ -M MODEL \ -c ./output \ -o console \ -s
Here is an example screenshot:
b) GUI Mode
python -m graphrag.query --engine local \ --chat-api-key API_KEY \ --chat-model MODEL \ --embedding-api-key API_KEY \ --embedding-model MODEL \ --context-dir ./output \ --mode gui
Here is an example screenshot:
4. Web API
Applied the refactored query module to a web service in
the graphrag-server repository, providing an OpenAI-compatible
Chat API interface.
git clone https://github.com/6ixGODD/graphrag-server.git cd graphrag-server
Modify the
.env
file with the appropriate API keys and models.Write a simple Python script to execute the web service:
Then you can use the OpenAI SDK to interact with the web service:
updates.
Checklist
Additional Notes
As mentioned, this PR involves significant code changes, but I believe it is a positive step forward. With thorough
testing, it will provide developers a more stable and modular version of GraphRAG for integration into their
applications, leading to greater overall benefits.
However, for this PR to be merged, some additional documentation work and test case development may require
collaboration with the official team.