Merge branch 'improve-property-tooltip' into feat-node-expand
This commit is contained in:
149
README.md
149
README.md
@@ -37,28 +37,30 @@ This repository hosts the code of LightRAG. The structure of this code is based
|
|||||||
</br>
|
</br>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary style="font-size: 1.4em; font-weight: bold; cursor: pointer; display: list-item;">
|
<summary style="font-size: 1.4em; font-weight: bold; cursor: pointer; display: list-item;">
|
||||||
🎉 News
|
🎉 News
|
||||||
</summary>
|
</summary>
|
||||||
|
|
||||||
|
- [X] [2025.02.05]🎯📢Our team has released [VideoRAG](https://github.com/HKUDS/VideoRAG) understanding extremely long-context videos.
|
||||||
- [x] [2025.02.05]🎯📢Our team has released [VideoRAG](https://github.com/HKUDS/VideoRAG) understanding extremely long-context videos.
|
- [X] [2025.01.13]🎯📢Our team has released [MiniRAG](https://github.com/HKUDS/MiniRAG) making RAG simpler with small models.
|
||||||
- [x] [2025.01.13]🎯📢Our team has released [MiniRAG](https://github.com/HKUDS/MiniRAG) making RAG simpler with small models.
|
- [X] [2025.01.06]🎯📢You can now [use PostgreSQL for Storage](#using-postgresql-for-storage).
|
||||||
- [x] [2025.01.06]🎯📢You can now [use PostgreSQL for Storage](#using-postgresql-for-storage).
|
- [X] [2024.12.31]🎯📢LightRAG now supports [deletion by document ID](https://github.com/HKUDS/LightRAG?tab=readme-ov-file#delete).
|
||||||
- [x] [2024.12.31]🎯📢LightRAG now supports [deletion by document ID](https://github.com/HKUDS/LightRAG?tab=readme-ov-file#delete).
|
- [X] [2024.11.25]🎯📢LightRAG now supports seamless integration of [custom knowledge graphs](https://github.com/HKUDS/LightRAG?tab=readme-ov-file#insert-custom-kg), empowering users to enhance the system with their own domain expertise.
|
||||||
- [x] [2024.11.25]🎯📢LightRAG now supports seamless integration of [custom knowledge graphs](https://github.com/HKUDS/LightRAG?tab=readme-ov-file#insert-custom-kg), empowering users to enhance the system with their own domain expertise.
|
- [X] [2024.11.19]🎯📢A comprehensive guide to LightRAG is now available on [LearnOpenCV](https://learnopencv.com/lightrag). Many thanks to the blog author.
|
||||||
- [x] [2024.11.19]🎯📢A comprehensive guide to LightRAG is now available on [LearnOpenCV](https://learnopencv.com/lightrag). Many thanks to the blog author.
|
- [X] [2024.11.12]🎯📢LightRAG now supports [Oracle Database 23ai for all storage types (KV, vector, and graph)](https://github.com/HKUDS/LightRAG/blob/main/examples/lightrag_oracle_demo.py).
|
||||||
- [x] [2024.11.12]🎯📢LightRAG now supports [Oracle Database 23ai for all storage types (KV, vector, and graph)](https://github.com/HKUDS/LightRAG/blob/main/examples/lightrag_oracle_demo.py).
|
- [X] [2024.11.11]🎯📢LightRAG now supports [deleting entities by their names](https://github.com/HKUDS/LightRAG?tab=readme-ov-file#delete).
|
||||||
- [x] [2024.11.11]🎯📢LightRAG now supports [deleting entities by their names](https://github.com/HKUDS/LightRAG?tab=readme-ov-file#delete).
|
- [X] [2024.11.09]🎯📢Introducing the [LightRAG Gui](https://lightrag-gui.streamlit.app), which allows you to insert, query, visualize, and download LightRAG knowledge.
|
||||||
- [x] [2024.11.09]🎯📢Introducing the [LightRAG Gui](https://lightrag-gui.streamlit.app), which allows you to insert, query, visualize, and download LightRAG knowledge.
|
- [X] [2024.11.04]🎯📢You can now [use Neo4J for Storage](https://github.com/HKUDS/LightRAG?tab=readme-ov-file#using-neo4j-for-storage).
|
||||||
- [x] [2024.11.04]🎯📢You can now [use Neo4J for Storage](https://github.com/HKUDS/LightRAG?tab=readme-ov-file#using-neo4j-for-storage).
|
- [X] [2024.10.29]🎯📢LightRAG now supports multiple file types, including PDF, DOC, PPT, and CSV via `textract`.
|
||||||
- [x] [2024.10.29]🎯📢LightRAG now supports multiple file types, including PDF, DOC, PPT, and CSV via `textract`.
|
- [X] [2024.10.20]🎯📢We've added a new feature to LightRAG: Graph Visualization.
|
||||||
- [x] [2024.10.20]🎯📢We've added a new feature to LightRAG: Graph Visualization.
|
- [X] [2024.10.18]🎯📢We've added a link to a [LightRAG Introduction Video](https://youtu.be/oageL-1I0GE). Thanks to the author!
|
||||||
- [x] [2024.10.18]🎯📢We've added a link to a [LightRAG Introduction Video](https://youtu.be/oageL-1I0GE). Thanks to the author!
|
- [X] [2024.10.17]🎯📢We have created a [Discord channel](https://discord.gg/yF2MmDJyGJ)! Welcome to join for sharing and discussions! 🎉🎉
|
||||||
- [x] [2024.10.17]🎯📢We have created a [Discord channel](https://discord.gg/yF2MmDJyGJ)! Welcome to join for sharing and discussions! 🎉🎉
|
- [X] [2024.10.16]🎯📢LightRAG now supports [Ollama models](https://github.com/HKUDS/LightRAG?tab=readme-ov-file#quick-start)!
|
||||||
- [x] [2024.10.16]🎯📢LightRAG now supports [Ollama models](https://github.com/HKUDS/LightRAG?tab=readme-ov-file#quick-start)!
|
- [X] [2024.10.15]🎯📢LightRAG now supports [Hugging Face models](https://github.com/HKUDS/LightRAG?tab=readme-ov-file#quick-start)!
|
||||||
- [x] [2024.10.15]🎯📢LightRAG now supports [Hugging Face models](https://github.com/HKUDS/LightRAG?tab=readme-ov-file#quick-start)!
|
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
@@ -82,16 +84,20 @@ This repository hosts the code of LightRAG. The structure of this code is based
|
|||||||
cd LightRAG
|
cd LightRAG
|
||||||
pip install -e .
|
pip install -e .
|
||||||
```
|
```
|
||||||
|
|
||||||
* Install from PyPI
|
* Install from PyPI
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pip install lightrag-hku
|
pip install lightrag-hku
|
||||||
```
|
```
|
||||||
|
|
||||||
## Quick Start
|
## Quick Start
|
||||||
|
|
||||||
* [Video demo](https://www.youtube.com/watch?v=g21royNJ4fw) of running LightRAG locally.
|
* [Video demo](https://www.youtube.com/watch?v=g21royNJ4fw) of running LightRAG locally.
|
||||||
* All the code can be found in the `examples`.
|
* All the code can be found in the `examples`.
|
||||||
* Set OpenAI API key in environment if using OpenAI models: `export OPENAI_API_KEY="sk-...".`
|
* Set OpenAI API key in environment if using OpenAI models: `export OPENAI_API_KEY="sk-...".`
|
||||||
* Download the demo text "A Christmas Carol by Charles Dickens":
|
* Download the demo text "A Christmas Carol by Charles Dickens":
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
curl https://raw.githubusercontent.com/gusye1234/nano-graphrag/main/tests/mock_data.txt > ./book.txt
|
curl https://raw.githubusercontent.com/gusye1234/nano-graphrag/main/tests/mock_data.txt > ./book.txt
|
||||||
```
|
```
|
||||||
@@ -187,6 +193,7 @@ class QueryParam:
|
|||||||
<summary> <b>Using Open AI-like APIs</b> </summary>
|
<summary> <b>Using Open AI-like APIs</b> </summary>
|
||||||
|
|
||||||
* LightRAG also supports Open AI-like chat/embeddings APIs:
|
* LightRAG also supports Open AI-like chat/embeddings APIs:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
async def llm_model_func(
|
async def llm_model_func(
|
||||||
prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
|
prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
|
||||||
@@ -225,6 +232,7 @@ async def initialize_rag():
|
|||||||
|
|
||||||
return rag
|
return rag
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
@@ -252,12 +260,14 @@ rag = LightRAG(
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary> <b>Using Ollama Models</b> </summary>
|
<summary> <b>Using Ollama Models</b> </summary>
|
||||||
|
|
||||||
### Overview
|
### Overview
|
||||||
|
|
||||||
If you want to use Ollama models, you need to pull model you plan to use and embedding model, for example `nomic-embed-text`.
|
If you want to use Ollama models, you need to pull model you plan to use and embedding model, for example `nomic-embed-text`.
|
||||||
|
|
||||||
Then you only need to set LightRAG as follows:
|
Then you only need to set LightRAG as follows:
|
||||||
@@ -281,31 +291,37 @@ rag = LightRAG(
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Increasing context size
|
### Increasing context size
|
||||||
|
|
||||||
In order for LightRAG to work context should be at least 32k tokens. By default Ollama models have context size of 8k. You can achieve this using one of two ways:
|
In order for LightRAG to work context should be at least 32k tokens. By default Ollama models have context size of 8k. You can achieve this using one of two ways:
|
||||||
|
|
||||||
#### Increasing the `num_ctx` parameter in Modelfile.
|
#### Increasing the `num_ctx` parameter in Modelfile.
|
||||||
|
|
||||||
1. Pull the model:
|
1. Pull the model:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
ollama pull qwen2
|
ollama pull qwen2
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Display the model file:
|
2. Display the model file:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
ollama show --modelfile qwen2 > Modelfile
|
ollama show --modelfile qwen2 > Modelfile
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Edit the Modelfile by adding the following line:
|
3. Edit the Modelfile by adding the following line:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
PARAMETER num_ctx 32768
|
PARAMETER num_ctx 32768
|
||||||
```
|
```
|
||||||
|
|
||||||
4. Create the modified model:
|
4. Create the modified model:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
ollama create -f Modelfile qwen2m
|
ollama create -f Modelfile qwen2m
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Setup `num_ctx` via Ollama API.
|
#### Setup `num_ctx` via Ollama API.
|
||||||
|
|
||||||
Tiy can use `llm_model_kwargs` param to configure ollama:
|
Tiy can use `llm_model_kwargs` param to configure ollama:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@@ -325,6 +341,7 @@ rag = LightRAG(
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Low RAM GPUs
|
#### Low RAM GPUs
|
||||||
|
|
||||||
In order to run this experiment on low RAM GPU you should select small model and tune context window (increasing context increase memory consumption). For example, running this ollama example on repurposed mining GPU with 6Gb of RAM required to set context size to 26k while using `gemma2:2b`. It was able to find 197 entities and 19 relations on `book.txt`.
|
In order to run this experiment on low RAM GPU you should select small model and tune context window (increasing context increase memory consumption). For example, running this ollama example on repurposed mining GPU with 6Gb of RAM required to set context size to 26k while using `gemma2:2b`. It was able to find 197 entities and 19 relations on `book.txt`.
|
||||||
@@ -402,6 +419,7 @@ if __name__ == "__main__":
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### For detailed documentation and examples, see:
|
#### For detailed documentation and examples, see:
|
||||||
|
|
||||||
- [LlamaIndex Documentation](lightrag/llm/Readme.md)
|
- [LlamaIndex Documentation](lightrag/llm/Readme.md)
|
||||||
- [Direct OpenAI Example](examples/lightrag_llamaindex_direct_demo.py)
|
- [Direct OpenAI Example](examples/lightrag_llamaindex_direct_demo.py)
|
||||||
- [LiteLLM Proxy Example](examples/lightrag_llamaindex_litellm_demo.py)
|
- [LiteLLM Proxy Example](examples/lightrag_llamaindex_litellm_demo.py)
|
||||||
@@ -483,13 +501,16 @@ print(response_custom)
|
|||||||
We've introduced a new function `query_with_separate_keyword_extraction` to enhance the keyword extraction capabilities. This function separates the keyword extraction process from the user's prompt, focusing solely on the query to improve the relevance of extracted keywords.
|
We've introduced a new function `query_with_separate_keyword_extraction` to enhance the keyword extraction capabilities. This function separates the keyword extraction process from the user's prompt, focusing solely on the query to improve the relevance of extracted keywords.
|
||||||
|
|
||||||
##### How It Works?
|
##### How It Works?
|
||||||
|
|
||||||
The function operates by dividing the input into two parts:
|
The function operates by dividing the input into two parts:
|
||||||
|
|
||||||
- `User Query`
|
- `User Query`
|
||||||
- `Prompt`
|
- `Prompt`
|
||||||
|
|
||||||
It then performs keyword extraction exclusively on the `user query`. This separation ensures that the extraction process is focused and relevant, unaffected by any additional language in the `prompt`. It also allows the `prompt` to serve purely for response formatting, maintaining the intent and clarity of the user's original question.
|
It then performs keyword extraction exclusively on the `user query`. This separation ensures that the extraction process is focused and relevant, unaffected by any additional language in the `prompt`. It also allows the `prompt` to serve purely for response formatting, maintaining the intent and clarity of the user's original question.
|
||||||
|
|
||||||
##### Usage Example
|
##### Usage Example
|
||||||
|
|
||||||
This `example` shows how to tailor the function for educational content, focusing on detailed explanations for older students.
|
This `example` shows how to tailor the function for educational content, focusing on detailed explanations for older students.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@@ -563,6 +584,7 @@ custom_kg = {
|
|||||||
|
|
||||||
rag.insert_custom_kg(custom_kg)
|
rag.insert_custom_kg(custom_kg)
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
## Insert
|
## Insert
|
||||||
@@ -593,6 +615,7 @@ rag.insert(["TEXT1", "TEXT2", "TEXT3", ...]) # Documents will be processed in b
|
|||||||
```
|
```
|
||||||
|
|
||||||
The `insert_batch_size` parameter in `addon_params` controls how many documents are processed in each batch during insertion. This is useful for:
|
The `insert_batch_size` parameter in `addon_params` controls how many documents are processed in each batch during insertion. This is useful for:
|
||||||
|
|
||||||
- Managing memory usage with large document collections
|
- Managing memory usage with large document collections
|
||||||
- Optimizing processing speed
|
- Optimizing processing speed
|
||||||
- Providing better progress tracking
|
- Providing better progress tracking
|
||||||
@@ -647,6 +670,7 @@ text_content = textract.process(file_path)
|
|||||||
|
|
||||||
rag.insert(text_content.decode('utf-8'))
|
rag.insert(text_content.decode('utf-8'))
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
## Storage
|
## Storage
|
||||||
@@ -685,6 +709,7 @@ async def initialize_rag():
|
|||||||
|
|
||||||
return rag
|
return rag
|
||||||
```
|
```
|
||||||
|
|
||||||
see test_neo4j.py for a working example.
|
see test_neo4j.py for a working example.
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
@@ -693,6 +718,7 @@ see test_neo4j.py for a working example.
|
|||||||
<summary> <b>Using PostgreSQL for Storage</b> </summary>
|
<summary> <b>Using PostgreSQL for Storage</b> </summary>
|
||||||
|
|
||||||
For production level scenarios you will most likely want to leverage an enterprise solution. PostgreSQL can provide a one-stop solution for you as KV store, VectorDB (pgvector) and GraphDB (apache AGE).
|
For production level scenarios you will most likely want to leverage an enterprise solution. PostgreSQL can provide a one-stop solution for you as KV store, VectorDB (pgvector) and GraphDB (apache AGE).
|
||||||
|
|
||||||
* PostgreSQL is lightweight,the whole binary distribution including all necessary plugins can be zipped to 40MB: Ref to [Windows Release](https://github.com/ShanGor/apache-age-windows/releases/tag/PG17%2Fv1.5.0-rc0) as it is easy to install for Linux/Mac.
|
* PostgreSQL is lightweight,the whole binary distribution including all necessary plugins can be zipped to 40MB: Ref to [Windows Release](https://github.com/ShanGor/apache-age-windows/releases/tag/PG17%2Fv1.5.0-rc0) as it is easy to install for Linux/Mac.
|
||||||
* If you prefer docker, please start with this image if you are a beginner to avoid hiccups (DO read the overview): https://hub.docker.com/r/shangor/postgres-for-rag
|
* If you prefer docker, please start with this image if you are a beginner to avoid hiccups (DO read the overview): https://hub.docker.com/r/shangor/postgres-for-rag
|
||||||
* How to start? Ref to: [examples/lightrag_zhipu_postgres_demo.py](https://github.com/HKUDS/LightRAG/blob/main/examples/lightrag_zhipu_postgres_demo.py)
|
* How to start? Ref to: [examples/lightrag_zhipu_postgres_demo.py](https://github.com/HKUDS/LightRAG/blob/main/examples/lightrag_zhipu_postgres_demo.py)
|
||||||
@@ -735,6 +761,7 @@ For production level scenarios you will most likely want to leverage an enterpri
|
|||||||
> It is a known issue of the release version: https://github.com/apache/age/pull/1721
|
> It is a known issue of the release version: https://github.com/apache/age/pull/1721
|
||||||
>
|
>
|
||||||
> You can Compile the AGE from source code and fix it.
|
> You can Compile the AGE from source code and fix it.
|
||||||
|
>
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
@@ -742,9 +769,11 @@ For production level scenarios you will most likely want to leverage an enterpri
|
|||||||
<summary> <b>Using Faiss for Storage</b> </summary>
|
<summary> <b>Using Faiss for Storage</b> </summary>
|
||||||
|
|
||||||
- Install the required dependencies:
|
- Install the required dependencies:
|
||||||
|
|
||||||
```
|
```
|
||||||
pip install faiss-cpu
|
pip install faiss-cpu
|
||||||
```
|
```
|
||||||
|
|
||||||
You can also install `faiss-gpu` if you have GPU support.
|
You can also install `faiss-gpu` if you have GPU support.
|
||||||
|
|
||||||
- Here we are using `sentence-transformers` but you can also use `OpenAIEmbedding` model with `3072` dimensions.
|
- Here we are using `sentence-transformers` but you can also use `OpenAIEmbedding` model with `3072` dimensions.
|
||||||
@@ -810,6 +839,7 @@ relation = rag.create_relation("Google", "Gmail", {
|
|||||||
"weight": 2.0
|
"weight": 2.0
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
@@ -835,6 +865,7 @@ updated_relation = rag.edit_relation("Google", "Google Mail", {
|
|||||||
"weight": 3.0
|
"weight": 3.0
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
All operations are available in both synchronous and asynchronous versions. The asynchronous versions have the prefix "a" (e.g., `acreate_entity`, `aedit_relation`).
|
All operations are available in both synchronous and asynchronous versions. The asynchronous versions have the prefix "a" (e.g., `acreate_entity`, `aedit_relation`).
|
||||||
@@ -851,6 +882,55 @@ All operations are available in both synchronous and asynchronous versions. The
|
|||||||
|
|
||||||
These operations maintain data consistency across both the graph database and vector database components, ensuring your knowledge graph remains coherent.
|
These operations maintain data consistency across both the graph database and vector database components, ensuring your knowledge graph remains coherent.
|
||||||
|
|
||||||
|
## Data Export Functions
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
LightRAG allows you to export your knowledge graph data in various formats for analysis, sharing, and backup purposes. The system supports exporting entities, relations, and relationship data.
|
||||||
|
|
||||||
|
## Export Functions
|
||||||
|
|
||||||
|
### Basic Usage
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Basic CSV export (default format)
|
||||||
|
rag.export_data("knowledge_graph.csv")
|
||||||
|
|
||||||
|
# Specify any format
|
||||||
|
rag.export_data("output.xlsx", file_format="excel")
|
||||||
|
```
|
||||||
|
|
||||||
|
### Different File Formats supported
|
||||||
|
|
||||||
|
```python
|
||||||
|
#Export data in CSV format
|
||||||
|
rag.export_data("graph_data.csv", file_format="csv")
|
||||||
|
|
||||||
|
# Export data in Excel sheet
|
||||||
|
rag.export_data("graph_data.xlsx", file_format="excel")
|
||||||
|
|
||||||
|
# Export data in markdown format
|
||||||
|
rag.export_data("graph_data.md", file_format="md")
|
||||||
|
|
||||||
|
# Export data in Text
|
||||||
|
rag.export_data("graph_data.txt", file_format="txt")
|
||||||
|
```
|
||||||
|
## Additional Options
|
||||||
|
|
||||||
|
Include vector embeddings in the export (optional):
|
||||||
|
|
||||||
|
```python
|
||||||
|
rag.export_data("complete_data.csv", include_vector_data=True)
|
||||||
|
```
|
||||||
|
## Data Included in Export
|
||||||
|
|
||||||
|
All exports include:
|
||||||
|
|
||||||
|
* Entity information (names, IDs, metadata)
|
||||||
|
* Relation data (connections between entities)
|
||||||
|
* Relationship information from vector database
|
||||||
|
|
||||||
|
|
||||||
## Entity Merging
|
## Entity Merging
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
@@ -913,6 +993,7 @@ rag.merge_entities(
|
|||||||
```
|
```
|
||||||
|
|
||||||
When merging entities:
|
When merging entities:
|
||||||
|
|
||||||
* All relationships from source entities are redirected to the target entity
|
* All relationships from source entities are redirected to the target entity
|
||||||
* Duplicate relationships are intelligently merged
|
* Duplicate relationships are intelligently merged
|
||||||
* Self-relationships (loops) are prevented
|
* Self-relationships (loops) are prevented
|
||||||
@@ -946,6 +1027,7 @@ rag.clear_cache(modes=["local"])
|
|||||||
```
|
```
|
||||||
|
|
||||||
Valid modes are:
|
Valid modes are:
|
||||||
|
|
||||||
- `"default"`: Extraction cache
|
- `"default"`: Extraction cache
|
||||||
- `"naive"`: Naive search cache
|
- `"naive"`: Naive search cache
|
||||||
- `"local"`: Local search cache
|
- `"local"`: Local search cache
|
||||||
@@ -961,11 +1043,11 @@ Valid modes are:
|
|||||||
<summary> Parameters </summary>
|
<summary> Parameters </summary>
|
||||||
|
|
||||||
| **Parameter** | **Type** | **Explanation** | **Default** |
|
| **Parameter** | **Type** | **Explanation** | **Default** |
|
||||||
|----------------------------------------------| --- |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------|
|
| -------------------------------------------------- | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
|
||||||
| **working\_dir** | `str` | Directory where the cache will be stored | `lightrag_cache+timestamp` |
|
| **working\_dir** | `str` | Directory where the cache will be stored | `lightrag_cache+timestamp` |
|
||||||
| **kv\_storage** | `str` | Storage type for documents and text chunks. Supported types: `JsonKVStorage`, `OracleKVStorage` | `JsonKVStorage` |
|
| **kv\_storage** | `str` | Storage type for documents and text chunks. Supported types:`JsonKVStorage`, `OracleKVStorage` | `JsonKVStorage` |
|
||||||
| **vector\_storage** | `str` | Storage type for embedding vectors. Supported types: `NanoVectorDBStorage`, `OracleVectorDBStorage` | `NanoVectorDBStorage` |
|
| **vector\_storage** | `str` | Storage type for embedding vectors. Supported types:`NanoVectorDBStorage`, `OracleVectorDBStorage` | `NanoVectorDBStorage` |
|
||||||
| **graph\_storage** | `str` | Storage type for graph edges and nodes. Supported types: `NetworkXStorage`, `Neo4JStorage`, `OracleGraphStorage` | `NetworkXStorage` |
|
| **graph\_storage** | `str` | Storage type for graph edges and nodes. Supported types:`NetworkXStorage`, `Neo4JStorage`, `OracleGraphStorage` | `NetworkXStorage` |
|
||||||
| **chunk\_token\_size** | `int` | Maximum token size per chunk when splitting documents | `1200` |
|
| **chunk\_token\_size** | `int` | Maximum token size per chunk when splitting documents | `1200` |
|
||||||
| **chunk\_overlap\_token\_size** | `int` | Overlap token size between two chunks when splitting documents | `100` |
|
| **chunk\_overlap\_token\_size** | `int` | Overlap token size between two chunks when splitting documents | `100` |
|
||||||
| **tiktoken\_model\_name** | `str` | Model name for the Tiktoken encoder used to calculate token numbers | `gpt-4o-mini` |
|
| **tiktoken\_model\_name** | `str` | Model name for the Tiktoken encoder used to calculate token numbers | `gpt-4o-mini` |
|
||||||
@@ -984,9 +1066,9 @@ Valid modes are:
|
|||||||
| **vector\_db\_storage\_cls\_kwargs** | `dict` | Additional parameters for vector database, like setting the threshold for nodes and relations retrieval. | cosine_better_than_threshold: 0.2(default value changed by env var COSINE_THRESHOLD) |
|
| **vector\_db\_storage\_cls\_kwargs** | `dict` | Additional parameters for vector database, like setting the threshold for nodes and relations retrieval. | cosine_better_than_threshold: 0.2(default value changed by env var COSINE_THRESHOLD) |
|
||||||
| **enable\_llm\_cache** | `bool` | If `TRUE`, stores LLM results in cache; repeated prompts return cached responses | `TRUE` |
|
| **enable\_llm\_cache** | `bool` | If `TRUE`, stores LLM results in cache; repeated prompts return cached responses | `TRUE` |
|
||||||
| **enable\_llm\_cache\_for\_entity\_extract** | `bool` | If `TRUE`, stores LLM results in cache for entity extraction; Good for beginners to debug your application | `TRUE` |
|
| **enable\_llm\_cache\_for\_entity\_extract** | `bool` | If `TRUE`, stores LLM results in cache for entity extraction; Good for beginners to debug your application | `TRUE` |
|
||||||
| **addon\_params** | `dict` | Additional parameters, e.g., `{"example_number": 1, "language": "Simplified Chinese", "entity_types": ["organization", "person", "geo", "event"], "insert_batch_size": 10}`: sets example limit, output language, and batch size for document processing | `example_number: all examples, language: English, insert_batch_size: 10` |
|
| **addon\_params** | `dict` | Additional parameters, e.g.,`{"example_number": 1, "language": "Simplified Chinese", "entity_types": ["organization", "person", "geo", "event"], "insert_batch_size": 10}`: sets example limit, output language, and batch size for document processing | `example_number: all examples, language: English, insert_batch_size: 10` |
|
||||||
| **convert\_response\_to\_json\_func** | `callable` | Not used | `convert_response_to_json` |
|
| **convert\_response\_to\_json\_func** | `callable` | Not used | `convert_response_to_json` |
|
||||||
| **embedding\_cache\_config** | `dict` | Configuration for question-answer caching. Contains three parameters:<br>- `enabled`: Boolean value to enable/disable cache lookup functionality. When enabled, the system will check cached responses before generating new answers.<br>- `similarity_threshold`: Float value (0-1), similarity threshold. When a new question's similarity with a cached question exceeds this threshold, the cached answer will be returned directly without calling the LLM.<br>- `use_llm_check`: Boolean value to enable/disable LLM similarity verification. When enabled, LLM will be used as a secondary check to verify the similarity between questions before returning cached answers. | Default: `{"enabled": False, "similarity_threshold": 0.95, "use_llm_check": False}` |
|
| **embedding\_cache\_config** | `dict` | Configuration for question-answer caching. Contains three parameters:`<br>`- `enabled`: Boolean value to enable/disable cache lookup functionality. When enabled, the system will check cached responses before generating new answers.`<br>`- `similarity_threshold`: Float value (0-1), similarity threshold. When a new question's similarity with a cached question exceeds this threshold, the cached answer will be returned directly without calling the LLM.`<br>`- `use_llm_check`: Boolean value to enable/disable LLM similarity verification. When enabled, LLM will be used as a secondary check to verify the similarity between questions before returning cached answers. | Default:`{"enabled": False, "similarity_threshold": 0.95, "use_llm_check": False}` |
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
@@ -996,12 +1078,15 @@ Valid modes are:
|
|||||||
<summary>Click to view error handling details</summary>
|
<summary>Click to view error handling details</summary>
|
||||||
|
|
||||||
The API includes comprehensive error handling:
|
The API includes comprehensive error handling:
|
||||||
|
|
||||||
- File not found errors (404)
|
- File not found errors (404)
|
||||||
- Processing errors (500)
|
- Processing errors (500)
|
||||||
- Supports multiple file encodings (UTF-8 and GBK)
|
- Supports multiple file encodings (UTF-8 and GBK)
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
LightRag can be installed with API support to serve a Fast api interface to perform data upload and indexing/Rag operations/Rescan of the input folder etc..
|
LightRag can be installed with API support to serve a Fast api interface to perform data upload and indexing/Rag operations/Rescan of the input folder etc..
|
||||||
|
|
||||||
[LightRag API](lightrag/api/README.md)
|
[LightRag API](lightrag/api/README.md)
|
||||||
@@ -1035,7 +1120,6 @@ net.show('knowledge_graph.html')
|
|||||||
<details>
|
<details>
|
||||||
<summary> <b>Graph visualization with Neo4</b> </summary>
|
<summary> <b>Graph visualization with Neo4</b> </summary>
|
||||||
|
|
||||||
|
|
||||||
* The following code can be found in `examples/graph_visual_with_neo4j.py`
|
* The following code can be found in `examples/graph_visual_with_neo4j.py`
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@@ -1171,10 +1255,13 @@ LightRag can be installed with Tools support to add extra tools like the graphml
|
|||||||
</details>
|
</details>
|
||||||
|
|
||||||
## Evaluation
|
## Evaluation
|
||||||
|
|
||||||
### Dataset
|
### Dataset
|
||||||
|
|
||||||
The dataset used in LightRAG can be downloaded from [TommyChien/UltraDomain](https://huggingface.co/datasets/TommyChien/UltraDomain).
|
The dataset used in LightRAG can be downloaded from [TommyChien/UltraDomain](https://huggingface.co/datasets/TommyChien/UltraDomain).
|
||||||
|
|
||||||
### Generate Query
|
### Generate Query
|
||||||
|
|
||||||
LightRAG uses the following prompt to generate high-level queries, with the corresponding code in `example/generate_query.py`.
|
LightRAG uses the following prompt to generate high-level queries, with the corresponding code in `example/generate_query.py`.
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
@@ -1203,9 +1290,11 @@ Output the results in the following structure:
|
|||||||
- User 5: [user description]
|
- User 5: [user description]
|
||||||
...
|
...
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
### Batch Eval
|
### Batch Eval
|
||||||
|
|
||||||
To evaluate the performance of two RAG systems on high-level queries, LightRAG uses the following prompt, with the specific code available in `example/batch_eval.py`.
|
To evaluate the performance of two RAG systems on high-level queries, LightRAG uses the following prompt, with the specific code available in `example/batch_eval.py`.
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
@@ -1253,12 +1342,13 @@ Output your evaluation in the following JSON format:
|
|||||||
}}
|
}}
|
||||||
}}
|
}}
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
### Overall Performance Table
|
### Overall Performance Table
|
||||||
|
|
||||||
| | **Agriculture** | | **CS** | | **Legal** | | **Mix** | |
|
| | **Agriculture** | | **CS** | | **Legal** | | **Mix** | |
|
||||||
|----------------------|-------------------------|-----------------------|-----------------------|-----------------------|-----------------------|-----------------------|-----------------------|-----------------------|
|
| --------------------------- | --------------------- | ------------------ | ------------ | ------------------ | --------------- | ------------------ | --------------- | ------------------ |
|
||||||
| | NaiveRAG | **LightRAG** | NaiveRAG | **LightRAG** | NaiveRAG | **LightRAG** | NaiveRAG | **LightRAG** |
|
| | NaiveRAG | **LightRAG** | NaiveRAG | **LightRAG** | NaiveRAG | **LightRAG** | NaiveRAG | **LightRAG** |
|
||||||
| **Comprehensiveness** | 32.4% | **67.6%** | 38.4% | **61.6%** | 16.4% | **83.6%** | 38.8% | **61.2%** |
|
| **Comprehensiveness** | 32.4% | **67.6%** | 38.4% | **61.6%** | 16.4% | **83.6%** | 38.8% | **61.2%** |
|
||||||
| **Diversity** | 23.6% | **76.4%** | 38.0% | **62.0%** | 13.6% | **86.4%** | 32.4% | **67.6%** |
|
| **Diversity** | 23.6% | **76.4%** | 38.0% | **62.0%** | 13.6% | **86.4%** | 32.4% | **67.6%** |
|
||||||
@@ -1281,9 +1371,11 @@ Output your evaluation in the following JSON format:
|
|||||||
| **Overall** | 45.2% | **54.8%** | 48.0% | **52.0%** | 47.2% | **52.8%** | **50.4%** | 49.6% |
|
| **Overall** | 45.2% | **54.8%** | 48.0% | **52.0%** | 47.2% | **52.8%** | **50.4%** | 49.6% |
|
||||||
|
|
||||||
## Reproduce
|
## Reproduce
|
||||||
|
|
||||||
All the code can be found in the `./reproduce` directory.
|
All the code can be found in the `./reproduce` directory.
|
||||||
|
|
||||||
### Step-0 Extract Unique Contexts
|
### Step-0 Extract Unique Contexts
|
||||||
|
|
||||||
First, we need to extract unique contexts in the datasets.
|
First, we need to extract unique contexts in the datasets.
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
@@ -1340,9 +1432,11 @@ def extract_unique_contexts(input_directory, output_directory):
|
|||||||
print("All files have been processed.")
|
print("All files have been processed.")
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
### Step-1 Insert Contexts
|
### Step-1 Insert Contexts
|
||||||
|
|
||||||
For the extracted contexts, we insert them into the LightRAG system.
|
For the extracted contexts, we insert them into the LightRAG system.
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
@@ -1366,6 +1460,7 @@ def insert_text(rag, file_path):
|
|||||||
if retries == max_retries:
|
if retries == max_retries:
|
||||||
print("Insertion failed after exceeding the maximum number of retries")
|
print("Insertion failed after exceeding the maximum number of retries")
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
### Step-2 Generate Queries
|
### Step-2 Generate Queries
|
||||||
@@ -1390,9 +1485,11 @@ def get_summary(context, tot_tokens=2000):
|
|||||||
|
|
||||||
return summary
|
return summary
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
### Step-3 Query
|
### Step-3 Query
|
||||||
|
|
||||||
For the queries generated in Step-2, we will extract them and query LightRAG.
|
For the queries generated in Step-2, we will extract them and query LightRAG.
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
@@ -1409,6 +1506,7 @@ def extract_queries(file_path):
|
|||||||
|
|
||||||
return queries
|
return queries
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
## Star History
|
## Star History
|
||||||
@@ -1441,4 +1539,5 @@ archivePrefix={arXiv},
|
|||||||
primaryClass={cs.IR}
|
primaryClass={cs.IR}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**Thank you for your interest in our work!**
|
**Thank you for your interest in our work!**
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
from .lightrag import LightRAG as LightRAG, QueryParam as QueryParam
|
from .lightrag import LightRAG as LightRAG, QueryParam as QueryParam
|
||||||
|
|
||||||
__version__ = "1.2.5"
|
__version__ = "1.2.6"
|
||||||
__author__ = "Zirui Guo"
|
__author__ = "Zirui Guo"
|
||||||
__url__ = "https://github.com/HKUDS/LightRAG"
|
__url__ = "https://github.com/HKUDS/LightRAG"
|
||||||
|
@@ -59,7 +59,7 @@ logconfig_dict = {
|
|||||||
},
|
},
|
||||||
"filters": {
|
"filters": {
|
||||||
"path_filter": {
|
"path_filter": {
|
||||||
"()": "lightrag.api.lightrag_server.LightragPathFilter",
|
"()": "lightrag.utils.LightragPathFilter",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"loggers": {
|
"loggers": {
|
||||||
|
@@ -55,41 +55,6 @@ config = configparser.ConfigParser()
|
|||||||
config.read("config.ini")
|
config.read("config.ini")
|
||||||
|
|
||||||
|
|
||||||
class LightragPathFilter(logging.Filter):
|
|
||||||
"""Filter for lightrag logger to filter out frequent path access logs"""
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
super().__init__()
|
|
||||||
# Define paths to be filtered
|
|
||||||
self.filtered_paths = ["/documents", "/health", "/webui/"]
|
|
||||||
|
|
||||||
def filter(self, record):
|
|
||||||
try:
|
|
||||||
# Check if record has the required attributes for an access log
|
|
||||||
if not hasattr(record, "args") or not isinstance(record.args, tuple):
|
|
||||||
return True
|
|
||||||
if len(record.args) < 5:
|
|
||||||
return True
|
|
||||||
|
|
||||||
# Extract method, path and status from the record args
|
|
||||||
method = record.args[1]
|
|
||||||
path = record.args[2]
|
|
||||||
status = record.args[4]
|
|
||||||
|
|
||||||
# Filter out successful GET requests to filtered paths
|
|
||||||
if (
|
|
||||||
method == "GET"
|
|
||||||
and (status == 200 or status == 304)
|
|
||||||
and path in self.filtered_paths
|
|
||||||
):
|
|
||||||
return False
|
|
||||||
|
|
||||||
return True
|
|
||||||
except Exception:
|
|
||||||
# In case of any error, let the message through
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def create_app(args):
|
def create_app(args):
|
||||||
# Setup logging
|
# Setup logging
|
||||||
logger.setLevel(args.log_level)
|
logger.setLevel(args.log_level)
|
||||||
@@ -177,6 +142,9 @@ def create_app(args):
|
|||||||
if api_key
|
if api_key
|
||||||
else "",
|
else "",
|
||||||
version=__api_version__,
|
version=__api_version__,
|
||||||
|
openapi_url="/openapi.json", # Explicitly set OpenAPI schema URL
|
||||||
|
docs_url="/docs", # Explicitly set docs URL
|
||||||
|
redoc_url="/redoc", # Explicitly set redoc URL
|
||||||
openapi_tags=[{"name": "api"}],
|
openapi_tags=[{"name": "api"}],
|
||||||
lifespan=lifespan,
|
lifespan=lifespan,
|
||||||
)
|
)
|
||||||
@@ -528,7 +496,7 @@ def configure_logging():
|
|||||||
},
|
},
|
||||||
"filters": {
|
"filters": {
|
||||||
"path_filter": {
|
"path_filter": {
|
||||||
"()": "lightrag.api.lightrag_server.LightragPathFilter",
|
"()": "lightrag.utils.LightragPathFilter",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
1
lightrag/api/webui/assets/index-BV5s8k-a.css
Normal file
1
lightrag/api/webui/assets/index-BV5s8k-a.css
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -8,8 +8,8 @@
|
|||||||
<link rel="icon" type="image/svg+xml" href="./logo.png" />
|
<link rel="icon" type="image/svg+xml" href="./logo.png" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title>Lightrag</title>
|
<title>Lightrag</title>
|
||||||
<script type="module" crossorigin src="./assets/index-Cs3ZbEon.js"></script>
|
<script type="module" crossorigin src="./assets/index-DwcJE583.js"></script>
|
||||||
<link rel="stylesheet" crossorigin href="./assets/index-DRGuXfZw.css">
|
<link rel="stylesheet" crossorigin href="./assets/index-BV5s8k-a.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="root"></div>
|
<div id="root"></div>
|
||||||
|
@@ -3,11 +3,14 @@ from __future__ import annotations
|
|||||||
import asyncio
|
import asyncio
|
||||||
import configparser
|
import configparser
|
||||||
import os
|
import os
|
||||||
|
import csv
|
||||||
import warnings
|
import warnings
|
||||||
from dataclasses import asdict, dataclass, field
|
from dataclasses import asdict, dataclass, field
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from typing import Any, AsyncIterator, Callable, Iterator, cast, final
|
from typing import Any, AsyncIterator, Callable, Iterator, cast, final, Literal
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
|
||||||
from lightrag.kg import (
|
from lightrag.kg import (
|
||||||
STORAGE_ENV_REQUIREMENTS,
|
STORAGE_ENV_REQUIREMENTS,
|
||||||
@@ -1111,6 +1114,7 @@ class LightRAG:
|
|||||||
|
|
||||||
# Prepare node data
|
# Prepare node data
|
||||||
node_data: dict[str, str] = {
|
node_data: dict[str, str] = {
|
||||||
|
"entity_id": entity_name,
|
||||||
"entity_type": entity_type,
|
"entity_type": entity_type,
|
||||||
"description": description,
|
"description": description,
|
||||||
"source_id": source_id,
|
"source_id": source_id,
|
||||||
@@ -1148,6 +1152,7 @@ class LightRAG:
|
|||||||
await self.chunk_entity_relation_graph.upsert_node(
|
await self.chunk_entity_relation_graph.upsert_node(
|
||||||
need_insert_id,
|
need_insert_id,
|
||||||
node_data={
|
node_data={
|
||||||
|
"entity_id": need_insert_id,
|
||||||
"source_id": source_id,
|
"source_id": source_id,
|
||||||
"description": "UNKNOWN",
|
"description": "UNKNOWN",
|
||||||
"entity_type": "UNKNOWN",
|
"entity_type": "UNKNOWN",
|
||||||
@@ -2157,6 +2162,7 @@ class LightRAG:
|
|||||||
|
|
||||||
# Prepare node data with defaults if missing
|
# Prepare node data with defaults if missing
|
||||||
node_data = {
|
node_data = {
|
||||||
|
"entity_id": entity_name,
|
||||||
"entity_type": entity_data.get("entity_type", "UNKNOWN"),
|
"entity_type": entity_data.get("entity_type", "UNKNOWN"),
|
||||||
"description": entity_data.get("description", ""),
|
"description": entity_data.get("description", ""),
|
||||||
"source_id": entity_data.get("source_id", "manual"),
|
"source_id": entity_data.get("source_id", "manual"),
|
||||||
@@ -2592,6 +2598,322 @@ class LightRAG:
|
|||||||
logger.error(f"Error merging entities: {e}")
|
logger.error(f"Error merging entities: {e}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
async def aexport_data(
|
||||||
|
self,
|
||||||
|
output_path: str,
|
||||||
|
file_format: Literal["csv", "excel", "md", "txt"] = "csv",
|
||||||
|
include_vector_data: bool = False,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
Asynchronously exports all entities, relations, and relationships to various formats.
|
||||||
|
Args:
|
||||||
|
output_path: The path to the output file (including extension).
|
||||||
|
file_format: Output format - "csv", "excel", "md", "txt".
|
||||||
|
- csv: Comma-separated values file
|
||||||
|
- excel: Microsoft Excel file with multiple sheets
|
||||||
|
- md: Markdown tables
|
||||||
|
- txt: Plain text formatted output
|
||||||
|
- table: Print formatted tables to console
|
||||||
|
include_vector_data: Whether to include data from the vector database.
|
||||||
|
"""
|
||||||
|
# Collect data
|
||||||
|
entities_data = []
|
||||||
|
relations_data = []
|
||||||
|
relationships_data = []
|
||||||
|
|
||||||
|
# --- Entities ---
|
||||||
|
all_entities = await self.chunk_entity_relation_graph.get_all_labels()
|
||||||
|
for entity_name in all_entities:
|
||||||
|
entity_info = await self.get_entity_info(
|
||||||
|
entity_name, include_vector_data=include_vector_data
|
||||||
|
)
|
||||||
|
entity_row = {
|
||||||
|
"entity_name": entity_name,
|
||||||
|
"source_id": entity_info["source_id"],
|
||||||
|
"graph_data": str(
|
||||||
|
entity_info["graph_data"]
|
||||||
|
), # Convert to string to ensure compatibility
|
||||||
|
}
|
||||||
|
if include_vector_data and "vector_data" in entity_info:
|
||||||
|
entity_row["vector_data"] = str(entity_info["vector_data"])
|
||||||
|
entities_data.append(entity_row)
|
||||||
|
|
||||||
|
# --- Relations ---
|
||||||
|
for src_entity in all_entities:
|
||||||
|
for tgt_entity in all_entities:
|
||||||
|
if src_entity == tgt_entity:
|
||||||
|
continue
|
||||||
|
|
||||||
|
edge_exists = await self.chunk_entity_relation_graph.has_edge(
|
||||||
|
src_entity, tgt_entity
|
||||||
|
)
|
||||||
|
if edge_exists:
|
||||||
|
relation_info = await self.get_relation_info(
|
||||||
|
src_entity, tgt_entity, include_vector_data=include_vector_data
|
||||||
|
)
|
||||||
|
relation_row = {
|
||||||
|
"src_entity": src_entity,
|
||||||
|
"tgt_entity": tgt_entity,
|
||||||
|
"source_id": relation_info["source_id"],
|
||||||
|
"graph_data": str(
|
||||||
|
relation_info["graph_data"]
|
||||||
|
), # Convert to string
|
||||||
|
}
|
||||||
|
if include_vector_data and "vector_data" in relation_info:
|
||||||
|
relation_row["vector_data"] = str(relation_info["vector_data"])
|
||||||
|
relations_data.append(relation_row)
|
||||||
|
|
||||||
|
# --- Relationships (from VectorDB) ---
|
||||||
|
all_relationships = await self.relationships_vdb.client_storage
|
||||||
|
for rel in all_relationships["data"]:
|
||||||
|
relationships_data.append(
|
||||||
|
{
|
||||||
|
"relationship_id": rel["__id__"],
|
||||||
|
"data": str(rel), # Convert to string for compatibility
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# Export based on format
|
||||||
|
if file_format == "csv":
|
||||||
|
# CSV export
|
||||||
|
with open(output_path, "w", newline="", encoding="utf-8") as csvfile:
|
||||||
|
# Entities
|
||||||
|
if entities_data:
|
||||||
|
csvfile.write("# ENTITIES\n")
|
||||||
|
writer = csv.DictWriter(csvfile, fieldnames=entities_data[0].keys())
|
||||||
|
writer.writeheader()
|
||||||
|
writer.writerows(entities_data)
|
||||||
|
csvfile.write("\n\n")
|
||||||
|
|
||||||
|
# Relations
|
||||||
|
if relations_data:
|
||||||
|
csvfile.write("# RELATIONS\n")
|
||||||
|
writer = csv.DictWriter(
|
||||||
|
csvfile, fieldnames=relations_data[0].keys()
|
||||||
|
)
|
||||||
|
writer.writeheader()
|
||||||
|
writer.writerows(relations_data)
|
||||||
|
csvfile.write("\n\n")
|
||||||
|
|
||||||
|
# Relationships
|
||||||
|
if relationships_data:
|
||||||
|
csvfile.write("# RELATIONSHIPS\n")
|
||||||
|
writer = csv.DictWriter(
|
||||||
|
csvfile, fieldnames=relationships_data[0].keys()
|
||||||
|
)
|
||||||
|
writer.writeheader()
|
||||||
|
writer.writerows(relationships_data)
|
||||||
|
|
||||||
|
elif file_format == "excel":
|
||||||
|
# Excel export
|
||||||
|
entities_df = (
|
||||||
|
pd.DataFrame(entities_data) if entities_data else pd.DataFrame()
|
||||||
|
)
|
||||||
|
relations_df = (
|
||||||
|
pd.DataFrame(relations_data) if relations_data else pd.DataFrame()
|
||||||
|
)
|
||||||
|
relationships_df = (
|
||||||
|
pd.DataFrame(relationships_data)
|
||||||
|
if relationships_data
|
||||||
|
else pd.DataFrame()
|
||||||
|
)
|
||||||
|
|
||||||
|
with pd.ExcelWriter(output_path, engine="xlsxwriter") as writer:
|
||||||
|
if not entities_df.empty:
|
||||||
|
entities_df.to_excel(writer, sheet_name="Entities", index=False)
|
||||||
|
if not relations_df.empty:
|
||||||
|
relations_df.to_excel(writer, sheet_name="Relations", index=False)
|
||||||
|
if not relationships_df.empty:
|
||||||
|
relationships_df.to_excel(
|
||||||
|
writer, sheet_name="Relationships", index=False
|
||||||
|
)
|
||||||
|
|
||||||
|
elif file_format == "md":
|
||||||
|
# Markdown export
|
||||||
|
with open(output_path, "w", encoding="utf-8") as mdfile:
|
||||||
|
mdfile.write("# LightRAG Data Export\n\n")
|
||||||
|
|
||||||
|
# Entities
|
||||||
|
mdfile.write("## Entities\n\n")
|
||||||
|
if entities_data:
|
||||||
|
# Write header
|
||||||
|
mdfile.write("| " + " | ".join(entities_data[0].keys()) + " |\n")
|
||||||
|
mdfile.write(
|
||||||
|
"| "
|
||||||
|
+ " | ".join(["---"] * len(entities_data[0].keys()))
|
||||||
|
+ " |\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Write rows
|
||||||
|
for entity in entities_data:
|
||||||
|
mdfile.write(
|
||||||
|
"| " + " | ".join(str(v) for v in entity.values()) + " |\n"
|
||||||
|
)
|
||||||
|
mdfile.write("\n\n")
|
||||||
|
else:
|
||||||
|
mdfile.write("*No entity data available*\n\n")
|
||||||
|
|
||||||
|
# Relations
|
||||||
|
mdfile.write("## Relations\n\n")
|
||||||
|
if relations_data:
|
||||||
|
# Write header
|
||||||
|
mdfile.write("| " + " | ".join(relations_data[0].keys()) + " |\n")
|
||||||
|
mdfile.write(
|
||||||
|
"| "
|
||||||
|
+ " | ".join(["---"] * len(relations_data[0].keys()))
|
||||||
|
+ " |\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Write rows
|
||||||
|
for relation in relations_data:
|
||||||
|
mdfile.write(
|
||||||
|
"| "
|
||||||
|
+ " | ".join(str(v) for v in relation.values())
|
||||||
|
+ " |\n"
|
||||||
|
)
|
||||||
|
mdfile.write("\n\n")
|
||||||
|
else:
|
||||||
|
mdfile.write("*No relation data available*\n\n")
|
||||||
|
|
||||||
|
# Relationships
|
||||||
|
mdfile.write("## Relationships\n\n")
|
||||||
|
if relationships_data:
|
||||||
|
# Write header
|
||||||
|
mdfile.write(
|
||||||
|
"| " + " | ".join(relationships_data[0].keys()) + " |\n"
|
||||||
|
)
|
||||||
|
mdfile.write(
|
||||||
|
"| "
|
||||||
|
+ " | ".join(["---"] * len(relationships_data[0].keys()))
|
||||||
|
+ " |\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Write rows
|
||||||
|
for relationship in relationships_data:
|
||||||
|
mdfile.write(
|
||||||
|
"| "
|
||||||
|
+ " | ".join(str(v) for v in relationship.values())
|
||||||
|
+ " |\n"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
mdfile.write("*No relationship data available*\n\n")
|
||||||
|
|
||||||
|
elif file_format == "txt":
|
||||||
|
# Plain text export
|
||||||
|
with open(output_path, "w", encoding="utf-8") as txtfile:
|
||||||
|
txtfile.write("LIGHTRAG DATA EXPORT\n")
|
||||||
|
txtfile.write("=" * 80 + "\n\n")
|
||||||
|
|
||||||
|
# Entities
|
||||||
|
txtfile.write("ENTITIES\n")
|
||||||
|
txtfile.write("-" * 80 + "\n")
|
||||||
|
if entities_data:
|
||||||
|
# Create fixed width columns
|
||||||
|
col_widths = {
|
||||||
|
k: max(len(k), max(len(str(e[k])) for e in entities_data))
|
||||||
|
for k in entities_data[0]
|
||||||
|
}
|
||||||
|
header = " ".join(k.ljust(col_widths[k]) for k in entities_data[0])
|
||||||
|
txtfile.write(header + "\n")
|
||||||
|
txtfile.write("-" * len(header) + "\n")
|
||||||
|
|
||||||
|
# Write rows
|
||||||
|
for entity in entities_data:
|
||||||
|
row = " ".join(
|
||||||
|
str(v).ljust(col_widths[k]) for k, v in entity.items()
|
||||||
|
)
|
||||||
|
txtfile.write(row + "\n")
|
||||||
|
txtfile.write("\n\n")
|
||||||
|
else:
|
||||||
|
txtfile.write("No entity data available\n\n")
|
||||||
|
|
||||||
|
# Relations
|
||||||
|
txtfile.write("RELATIONS\n")
|
||||||
|
txtfile.write("-" * 80 + "\n")
|
||||||
|
if relations_data:
|
||||||
|
# Create fixed width columns
|
||||||
|
col_widths = {
|
||||||
|
k: max(len(k), max(len(str(r[k])) for r in relations_data))
|
||||||
|
for k in relations_data[0]
|
||||||
|
}
|
||||||
|
header = " ".join(
|
||||||
|
k.ljust(col_widths[k]) for k in relations_data[0]
|
||||||
|
)
|
||||||
|
txtfile.write(header + "\n")
|
||||||
|
txtfile.write("-" * len(header) + "\n")
|
||||||
|
|
||||||
|
# Write rows
|
||||||
|
for relation in relations_data:
|
||||||
|
row = " ".join(
|
||||||
|
str(v).ljust(col_widths[k]) for k, v in relation.items()
|
||||||
|
)
|
||||||
|
txtfile.write(row + "\n")
|
||||||
|
txtfile.write("\n\n")
|
||||||
|
else:
|
||||||
|
txtfile.write("No relation data available\n\n")
|
||||||
|
|
||||||
|
# Relationships
|
||||||
|
txtfile.write("RELATIONSHIPS\n")
|
||||||
|
txtfile.write("-" * 80 + "\n")
|
||||||
|
if relationships_data:
|
||||||
|
# Create fixed width columns
|
||||||
|
col_widths = {
|
||||||
|
k: max(len(k), max(len(str(r[k])) for r in relationships_data))
|
||||||
|
for k in relationships_data[0]
|
||||||
|
}
|
||||||
|
header = " ".join(
|
||||||
|
k.ljust(col_widths[k]) for k in relationships_data[0]
|
||||||
|
)
|
||||||
|
txtfile.write(header + "\n")
|
||||||
|
txtfile.write("-" * len(header) + "\n")
|
||||||
|
|
||||||
|
# Write rows
|
||||||
|
for relationship in relationships_data:
|
||||||
|
row = " ".join(
|
||||||
|
str(v).ljust(col_widths[k]) for k, v in relationship.items()
|
||||||
|
)
|
||||||
|
txtfile.write(row + "\n")
|
||||||
|
else:
|
||||||
|
txtfile.write("No relationship data available\n\n")
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise ValueError(
|
||||||
|
f"Unsupported file format: {file_format}. "
|
||||||
|
f"Choose from: csv, excel, md, txt"
|
||||||
|
)
|
||||||
|
if file_format is not None:
|
||||||
|
print(f"Data exported to: {output_path} with format: {file_format}")
|
||||||
|
else:
|
||||||
|
print("Data displayed as table format")
|
||||||
|
|
||||||
|
def export_data(
|
||||||
|
self,
|
||||||
|
output_path: str,
|
||||||
|
file_format: Literal["csv", "excel", "md", "txt"] = "csv",
|
||||||
|
include_vector_data: bool = False,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
Synchronously exports all entities, relations, and relationships to various formats.
|
||||||
|
Args:
|
||||||
|
output_path: The path to the output file (including extension).
|
||||||
|
file_format: Output format - "csv", "excel", "md", "txt".
|
||||||
|
- csv: Comma-separated values file
|
||||||
|
- excel: Microsoft Excel file with multiple sheets
|
||||||
|
- md: Markdown tables
|
||||||
|
- txt: Plain text formatted output
|
||||||
|
- table: Print formatted tables to console
|
||||||
|
include_vector_data: Whether to include data from the vector database.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
except RuntimeError:
|
||||||
|
loop = asyncio.new_event_loop()
|
||||||
|
asyncio.set_event_loop(loop)
|
||||||
|
|
||||||
|
loop.run_until_complete(
|
||||||
|
self.aexport_data(output_path, file_format, include_vector_data)
|
||||||
|
)
|
||||||
|
|
||||||
def merge_entities(
|
def merge_entities(
|
||||||
self,
|
self,
|
||||||
source_entities: list[str],
|
source_entities: list[str],
|
||||||
|
@@ -76,6 +76,7 @@ class LightragPathFilter(logging.Filter):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
# Define paths to be filtered
|
# Define paths to be filtered
|
||||||
self.filtered_paths = ["/documents", "/health", "/webui/"]
|
self.filtered_paths = ["/documents", "/health", "/webui/"]
|
||||||
|
# self.filtered_paths = ["/health", "/webui/"]
|
||||||
|
|
||||||
def filter(self, record):
|
def filter(self, record):
|
||||||
try:
|
try:
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
import { useState, useCallback } from 'react'
|
import { useState, useCallback } from 'react'
|
||||||
import ThemeProvider from '@/components/ThemeProvider'
|
import ThemeProvider from '@/components/ThemeProvider'
|
||||||
|
import TabVisibilityProvider from '@/contexts/TabVisibilityProvider'
|
||||||
import MessageAlert from '@/components/MessageAlert'
|
import MessageAlert from '@/components/MessageAlert'
|
||||||
import ApiKeyAlert from '@/components/ApiKeyAlert'
|
import ApiKeyAlert from '@/components/ApiKeyAlert'
|
||||||
import StatusIndicator from '@/components/graph/StatusIndicator'
|
import StatusIndicator from '@/components/graph/StatusIndicator'
|
||||||
@@ -21,7 +22,7 @@ import { Tabs, TabsContent } from '@/components/ui/Tabs'
|
|||||||
function App() {
|
function App() {
|
||||||
const message = useBackendState.use.message()
|
const message = useBackendState.use.message()
|
||||||
const enableHealthCheck = useSettingsStore.use.enableHealthCheck()
|
const enableHealthCheck = useSettingsStore.use.enableHealthCheck()
|
||||||
const [currentTab] = useState(() => useSettingsStore.getState().currentTab)
|
const currentTab = useSettingsStore.use.currentTab()
|
||||||
const [apiKeyInvalid, setApiKeyInvalid] = useState(false)
|
const [apiKeyInvalid, setApiKeyInvalid] = useState(false)
|
||||||
|
|
||||||
// Health check
|
// Health check
|
||||||
@@ -54,6 +55,7 @@ function App() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<ThemeProvider>
|
<ThemeProvider>
|
||||||
|
<TabVisibilityProvider>
|
||||||
<main className="flex h-screen w-screen overflow-x-hidden">
|
<main className="flex h-screen w-screen overflow-x-hidden">
|
||||||
<Tabs
|
<Tabs
|
||||||
defaultValue={currentTab}
|
defaultValue={currentTab}
|
||||||
@@ -81,6 +83,7 @@ function App() {
|
|||||||
{apiKeyInvalid && <ApiKeyAlert />}
|
{apiKeyInvalid && <ApiKeyAlert />}
|
||||||
<Toaster />
|
<Toaster />
|
||||||
</main>
|
</main>
|
||||||
|
</TabVisibilityProvider>
|
||||||
</ThemeProvider>
|
</ThemeProvider>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@@ -34,6 +34,9 @@ const GraphControl = ({ disableHoverEffect }: { disableHoverEffect?: boolean })
|
|||||||
|
|
||||||
const { theme } = useTheme()
|
const { theme } = useTheme()
|
||||||
const hideUnselectedEdges = useSettingsStore.use.enableHideUnselectedEdges()
|
const hideUnselectedEdges = useSettingsStore.use.enableHideUnselectedEdges()
|
||||||
|
const enableEdgeEvents = useSettingsStore.use.enableEdgeEvents()
|
||||||
|
const renderEdgeLabels = useSettingsStore.use.showEdgeLabel()
|
||||||
|
const renderLabels = useSettingsStore.use.showNodeLabel()
|
||||||
const selectedNode = useGraphStore.use.selectedNode()
|
const selectedNode = useGraphStore.use.selectedNode()
|
||||||
const focusedNode = useGraphStore.use.focusedNode()
|
const focusedNode = useGraphStore.use.focusedNode()
|
||||||
const selectedEdge = useGraphStore.use.selectedEdge()
|
const selectedEdge = useGraphStore.use.selectedEdge()
|
||||||
@@ -59,39 +62,52 @@ const GraphControl = ({ disableHoverEffect }: { disableHoverEffect?: boolean })
|
|||||||
const { setFocusedNode, setSelectedNode, setFocusedEdge, setSelectedEdge, clearSelection } =
|
const { setFocusedNode, setSelectedNode, setFocusedEdge, setSelectedEdge, clearSelection } =
|
||||||
useGraphStore.getState()
|
useGraphStore.getState()
|
||||||
|
|
||||||
// Register the events
|
// Define event types
|
||||||
registerEvents({
|
type NodeEvent = { node: string; event: { original: MouseEvent | TouchEvent } }
|
||||||
enterNode: (event) => {
|
type EdgeEvent = { edge: string; event: { original: MouseEvent | TouchEvent } }
|
||||||
|
|
||||||
|
// Register all events, but edge events will only be processed if enableEdgeEvents is true
|
||||||
|
const events: Record<string, any> = {
|
||||||
|
enterNode: (event: NodeEvent) => {
|
||||||
if (!isButtonPressed(event.event.original)) {
|
if (!isButtonPressed(event.event.original)) {
|
||||||
setFocusedNode(event.node)
|
setFocusedNode(event.node)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
leaveNode: (event) => {
|
leaveNode: (event: NodeEvent) => {
|
||||||
if (!isButtonPressed(event.event.original)) {
|
if (!isButtonPressed(event.event.original)) {
|
||||||
setFocusedNode(null)
|
setFocusedNode(null)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
clickNode: (event) => {
|
clickNode: (event: NodeEvent) => {
|
||||||
setSelectedNode(event.node)
|
setSelectedNode(event.node)
|
||||||
setSelectedEdge(null)
|
setSelectedEdge(null)
|
||||||
},
|
},
|
||||||
clickEdge: (event) => {
|
clickStage: () => clearSelection()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only add edge event handlers if enableEdgeEvents is true
|
||||||
|
if (enableEdgeEvents) {
|
||||||
|
events.clickEdge = (event: EdgeEvent) => {
|
||||||
setSelectedEdge(event.edge)
|
setSelectedEdge(event.edge)
|
||||||
setSelectedNode(null)
|
setSelectedNode(null)
|
||||||
},
|
}
|
||||||
enterEdge: (event) => {
|
|
||||||
|
events.enterEdge = (event: EdgeEvent) => {
|
||||||
if (!isButtonPressed(event.event.original)) {
|
if (!isButtonPressed(event.event.original)) {
|
||||||
setFocusedEdge(event.edge)
|
setFocusedEdge(event.edge)
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
leaveEdge: (event) => {
|
|
||||||
|
events.leaveEdge = (event: EdgeEvent) => {
|
||||||
if (!isButtonPressed(event.event.original)) {
|
if (!isButtonPressed(event.event.original)) {
|
||||||
setFocusedEdge(null)
|
setFocusedEdge(null)
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
clickStage: () => clearSelection()
|
}
|
||||||
})
|
|
||||||
}, [registerEvents])
|
// Register the events
|
||||||
|
registerEvents(events)
|
||||||
|
}, [registerEvents, enableEdgeEvents])
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When component mount or hovered node change
|
* When component mount or hovered node change
|
||||||
@@ -102,7 +118,14 @@ const GraphControl = ({ disableHoverEffect }: { disableHoverEffect?: boolean })
|
|||||||
const labelColor = isDarkTheme ? Constants.labelColorDarkTheme : undefined
|
const labelColor = isDarkTheme ? Constants.labelColorDarkTheme : undefined
|
||||||
const edgeColor = isDarkTheme ? Constants.edgeColorDarkTheme : undefined
|
const edgeColor = isDarkTheme ? Constants.edgeColorDarkTheme : undefined
|
||||||
|
|
||||||
|
// Update all dynamic settings directly without recreating the sigma container
|
||||||
setSettings({
|
setSettings({
|
||||||
|
// Update display settings
|
||||||
|
enableEdgeEvents,
|
||||||
|
renderEdgeLabels,
|
||||||
|
renderLabels,
|
||||||
|
|
||||||
|
// Node reducer for node appearance
|
||||||
nodeReducer: (node, data) => {
|
nodeReducer: (node, data) => {
|
||||||
const graph = sigma.getGraph()
|
const graph = sigma.getGraph()
|
||||||
const newData: NodeType & {
|
const newData: NodeType & {
|
||||||
@@ -141,6 +164,8 @@ const GraphControl = ({ disableHoverEffect }: { disableHoverEffect?: boolean })
|
|||||||
}
|
}
|
||||||
return newData
|
return newData
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Edge reducer for edge appearance
|
||||||
edgeReducer: (edge, data) => {
|
edgeReducer: (edge, data) => {
|
||||||
const graph = sigma.getGraph()
|
const graph = sigma.getGraph()
|
||||||
const newData = { ...data, hidden: false, labelColor, color: edgeColor }
|
const newData = { ...data, hidden: false, labelColor, color: edgeColor }
|
||||||
@@ -182,7 +207,10 @@ const GraphControl = ({ disableHoverEffect }: { disableHoverEffect?: boolean })
|
|||||||
sigma,
|
sigma,
|
||||||
disableHoverEffect,
|
disableHoverEffect,
|
||||||
theme,
|
theme,
|
||||||
hideUnselectedEdges
|
hideUnselectedEdges,
|
||||||
|
enableEdgeEvents,
|
||||||
|
renderEdgeLabels,
|
||||||
|
renderLabels
|
||||||
])
|
])
|
||||||
|
|
||||||
return null
|
return null
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { useCallback } from 'react'
|
import { useCallback, useEffect, useRef } from 'react'
|
||||||
import { AsyncSelect } from '@/components/ui/AsyncSelect'
|
import { AsyncSelect } from '@/components/ui/AsyncSelect'
|
||||||
import { useSettingsStore } from '@/stores/settings'
|
import { useSettingsStore } from '@/stores/settings'
|
||||||
import { useGraphStore } from '@/stores/graph'
|
import { useGraphStore } from '@/stores/graph'
|
||||||
@@ -10,6 +10,37 @@ const GraphLabels = () => {
|
|||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const label = useSettingsStore.use.queryLabel()
|
const label = useSettingsStore.use.queryLabel()
|
||||||
const allDatabaseLabels = useGraphStore.use.allDatabaseLabels()
|
const allDatabaseLabels = useGraphStore.use.allDatabaseLabels()
|
||||||
|
const labelsLoadedRef = useRef(false)
|
||||||
|
|
||||||
|
// Track if a fetch is in progress to prevent multiple simultaneous fetches
|
||||||
|
const fetchInProgressRef = useRef(false)
|
||||||
|
|
||||||
|
// Fetch labels once on component mount, using global flag to prevent duplicates
|
||||||
|
useEffect(() => {
|
||||||
|
// Check if we've already attempted to fetch labels in this session
|
||||||
|
const labelsFetchAttempted = useGraphStore.getState().labelsFetchAttempted
|
||||||
|
|
||||||
|
// Only fetch if we haven't attempted in this session and no fetch is in progress
|
||||||
|
if (!labelsFetchAttempted && !fetchInProgressRef.current) {
|
||||||
|
fetchInProgressRef.current = true
|
||||||
|
// Set global flag to indicate we've attempted to fetch in this session
|
||||||
|
useGraphStore.getState().setLabelsFetchAttempted(true)
|
||||||
|
|
||||||
|
console.log('Fetching graph labels (once per session)...')
|
||||||
|
|
||||||
|
useGraphStore.getState().fetchAllDatabaseLabels()
|
||||||
|
.then(() => {
|
||||||
|
labelsLoadedRef.current = true
|
||||||
|
fetchInProgressRef.current = false
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error('Failed to fetch labels:', error)
|
||||||
|
fetchInProgressRef.current = false
|
||||||
|
// Reset global flag to allow retry
|
||||||
|
useGraphStore.getState().setLabelsFetchAttempted(false)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, []) // Empty dependency array ensures this only runs once on mount
|
||||||
|
|
||||||
const getSearchEngine = useCallback(() => {
|
const getSearchEngine = useCallback(() => {
|
||||||
// Create search engine
|
// Create search engine
|
||||||
@@ -69,11 +100,28 @@ const GraphLabels = () => {
|
|||||||
onChange={(newLabel) => {
|
onChange={(newLabel) => {
|
||||||
const currentLabel = useSettingsStore.getState().queryLabel
|
const currentLabel = useSettingsStore.getState().queryLabel
|
||||||
|
|
||||||
|
// select the last item means query all
|
||||||
if (newLabel === '...') {
|
if (newLabel === '...') {
|
||||||
newLabel = '*'
|
newLabel = '*'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset the fetch attempted flag to force a new data fetch
|
||||||
|
useGraphStore.getState().setGraphDataFetchAttempted(false)
|
||||||
|
|
||||||
|
// Clear current graph data to ensure complete reload when label changes
|
||||||
|
if (newLabel !== currentLabel) {
|
||||||
|
const graphStore = useGraphStore.getState();
|
||||||
|
graphStore.clearSelection();
|
||||||
|
|
||||||
|
// Reset the graph state but preserve the instance
|
||||||
|
if (graphStore.sigmaGraph) {
|
||||||
|
const nodes = Array.from(graphStore.sigmaGraph.nodes());
|
||||||
|
nodes.forEach(node => graphStore.sigmaGraph?.dropNode(node));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (newLabel === currentLabel && newLabel !== '*') {
|
if (newLabel === currentLabel && newLabel !== '*') {
|
||||||
// 选择相同标签时切换到'*'
|
// reselect the same itme means qery all
|
||||||
useSettingsStore.getState().setQueryLabel('*')
|
useSettingsStore.getState().setQueryLabel('*')
|
||||||
} else {
|
} else {
|
||||||
useSettingsStore.getState().setQueryLabel(newLabel)
|
useSettingsStore.getState().setQueryLabel(newLabel)
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { FC, useCallback, useMemo } from 'react'
|
import { FC, useCallback, useEffect, useMemo } from 'react'
|
||||||
import {
|
import {
|
||||||
EdgeById,
|
EdgeById,
|
||||||
NodeById,
|
NodeById,
|
||||||
@@ -28,6 +28,7 @@ function OptionComponent(item: OptionItem) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const messageId = '__message_item'
|
const messageId = '__message_item'
|
||||||
|
// Reset this cache when graph changes to ensure fresh search results
|
||||||
const lastGraph: any = {
|
const lastGraph: any = {
|
||||||
graph: null,
|
graph: null,
|
||||||
searchEngine: null
|
searchEngine: null
|
||||||
@@ -48,6 +49,15 @@ export const GraphSearchInput = ({
|
|||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const graph = useGraphStore.use.sigmaGraph()
|
const graph = useGraphStore.use.sigmaGraph()
|
||||||
|
|
||||||
|
// Force reset the cache when graph changes
|
||||||
|
useEffect(() => {
|
||||||
|
if (graph) {
|
||||||
|
// Reset cache to ensure fresh search results with new graph data
|
||||||
|
lastGraph.graph = null;
|
||||||
|
lastGraph.searchEngine = null;
|
||||||
|
}
|
||||||
|
}, [graph]);
|
||||||
|
|
||||||
const searchEngine = useMemo(() => {
|
const searchEngine = useMemo(() => {
|
||||||
if (lastGraph.graph == graph) {
|
if (lastGraph.graph == graph) {
|
||||||
return lastGraph.searchEngine
|
return lastGraph.searchEngine
|
||||||
|
@@ -132,11 +132,19 @@ const PropertyRow = ({
|
|||||||
onClick?: () => void
|
onClick?: () => void
|
||||||
tooltip?: string
|
tooltip?: string
|
||||||
}) => {
|
}) => {
|
||||||
|
const { t } = useTranslation()
|
||||||
|
|
||||||
|
const getPropertyNameTranslation = (name: string) => {
|
||||||
|
const translationKey = `graphPanel.propertiesView.node.propertyNames.${name}`
|
||||||
|
const translation = t(translationKey)
|
||||||
|
return translation === translationKey ? name : translation
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<label className="text-primary/60 tracking-wide">{name}</label>:
|
<label className="text-primary/60 tracking-wide whitespace-nowrap">{getPropertyNameTranslation(name)}</label>:
|
||||||
<Text
|
<Text
|
||||||
className="hover:bg-primary/20 rounded p-1 text-ellipsis"
|
className="hover:bg-primary/20 rounded p-1 overflow-hidden text-ellipsis"
|
||||||
tooltipClassName="max-w-80"
|
tooltipClassName="max-w-80"
|
||||||
text={value}
|
text={value}
|
||||||
tooltip={tooltip || (typeof value === 'string' ? value : JSON.stringify(value, null, 2))}
|
tooltip={tooltip || (typeof value === 'string' ? value : JSON.stringify(value, null, 2))}
|
||||||
|
@@ -25,7 +25,7 @@ export default function QuerySettings() {
|
|||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card className="flex shrink-0 flex-col">
|
<Card className="flex shrink-0 flex-col min-w-[180px]">
|
||||||
<CardHeader className="px-4 pt-4 pb-2">
|
<CardHeader className="px-4 pt-4 pb-2">
|
||||||
<CardTitle>{t('retrievePanel.querySettings.parametersTitle')}</CardTitle>
|
<CardTitle>{t('retrievePanel.querySettings.parametersTitle')}</CardTitle>
|
||||||
<CardDescription>{t('retrievePanel.querySettings.parametersDescription')}</CardDescription>
|
<CardDescription>{t('retrievePanel.querySettings.parametersDescription')}</CardDescription>
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { useState, useEffect, useCallback } from 'react'
|
import React, { useState, useEffect, useCallback } from 'react'
|
||||||
import { Loader2 } from 'lucide-react'
|
import { Loader2 } from 'lucide-react'
|
||||||
import { useDebounce } from '@/hooks/useDebounce'
|
import { useDebounce } from '@/hooks/useDebounce'
|
||||||
|
|
||||||
@@ -204,7 +204,7 @@ export function AsyncSearch<T>({
|
|||||||
))}
|
))}
|
||||||
<CommandGroup>
|
<CommandGroup>
|
||||||
{options.map((option, idx) => (
|
{options.map((option, idx) => (
|
||||||
<>
|
<React.Fragment key={getOptionValue(option) + `-fragment-${idx}`}>
|
||||||
<CommandItem
|
<CommandItem
|
||||||
key={getOptionValue(option) + `${idx}`}
|
key={getOptionValue(option) + `${idx}`}
|
||||||
value={getOptionValue(option)}
|
value={getOptionValue(option)}
|
||||||
@@ -215,9 +215,9 @@ export function AsyncSearch<T>({
|
|||||||
{renderOption(option)}
|
{renderOption(option)}
|
||||||
</CommandItem>
|
</CommandItem>
|
||||||
{idx !== options.length - 1 && (
|
{idx !== options.length - 1 && (
|
||||||
<div key={idx} className="bg-foreground/10 h-[1px]" />
|
<div key={`divider-${idx}`} className="bg-foreground/10 h-[1px]" />
|
||||||
)}
|
)}
|
||||||
</>
|
</React.Fragment>
|
||||||
))}
|
))}
|
||||||
</CommandGroup>
|
</CommandGroup>
|
||||||
</CommandList>
|
</CommandList>
|
||||||
|
37
lightrag_webui/src/components/ui/TabContent.tsx
Normal file
37
lightrag_webui/src/components/ui/TabContent.tsx
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import React, { useEffect } from 'react';
|
||||||
|
import { useTabVisibility } from '@/contexts/useTabVisibility';
|
||||||
|
|
||||||
|
interface TabContentProps {
|
||||||
|
tabId: string;
|
||||||
|
children: React.ReactNode;
|
||||||
|
className?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TabContent component that manages visibility based on tab selection
|
||||||
|
* Works with the TabVisibilityContext to show/hide content based on active tab
|
||||||
|
*/
|
||||||
|
const TabContent: React.FC<TabContentProps> = ({ tabId, children, className = '' }) => {
|
||||||
|
const { isTabVisible, setTabVisibility } = useTabVisibility();
|
||||||
|
const isVisible = isTabVisible(tabId);
|
||||||
|
|
||||||
|
// Register this tab with the context when mounted
|
||||||
|
useEffect(() => {
|
||||||
|
setTabVisibility(tabId, true);
|
||||||
|
|
||||||
|
// Cleanup when unmounted
|
||||||
|
return () => {
|
||||||
|
setTabVisibility(tabId, false);
|
||||||
|
};
|
||||||
|
}, [tabId, setTabVisibility]);
|
||||||
|
|
||||||
|
// Use CSS to hide content instead of not rendering it
|
||||||
|
// This prevents components from unmounting when tabs are switched
|
||||||
|
return (
|
||||||
|
<div className={`${className} ${isVisible ? '' : 'hidden'}`}>
|
||||||
|
{children}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default TabContent;
|
@@ -42,9 +42,13 @@ const TabsContent = React.forwardRef<
|
|||||||
<TabsPrimitive.Content
|
<TabsPrimitive.Content
|
||||||
ref={ref}
|
ref={ref}
|
||||||
className={cn(
|
className={cn(
|
||||||
'ring-offset-background focus-visible:ring-ring mt-2 focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-none',
|
'ring-offset-background focus-visible:ring-ring focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-none',
|
||||||
|
'data-[state=inactive]:invisible data-[state=active]:visible',
|
||||||
|
'h-full w-full',
|
||||||
className
|
className
|
||||||
)}
|
)}
|
||||||
|
// Force mounting of inactive tabs to preserve WebGL contexts
|
||||||
|
forceMount
|
||||||
{...props}
|
{...props}
|
||||||
/>
|
/>
|
||||||
))
|
))
|
||||||
|
53
lightrag_webui/src/contexts/TabVisibilityProvider.tsx
Normal file
53
lightrag_webui/src/contexts/TabVisibilityProvider.tsx
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import React, { useState, useEffect, useMemo } from 'react';
|
||||||
|
import { TabVisibilityContext } from './context';
|
||||||
|
import { TabVisibilityContextType } from './types';
|
||||||
|
import { useSettingsStore } from '@/stores/settings';
|
||||||
|
|
||||||
|
interface TabVisibilityProviderProps {
|
||||||
|
children: React.ReactNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provider component for the TabVisibility context
|
||||||
|
* Manages the visibility state of tabs throughout the application
|
||||||
|
*/
|
||||||
|
export const TabVisibilityProvider: React.FC<TabVisibilityProviderProps> = ({ children }) => {
|
||||||
|
// Get current tab from settings store
|
||||||
|
const currentTab = useSettingsStore.use.currentTab();
|
||||||
|
|
||||||
|
// Initialize visibility state with current tab as visible
|
||||||
|
const [visibleTabs, setVisibleTabs] = useState<Record<string, boolean>>(() => ({
|
||||||
|
[currentTab]: true
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Update visibility when current tab changes
|
||||||
|
useEffect(() => {
|
||||||
|
setVisibleTabs((prev) => ({
|
||||||
|
...prev,
|
||||||
|
[currentTab]: true
|
||||||
|
}));
|
||||||
|
}, [currentTab]);
|
||||||
|
|
||||||
|
// Create the context value with memoization to prevent unnecessary re-renders
|
||||||
|
const contextValue = useMemo<TabVisibilityContextType>(
|
||||||
|
() => ({
|
||||||
|
visibleTabs,
|
||||||
|
setTabVisibility: (tabId: string, isVisible: boolean) => {
|
||||||
|
setVisibleTabs((prev) => ({
|
||||||
|
...prev,
|
||||||
|
[tabId]: isVisible,
|
||||||
|
}));
|
||||||
|
},
|
||||||
|
isTabVisible: (tabId: string) => !!visibleTabs[tabId],
|
||||||
|
}),
|
||||||
|
[visibleTabs]
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<TabVisibilityContext.Provider value={contextValue}>
|
||||||
|
{children}
|
||||||
|
</TabVisibilityContext.Provider>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default TabVisibilityProvider;
|
12
lightrag_webui/src/contexts/context.ts
Normal file
12
lightrag_webui/src/contexts/context.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import { createContext } from 'react';
|
||||||
|
import { TabVisibilityContextType } from './types';
|
||||||
|
|
||||||
|
// Default context value
|
||||||
|
const defaultContext: TabVisibilityContextType = {
|
||||||
|
visibleTabs: {},
|
||||||
|
setTabVisibility: () => {},
|
||||||
|
isTabVisible: () => false,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create the context
|
||||||
|
export const TabVisibilityContext = createContext<TabVisibilityContextType>(defaultContext);
|
5
lightrag_webui/src/contexts/types.ts
Normal file
5
lightrag_webui/src/contexts/types.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
export interface TabVisibilityContextType {
|
||||||
|
visibleTabs: Record<string, boolean>;
|
||||||
|
setTabVisibility: (tabId: string, isVisible: boolean) => void;
|
||||||
|
isTabVisible: (tabId: string) => boolean;
|
||||||
|
}
|
17
lightrag_webui/src/contexts/useTabVisibility.ts
Normal file
17
lightrag_webui/src/contexts/useTabVisibility.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import { useContext } from 'react';
|
||||||
|
import { TabVisibilityContext } from './context';
|
||||||
|
import { TabVisibilityContextType } from './types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom hook to access the tab visibility context
|
||||||
|
* @returns The tab visibility context
|
||||||
|
*/
|
||||||
|
export const useTabVisibility = (): TabVisibilityContextType => {
|
||||||
|
const context = useContext(TabVisibilityContext);
|
||||||
|
|
||||||
|
if (!context) {
|
||||||
|
throw new Error('useTabVisibility must be used within a TabVisibilityProvider');
|
||||||
|
}
|
||||||
|
|
||||||
|
return context;
|
||||||
|
};
|
@@ -1,5 +1,40 @@
|
|||||||
|
import { useState, useEffect } from 'react'
|
||||||
|
import { useTabVisibility } from '@/contexts/useTabVisibility'
|
||||||
import { backendBaseUrl } from '@/lib/constants'
|
import { backendBaseUrl } from '@/lib/constants'
|
||||||
|
import { useTranslation } from 'react-i18next'
|
||||||
|
|
||||||
export default function ApiSite() {
|
export default function ApiSite() {
|
||||||
return <iframe src={backendBaseUrl + '/docs'} className="size-full" />
|
const { t } = useTranslation()
|
||||||
|
const { isTabVisible } = useTabVisibility()
|
||||||
|
const isApiTabVisible = isTabVisible('api')
|
||||||
|
const [iframeLoaded, setIframeLoaded] = useState(false)
|
||||||
|
|
||||||
|
// Load the iframe once on component mount
|
||||||
|
useEffect(() => {
|
||||||
|
if (!iframeLoaded) {
|
||||||
|
setIframeLoaded(true)
|
||||||
|
}
|
||||||
|
}, [iframeLoaded])
|
||||||
|
|
||||||
|
// Use CSS to hide content when tab is not visible
|
||||||
|
return (
|
||||||
|
<div className={`size-full ${isApiTabVisible ? '' : 'hidden'}`}>
|
||||||
|
{iframeLoaded ? (
|
||||||
|
<iframe
|
||||||
|
src={backendBaseUrl + '/docs'}
|
||||||
|
className="size-full w-full h-full"
|
||||||
|
style={{ width: '100%', height: '100%', border: 'none' }}
|
||||||
|
// Use key to ensure iframe doesn't reload
|
||||||
|
key="api-docs-iframe"
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<div className="flex h-full w-full items-center justify-center bg-background">
|
||||||
|
<div className="text-center">
|
||||||
|
<div className="mb-2 h-8 w-8 animate-spin rounded-full border-4 border-primary border-t-transparent"></div>
|
||||||
|
<p>{t('apiSite.loading')}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
import { useState, useEffect, useCallback } from 'react'
|
import { useState, useEffect, useCallback, useRef } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
|
import { useTabVisibility } from '@/contexts/useTabVisibility'
|
||||||
import Button from '@/components/ui/Button'
|
import Button from '@/components/ui/Button'
|
||||||
import {
|
import {
|
||||||
Table,
|
Table,
|
||||||
@@ -26,6 +27,9 @@ export default function DocumentManager() {
|
|||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const health = useBackendState.use.health()
|
const health = useBackendState.use.health()
|
||||||
const [docs, setDocs] = useState<DocsStatusesResponse | null>(null)
|
const [docs, setDocs] = useState<DocsStatusesResponse | null>(null)
|
||||||
|
const { isTabVisible } = useTabVisibility()
|
||||||
|
const isDocumentsTabVisible = isTabVisible('documents')
|
||||||
|
const initialLoadRef = useRef(false)
|
||||||
|
|
||||||
const fetchDocuments = useCallback(async () => {
|
const fetchDocuments = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
@@ -48,11 +52,15 @@ export default function DocumentManager() {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
toast.error(t('documentPanel.documentManager.errors.loadFailed', { error: errorMessage(err) }))
|
toast.error(t('documentPanel.documentManager.errors.loadFailed', { error: errorMessage(err) }))
|
||||||
}
|
}
|
||||||
}, [setDocs])
|
}, [setDocs, t])
|
||||||
|
|
||||||
|
// Only fetch documents when the tab becomes visible for the first time
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (isDocumentsTabVisible && !initialLoadRef.current) {
|
||||||
fetchDocuments()
|
fetchDocuments()
|
||||||
}, []) // eslint-disable-line react-hooks/exhaustive-deps
|
initialLoadRef.current = true
|
||||||
|
}
|
||||||
|
}, [isDocumentsTabVisible, fetchDocuments])
|
||||||
|
|
||||||
const scanDocuments = useCallback(async () => {
|
const scanDocuments = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
@@ -61,21 +69,24 @@ export default function DocumentManager() {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
toast.error(t('documentPanel.documentManager.errors.scanFailed', { error: errorMessage(err) }))
|
toast.error(t('documentPanel.documentManager.errors.scanFailed', { error: errorMessage(err) }))
|
||||||
}
|
}
|
||||||
}, [])
|
}, [t])
|
||||||
|
|
||||||
|
// Only set up polling when the tab is visible and health is good
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const interval = setInterval(async () => {
|
if (!isDocumentsTabVisible || !health) {
|
||||||
if (!health) {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const interval = setInterval(async () => {
|
||||||
try {
|
try {
|
||||||
await fetchDocuments()
|
await fetchDocuments()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
toast.error(t('documentPanel.documentManager.errors.scanProgressFailed', { error: errorMessage(err) }))
|
toast.error(t('documentPanel.documentManager.errors.scanProgressFailed', { error: errorMessage(err) }))
|
||||||
}
|
}
|
||||||
}, 5000)
|
}, 5000)
|
||||||
|
|
||||||
return () => clearInterval(interval)
|
return () => clearInterval(interval)
|
||||||
}, [health, fetchDocuments])
|
}, [health, fetchDocuments, t, isDocumentsTabVisible])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card className="!size-full !rounded-none !border-none">
|
<Card className="!size-full !rounded-none !border-none">
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
import { useEffect, useState, useCallback, useMemo, useRef } from 'react'
|
import { useEffect, useState, useCallback, useMemo, useRef } from 'react'
|
||||||
|
import { useTabVisibility } from '@/contexts/useTabVisibility'
|
||||||
// import { MiniMap } from '@react-sigma/minimap'
|
// import { MiniMap } from '@react-sigma/minimap'
|
||||||
import { SigmaContainer, useRegisterEvents, useSigma } from '@react-sigma/core'
|
import { SigmaContainer, useRegisterEvents, useSigma } from '@react-sigma/core'
|
||||||
import { Settings as SigmaSettings } from 'sigma/settings'
|
import { Settings as SigmaSettings } from 'sigma/settings'
|
||||||
@@ -107,27 +108,45 @@ const GraphEvents = () => {
|
|||||||
const GraphViewer = () => {
|
const GraphViewer = () => {
|
||||||
const [sigmaSettings, setSigmaSettings] = useState(defaultSigmaSettings)
|
const [sigmaSettings, setSigmaSettings] = useState(defaultSigmaSettings)
|
||||||
const sigmaRef = useRef<any>(null)
|
const sigmaRef = useRef<any>(null)
|
||||||
|
const initAttemptedRef = useRef(false)
|
||||||
|
|
||||||
const selectedNode = useGraphStore.use.selectedNode()
|
const selectedNode = useGraphStore.use.selectedNode()
|
||||||
const focusedNode = useGraphStore.use.focusedNode()
|
const focusedNode = useGraphStore.use.focusedNode()
|
||||||
const moveToSelectedNode = useGraphStore.use.moveToSelectedNode()
|
const moveToSelectedNode = useGraphStore.use.moveToSelectedNode()
|
||||||
|
const isFetching = useGraphStore.use.isFetching()
|
||||||
|
const shouldRender = useGraphStore.use.shouldRender() // Rendering control state
|
||||||
|
|
||||||
|
// Get tab visibility
|
||||||
|
const { isTabVisible } = useTabVisibility()
|
||||||
|
const isGraphTabVisible = isTabVisible('knowledge-graph')
|
||||||
|
|
||||||
const showPropertyPanel = useSettingsStore.use.showPropertyPanel()
|
const showPropertyPanel = useSettingsStore.use.showPropertyPanel()
|
||||||
const showNodeSearchBar = useSettingsStore.use.showNodeSearchBar()
|
const showNodeSearchBar = useSettingsStore.use.showNodeSearchBar()
|
||||||
const renderLabels = useSettingsStore.use.showNodeLabel()
|
|
||||||
|
|
||||||
const enableEdgeEvents = useSettingsStore.use.enableEdgeEvents()
|
|
||||||
const enableNodeDrag = useSettingsStore.use.enableNodeDrag()
|
const enableNodeDrag = useSettingsStore.use.enableNodeDrag()
|
||||||
const renderEdgeLabels = useSettingsStore.use.showEdgeLabel()
|
|
||||||
|
|
||||||
|
// Handle component mount/unmount and tab visibility
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setSigmaSettings({
|
// When component mounts or tab becomes visible
|
||||||
...defaultSigmaSettings,
|
if (isGraphTabVisible && !shouldRender && !isFetching && !initAttemptedRef.current) {
|
||||||
enableEdgeEvents,
|
// If tab is visible but graph is not rendering, try to enable rendering
|
||||||
renderEdgeLabels,
|
useGraphStore.getState().setShouldRender(true)
|
||||||
renderLabels
|
initAttemptedRef.current = true
|
||||||
})
|
console.log('Graph viewer initialized')
|
||||||
}, [renderLabels, enableEdgeEvents, renderEdgeLabels])
|
}
|
||||||
|
|
||||||
|
// Cleanup function when component unmounts
|
||||||
|
return () => {
|
||||||
|
// Only log cleanup, don't actually clean up the WebGL context
|
||||||
|
// This allows the WebGL context to persist across tab switches
|
||||||
|
console.log('Graph viewer cleanup')
|
||||||
|
}
|
||||||
|
}, [isGraphTabVisible, shouldRender, isFetching])
|
||||||
|
|
||||||
|
// Initialize sigma settings once on component mount
|
||||||
|
// All dynamic settings will be updated in GraphControl using useSetSettings
|
||||||
|
useEffect(() => {
|
||||||
|
setSigmaSettings(defaultSigmaSettings)
|
||||||
|
}, [])
|
||||||
|
|
||||||
const onSearchFocus = useCallback((value: GraphSearchOption | null) => {
|
const onSearchFocus = useCallback((value: GraphSearchOption | null) => {
|
||||||
if (value === null) useGraphStore.getState().setFocusedNode(null)
|
if (value === null) useGraphStore.getState().setFocusedNode(null)
|
||||||
@@ -148,7 +167,12 @@ const GraphViewer = () => {
|
|||||||
[selectedNode]
|
[selectedNode]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Since TabsContent now forces mounting of all tabs, we need to conditionally render
|
||||||
|
// the SigmaContainer based on visibility to avoid unnecessary rendering
|
||||||
return (
|
return (
|
||||||
|
<div className="relative h-full w-full">
|
||||||
|
{/* Only render the SigmaContainer when the tab is visible */}
|
||||||
|
{isGraphTabVisible ? (
|
||||||
<SigmaContainer
|
<SigmaContainer
|
||||||
settings={sigmaSettings}
|
settings={sigmaSettings}
|
||||||
className="!bg-background !size-full overflow-hidden"
|
className="!bg-background !size-full overflow-hidden"
|
||||||
@@ -191,6 +215,25 @@ const GraphViewer = () => {
|
|||||||
|
|
||||||
<SettingsDisplay />
|
<SettingsDisplay />
|
||||||
</SigmaContainer>
|
</SigmaContainer>
|
||||||
|
) : (
|
||||||
|
// Placeholder when tab is not visible
|
||||||
|
<div className="flex h-full w-full items-center justify-center">
|
||||||
|
<div className="text-center text-muted-foreground">
|
||||||
|
{/* Placeholder content */}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Loading overlay - shown when data is loading */}
|
||||||
|
{isFetching && (
|
||||||
|
<div className="absolute inset-0 flex items-center justify-center bg-background/80 z-10">
|
||||||
|
<div className="text-center">
|
||||||
|
<div className="mb-2 h-8 w-8 animate-spin rounded-full border-4 border-primary border-t-transparent"></div>
|
||||||
|
<p>Loading Graph Data...</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -6,6 +6,7 @@ import { useGraphStore, RawGraph } from '@/stores/graph'
|
|||||||
import { queryGraphs } from '@/api/lightrag'
|
import { queryGraphs } from '@/api/lightrag'
|
||||||
import { useBackendState } from '@/stores/state'
|
import { useBackendState } from '@/stores/state'
|
||||||
import { useSettingsStore } from '@/stores/settings'
|
import { useSettingsStore } from '@/stores/settings'
|
||||||
|
import { useTabVisibility } from '@/contexts/useTabVisibility'
|
||||||
|
|
||||||
import seedrandom from 'seedrandom'
|
import seedrandom from 'seedrandom'
|
||||||
|
|
||||||
@@ -136,15 +137,23 @@ const fetchGraph = async (label: string, maxDepth: number, minDegree: number) =>
|
|||||||
return rawGraph
|
return rawGraph
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a new graph instance with the raw graph data
|
||||||
const createSigmaGraph = (rawGraph: RawGraph | null) => {
|
const createSigmaGraph = (rawGraph: RawGraph | null) => {
|
||||||
|
// Always create a new graph instance
|
||||||
const graph = new DirectedGraph()
|
const graph = new DirectedGraph()
|
||||||
|
|
||||||
|
// Add nodes from raw graph data
|
||||||
for (const rawNode of rawGraph?.nodes ?? []) {
|
for (const rawNode of rawGraph?.nodes ?? []) {
|
||||||
|
// Ensure we have fresh random positions for nodes
|
||||||
|
seedrandom(rawNode.id + Date.now().toString(), { global: true })
|
||||||
|
const x = Math.random()
|
||||||
|
const y = Math.random()
|
||||||
|
|
||||||
graph.addNode(rawNode.id, {
|
graph.addNode(rawNode.id, {
|
||||||
label: rawNode.labels.join(', '),
|
label: rawNode.labels.join(', '),
|
||||||
color: rawNode.color,
|
color: rawNode.color,
|
||||||
x: rawNode.x,
|
x: x,
|
||||||
y: rawNode.y,
|
y: y,
|
||||||
size: rawNode.size,
|
size: rawNode.size,
|
||||||
// for node-border
|
// for node-border
|
||||||
borderColor: Constants.nodeBorderColor,
|
borderColor: Constants.nodeBorderColor,
|
||||||
@@ -152,6 +161,7 @@ const createSigmaGraph = (rawGraph: RawGraph | null) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add edges from raw graph data
|
||||||
for (const rawEdge of rawGraph?.edges ?? []) {
|
for (const rawEdge of rawGraph?.edges ?? []) {
|
||||||
rawEdge.dynamicId = graph.addDirectedEdge(rawEdge.source, rawEdge.target, {
|
rawEdge.dynamicId = graph.addDirectedEdge(rawEdge.source, rawEdge.target, {
|
||||||
label: rawEdge.type || undefined
|
label: rawEdge.type || undefined
|
||||||
@@ -169,30 +179,22 @@ const useLightrangeGraph = () => {
|
|||||||
const minDegree = useSettingsStore.use.graphMinDegree()
|
const minDegree = useSettingsStore.use.graphMinDegree()
|
||||||
const isFetching = useGraphStore.use.isFetching()
|
const isFetching = useGraphStore.use.isFetching()
|
||||||
|
|
||||||
// Fetch all database labels on mount
|
// Get tab visibility
|
||||||
useEffect(() => {
|
const { isTabVisible } = useTabVisibility()
|
||||||
useGraphStore.getState().fetchAllDatabaseLabels()
|
const isGraphTabVisible = isTabVisible('knowledge-graph')
|
||||||
}, [])
|
|
||||||
|
|
||||||
// Use ref to track fetch status
|
|
||||||
const fetchStatusRef = useRef<Record<string, boolean>>({});
|
|
||||||
|
|
||||||
// Track previous parameters to detect actual changes
|
// Track previous parameters to detect actual changes
|
||||||
const prevParamsRef = useRef({ queryLabel, maxQueryDepth, minDegree });
|
const prevParamsRef = useRef({ queryLabel, maxQueryDepth, minDegree })
|
||||||
|
|
||||||
// Reset fetch status only when parameters actually change
|
// Use ref to track if data has been loaded and initial load
|
||||||
useEffect(() => {
|
const dataLoadedRef = useRef(false)
|
||||||
const prevParams = prevParamsRef.current;
|
const initialLoadRef = useRef(false)
|
||||||
if (prevParams.queryLabel !== queryLabel ||
|
|
||||||
prevParams.maxQueryDepth !== maxQueryDepth ||
|
// Check if parameters have changed
|
||||||
prevParams.minDegree !== minDegree) {
|
const paramsChanged =
|
||||||
useGraphStore.getState().setIsFetching(false);
|
prevParamsRef.current.queryLabel !== queryLabel ||
|
||||||
// Reset fetch status for new parameters
|
prevParamsRef.current.maxQueryDepth !== maxQueryDepth ||
|
||||||
fetchStatusRef.current = {};
|
prevParamsRef.current.minDegree !== minDegree
|
||||||
// Update previous parameters
|
|
||||||
prevParamsRef.current = { queryLabel, maxQueryDepth, minDegree };
|
|
||||||
}
|
|
||||||
}, [queryLabel, maxQueryDepth, minDegree, isFetching])
|
|
||||||
|
|
||||||
const getNode = useCallback(
|
const getNode = useCallback(
|
||||||
(nodeId: string) => {
|
(nodeId: string) => {
|
||||||
@@ -208,81 +210,131 @@ const useLightrangeGraph = () => {
|
|||||||
[rawGraph]
|
[rawGraph]
|
||||||
)
|
)
|
||||||
|
|
||||||
useEffect(() => {
|
// Track if a fetch is in progress to prevent multiple simultaneous fetches
|
||||||
if (queryLabel) {
|
const fetchInProgressRef = useRef(false)
|
||||||
const fetchKey = `${queryLabel}-${maxQueryDepth}-${minDegree}`;
|
|
||||||
|
|
||||||
// Only fetch if we haven't fetched this combination in the current component lifecycle
|
// Data fetching logic - simplified but preserving TAB visibility check
|
||||||
if (!isFetching && !fetchStatusRef.current[fetchKey]) {
|
useEffect(() => {
|
||||||
const state = useGraphStore.getState();
|
// Skip if fetch is already in progress
|
||||||
// Clear selection and highlighted nodes before fetching new graph
|
if (fetchInProgressRef.current) {
|
||||||
state.clearSelection();
|
return
|
||||||
if (state.sigmaGraph) {
|
|
||||||
state.sigmaGraph.forEachNode((node) => {
|
|
||||||
state.sigmaGraph?.setNodeAttribute(node, 'highlighted', false);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
state.setIsFetching(true);
|
// If there's no query label, reset the graph
|
||||||
fetchStatusRef.current[fetchKey] = true;
|
if (!queryLabel) {
|
||||||
fetchGraph(queryLabel, maxQueryDepth, minDegree).then((data) => {
|
if (rawGraph !== null || sigmaGraph !== null) {
|
||||||
const state = useGraphStore.getState()
|
const state = useGraphStore.getState()
|
||||||
|
state.reset()
|
||||||
|
state.setGraphDataFetchAttempted(false)
|
||||||
|
state.setLabelsFetchAttempted(false)
|
||||||
|
}
|
||||||
|
dataLoadedRef.current = false
|
||||||
|
initialLoadRef.current = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if parameters have changed
|
||||||
|
if (!isFetching && !fetchInProgressRef.current &&
|
||||||
|
(paramsChanged || !useGraphStore.getState().graphDataFetchAttempted)) {
|
||||||
|
|
||||||
|
// Only fetch data if the Graph tab is visible
|
||||||
|
if (!isGraphTabVisible) {
|
||||||
|
console.log('Graph tab not visible, skipping data fetch');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set flags
|
||||||
|
fetchInProgressRef.current = true
|
||||||
|
useGraphStore.getState().setGraphDataFetchAttempted(true)
|
||||||
|
|
||||||
|
const state = useGraphStore.getState()
|
||||||
|
state.setIsFetching(true)
|
||||||
|
state.setShouldRender(false) // Disable rendering during data loading
|
||||||
|
|
||||||
|
// Clear selection and highlighted nodes before fetching new graph
|
||||||
|
state.clearSelection()
|
||||||
|
if (state.sigmaGraph) {
|
||||||
|
state.sigmaGraph.forEachNode((node) => {
|
||||||
|
state.sigmaGraph?.setNodeAttribute(node, 'highlighted', false)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update parameter reference
|
||||||
|
prevParamsRef.current = { queryLabel, maxQueryDepth, minDegree }
|
||||||
|
|
||||||
|
console.log('Fetching graph data...')
|
||||||
|
|
||||||
|
// Use a local copy of the parameters
|
||||||
|
const currentQueryLabel = queryLabel
|
||||||
|
const currentMaxQueryDepth = maxQueryDepth
|
||||||
|
const currentMinDegree = minDegree
|
||||||
|
|
||||||
|
// Fetch graph data
|
||||||
|
fetchGraph(currentQueryLabel, currentMaxQueryDepth, currentMinDegree).then((data) => {
|
||||||
|
const state = useGraphStore.getState()
|
||||||
|
|
||||||
|
// Reset state
|
||||||
|
state.reset()
|
||||||
|
|
||||||
|
// Create and set new graph directly
|
||||||
const newSigmaGraph = createSigmaGraph(data)
|
const newSigmaGraph = createSigmaGraph(data)
|
||||||
data?.buildDynamicMap()
|
data?.buildDynamicMap()
|
||||||
|
|
||||||
// Update all graph data at once to minimize UI flicker
|
// Set new graph data
|
||||||
state.clearSelection()
|
|
||||||
state.setMoveToSelectedNode(false)
|
|
||||||
state.setSigmaGraph(newSigmaGraph)
|
state.setSigmaGraph(newSigmaGraph)
|
||||||
state.setRawGraph(data)
|
state.setRawGraph(data)
|
||||||
|
|
||||||
// Extract labels from current graph data
|
// No longer need to extract labels from graph data
|
||||||
if (data) {
|
|
||||||
const labelSet = new Set<string>();
|
|
||||||
for (const node of data.nodes) {
|
|
||||||
if (node.labels && Array.isArray(node.labels)) {
|
|
||||||
for (const label of node.labels) {
|
|
||||||
if (label !== '*') { // filter out label "*"
|
|
||||||
labelSet.add(label);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Put * on top of other labels
|
|
||||||
const sortedLabels = Array.from(labelSet).sort();
|
|
||||||
state.setGraphLabels(['*', ...sortedLabels]);
|
|
||||||
} else {
|
|
||||||
// Ensure * is there eventhough there is no graph data
|
|
||||||
state.setGraphLabels(['*']);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch all database labels after graph update
|
// Update flags
|
||||||
state.fetchAllDatabaseLabels();
|
dataLoadedRef.current = true
|
||||||
if (!data) {
|
initialLoadRef.current = true
|
||||||
// If data is invalid, remove the fetch flag to allow retry
|
fetchInProgressRef.current = false
|
||||||
delete fetchStatusRef.current[fetchKey];
|
|
||||||
}
|
// Reset camera view
|
||||||
// Reset fetching state after all updates are complete
|
state.setMoveToSelectedNode(true)
|
||||||
// Reset camera view by triggering FocusOnNode component
|
|
||||||
state.setMoveToSelectedNode(true);
|
// Enable rendering if the tab is visible
|
||||||
state.setIsFetching(false);
|
state.setShouldRender(isGraphTabVisible)
|
||||||
}).catch(() => {
|
state.setIsFetching(false)
|
||||||
// Reset fetching state and remove flag in case of error
|
}).catch((error) => {
|
||||||
useGraphStore.getState().setIsFetching(false);
|
console.error('Error fetching graph data:', error)
|
||||||
delete fetchStatusRef.current[fetchKey];
|
|
||||||
|
// Reset state on error
|
||||||
|
const state = useGraphStore.getState()
|
||||||
|
state.setIsFetching(false)
|
||||||
|
state.setShouldRender(isGraphTabVisible)
|
||||||
|
dataLoadedRef.current = false
|
||||||
|
fetchInProgressRef.current = false
|
||||||
|
state.setGraphDataFetchAttempted(false)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} else {
|
}, [queryLabel, maxQueryDepth, minDegree, isFetching, paramsChanged, isGraphTabVisible, rawGraph, sigmaGraph])
|
||||||
const state = useGraphStore.getState()
|
|
||||||
state.reset()
|
// Update rendering state and handle tab visibility changes
|
||||||
state.setSigmaGraph(new DirectedGraph())
|
useEffect(() => {
|
||||||
|
// When tab becomes visible
|
||||||
|
if (isGraphTabVisible) {
|
||||||
|
// If we have data, enable rendering
|
||||||
|
if (rawGraph) {
|
||||||
|
useGraphStore.getState().setShouldRender(true)
|
||||||
}
|
}
|
||||||
}, [queryLabel, maxQueryDepth, minDegree, isFetching])
|
|
||||||
|
// We no longer reset the fetch attempted flag here to prevent continuous API calls
|
||||||
|
} else {
|
||||||
|
// When tab becomes invisible, disable rendering
|
||||||
|
useGraphStore.getState().setShouldRender(false)
|
||||||
|
}
|
||||||
|
}, [isGraphTabVisible, rawGraph])
|
||||||
|
|
||||||
const lightrageGraph = useCallback(() => {
|
const lightrageGraph = useCallback(() => {
|
||||||
|
// If we already have a graph instance, return it
|
||||||
if (sigmaGraph) {
|
if (sigmaGraph) {
|
||||||
return sigmaGraph as Graph<NodeType, EdgeType>
|
return sigmaGraph as Graph<NodeType, EdgeType>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If no graph exists yet, create a new one and store it
|
||||||
|
console.log('Creating new Sigma graph instance')
|
||||||
const graph = new DirectedGraph()
|
const graph = new DirectedGraph()
|
||||||
useGraphStore.getState().setSigmaGraph(graph)
|
useGraphStore.getState().setSigmaGraph(graph)
|
||||||
return graph as Graph<NodeType, EdgeType>
|
return graph as Graph<NodeType, EdgeType>
|
||||||
|
@@ -150,7 +150,14 @@
|
|||||||
"labels": "Labels",
|
"labels": "Labels",
|
||||||
"degree": "Degree",
|
"degree": "Degree",
|
||||||
"properties": "Properties",
|
"properties": "Properties",
|
||||||
"relationships": "Relationships"
|
"relationships": "Relationships",
|
||||||
|
"propertyNames": {
|
||||||
|
"description": "Description",
|
||||||
|
"entity_id": "Name",
|
||||||
|
"entity_type": "Type",
|
||||||
|
"source_id": "SrcID",
|
||||||
|
"Neighbour": "Neigh"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"edge": {
|
"edge": {
|
||||||
"title": "Relationship",
|
"title": "Relationship",
|
||||||
@@ -240,5 +247,8 @@
|
|||||||
"streamResponse": "Stream Response",
|
"streamResponse": "Stream Response",
|
||||||
"streamResponseTooltip": "If True, enables streaming output for real-time responses"
|
"streamResponseTooltip": "If True, enables streaming output for real-time responses"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"apiSite": {
|
||||||
|
"loading": "Loading API Documentation..."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -147,7 +147,14 @@
|
|||||||
"labels": "标签",
|
"labels": "标签",
|
||||||
"degree": "度数",
|
"degree": "度数",
|
||||||
"properties": "属性",
|
"properties": "属性",
|
||||||
"relationships": "关系"
|
"relationships": "关系",
|
||||||
|
"propertyNames": {
|
||||||
|
"description": "描述",
|
||||||
|
"entity_id": "名称",
|
||||||
|
"entity_type": "类型",
|
||||||
|
"source_id": "信源ID",
|
||||||
|
"Neighbour": "邻接"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"edge": {
|
"edge": {
|
||||||
"title": "关系",
|
"title": "关系",
|
||||||
@@ -225,5 +232,8 @@
|
|||||||
"streamResponse": "流式响应",
|
"streamResponse": "流式响应",
|
||||||
"streamResponseTooltip": "如果为True,启用实时流式输出响应"
|
"streamResponseTooltip": "如果为True,启用实时流式输出响应"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"apiSite": {
|
||||||
|
"loading": "正在加载 API 文档..."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -66,11 +66,15 @@ interface GraphState {
|
|||||||
|
|
||||||
rawGraph: RawGraph | null
|
rawGraph: RawGraph | null
|
||||||
sigmaGraph: DirectedGraph | null
|
sigmaGraph: DirectedGraph | null
|
||||||
graphLabels: string[]
|
|
||||||
allDatabaseLabels: string[]
|
allDatabaseLabels: string[]
|
||||||
|
|
||||||
moveToSelectedNode: boolean
|
moveToSelectedNode: boolean
|
||||||
isFetching: boolean
|
isFetching: boolean
|
||||||
|
shouldRender: boolean
|
||||||
|
|
||||||
|
// Global flags to track data fetching attempts
|
||||||
|
graphDataFetchAttempted: boolean
|
||||||
|
labelsFetchAttempted: boolean
|
||||||
|
|
||||||
refreshLayout: () => void
|
refreshLayout: () => void
|
||||||
setSelectedNode: (nodeId: string | null, moveToSelectedNode?: boolean) => void
|
setSelectedNode: (nodeId: string | null, moveToSelectedNode?: boolean) => void
|
||||||
@@ -84,10 +88,14 @@ interface GraphState {
|
|||||||
|
|
||||||
setRawGraph: (rawGraph: RawGraph | null) => void
|
setRawGraph: (rawGraph: RawGraph | null) => void
|
||||||
setSigmaGraph: (sigmaGraph: DirectedGraph | null) => void
|
setSigmaGraph: (sigmaGraph: DirectedGraph | null) => void
|
||||||
setGraphLabels: (labels: string[]) => void
|
|
||||||
setAllDatabaseLabels: (labels: string[]) => void
|
setAllDatabaseLabels: (labels: string[]) => void
|
||||||
fetchAllDatabaseLabels: () => Promise<void>
|
fetchAllDatabaseLabels: () => Promise<void>
|
||||||
setIsFetching: (isFetching: boolean) => void
|
setIsFetching: (isFetching: boolean) => void
|
||||||
|
setShouldRender: (shouldRender: boolean) => void
|
||||||
|
|
||||||
|
// Methods to set global flags
|
||||||
|
setGraphDataFetchAttempted: (attempted: boolean) => void
|
||||||
|
setLabelsFetchAttempted: (attempted: boolean) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
const useGraphStoreBase = create<GraphState>()((set, get) => ({
|
const useGraphStoreBase = create<GraphState>()((set, get) => ({
|
||||||
@@ -98,10 +106,14 @@ const useGraphStoreBase = create<GraphState>()((set, get) => ({
|
|||||||
|
|
||||||
moveToSelectedNode: false,
|
moveToSelectedNode: false,
|
||||||
isFetching: false,
|
isFetching: false,
|
||||||
|
shouldRender: false,
|
||||||
|
|
||||||
|
// Initialize global flags
|
||||||
|
graphDataFetchAttempted: false,
|
||||||
|
labelsFetchAttempted: false,
|
||||||
|
|
||||||
rawGraph: null,
|
rawGraph: null,
|
||||||
sigmaGraph: null,
|
sigmaGraph: null,
|
||||||
graphLabels: ['*'],
|
|
||||||
allDatabaseLabels: ['*'],
|
allDatabaseLabels: ['*'],
|
||||||
|
|
||||||
refreshLayout: () => {
|
refreshLayout: () => {
|
||||||
@@ -116,6 +128,7 @@ const useGraphStoreBase = create<GraphState>()((set, get) => ({
|
|||||||
},
|
},
|
||||||
|
|
||||||
setIsFetching: (isFetching: boolean) => set({ isFetching }),
|
setIsFetching: (isFetching: boolean) => set({ isFetching }),
|
||||||
|
setShouldRender: (shouldRender: boolean) => set({ shouldRender }),
|
||||||
setSelectedNode: (nodeId: string | null, moveToSelectedNode?: boolean) =>
|
setSelectedNode: (nodeId: string | null, moveToSelectedNode?: boolean) =>
|
||||||
set({ selectedNode: nodeId, moveToSelectedNode }),
|
set({ selectedNode: nodeId, moveToSelectedNode }),
|
||||||
setFocusedNode: (nodeId: string | null) => set({ focusedNode: nodeId }),
|
setFocusedNode: (nodeId: string | null) => set({ focusedNode: nodeId }),
|
||||||
@@ -128,40 +141,58 @@ const useGraphStoreBase = create<GraphState>()((set, get) => ({
|
|||||||
selectedEdge: null,
|
selectedEdge: null,
|
||||||
focusedEdge: null
|
focusedEdge: null
|
||||||
}),
|
}),
|
||||||
reset: () =>
|
reset: () => {
|
||||||
|
// Get the existing graph
|
||||||
|
const existingGraph = get().sigmaGraph;
|
||||||
|
|
||||||
|
// If we have an existing graph, clear it by removing all nodes
|
||||||
|
if (existingGraph) {
|
||||||
|
const nodes = Array.from(existingGraph.nodes());
|
||||||
|
nodes.forEach(node => existingGraph.dropNode(node));
|
||||||
|
}
|
||||||
|
|
||||||
set({
|
set({
|
||||||
selectedNode: null,
|
selectedNode: null,
|
||||||
focusedNode: null,
|
focusedNode: null,
|
||||||
selectedEdge: null,
|
selectedEdge: null,
|
||||||
focusedEdge: null,
|
focusedEdge: null,
|
||||||
rawGraph: null,
|
rawGraph: null,
|
||||||
sigmaGraph: null,
|
// Keep the existing graph instance but with cleared data
|
||||||
graphLabels: ['*'],
|
moveToSelectedNode: false,
|
||||||
moveToSelectedNode: false
|
shouldRender: false
|
||||||
}),
|
});
|
||||||
|
},
|
||||||
|
|
||||||
setRawGraph: (rawGraph: RawGraph | null) =>
|
setRawGraph: (rawGraph: RawGraph | null) =>
|
||||||
set({
|
set({
|
||||||
rawGraph
|
rawGraph
|
||||||
}),
|
}),
|
||||||
|
|
||||||
setSigmaGraph: (sigmaGraph: DirectedGraph | null) => set({ sigmaGraph }),
|
setSigmaGraph: (sigmaGraph: DirectedGraph | null) => {
|
||||||
|
// Replace graph instance, no need to keep WebGL context
|
||||||
setGraphLabels: (labels: string[]) => set({ graphLabels: labels }),
|
set({ sigmaGraph });
|
||||||
|
},
|
||||||
|
|
||||||
setAllDatabaseLabels: (labels: string[]) => set({ allDatabaseLabels: labels }),
|
setAllDatabaseLabels: (labels: string[]) => set({ allDatabaseLabels: labels }),
|
||||||
|
|
||||||
fetchAllDatabaseLabels: async () => {
|
fetchAllDatabaseLabels: async () => {
|
||||||
try {
|
try {
|
||||||
|
console.log('Fetching all database labels...');
|
||||||
const labels = await getGraphLabels();
|
const labels = await getGraphLabels();
|
||||||
set({ allDatabaseLabels: ['*', ...labels] });
|
set({ allDatabaseLabels: ['*', ...labels] });
|
||||||
|
return;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to fetch all database labels:', error);
|
console.error('Failed to fetch all database labels:', error);
|
||||||
set({ allDatabaseLabels: ['*'] });
|
set({ allDatabaseLabels: ['*'] });
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
setMoveToSelectedNode: (moveToSelectedNode?: boolean) => set({ moveToSelectedNode })
|
setMoveToSelectedNode: (moveToSelectedNode?: boolean) => set({ moveToSelectedNode }),
|
||||||
|
|
||||||
|
// Methods to set global flags
|
||||||
|
setGraphDataFetchAttempted: (attempted: boolean) => set({ graphDataFetchAttempted: attempted }),
|
||||||
|
setLabelsFetchAttempted: (attempted: boolean) => set({ labelsFetchAttempted: attempted })
|
||||||
}))
|
}))
|
||||||
|
|
||||||
const useGraphStore = createSelectors(useGraphStoreBase)
|
const useGraphStore = createSelectors(useGraphStoreBase)
|
||||||
|
@@ -26,7 +26,9 @@ export default defineConfig({
|
|||||||
target: import.meta.env.VITE_BACKEND_URL || 'http://localhost:9621',
|
target: import.meta.env.VITE_BACKEND_URL || 'http://localhost:9621',
|
||||||
changeOrigin: true,
|
changeOrigin: true,
|
||||||
rewrite: endpoint === '/api' ?
|
rewrite: endpoint === '/api' ?
|
||||||
(path) => path.replace(/^\/api/, '') : undefined
|
(path) => path.replace(/^\/api/, '') :
|
||||||
|
endpoint === '/docs' || endpoint === '/openapi.json' ?
|
||||||
|
(path) => path : undefined
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
) : {}
|
) : {}
|
||||||
|
@@ -4,6 +4,12 @@ future
|
|||||||
|
|
||||||
# Basic modules
|
# Basic modules
|
||||||
gensim
|
gensim
|
||||||
|
|
||||||
|
# Additional Packages for export Functionality
|
||||||
|
pandas>=2.0.0
|
||||||
|
|
||||||
|
# Extra libraries are installed when needed using pipmaster
|
||||||
|
|
||||||
pipmaster
|
pipmaster
|
||||||
pydantic
|
pydantic
|
||||||
python-dotenv
|
python-dotenv
|
||||||
@@ -13,5 +19,4 @@ tenacity
|
|||||||
|
|
||||||
# LLM packages
|
# LLM packages
|
||||||
tiktoken
|
tiktoken
|
||||||
|
xlsxwriter>=3.1.0
|
||||||
# Extra libraries are installed when needed using pipmaster
|
|
||||||
|
Reference in New Issue
Block a user