YanSte commited on
Commit
b22e80b
·
1 Parent(s): d971dd5

cleanup storages

Browse files
examples/lightrag_openai_compatible_stream_demo.py CHANGED
@@ -44,5 +44,3 @@ async def print_stream(stream):
44
  async for chunk in stream:
45
  if chunk:
46
  print(chunk, end="", flush=True)
47
-
48
-
 
44
  async for chunk in stream:
45
  if chunk:
46
  print(chunk, end="", flush=True)
 
 
lightrag/kg/__init__.py CHANGED
@@ -1 +1,136 @@
1
- # print ("init package vars here. ......")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ STORAGE_IMPLEMENTATIONS = {
2
+ "KV_STORAGE": {
3
+ "implementations": [
4
+ "JsonKVStorage",
5
+ "MongoKVStorage",
6
+ "RedisKVStorage",
7
+ "TiDBKVStorage",
8
+ "PGKVStorage",
9
+ "OracleKVStorage",
10
+ ],
11
+ "required_methods": ["get_by_id", "upsert"],
12
+ },
13
+ "GRAPH_STORAGE": {
14
+ "implementations": [
15
+ "NetworkXStorage",
16
+ "Neo4JStorage",
17
+ "MongoGraphStorage",
18
+ "TiDBGraphStorage",
19
+ "AGEStorage",
20
+ "GremlinStorage",
21
+ "PGGraphStorage",
22
+ "OracleGraphStorage",
23
+ ],
24
+ "required_methods": ["upsert_node", "upsert_edge"],
25
+ },
26
+ "VECTOR_STORAGE": {
27
+ "implementations": [
28
+ "NanoVectorDBStorage",
29
+ "MilvusVectorDBStorage",
30
+ "ChromaVectorDBStorage",
31
+ "TiDBVectorDBStorage",
32
+ "PGVectorStorage",
33
+ "FaissVectorDBStorage",
34
+ "QdrantVectorDBStorage",
35
+ "OracleVectorDBStorage",
36
+ "MongoVectorDBStorage",
37
+ ],
38
+ "required_methods": ["query", "upsert"],
39
+ },
40
+ "DOC_STATUS_STORAGE": {
41
+ "implementations": [
42
+ "JsonDocStatusStorage",
43
+ "PGDocStatusStorage",
44
+ "PGDocStatusStorage",
45
+ "MongoDocStatusStorage",
46
+ ],
47
+ "required_methods": ["get_docs_by_status"],
48
+ },
49
+ }
50
+
51
+ # Storage implementation environment variable without default value
52
+ STORAGE_ENV_REQUIREMENTS: dict[str, list[str]] = {
53
+ # KV Storage Implementations
54
+ "JsonKVStorage": [],
55
+ "MongoKVStorage": [],
56
+ "RedisKVStorage": ["REDIS_URI"],
57
+ "TiDBKVStorage": ["TIDB_USER", "TIDB_PASSWORD", "TIDB_DATABASE"],
58
+ "PGKVStorage": ["POSTGRES_USER", "POSTGRES_PASSWORD", "POSTGRES_DATABASE"],
59
+ "OracleKVStorage": [
60
+ "ORACLE_DSN",
61
+ "ORACLE_USER",
62
+ "ORACLE_PASSWORD",
63
+ "ORACLE_CONFIG_DIR",
64
+ ],
65
+ # Graph Storage Implementations
66
+ "NetworkXStorage": [],
67
+ "Neo4JStorage": ["NEO4J_URI", "NEO4J_USERNAME", "NEO4J_PASSWORD"],
68
+ "MongoGraphStorage": [],
69
+ "TiDBGraphStorage": ["TIDB_USER", "TIDB_PASSWORD", "TIDB_DATABASE"],
70
+ "AGEStorage": [
71
+ "AGE_POSTGRES_DB",
72
+ "AGE_POSTGRES_USER",
73
+ "AGE_POSTGRES_PASSWORD",
74
+ ],
75
+ "GremlinStorage": ["GREMLIN_HOST", "GREMLIN_PORT", "GREMLIN_GRAPH"],
76
+ "PGGraphStorage": [
77
+ "POSTGRES_USER",
78
+ "POSTGRES_PASSWORD",
79
+ "POSTGRES_DATABASE",
80
+ ],
81
+ "OracleGraphStorage": [
82
+ "ORACLE_DSN",
83
+ "ORACLE_USER",
84
+ "ORACLE_PASSWORD",
85
+ "ORACLE_CONFIG_DIR",
86
+ ],
87
+ # Vector Storage Implementations
88
+ "NanoVectorDBStorage": [],
89
+ "MilvusVectorDBStorage": [],
90
+ "ChromaVectorDBStorage": [],
91
+ "TiDBVectorDBStorage": ["TIDB_USER", "TIDB_PASSWORD", "TIDB_DATABASE"],
92
+ "PGVectorStorage": ["POSTGRES_USER", "POSTGRES_PASSWORD", "POSTGRES_DATABASE"],
93
+ "FaissVectorDBStorage": [],
94
+ "QdrantVectorDBStorage": ["QDRANT_URL"], # QDRANT_API_KEY has default value None
95
+ "OracleVectorDBStorage": [
96
+ "ORACLE_DSN",
97
+ "ORACLE_USER",
98
+ "ORACLE_PASSWORD",
99
+ "ORACLE_CONFIG_DIR",
100
+ ],
101
+ "MongoVectorDBStorage": [],
102
+ # Document Status Storage Implementations
103
+ "JsonDocStatusStorage": [],
104
+ "PGDocStatusStorage": ["POSTGRES_USER", "POSTGRES_PASSWORD", "POSTGRES_DATABASE"],
105
+ "MongoDocStatusStorage": [],
106
+ }
107
+
108
+ # Storage implementation module mapping
109
+ STORAGES = {
110
+ "NetworkXStorage": ".kg.networkx_impl",
111
+ "JsonKVStorage": ".kg.json_kv_impl",
112
+ "NanoVectorDBStorage": ".kg.nano_vector_db_impl",
113
+ "JsonDocStatusStorage": ".kg.json_doc_status_impl",
114
+ "Neo4JStorage": ".kg.neo4j_impl",
115
+ "OracleKVStorage": ".kg.oracle_impl",
116
+ "OracleGraphStorage": ".kg.oracle_impl",
117
+ "OracleVectorDBStorage": ".kg.oracle_impl",
118
+ "MilvusVectorDBStorage": ".kg.milvus_impl",
119
+ "MongoKVStorage": ".kg.mongo_impl",
120
+ "MongoDocStatusStorage": ".kg.mongo_impl",
121
+ "MongoGraphStorage": ".kg.mongo_impl",
122
+ "MongoVectorDBStorage": ".kg.mongo_impl",
123
+ "RedisKVStorage": ".kg.redis_impl",
124
+ "ChromaVectorDBStorage": ".kg.chroma_impl",
125
+ "TiDBKVStorage": ".kg.tidb_impl",
126
+ "TiDBVectorDBStorage": ".kg.tidb_impl",
127
+ "TiDBGraphStorage": ".kg.tidb_impl",
128
+ "PGKVStorage": ".kg.postgres_impl",
129
+ "PGVectorStorage": ".kg.postgres_impl",
130
+ "AGEStorage": ".kg.age_impl",
131
+ "PGGraphStorage": ".kg.postgres_impl",
132
+ "GremlinStorage": ".kg.gremlin_impl",
133
+ "PGDocStatusStorage": ".kg.postgres_impl",
134
+ "FaissVectorDBStorage": ".kg.faiss_impl",
135
+ "QdrantVectorDBStorage": ".kg.qdrant_impl",
136
+ }
lightrag/lightrag.py CHANGED
@@ -8,6 +8,8 @@ from datetime import datetime
8
  from functools import partial
