2025-02-09 11:24:08 +01:00
|
|
|
from enum import Enum
|
2025-01-29 21:34:34 +08:00
|
|
|
import os
|
2024-10-10 15:02:30 +08:00
|
|
|
from dataclasses import dataclass, field
|
2025-01-25 18:54:40 +08:00
|
|
|
from typing import (
|
2025-02-09 11:24:08 +01:00
|
|
|
Optional,
|
2025-01-25 18:54:40 +08:00
|
|
|
TypedDict,
|
|
|
|
Union,
|
|
|
|
Literal,
|
|
|
|
TypeVar,
|
|
|
|
Any,
|
|
|
|
)
|
2024-10-10 15:02:30 +08:00
|
|
|
|
2025-02-09 11:24:08 +01:00
|
|
|
import numpy as np
|
|
|
|
|
2024-10-10 15:02:30 +08:00
|
|
|
|
|
|
|
from .utils import EmbeddingFunc
|
|
|
|
|
|
|
|
TextChunkSchema = TypedDict(
|
|
|
|
"TextChunkSchema",
|
|
|
|
{"tokens": int, "content": str, "full_doc_id": str, "chunk_order_index": int},
|
|
|
|
)
|
|
|
|
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
2024-10-19 09:43:17 +05:30
|
|
|
|
2024-10-10 15:02:30 +08:00
|
|
|
@dataclass
|
|
|
|
class QueryParam:
|
2024-12-28 11:56:28 +08:00
|
|
|
mode: Literal["local", "global", "hybrid", "naive", "mix"] = "global"
|
2024-10-10 15:02:30 +08:00
|
|
|
only_need_context: bool = False
|
2024-11-15 12:57:01 +08:00
|
|
|
only_need_prompt: bool = False
|
2024-10-10 15:02:30 +08:00
|
|
|
response_type: str = "Multiple Paragraphs"
|
2024-12-06 08:48:55 +08:00
|
|
|
stream: bool = False
|
2024-10-23 11:50:29 +08:00
|
|
|
# Number of top-k items to retrieve; corresponds to entities in "local" mode and relationships in "global" mode.
|
2025-01-29 21:34:34 +08:00
|
|
|
top_k: int = int(os.getenv("TOP_K", "60"))
|
2024-11-25 13:29:55 +08:00
|
|
|
# Number of document chunks to retrieve.
|
|
|
|
# top_n: int = 10
|
2024-10-23 11:50:29 +08:00
|
|
|
# Number of tokens for the original chunks.
|
2024-10-19 09:43:17 +05:30
|
|
|
max_token_for_text_unit: int = 4000
|
2024-10-23 11:50:29 +08:00
|
|
|
# Number of tokens for the relationship descriptions
|
2024-10-10 15:02:30 +08:00
|
|
|
max_token_for_global_context: int = 4000
|
2024-10-23 11:50:29 +08:00
|
|
|
# Number of tokens for the entity descriptions
|
2024-10-19 09:43:17 +05:30
|
|
|
max_token_for_local_context: int = 4000
|
2025-01-14 22:10:47 +05:30
|
|
|
hl_keywords: list[str] = field(default_factory=list)
|
|
|
|
ll_keywords: list[str] = field(default_factory=list)
|
2025-01-24 18:59:24 +08:00
|
|
|
# Conversation history support
|
|
|
|
conversation_history: list[dict] = field(
|
|
|
|
default_factory=list
|
|
|
|
) # Format: [{"role": "user/assistant", "content": "message"}]
|
|
|
|
history_turns: int = (
|
|
|
|
3 # Number of complete conversation turns (user-assistant pairs) to consider
|
|
|
|
)
|
2024-10-10 15:02:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class StorageNameSpace:
|
|
|
|
namespace: str
|
2025-02-08 22:57:37 +01:00
|
|
|
global_config: dict[str, Any]
|
2024-10-10 15:02:30 +08:00
|
|
|
|
|
|
|
async def index_done_callback(self):
|
|
|
|
"""commit the storage operations after indexing"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
async def query_done_callback(self):
|
|
|
|
"""commit the storage operations after querying"""
|
|
|
|
pass
|
|
|
|
|
2024-10-19 09:43:17 +05:30
|
|
|
|
2024-10-10 15:02:30 +08:00
|
|
|
@dataclass
|
|
|
|
class BaseVectorStorage(StorageNameSpace):
|
|
|
|
embedding_func: EmbeddingFunc
|
|
|
|
meta_fields: set = field(default_factory=set)
|
|
|
|
|
2025-02-09 11:00:04 +01:00
|
|
|
async def query(self, query: str, top_k: int) -> list[dict[str, Any]]:
|
2024-10-10 15:02:30 +08:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2025-02-09 11:00:04 +01:00
|
|
|
async def upsert(self, data: dict[str, dict[str, Any]]) -> None:
|
2024-10-10 15:02:30 +08:00
|
|
|
"""Use 'content' field from value for embedding, use key as id.
|
|
|
|
If embedding_func is None, use 'embedding' field from value
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2024-10-19 09:43:17 +05:30
|
|
|
|
2024-10-10 15:02:30 +08:00
|
|
|
@dataclass
|
2025-02-08 23:16:04 +01:00
|
|
|
class BaseKVStorage(StorageNameSpace):
|
2024-11-08 14:58:41 +08:00
|
|
|
embedding_func: EmbeddingFunc
|
2024-11-12 13:32:40 +08:00
|
|
|
|
2025-02-09 10:32:59 +01:00
|
|
|
async def get_by_id(self, id: str) -> dict[str, Any]:
|
2024-10-10 15:02:30 +08:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2025-02-09 10:32:59 +01:00
|
|
|
async def get_by_ids(self, ids: list[str]) -> list[dict[str, Any]]:
|
2024-10-10 15:02:30 +08:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
async def filter_keys(self, data: list[str]) -> set[str]:
|
|
|
|
"""return un-exist keys"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2025-02-08 23:16:04 +01:00
|
|
|
async def upsert(self, data: dict[str, Any]) -> None:
|
2024-10-10 15:02:30 +08:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2025-02-08 22:57:37 +01:00
|
|
|
async def drop(self) -> None:
|
2024-10-10 15:02:30 +08:00
|
|
|
raise NotImplementedError
|
2025-02-08 23:58:15 +01:00
|
|
|
|
|
|
|
|
2024-10-10 15:02:30 +08:00
|
|
|
@dataclass
|
|
|
|
class BaseGraphStorage(StorageNameSpace):
|
2024-11-11 15:19:42 +08:00
|
|
|
embedding_func: EmbeddingFunc = None
|
2024-11-12 13:32:40 +08:00
|
|
|
|
2024-10-10 15:02:30 +08:00
|
|
|
async def has_node(self, node_id: str) -> bool:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
async def has_edge(self, source_node_id: str, target_node_id: str) -> bool:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
async def node_degree(self, node_id: str) -> int:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
async def edge_degree(self, src_id: str, tgt_id: str) -> int:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
async def get_node(self, node_id: str) -> Union[dict, None]:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
async def get_edge(
|
|
|
|
self, source_node_id: str, target_node_id: str
|
|
|
|
) -> Union[dict, None]:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
async def get_node_edges(
|
|
|
|
self, source_node_id: str
|
|
|
|
) -> Union[list[tuple[str, str]], None]:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
async def upsert_node(self, node_id: str, node_data: dict[str, str]):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
async def upsert_edge(
|
|
|
|
self, source_node_id: str, target_node_id: str, edge_data: dict[str, str]
|
|
|
|
):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2024-11-11 17:48:40 +08:00
|
|
|
async def delete_node(self, node_id: str):
|
2024-10-10 15:02:30 +08:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
async def embed_nodes(self, algorithm: str) -> tuple[np.ndarray, list[str]]:
|
2024-10-19 09:43:17 +05:30
|
|
|
raise NotImplementedError("Node embedding is not used in lightrag.")
|
2024-12-28 00:11:25 +08:00
|
|
|
|
2025-02-09 11:24:08 +01:00
|
|
|
async def get_all_labels(self) -> list[str]:
|
2025-01-25 18:54:40 +08:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
async def get_knowledge_graph(
|
|
|
|
self, node_label: str, max_depth: int = 5
|
2025-02-09 11:24:08 +01:00
|
|
|
) -> dict[str, list[dict]]:
|
2025-01-25 18:54:40 +08:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2024-12-28 00:11:25 +08:00
|
|
|
|
|
|
|
class DocStatus(str, Enum):
|
|
|
|
"""Document processing status enum"""
|
|
|
|
|
|
|
|
PENDING = "pending"
|
|
|
|
PROCESSING = "processing"
|
|
|
|
PROCESSED = "processed"
|
|
|
|
FAILED = "failed"
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class DocProcessingStatus:
|
|
|
|
"""Document processing status data structure"""
|
2025-02-09 15:25:58 +01:00
|
|
|
|
2025-02-09 15:24:37 +01:00
|
|
|
content: str
|
|
|
|
"""Original content of the document"""
|
|
|
|
content_summary: str
|
|
|
|
"""First 100 chars of document content, used for preview"""
|
|
|
|
content_length: int
|
|
|
|
"""Total length of document"""
|
|
|
|
status: DocStatus
|
|
|
|
"""Current processing status"""
|
|
|
|
created_at: str
|
|
|
|
"""ISO format timestamp when document was created"""
|
|
|
|
updated_at: str
|
|
|
|
"""ISO format timestamp when document was last updated"""
|
|
|
|
chunks_count: Optional[int] = None
|
|
|
|
"""Number of chunks after splitting, used for processing"""
|
|
|
|
error: Optional[str] = None
|
|
|
|
"""Error message if failed"""
|
|
|
|
metadata: dict[str, Any] = field(default_factory=dict)
|
|
|
|
"""Additional metadata"""
|
2024-12-28 00:11:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
class DocStatusStorage(BaseKVStorage):
|
|
|
|
"""Base class for document status storage"""
|
|
|
|
|
2025-02-09 11:24:08 +01:00
|
|
|
async def get_status_counts(self) -> dict[str, int]:
|
2024-12-28 00:11:25 +08:00
|
|
|
"""Get counts of documents in each status"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2025-02-09 11:24:08 +01:00
|
|
|
async def get_failed_docs(self) -> dict[str, DocProcessingStatus]:
|
2024-12-28 00:11:25 +08:00
|
|
|
"""Get all failed documents"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2025-02-09 11:24:08 +01:00
|
|
|
async def get_pending_docs(self) -> dict[str, DocProcessingStatus]:
|
2024-12-28 00:11:25 +08:00
|
|
|
"""Get all pending documents"""
|
|
|
|
raise NotImplementedError
|