YanSte commited on
Commit
8909488
·
1 Parent(s): f439ca5

added type and cleaned code

Browse files
Files changed (2) hide show
  1. lightrag/base.py +24 -10
  2. lightrag/lightrag.py +11 -5
lightrag/base.py CHANGED
@@ -95,7 +95,7 @@ class StorageNameSpace:
95
  @dataclass
96
  class BaseVectorStorage(StorageNameSpace):
97
  embedding_func: EmbeddingFunc
98
- meta_fields: set = field(default_factory=set)
99
 
100
  async def query(self, query: str, top_k: int) -> list[dict[str, Any]]:
101
  raise NotImplementedError
@@ -130,50 +130,64 @@ class BaseKVStorage(StorageNameSpace):
130
 
131
  @dataclass
132
  class BaseGraphStorage(StorageNameSpace):
133
- embedding_func: EmbeddingFunc = None
134
-
135
  async def has_node(self, node_id: str) -> bool:
136
  raise NotImplementedError
137
 
 
138
  async def has_edge(self, source_node_id: str, target_node_id: str) -> bool:
139
  raise NotImplementedError
140
 
 
141
  async def node_degree(self, node_id: str) -> int:
142
  raise NotImplementedError
143
 
 
144
  async def edge_degree(self, src_id: str, tgt_id: str) -> int:
145
  raise NotImplementedError
146
 
147
- async def get_node(self, node_id: str) -> Union[dict, None]:
 
148
  raise NotImplementedError
149
 
 
150
  async def get_edge(
151
  self, source_node_id: str, target_node_id: str
152
- ) -> Union[dict, None]:
153
  raise NotImplementedError
154
 
 
155
  async def get_node_edges(
156
  self, source_node_id: str
157
  ) -> Union[list[tuple[str, str]], None]:
158
  raise NotImplementedError
159
 
160
- async def upsert_node(self, node_id: str, node_data: dict[str, str]):
 
161
  raise NotImplementedError
162
 
 
163
  async def upsert_edge(
164
- self, source_node_id: str, target_node_id: str, edge_data: dict[str, str]
165
- ):
 
 
166
  raise NotImplementedError
167
 
168
- async def delete_node(self, node_id: str):
 
169
  raise NotImplementedError
170
 
171
- async def embed_nodes(self, algorithm: str) -> tuple[np.ndarray, list[str]]:
 
172
  raise NotImplementedError("Node embedding is not used in lightrag.")
173
 
 
174
  async def get_all_labels(self) -> list[str]:
175
  raise NotImplementedError
176
 
 
177
  async def get_knowledge_graph(
178
  self, node_label: str, max_depth: int = 5
179
  ) -> KnowledgeGraph:
 
95
  @dataclass
96
  class BaseVectorStorage(StorageNameSpace):
97
  embedding_func: EmbeddingFunc
98
+ meta_fields: set[str] = field(default_factory=set)
99
 
100
  async def query(self, query: str, top_k: int) -> list[dict[str, Any]]:
101
  raise NotImplementedError
 
130
 
131
  @dataclass
132
  class BaseGraphStorage(StorageNameSpace):
133
+ embedding_func: EmbeddingFunc | None = None
134
+ """Check if a node exists in the graph."""
135
  async def has_node(self, node_id: str) -> bool:
136
  raise NotImplementedError
137
 
138
+ """Check if an edge exists in the graph."""
139
  async def has_edge(self, source_node_id: str, target_node_id: str) -> bool:
140
  raise NotImplementedError
141
 
142
+ """Get the degree of a node."""
143
  async def node_degree(self, node_id: str) -> int:
144
  raise NotImplementedError
145
 
146
+ """Get the degree of an edge."""
147
  async def edge_degree(self, src_id: str, tgt_id: str) -> int:
148
  raise NotImplementedError
149
 
150
+ """Get a node by its id."""
151
+ async def get_node(self, node_id: str) -> Union[dict[str, str], None]:
152
  raise NotImplementedError
153
 
154
+ """Get an edge by its source and target node ids."""
155
  async def get_edge(
156
  self, source_node_id: str, target_node_id: str
157
+ ) -> Union[dict[str, str], None]:
158
  raise NotImplementedError
159
 
