ForcePilot/src/knowledge/adapters/base.py
Wenjie Zhang 3d63418adf feat: 实现统一图谱适配器及前端组件重构
重构图谱系统架构,引入适配器模式支持多种图数据库类型。主要变更包括:

1. 新增 GraphAdapter 基类及 LightRAG/Upload 适配器实现
2. 重构前端组件结构,新增 GraphDetailPanel 等可复用组件
3. 实现 useGraph 组合式函数统一管理图谱状态
4. 优化 Neo4j 节点/边数据标准化处理
5. 新增统一图谱 API 接口及对应测试用例
6. 改进前端交互体验,增加节点详情展示等功能

前端组件库重构为模块化结构,提升代码复用性。适配器模式使系统可灵活支持不同图数据源,为后续扩展奠定基础。

Next:上传图谱文件的时候,支持属性解析
2025-12-15 23:25:56 +08:00

88 lines
2.3 KiB
Python

from abc import ABC, abstractmethod
from typing import Any
class GraphAdapter(ABC):
"""图谱适配器基类 (Base Graph Adapter)"""
@abstractmethod
async def query_nodes(self, keyword: str, **kwargs) -> dict[str, Any]:
"""查询节点 (Query nodes)"""
pass
@abstractmethod
async def add_entity(self, triples: list[dict], **kwargs) -> bool:
"""添加实体三元组 (Add entity triples)"""
pass
@abstractmethod
async def get_sample_nodes(self, num: int = 50, **kwargs) -> dict[str, list]:
"""获取样本节点 (Get sample nodes)"""
pass
@abstractmethod
def normalize_node(self, raw_node: Any) -> dict[str, Any]:
"""标准化节点格式 (Normalize node format)"""
pass
@abstractmethod
def normalize_edge(self, raw_edge: Any) -> dict[str, Any]:
"""标准化边格式 (Normalize edge format)"""
pass
@abstractmethod
async def get_labels(self) -> list[str]:
"""获取所有标签 (Get all labels)"""
pass
def _create_standard_node(
self,
node_id: str,
name: str,
entity_type: str,
labels: list[str],
properties: dict[str, Any],
source: str,
) -> dict[str, Any]:
"""
Helper to create a standardized node dictionary.
"""
return {
"id": node_id,
"name": name,
"original_id": node_id,
"type": entity_type,
"labels": labels,
"properties": properties,
"normalized": {
"name": name,
"type": entity_type,
"source": source,
},
"graph_type": source,
}
def _create_standard_edge(
self,
edge_id: str,
source_id: str,
target_id: str,
edge_type: str,
properties: dict[str, Any],
direction: str = "directed",
) -> dict[str, Any]:
"""
Helper to create a standardized edge dictionary.
"""
return {
"id": edge_id,
"source_id": source_id,
"target_id": target_id,
"type": edge_type,
"properties": properties,
"normalized": {
"type": edge_type,
"direction": direction,
},
}