图RAG系统 核心特性: 1. 智能路由:自动选择最适合的检索策略 2. 双引擎检索:传统混合检索 + 图RAG检索 3. 图结构推理:多跳遍历、子图提取、关系推理 4. 查询复杂度分析:深度理解用户意图 5. 自适应学习:基于反馈优化系统性能
| 31 | load_dotenv() |
| 32 | |
| 33 | class AdvancedGraphRAGSystem: |
| 34 | """ |
| 35 | 图RAG系统 |
| 36 | |
| 37 | 核心特性: |
| 38 | 1. 智能路由:自动选择最适合的检索策略 |
| 39 | 2. 双引擎检索:传统混合检索 + 图RAG检索 |
| 40 | 3. 图结构推理:多跳遍历、子图提取、关系推理 |
| 41 | 4. 查询复杂度分析:深度理解用户意图 |
| 42 | 5. 自适应学习:基于反馈优化系统性能 |
| 43 | """ |
| 44 | |
| 45 | def __init__(self, config: Optional[GraphRAGConfig] = None): |
| 46 | self.config = config or DEFAULT_CONFIG |
| 47 | |
| 48 | # 核心模块 |
| 49 | self.data_module = None |
| 50 | self.index_module = None |
| 51 | self.generation_module = None |
| 52 | |
| 53 | # 检索引擎 |
| 54 | self.traditional_retrieval = None |
| 55 | self.graph_rag_retrieval = None |
| 56 | self.query_router = None |
| 57 | |
| 58 | # 系统状态 |
| 59 | self.system_ready = False |
| 60 | |
| 61 | def initialize_system(self): |
| 62 | """初始化高级图RAG系统""" |
| 63 | logger.info("启动高级图RAG系统...") |
| 64 | |
| 65 | try: |
| 66 | # 1. 数据准备模块 |
| 67 | print("初始化数据准备模块...") |
| 68 | self.data_module = GraphDataPreparationModule( |
| 69 | uri=self.config.neo4j_uri, |
| 70 | user=self.config.neo4j_user, |
| 71 | password=self.config.neo4j_password, |
| 72 | database=self.config.neo4j_database |
| 73 | ) |
| 74 | |
| 75 | # 2. 向量索引模块 |
| 76 | print("初始化Milvus向量索引...") |
| 77 | self.index_module = MilvusIndexConstructionModule( |
| 78 | host=self.config.milvus_host, |
| 79 | port=self.config.milvus_port, |
| 80 | collection_name=self.config.milvus_collection_name, |
| 81 | dimension=self.config.milvus_dimension, |
| 82 | model_name=self.config.embedding_model |
| 83 | ) |
| 84 | |
| 85 | # 3. 生成模块 |
| 86 | print("初始化生成模块...") |
| 87 | self.generation_module = GenerationIntegrationModule( |
| 88 | model_name=self.config.llm_model, |
| 89 | temperature=self.config.temperature, |
| 90 | max_tokens=self.config.max_tokens |