HKUDS/LightRAGView GitHub Homepage for Latest Official Releases
LightRAGは、シンプルで高速な検索拡張生成フレームワークであり、多様なクエリモードと知識グラフの構築をサポートします。
MITPythonLightRAGHKUDS 19.2k Last Updated: August 06, 2025
LightRAG - シンプルかつ高速な検索拡張生成フレームワーク
プロジェクト概要
LightRAGは、香港大学データサイエンス学院(HKUDS)が開発した「シンプルかつ高速な検索拡張生成」フレームワークです。このプロジェクトは、開発者向けに、ドキュメントインデックス、知識グラフ構築、およびインテリジェントな質問応答機能をサポートする、完全なRAG(Retrieval-Augmented Generation)ソリューションを提供することを目的としています。
主要な特徴
🔍 多様な検索モード
LightRAGは、さまざまなシナリオのニーズを満たすために、5つの異なる検索モードをサポートしています。
- naiveモード: 高度な技術を使用しない基本的な検索
- localモード: コンテキストに関連する情報の検索に焦点を当てる
- globalモード: グローバルな知識を利用して検索を行う
- hybridモード: ローカルおよびグローバルな検索方法を組み合わせる
- mixモード: 知識グラフとベクトル検索を統合し、最も包括的な回答を提供する
🎯 知識グラフ構築
- ドキュメントからエンティティと関係を自動的に抽出
- 知識グラフの可視化表示をサポート
- エンティティと関係の追加、削除、変更、および検索機能を提供
- エンティティのマージと重複排除をサポート
🚀 柔軟なモデルサポート
- OpenAIモデル: GPT-4などのOpenAIシリーズのモデルをサポート
- Hugging Faceモデル: ローカルにデプロイされたオープンソースモデルをサポート
- Ollamaモデル: ローカルで実行される量子化モデルをサポート
- LlamaIndex統合: LlamaIndexを通じて、より多くのモデルプロバイダーをサポート
📊 多様なストレージバックエンド
- ベクトルデータベース: Faiss、PGVectorなどをサポート
- グラフデータベース: Neo4j、PostgreSQL+Apache AGEをサポート
- デフォルトストレージ: 内蔵のNetworkXグラフストレージ
インストール方法
PyPIからのインストール
pip install "lightrag-hku[api]"
ソースコードからのインストール
# Python仮想環境の作成(必要に応じて)
# 編集可能なモードでインストール、APIサポートを含む
pip install -e ".[api]"
基本的な使用例
初期化とクエリ
import os
import asyncio
from lightrag import LightRAG, QueryParam
from lightrag.llm.openai import gpt_4o_mini_complete, openai_embed
from lightrag.kg.shared_storage import initialize_pipeline_status
from lightrag.utils import setup_logger
setup_logger("lightrag", level="INFO")
async def initialize_rag():
rag = LightRAG(
working_dir="your/path",
embedding_func=openai_embed,
llm_model_func=gpt_4o_mini_complete
)
await rag.initialize_storages()
await initialize_pipeline_status()
return rag
def main():
rag = asyncio.run(initialize_rag())
rag.insert("Your text")
result = rag.query(
"What are the top themes in this story?",
param=QueryParam(mode="mix")
)
print(result)
if __name__ == "__main__":
main()
高度な機能
対話履歴のサポート
# Create conversation history
conversation_history = [
{"role": "user", "content": "What is the main character's attitude towards Christmas?"},
{"role": "assistant", "content": "At the beginning of the story, Ebenezer Scrooge has a very negative attitude towards Christmas..."},
{"role": "user", "content": "How does his attitude change?"}
]
# Create query parameters with conversation history
query_param = QueryParam(
mode="mix", # or any other mode: "local", "global", "hybrid"
conversation_history=conversation_history, # Add the conversation history
history_turns=3 # Number of recent conversation turns to consider
)
# Make a query that takes into account the conversation history
response = rag.query(
"What causes this change in his character?",
param=query_param
)
知識グラフ管理
# Create new entity
entity = rag.create_entity("Google", {
"description": "Google is a multinational technology company specializing in internet-related services and products.",
"entity_type": "company"
})
# Create another entity
product = rag.create_entity("Gmail", {
"description": "Gmail is an email service developed by Google.",
"entity_type": "product"
})
# Create relation between entities
relation = rag.create_relation("Google", "Gmail", {
"description": "Google develops and operates Gmail.",
"keywords": "develops operates service",
"weight": 2.0
})
LightRAG Server
Web UI機能
LightRAG Serverは、以下の機能を含む完全なWebインターフェースを提供します。
- ドキュメントインデックス管理
- 知識グラフの可視化
- 簡単なRAGクエリインターフェース
- 重力レイアウト、ノードクエリ、サブグラフフィルタリングなどの機能をサポート
APIインターフェース
- RESTful APIインターフェースを提供
- Ollama API形式と互換性あり
- AIチャットボットの統合をサポート(例:Open WebUI)
設定パラメータ
コアパラメータ
working_dir
: 作業ディレクトリのパスembedding_func
: 埋め込み関数llm_model_func
: 大規模言語モデル関数vector_storage
: ベクトルストレージのタイプgraph_storage
: グラフストレージのタイプ
パフォーマンスチューニングパラメータ
embedding_batch_size
: 埋め込みバッチサイズ(デフォルト32)embedding_func_max_async
: 最大同時埋め込みプロセス数(デフォルト16)llm_model_max_async
: 最大同時LLMプロセス数(デフォルト4)enable_llm_cache
: LLMキャッシュを有効にするかどうか(デフォルトTrue)
データのエクスポートとバックアップ
さまざまな形式でのデータエクスポートをサポートします。
#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")
Token使用量トラッキング
組み込みのToken消費量監視ツール:
from lightrag.utils import TokenTracker
# Create TokenTracker instance
token_tracker = TokenTracker()
# Method 1: Using context manager (Recommended)
# Suitable for scenarios requiring automatic token usage tracking
with token_tracker:
result1 = await llm_model_func("your question 1")
result2 = await llm_model_func("your question 2")
# Method 2: Manually adding token usage records
# Suitable for scenarios requiring more granular control over token statistics
token_tracker.reset()
rag.insert()
rag.query("your question 1", param=QueryParam(mode="naive"))
rag.query("your question 2", param=QueryParam(mode="mix"))
# Display total token usage (including insert and query operations)
print("Token usage:", token_tracker.get_usage())
適用可能なシナリオ
企業知識管理
- 内部ドキュメントの検索と質問応答
- 知識ベースの構築とメンテナンス
- 技術ドキュメントのインテリジェントアシスタント
学術研究
- 文献検索と分析
- 知識グラフ構築の研究
- RAGシステムの性能評価
コンテンツ作成
- 執筆支援と素材検索
- 複数ドキュメントのコンテンツ統合
- インテリジェントなコンテンツレコメンデーション
プロジェクトの利点
- 統合が容易: シンプルなPython APIとREST APIを提供
- 高度なカスタマイズ性: さまざまなモデルとストレージバックエンドをサポート
- パフォーマンス最適化: バッチ処理と非同期処理をサポート
- 可視化: 知識グラフの可視化機能を内蔵
- エンタープライズレベル: PostgreSQLなどのエンタープライズレベルのデータベースをサポート
まとめ
LightRAGは、機能が充実し、使いやすいRAGフレームワークであり、特にインテリジェントな質問応答システムと知識管理プラットフォームを構築する必要があるシナリオに適しています。その柔軟なアーキテクチャ設計と豊富な機能特性により、RAG分野における優れたオープンソースソリューションとなっています。