diff --git a/README.md b/README.md index dd570608..06e914ba 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ This repository hosts the code of LightRAG. The structure of this code is based ## 🎉 News +- [x] [2025.02.05]🎯📢Our team has released [VideoRAG](https://github.com/HKUDS/VideoRAG) for processing and 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.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). @@ -1058,6 +1059,12 @@ LightRag can be installed with API support to serve a Fast api interface to perf The documentation can be found [here](lightrag/api/README.md) +## Graph viewer +LightRag can be installed with Tools support to add extra tools like the graphml 3d visualizer. + +The documentation can be found [here](lightrag/tools/lightrag_visualizer/README.md) + + ## Star History diff --git a/extra/OpenWebuiTool/openwebui_tool.py b/external_bindings/OpenWebuiTool/openwebui_tool.py similarity index 100% rename from extra/OpenWebuiTool/openwebui_tool.py rename to external_bindings/OpenWebuiTool/openwebui_tool.py diff --git a/extra/VisualizationTool/README.md b/extra/VisualizationTool/README.md deleted file mode 100644 index a2581703..00000000 --- a/extra/VisualizationTool/README.md +++ /dev/null @@ -1,88 +0,0 @@ -# 3D GraphML Viewer - -An interactive 3D graph visualization tool based on Dear ImGui and ModernGL. - -## Features - -- **3D Interactive Visualization**: High-performance 3D graphics rendering using ModernGL -- **Multiple Layout Algorithms**: Support for various graph layouts - - Spring layout - - Circular layout - - Shell layout - - Random layout -- **Community Detection**: Automatic detection and visualization of graph community structures -- **Interactive Controls**: - - WASD + QE keys for camera movement - - Right mouse drag for view angle control - - Node selection and highlighting - - Adjustable node size and edge width - - Configurable label display - - Quick navigation between node connections - -## Tech Stack - -- **imgui_bundle**: User interface -- **ModernGL**: OpenGL graphics rendering -- **NetworkX**: Graph data structures and algorithms -- **NumPy**: Numerical computations -- **community**: Community detection - -## Usage - -1. **Launch the Program**: - ```bash - python -m pip install -r requirements.txt - python graph_visualizer.py - ``` - -2. **Load Font**: - - Place the font file `font.ttf` in the `assets` directory - - Or modify the `CUSTOM_FONT` constant to use a different font file - -3. **Load Graph File**: - - Click the "Load GraphML" button in the interface - - Select a graph file in GraphML format - -4. **Interactive Controls**: - - **Camera Movement**: - - W: Move forward - - S: Move backward - - A: Move left - - D: Move right - - Q: Move up - - E: Move down - - **View Control**: - - Hold right mouse button and drag to rotate view - - **Node Interaction**: - - Hover mouse to highlight nodes - - Click to select nodes - -5. **Visualization Settings**: - - Adjustable via UI control panel: - - Layout type - - Node size - - Edge width - - Label visibility - - Label size - - Background color - -## Customization Options - -- **Node Scaling**: Adjust node size via `node_scale` parameter -- **Edge Width**: Modify edge width using `edge_width` parameter -- **Label Display**: Toggle label visibility with `show_labels` -- **Label Size**: Adjust label size using `label_size` -- **Label Color**: Set label color through `label_color` -- **View Distance**: Control maximum label display distance with `label_culling_distance` - -## Performance Optimizations - -- Efficient graphics rendering using ModernGL -- View distance culling for label display optimization -- Community detection algorithms for optimized visualization of large-scale graphs - -## System Requirements - -- Python 3.10+ -- Graphics card with OpenGL 3.3+ support -- Supported Operating Systems: Windows/Linux/MacOS diff --git a/lightrag/__init__.py b/lightrag/__init__.py index 12bd2ea9..d68bded0 100644 --- a/lightrag/__init__.py +++ b/lightrag/__init__.py @@ -1,5 +1,5 @@ from .lightrag import LightRAG as LightRAG, QueryParam as QueryParam -__version__ = "1.1.4" +__version__ = "1.1.5" __author__ = "Zirui Guo" __url__ = "https://github.com/HKUDS/LightRAG" diff --git a/lightrag/api/__init__.py b/lightrag/api/__init__.py index 70239ed3..9f0b3540 100644 --- a/lightrag/api/__init__.py +++ b/lightrag/api/__init__.py @@ -1 +1 @@ -__api_version__ = "1.0.3" +__api_version__ = "1.0.4" diff --git a/lightrag/api/lightrag_server.py b/lightrag/api/lightrag_server.py index c9144d0e..aadd1c09 100644 --- a/lightrag/api/lightrag_server.py +++ b/lightrag/api/lightrag_server.py @@ -557,7 +557,14 @@ class DocumentManager: def __init__( self, input_dir: str, - supported_extensions: tuple = (".txt", ".md", ".pdf", ".docx", ".pptx", "xlsx"), + supported_extensions: tuple = ( + ".txt", + ".md", + ".pdf", + ".docx", + ".pptx", + ".xlsx", + ), ): self.input_dir = Path(input_dir) self.supported_extensions = supported_extensions diff --git a/lightrag/kg/mongo_impl.py b/lightrag/kg/mongo_impl.py index 66331520..1c9ce50e 100644 --- a/lightrag/kg/mongo_impl.py +++ b/lightrag/kg/mongo_impl.py @@ -2,11 +2,14 @@ import os from tqdm.asyncio import tqdm as tqdm_async from dataclasses import dataclass import pipmaster as pm -import np +import numpy as np if not pm.is_installed("pymongo"): pm.install("pymongo") +if not pm.is_installed("motor"): + pm.install("motor") + from pymongo import MongoClient from motor.motor_asyncio import AsyncIOMotorClient from typing import Union, List, Tuple diff --git a/lightrag/kg/postgres_impl.py b/lightrag/kg/postgres_impl.py index af62c522..ba91c94a 100644 --- a/lightrag/kg/postgres_impl.py +++ b/lightrag/kg/postgres_impl.py @@ -447,7 +447,7 @@ class PGDocStatusStorage(DocStatusStorage): sql = "select * from LIGHTRAG_DOC_STATUS where workspace=$1 and id=$2" params = {"workspace": self.db.workspace, "id": id} result = await self.db.query(sql, params, True) - if result is None: + if result is None or result == []: return None else: return DocProcessingStatus( diff --git a/lightrag/lightrag.py b/lightrag/lightrag.py index 3014f737..420b82eb 100644 --- a/lightrag/lightrag.py +++ b/lightrag/lightrag.py @@ -372,12 +372,23 @@ class LightRAG: # 3. Filter out already processed documents # _add_doc_keys = await self.doc_status.filter_keys(list(new_docs.keys())) - _add_doc_keys = { - doc_id - for doc_id in new_docs.keys() - if (current_doc := await self.doc_status.get_by_id(doc_id)) is None - or current_doc.status == DocStatus.FAILED - } + _add_doc_keys = set() + for doc_id in new_docs.keys(): + current_doc = await self.doc_status.get_by_id(doc_id) + + if current_doc is None: + _add_doc_keys.add(doc_id) + continue # skip to the next doc_id + + status = None + if isinstance(current_doc, dict): + status = current_doc["status"] + else: + status = current_doc.status + + if status == DocStatus.FAILED: + _add_doc_keys.add(doc_id) + new_docs = {k: v for k, v in new_docs.items() if k in _add_doc_keys} if not new_docs: diff --git a/extra/VisualizationTool/assets/place_font_here b/lightrag/tools/__init__.py similarity index 100% rename from extra/VisualizationTool/assets/place_font_here rename to lightrag/tools/__init__.py diff --git a/extra/VisualizationTool/README-zh.md b/lightrag/tools/lightrag_visualizer/README-zh.md similarity index 97% rename from extra/VisualizationTool/README-zh.md rename to lightrag/tools/lightrag_visualizer/README-zh.md index d76c93a2..949178ff 100644 --- a/extra/VisualizationTool/README-zh.md +++ b/lightrag/tools/lightrag_visualizer/README-zh.md @@ -38,8 +38,8 @@ 1. **启动程序**: ```bash - python -m pip install -r requirements.txt - python graph_visualizer.py + pip install lightrag-hku[tools] + lightrag-viewer ``` 2. **加载字体**: diff --git a/lightrag/tools/lightrag_visualizer/README.md b/lightrag/tools/lightrag_visualizer/README.md new file mode 100644 index 00000000..f0567bf4 --- /dev/null +++ b/lightrag/tools/lightrag_visualizer/README.md @@ -0,0 +1,136 @@ +# LightRAG 3D Graph Viewer + +An interactive 3D graph visualization tool included in the LightRAG package for visualizing and analyzing RAG (Retrieval-Augmented Generation) graphs and other graph structures. + +![image](https://github.com/user-attachments/assets/b0d86184-99fc-468c-96ed-c611f14292bf) + +## Installation + +### Quick Install +```bash +pip install lightrag-hku[tools] # Install with visualization tool only +# or +pip install lightrag-hku[api,tools] # Install with both API and visualization tools +``` + +## Launch the Viewer +```bash +lightrag-viewer +``` + +## Features + +- **3D Interactive Visualization**: High-performance 3D graphics rendering using ModernGL +- **Multiple Layout Algorithms**: Support for various graph layouts + - Spring layout + - Circular layout + - Shell layout + - Random layout +- **Community Detection**: Automatic detection and visualization of graph community structures +- **Interactive Controls**: + - WASD + QE keys for camera movement + - Right mouse drag for view angle control + - Node selection and highlighting + - Adjustable node size and edge width + - Configurable label display + - Quick navigation between node connections + +## Tech Stack + +- **imgui_bundle**: User interface +- **ModernGL**: OpenGL graphics rendering +- **NetworkX**: Graph data structures and algorithms +- **NumPy**: Numerical computations +- **community**: Community detection + +## Interactive Controls + +### Camera Movement +- W: Move forward +- S: Move backward +- A: Move left +- D: Move right +- Q: Move up +- E: Move down + +### View Control +- Hold right mouse button and drag to rotate view + +### Node Interaction +- Hover mouse to highlight nodes +- Click to select nodes + +## Visualization Settings + +Adjustable via UI control panel: +- Layout type +- Node size +- Edge width +- Label visibility +- Label size +- Background color + +## Customization Options + +- **Node Scaling**: Adjust node size via `node_scale` parameter +- **Edge Width**: Modify edge width using `edge_width` parameter +- **Label Display**: Toggle label visibility with `show_labels` +- **Label Size**: Adjust label size using `label_size` +- **Label Color**: Set label color through `label_color` +- **View Distance**: Control maximum label display distance with `label_culling_distance` + +## System Requirements + +- Python 3.9+ +- Graphics card with OpenGL 3.3+ support +- Supported Operating Systems: Windows/Linux/MacOS + +## Troubleshooting + +### Common Issues + +1. **Command Not Found** + ```bash + # Make sure you installed with the 'tools' option + pip install lightrag-hku[tools] + + # Verify installation + pip list | grep lightrag-hku + ``` + +2. **ModernGL Initialization Failed** + ```bash + # Check OpenGL version + glxinfo | grep "OpenGL version" + + # Update graphics drivers if needed + ``` + +3. **Font Loading Issues** + - The required fonts are included in the package + - If issues persist, check your graphics drivers + +## Usage with LightRAG + +The viewer is particularly useful for: +- Visualizing RAG knowledge graphs +- Analyzing document relationships +- Exploring semantic connections +- Debugging retrieval patterns + +## Performance Optimizations + +- Efficient graphics rendering using ModernGL +- View distance culling for label display optimization +- Community detection algorithms for optimized visualization of large-scale graphs + +## Support + +- GitHub Issues: [LightRAG Repository](https://github.com/HKUDS/LightRAG) +- Documentation: [LightRAG Docs](https://URL-to-docs) + +## License + +This tool is part of LightRAG and is distributed under the MIT License. See `LICENSE` for more information. + +Note: This visualization tool is an optional component of the LightRAG package. Install with the [tools] option to access the viewer functionality. diff --git a/lightrag/tools/lightrag_visualizer/__init__.py b/lightrag/tools/lightrag_visualizer/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/lightrag/tools/lightrag_visualizer/assets/Geist-Regular.ttf b/lightrag/tools/lightrag_visualizer/assets/Geist-Regular.ttf new file mode 100644 index 00000000..6fdfffa7 Binary files /dev/null and b/lightrag/tools/lightrag_visualizer/assets/Geist-Regular.ttf differ diff --git a/lightrag/tools/lightrag_visualizer/assets/LICENSE - Geist.txt b/lightrag/tools/lightrag_visualizer/assets/LICENSE - Geist.txt new file mode 100644 index 00000000..8d003fec --- /dev/null +++ b/lightrag/tools/lightrag_visualizer/assets/LICENSE - Geist.txt @@ -0,0 +1,92 @@ +Copyright (c) 2023 Vercel, in collaboration with basement.studio + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION AND CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/lightrag/tools/lightrag_visualizer/assets/LICENSE - SmileySans.txt b/lightrag/tools/lightrag_visualizer/assets/LICENSE - SmileySans.txt new file mode 100644 index 00000000..3eda3911 --- /dev/null +++ b/lightrag/tools/lightrag_visualizer/assets/LICENSE - SmileySans.txt @@ -0,0 +1,93 @@ +Copyright (c) 2022--2024, atelierAnchor , +with Reserved Font Name and <得意黑>. + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/lightrag/tools/lightrag_visualizer/assets/SmileySans-Oblique.ttf b/lightrag/tools/lightrag_visualizer/assets/SmileySans-Oblique.ttf new file mode 100644 index 00000000..c297dc69 Binary files /dev/null and b/lightrag/tools/lightrag_visualizer/assets/SmileySans-Oblique.ttf differ diff --git a/lightrag/tools/lightrag_visualizer/assets/place_font_here b/lightrag/tools/lightrag_visualizer/assets/place_font_here new file mode 100644 index 00000000..e69de29b diff --git a/extra/VisualizationTool/graph_visualizer.py b/lightrag/tools/lightrag_visualizer/graph_visualizer.py similarity index 90% rename from extra/VisualizationTool/graph_visualizer.py rename to lightrag/tools/lightrag_visualizer/graph_visualizer.py index 9bbd6235..dd20553a 100644 --- a/extra/VisualizationTool/graph_visualizer.py +++ b/lightrag/tools/lightrag_visualizer/graph_visualizer.py @@ -1,6 +1,6 @@ """ 3D GraphML Viewer using Dear ImGui and ModernGL -Author: LoLLMs, ArnoChen +Author: ParisNeo, ArnoChen Description: An interactive 3D GraphML viewer using imgui_bundle and ModernGL Version: 2.0 """ @@ -8,6 +8,18 @@ Version: 2.0 from typing import Optional, Tuple, Dict, List import numpy as np import networkx as nx +import pipmaster as pm + +# Added automatic libraries install using pipmaster +if not pm.is_installed("moderngl"): + pm.install("moderngl") +if not pm.is_installed("imgui_bundle"): + pm.install("imgui_bundle") +if not pm.is_installed("pyglm"): + pm.install("pyglm") +if not pm.is_installed("python-louvain"): + pm.install("python-louvain") + import moderngl from imgui_bundle import imgui, immapp, hello_imgui import community @@ -20,6 +32,9 @@ import os CUSTOM_FONT = "font.ttf" +DEFAULT_FONT_ENG = "Geist-Regular.ttf" +DEFAULT_FONT_CHI = "SmileySans-Oblique.ttf" + class Node3D: """Class representing a 3D node in the graph""" @@ -172,6 +187,10 @@ class GraphViewer: np.sin(np.radians(self.pitch)), ) ) + + if not imgui.is_window_hovered(): + return + if io.mouse_wheel != 0: self.move_speed += io.mouse_wheel * 0.05 self.move_speed = np.max([self.move_speed, 0.01]) @@ -502,6 +521,14 @@ class GraphViewer: def load_file(self, filepath: str): """Load a GraphML file with error handling""" try: + # Clear existing data + self.id_node_map.clear() + self.nodes.clear() + self.selected_node = None + self.highlighted_node = None + self.setup_buffers() + + # Load new graph self.graph = nx.read_graphml(filepath) self.calculate_layout() self.update_buffers() @@ -672,10 +699,6 @@ class GraphViewer: self.position, self.position + self.front, self.up ) - io = imgui.get_io() - self.window_width = int(io.display_size.x) - self.window_height = int(io.display_size.y) - aspect_ratio = self.window_width / self.window_height self.proj_matrix = glm.perspective( glm.radians(60.0), # FOV @@ -839,7 +862,7 @@ class GraphViewer: def render(self): """Render the graph""" # Clear screen - self.glctx.clear(*self.background_color) + self.glctx.clear(*self.background_color, depth=1) if not self.graph: return @@ -886,11 +909,15 @@ class GraphViewer: # Render id map self.render_id_map(mvp) + def render_labels(self): # Render labels if enabled - if self.show_labels: + if self.show_labels and self.nodes: # Save current font scale original_scale = imgui.get_font_size() + self.update_view_proj_matrix() + mvp = self.proj_matrix * self.view_matrix + for node in self.nodes: # Project node position to screen space pos = mvp * glm.vec4( @@ -1013,13 +1040,39 @@ def create_sphere(sectors: int = 32, rings: int = 16) -> Tuple: return (vbo_vertices, vbo_elements) +def draw_text_with_bg( + text: str, + text_pos: imgui.ImVec2Like, + text_size: imgui.ImVec2Like, + bg_color: int, +): + imgui.get_window_draw_list().add_rect_filled( + (text_pos[0] - 5, text_pos[1] - 5), + (text_pos[0] + text_size[0] + 5, text_pos[1] + text_size[1] + 5), + bg_color, + 3.0, + ) + imgui.set_cursor_pos(text_pos) + imgui.text(text) + + def main(): """Main application entry point""" viewer = GraphViewer() + show_fps = True + text_bg_color = imgui.IM_COL32(0, 0, 0, 100) + def gui(): if not viewer.initialized: viewer.setup() + # # Change the theme + # tweaked_theme = hello_imgui.get_runner_params().imgui_window_params.tweaked_theme + # tweaked_theme.theme = hello_imgui.ImGuiTheme_.darcula_darker + # hello_imgui.apply_tweaked_theme(tweaked_theme) + + viewer.window_width = int(imgui.get_window_width()) + viewer.window_height = int(imgui.get_window_height()) # Handle keyboard and mouse input viewer.handle_keyboard_input() @@ -1089,11 +1142,34 @@ def main(): # Render graph settings window viewer.render_settings() - window_bg_color.w = 0.0 + # Render FPS + if show_fps: + imgui.set_window_font_scale(1) + fps_text = f"FPS: {hello_imgui.frame_rate():.1f}" + text_size = imgui.calc_text_size(fps_text) + cursor_pos = (10, viewer.window_height - text_size.y - 10) + draw_text_with_bg(fps_text, cursor_pos, text_size, text_bg_color) + + # Render highlighted node ID + if viewer.highlighted_node: + imgui.set_window_font_scale(1) + node_text = f"Node ID: {viewer.highlighted_node.label}" + text_size = imgui.calc_text_size(node_text) + cursor_pos = ( + viewer.window_width - text_size.x - 10, + viewer.window_height - text_size.y - 10, + ) + draw_text_with_bg(node_text, cursor_pos, text_size, text_bg_color) + + window_bg_color.w = 0 style.set_color_(imgui.Col_.window_bg.value, window_bg_color) - # Render the graph - viewer.render() + # Render labels + viewer.render_labels() + + def custom_background(): + if viewer.initialized: + viewer.render() runner_params = hello_imgui.RunnerParams() runner_params.app_window_params.window_geometry.size = ( @@ -1102,47 +1178,48 @@ def main(): ) runner_params.app_window_params.window_title = "3D GraphML Viewer" runner_params.callbacks.show_gui = gui - addons = immapp.AddOnsParams() - addons.with_markdown = True + runner_params.callbacks.custom_background = custom_background def load_font(): - io = imgui.get_io() - io.fonts.add_font_default() - - # Load font for Chinese character support # You will need to provide it yourself, or use another font. font_filename = CUSTOM_FONT - if not os.path.exists("assets/" + font_filename): + io = imgui.get_io() + io.fonts.tex_desired_width = 4096 # Larger texture for better CJK font quality + font_size_pixels = 14 + asset_dir = os.path.join(os.path.dirname(__file__), "assets") + + # Try to load custom font + if not os.path.isfile(font_filename): + font_filename = os.path.join(asset_dir, font_filename) + if os.path.isfile(font_filename): + custom_font = io.fonts.add_font_from_file_ttf( + filename=font_filename, + size_pixels=font_size_pixels, + glyph_ranges_as_int_list=io.fonts.get_glyph_ranges_chinese_full(), + ) + io.font_default = custom_font return - # Get the full Chinese character range for ImGui - # This includes all Chinese characters supported by ImGui - cn_glyph_ranges_imgui = imgui.get_io().fonts.get_glyph_ranges_chinese_full() - - # Set up font loading parameters with Chinese character support - font_loading_params = hello_imgui.FontLoadingParams() - font_loading_params.glyph_ranges = hello_imgui.translate_common_glyph_ranges( - cn_glyph_ranges_imgui + # Load default fonts + io.fonts.add_font_from_file_ttf( + filename=os.path.join(asset_dir, DEFAULT_FONT_ENG), + size_pixels=font_size_pixels, ) - custom_font = hello_imgui.load_font(font_filename, 16.0, font_loading_params) - # # Merge with default font - # font_config = imgui.ImFontConfig() - # font_config.merge_mode = True - # custom_font = io.fonts.add_font_from_file_ttf( - # filename= "assets/" + font_filename, - # size_pixels=16.0, - # font_cfg=font_config, - # glyph_ranges_as_int_list=cn_glyph_ranges_imgui, - # ) + font_config = imgui.ImFontConfig() + font_config.merge_mode = True - io.fonts.tex_desired_width = 4096 # Larger texture for better CJK font quality - io.font_default = custom_font + io.font_default = io.fonts.add_font_from_file_ttf( + filename=os.path.join(asset_dir, DEFAULT_FONT_CHI), + size_pixels=font_size_pixels, + font_cfg=font_config, + glyph_ranges_as_int_list=io.fonts.get_glyph_ranges_chinese_full(), + ) runner_params.callbacks.load_additional_fonts = load_font - immapp.run(runner_params, addons) + immapp.run(runner_params) if __name__ == "__main__": diff --git a/extra/VisualizationTool/requirements.txt b/lightrag/tools/lightrag_visualizer/requirements.txt similarity index 100% rename from extra/VisualizationTool/requirements.txt rename to lightrag/tools/lightrag_visualizer/requirements.txt diff --git a/setup.py b/setup.py index 38eff646..c190bd4d 100644 --- a/setup.py +++ b/setup.py @@ -62,6 +62,16 @@ def read_api_requirements(): return api_deps +def read_extra_requirements(): + api_deps = [] + try: + with open("./lightrag/tools/lightrag_visualizer/requirements.txt") as f: + api_deps = [line.strip() for line in f if line.strip()] + except FileNotFoundError: + print("Warning: API requirements.txt not found.") + return api_deps + + metadata = retrieve_metadata() long_description = read_long_description() requirements = read_requirements() @@ -97,10 +107,12 @@ setuptools.setup( }, extras_require={ "api": read_api_requirements(), # API requirements as optional + "tools": read_extra_requirements(), # API requirements as optional }, entry_points={ "console_scripts": [ "lightrag-server=lightrag.api.lightrag_server:main [api]", + "lightrag-viewer=lightrag.tools.lightrag_visualizer.graph_visualizer:main [tools]", ], }, )