160
+ """Get all edges connected to a node."""
161
  async def get_node_edges(
162
  self, source_node_id: str
163
  ) -> Union[list[tuple[str, str]], None]:
164
  raise NotImplementedError
165
 
166
+ """Upsert a node into the graph."""
167
+ async def upsert_node(self, node_id: str, node_data: dict[str, str]) -> None:
168
  raise NotImplementedError
169
 
170
+ """Upsert an edge into the graph."""
171
  async def upsert_edge(
172
+ self, source_node_id: str,
173
+ target_node_id: str,
174
+ edge_data: dict[str, str]
175
+ ) -> None:
176
  raise NotImplementedError
177
 
178
+ """Delete a node from the graph."""
179
+ async def delete_node(self, node_id: str) -> None:
180
  raise NotImplementedError
181
 
182
+ """Embed nodes using an algorithm."""
183
+ async def embed_nodes(self, algorithm: str) -> tuple[np.ndarray[Any, Any], list[str]]:
184
  raise NotImplementedError("Node embedding is not used in lightrag.")
185
 
186
+ """Get all labels in the graph."""
187
  async def get_all_labels(self) -> list[str]:
188
  raise NotImplementedError
189
 
190
+ """Get a knowledge graph of a node."""
191
  async def get_knowledge_graph(
192
  self, node_label: str, max_depth: int = 5
193
  ) -> KnowledgeGraph:
lightrag/lightrag.py CHANGED
@@ -6,7 +6,7 @@ import configparser
6
  from dataclasses import asdict, dataclass, field
7
  from datetime import datetime
8
  from functools import partial
9
- from typing import Any, Callable, Optional, Union, cast
10
 
11
  from .base import (
12
  BaseGraphStorage,
@@ -983,7 +983,7 @@ class LightRAG:
983
 
984
  def query(
985
  self, query: str, param: QueryParam = QueryParam(), prompt: str | None = None
986
- ) -> str:
987
  """
988
  Perform a sync query.
989
 
@@ -1003,7 +1003,7 @@ class LightRAG:
1003
  query: str,
1004
  param: QueryParam = QueryParam(),
1005
  prompt: str | None = None,
1006
- ) -> str:
1007
  """
1008
  Perform a async query.
1009
 
@@ -1081,7 +1081,10 @@ class LightRAG:
1081
  return response
1082
 
1083
  def query_with_separate_keyword_extraction(
1084
- self, query: str, prompt: str, param: QueryParam = QueryParam()
 
 
 
1085
  ):
1086
  """
1087
  1. Extract keywords from the 'query' using new function in operate.py.
@@ -1093,7 +1096,10 @@ class LightRAG:
1093
  )
1094
 
1095
  async def aquery_with_separate_keyword_extraction(
1096
- self, query: str, prompt: str, param: QueryParam = QueryParam()
 
 
 
1097
  ):
1098
  """
1099
  1. Calls extract_keywords_only to get HL/LL keywords from 'query'.
 
6
  from dataclasses import asdict, dataclass, field
7
  from datetime import datetime
8
  from functools import partial
9
+ from typing import Any, AsyncIterator, Callable, Iterator, Optional, Union, cast
10
 
11
  from .base import (
12
  BaseGraphStorage,
 
983
 
984
  def query(
985
  self, query: str, param: QueryParam = QueryParam(), prompt: str | None = None
986
+ ) -> str | Iterator[str]:
987
  """
988
  Perform a sync query.
989
 
 
1003
  query: str,
1004
  param: QueryParam = QueryParam(),
1005
  prompt: str | None = None,
1006
+ ) -> str | AsyncIterator[str]:
1007
  """
1008
  Perform a async query.
1009
 
 
1081
  return response
1082
 
1083
  def query_with_separate_keyword_extraction(
1084
+ self,
1085
+ query: str,
1086
+ prompt: str,
1087
+ param: QueryParam = QueryParam()
1088
  ):
1089
  """
1090
  1. Extract keywords from the 'query' using new function in operate.py.
 
1096
  )
1097
 
1098
  async def aquery_with_separate_keyword_extraction(
1099
+ self,
1100
+ query: str,
1101
+ prompt: str,
1102
+ param: QueryParam = QueryParam()
1103
  ):
1104
  """
1105
  1. Calls extract_keywords_only to get HL/LL keywords from 'query'.