9
  from typing import Any, AsyncIterator, Callable, Iterator, cast, final
10
 
 
 
11
  from .base import (
12
  BaseGraphStorage,
13
  BaseKVStorage,
@@ -45,149 +47,6 @@ from .utils import (
45
  config = configparser.ConfigParser()
46
  config.read("config.ini", "utf-8")
47
 
48
- # Storage type and implementation compatibility validation table
49
- STORAGE_IMPLEMENTATIONS = {
50
- "KV_STORAGE": {
51
- "implementations": [
52
- "JsonKVStorage",
53
- "MongoKVStorage",
54
- "RedisKVStorage",
55
- "TiDBKVStorage",
56
- "PGKVStorage",
57
- "OracleKVStorage",
58
- ],
59
- "required_methods": ["get_by_id", "upsert"],
60
- },
61
- "GRAPH_STORAGE": {
62
- "implementations": [
63
- "NetworkXStorage",
64
- "Neo4JStorage",
65
- "MongoGraphStorage",
66
- "TiDBGraphStorage",
67
- "AGEStorage",
68
- "GremlinStorage",
69
- "PGGraphStorage",
70
- "OracleGraphStorage",
71
- ],
72
- "required_methods": ["upsert_node", "upsert_edge"],
73
- },
74
- "VECTOR_STORAGE": {
75
- "implementations": [
76
- "NanoVectorDBStorage",
77
- "MilvusVectorDBStorage",
78
- "ChromaVectorDBStorage",
79
- "TiDBVectorDBStorage",
80
- "PGVectorStorage",
81
- "FaissVectorDBStorage",
82
- "QdrantVectorDBStorage",
83
- "OracleVectorDBStorage",
84
- "MongoVectorDBStorage",
85
- ],
86
- "required_methods": ["query", "upsert"],
87
- },
88
- "DOC_STATUS_STORAGE": {
89
- "implementations": [
90
- "JsonDocStatusStorage",
91
- "PGDocStatusStorage",
92
- "PGDocStatusStorage",
93
- "MongoDocStatusStorage",
94
- ],
95
- "required_methods": ["get_docs_by_status"],
96
- },
97
- }
98
-
99
- # Storage implementation environment variable without default value
100
- STORAGE_ENV_REQUIREMENTS: dict[str, list[str]] = {
101
- # KV Storage Implementations
102
- "JsonKVStorage": [],
103
- "MongoKVStorage": [],
104
- "RedisKVStorage": ["REDIS_URI"],
105
- "TiDBKVStorage": ["TIDB_USER", "TIDB_PASSWORD", "TIDB_DATABASE"],
106
- "PGKVStorage": ["POSTGRES_USER", "POSTGRES_PASSWORD", "POSTGRES_DATABASE"],
107
- "OracleKVStorage": [
108
- "ORACLE_DSN",
109
- "ORACLE_USER",
110
- "ORACLE_PASSWORD",
111
- "ORACLE_CONFIG_DIR",
112
- ],
113
- # Graph Storage Implementations
114
- "NetworkXStorage": [],
115
- "Neo4JStorage": ["NEO4J_URI", "NEO4J_USERNAME", "NEO4J_PASSWORD"],
116
- "MongoGraphStorage": [],
117
- "TiDBGraphStorage": ["TIDB_USER", "TIDB_PASSWORD", "TIDB_DATABASE"],
118
- "AGEStorage": [
119
- "AGE_POSTGRES_DB",
120
- "AGE_POSTGRES_USER",
121
- "AGE_POSTGRES_PASSWORD",
122
- ],
123
- "GremlinStorage": ["GREMLIN_HOST", "GREMLIN_PORT", "GREMLIN_GRAPH"],
124
- "PGGraphStorage": [
125
- "POSTGRES_USER",
126
- "POSTGRES_PASSWORD",
127
- "POSTGRES_DATABASE",
128
- ],
129
- "OracleGraphStorage": [
130
- "ORACLE_DSN",
131
- "ORACLE_USER",
132
- "ORACLE_PASSWORD",
133
- "ORACLE_CONFIG_DIR",
134
- ],
135
- # Vector Storage Implementations
136
- "NanoVectorDBStorage": [],
137
- "MilvusVectorDBStorage": [],
138
- "ChromaVectorDBStorage": [],
139
- "TiDBVectorDBStorage": ["TIDB_USER", "TIDB_PASSWORD", "TIDB_DATABASE"],
140
- "PGVectorStorage": ["POSTGRES_USER", "POSTGRES_PASSWORD", "POSTGRES_DATABASE"],
141
- "FaissVectorDBStorage": [],
142
- "QdrantVectorDBStorage": ["QDRANT_URL"], # QDRANT_API_KEY has default value None
143
- "OracleVectorDBStorage": [
144
- "ORACLE_DSN",
145
- "ORACLE_USER",
146
- "ORACLE_PASSWORD",
147
- "ORACLE_CONFIG_DIR",
148
- ],
149
- "MongoVectorDBStorage": [],
150
- # Document Status Storage Implementations
151
- "JsonDocStatusStorage": [],
152
- "PGDocStatusStorage": ["POSTGRES_USER", "POSTGRES_PASSWORD", "POSTGRES_DATABASE"],
153
- "MongoDocStatusStorage": [],
154
- }
155
-
156
- # Storage implementation module mapping
157
- STORAGES = {
158
- "NetworkXStorage": ".kg.networkx_impl",
159
- "JsonKVStorage": ".kg.json_kv_impl",
160
- "NanoVectorDBStorage": ".kg.nano_vector_db_impl",
161
- "JsonDocStatusStorage": ".kg.json_doc_status_impl",
162
- "Neo4JStorage": ".kg.neo4j_impl",
163
- "OracleKVStorage": ".kg.oracle_impl",
164
- "OracleGraphStorage": ".kg.oracle_impl",
165
- "OracleVectorDBStorage": ".kg.oracle_impl",
166
- "MilvusVectorDBStorage": ".kg.milvus_impl",
167
- "MongoKVStorage": ".kg.mongo_impl",
168
- "MongoDocStatusStorage": ".kg.mongo_impl",
169
- "MongoGraphStorage": ".kg.mongo_impl",
170
- "MongoVectorDBStorage": ".kg.mongo_impl",
171
- "RedisKVStorage": ".kg.redis_impl",
172
- "ChromaVectorDBStorage": ".kg.chroma_impl",
173
- "TiDBKVStorage": ".kg.tidb_impl",
174
- "TiDBVectorDBStorage": ".kg.tidb_impl",
175
- "TiDBGraphStorage": ".kg.tidb_impl",
176
- "PGKVStorage": ".kg.postgres_impl",
177
- "PGVectorStorage": ".kg.postgres_impl",
178
- "AGEStorage": ".kg.age_impl",
179
- "PGGraphStorage": ".kg.postgres_impl",
180
- "GremlinStorage": ".kg.gremlin_impl",
181
- "PGDocStatusStorage": ".kg.postgres_impl",
182
- "FaissVectorDBStorage": ".kg.faiss_impl",
183
- "QdrantVectorDBStorage": ".kg.qdrant_impl",
184
- }
185
-
186
-
187
-
188
-
189
-
190
-
191
 
192
  @final
193
  @dataclass
@@ -1643,4 +1502,4 @@ class LightRAG:
1643
  raise ValueError(
1644
  f"Storage implementation '{storage_name}' requires the following "
1645
  f"environment variables: {', '.join(missing_vars)}"
1646
- )
 
8
  from functools import partial
9
  from typing import Any, AsyncIterator, Callable, Iterator, cast, final
10
 
11
+ from lightrag.kg import STORAGE_ENV_REQUIREMENTS, STORAGE_IMPLEMENTATIONS, STORAGES
12
+
13
  from .base import (
14
  BaseGraphStorage,
15
  BaseKVStorage,
 
47
  config = configparser.ConfigParser()
48
  config.read("config.ini", "utf-8")
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
  @final
52
  @dataclass
 
1502
  raise ValueError(
1503
  f"Storage implementation '{storage_name}' requires the following "
1504
  f"environment variables: {', '.join(missing_vars)}"
1505
+ )
lightrag/utils.py CHANGED
@@ -714,6 +714,7 @@ def get_conversation_turns(
714
 
715
  return "\n".join(formatted_turns)
716
 
 
717
  def always_get_an_event_loop() -> asyncio.AbstractEventLoop:
718
  """
719
  Ensure that there is always an event loop available.
@@ -737,8 +738,8 @@ def always_get_an_event_loop() -> asyncio.AbstractEventLoop:
737
  new_loop = asyncio.new_event_loop()
738
  asyncio.set_event_loop(new_loop)
739
  return new_loop
740
-
741
-
742
  def lazy_external_import(module_name: str, class_name: str) -> Callable[..., Any]:
743
  """Lazily import a class from an external module based on the package of the caller."""
744
  # Get the caller's module and package
@@ -756,4 +757,3 @@ def lazy_external_import(module_name: str, class_name: str) -> Callable[..., Any
756
  return cls(*args, **kwargs)
757
 
758
  return import_class
759
-
 
714
 
715
  return "\n".join(formatted_turns)
716
 
717
+
718
  def always_get_an_event_loop() -> asyncio.AbstractEventLoop:
719
  """
720
  Ensure that there is always an event loop available.
 
738
  new_loop = asyncio.new_event_loop()
739
  asyncio.set_event_loop(new_loop)
740
  return new_loop
741
+
742
+
743
  def lazy_external_import(module_name: str, class_name: str) -> Callable[..., Any]:
744
  """Lazily import a class from an external module based on the package of the caller."""
745
  # Get the caller's module and package
 
757
  return cls(*args, **kwargs)
758
 
759
  return import_class
 
reproduce/Step_3.py CHANGED
@@ -23,7 +23,6 @@ async def process_query(query_text, rag_instance, query_param):
23
  return None, {"query": query_text, "error": str(e)}
24
 
25
 
26
-
27
  def run_queries_and_save_to_json(
28
  queries, rag_instance, query_param, output_file, error_file
29
  ):
 
23
  return None, {"query": query_text, "error": str(e)}
24
 
25
 
 
26
  def run_queries_and_save_to_json(
27
  queries, rag_instance, query_param, output_file, error_file
28
  ):
reproduce/Step_3_openai_compatible.py CHANGED
@@ -54,9 +54,6 @@ async def process_query(query_text, rag_instance, query_param):
54
  return None, {"query": query_text, "error": str(e)}
55
 
56
 
57
-
58
-
59
-
60
  def run_queries_and_save_to_json(
61
  queries, rag_instance, query_param, output_file, error_file
62
  ):
 
54
  return None, {"query": query_text, "error": str(e)}
55
 
56
 
 
 
 
57
  def run_queries_and_save_to_json(
58
  queries, rag_instance, query_param, output_file, error_file
59
  ):