yangdx commited on
Commit
79f312f
·
1 Parent(s): 77d8960

Add finalize_storages to sample code

Browse files
Files changed (1) hide show
  1. 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
- # Initialize RAG instance
96
- rag = await initialize_rag()
97
-
98
- with open("./book.txt", "r", encoding="utf-8") as f:
99
- await rag.ainsert(f.read())
100
-
101
- # Perform naive search
102
- print("\n=====================")
103
- print("Query mode: naive")
104
- print("=====================")
105
- print(
106
- await rag.aquery(
107
- "What are the top themes in this story?", param=QueryParam(mode="naive")
 
 
 
108
  )
109
- )
110
 
111
- # Perform local search
112
- print("\n=====================")
113
- print("Query mode: local")
114
- print("=====================")
115
- print(
116
- await rag.aquery(
117
- "What are the top themes in this story?", param=QueryParam(mode="local")
 
118
  )
119
- )
120
 
121
- # Perform global search
122
- print("\n=====================")
123
- print("Query mode: global")
124
- print("=====================")
125
- print(
126
- await rag.aquery(
127
- "What are the top themes in this story?", param=QueryParam(mode="global")
 
128
  )
129
- )
130
 
131
- # Perform hybrid search
132
- print("\n=====================")
133
- print("Query mode: hybrid")
134
- print("=====================")
135
- print(
136
- await rag.aquery(
137
- "What are the top themes in this story?", param=QueryParam(mode="hybrid")
 
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!")