yangdx
commited on
Commit
·
79f312f
1
Parent(s):
77d8960
Add finalize_storages to sample code
Browse files- examples/lightrag_openai_demo.py +46 -38
examples/lightrag_openai_demo.py
CHANGED
@@ -92,54 +92,62 @@ async def initialize_rag():
|
|
92 |
|
93 |
|
94 |
async def main():
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
|
|
|
|
|
|
108 |
)
|
109 |
-
)
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
118 |
)
|
119 |
-
)
|
120 |
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
128 |
)
|
129 |
-
)
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
|
|
138 |
)
|
139 |
-
|
|
|
|
|
|
|
|
|
140 |
|
141 |
|
142 |
if __name__ == "__main__":
|
143 |
# Configure logging before running the main function
|
144 |
configure_logging()
|
145 |
asyncio.run(main())
|
|
|
|
92 |
|
93 |
|
94 |
async def main():
|
95 |
+
rag = None
|
96 |
+
try:
|
97 |
+
# Initialize RAG instance
|
98 |
+
rag = await initialize_rag()
|
99 |
+
|
100 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
101 |
+
await rag.ainsert(f.read())
|
102 |
+
|
103 |
+
# Perform naive search
|
104 |
+
print("\n=====================")
|
105 |
+
print("Query mode: naive")
|
106 |
+
print("=====================")
|
107 |
+
print(
|
108 |
+
await rag.aquery(
|
109 |
+
"What are the top themes in this story?", param=QueryParam(mode="naive")
|
110 |
+
)
|
111 |
)
|
|
|
112 |
|
113 |
+
# Perform local search
|
114 |
+
print("\n=====================")
|
115 |
+
print("Query mode: local")
|
116 |
+
print("=====================")
|
117 |
+
print(
|
118 |
+
await rag.aquery(
|
119 |
+
"What are the top themes in this story?", param=QueryParam(mode="local")
|
120 |
+
)
|
121 |
)
|
|
|
122 |
|
123 |
+
# Perform global search
|
124 |
+
print("\n=====================")
|
125 |
+
print("Query mode: global")
|
126 |
+
print("=====================")
|
127 |
+
print(
|
128 |
+
await rag.aquery(
|
129 |
+
"What are the top themes in this story?", param=QueryParam(mode="global")
|
130 |
+
)
|
131 |
)
|
|
|
132 |
|
133 |
+
# Perform hybrid search
|
134 |
+
print("\n=====================")
|
135 |
+
print("Query mode: hybrid")
|
136 |
+
print("=====================")
|
137 |
+
print(
|
138 |
+
await rag.aquery(
|
139 |
+
"What are the top themes in this story?", param=QueryParam(mode="hybrid")
|
140 |
+
)
|
141 |
)
|
142 |
+
except Exception as e:
|
143 |
+
print(f"An error occurred: {e}")
|
144 |
+
finally:
|
145 |
+
if rag:
|
146 |
+
await rag.finalize_storages()
|
147 |
|
148 |
|
149 |
if __name__ == "__main__":
|
150 |
# Configure logging before running the main function
|
151 |
configure_logging()
|
152 |
asyncio.run(main())
|
153 |
+
print("\nDone!")
|