fix demo
Browse files- .gitignore +1 -1
- README.md +114 -115
- examples/copy_llm_cache_to_another_storage.py +1 -1
- examples/lightrag_api_ollama_demo.py +34 -15
- examples/lightrag_api_openai_compatible_demo.py +30 -9
- examples/lightrag_api_openai_compatible_demo_simplified.py +0 -101
- examples/lightrag_api_oracle_demo.py +4 -0
- examples/lightrag_azure_openai_demo.py +4 -0
- examples/lightrag_bedrock_demo.py +33 -18
- examples/lightrag_gemini_demo.py +36 -18
- examples/lightrag_hf_demo.py +51 -34
- examples/lightrag_jinaai_demo.py +0 -115
- examples/lightrag_llamaindex_direct_demo.py +55 -35
- examples/lightrag_llamaindex_litellm_demo.py +55 -35
- examples/lightrag_lmdeploy_demo.py +54 -35
- examples/lightrag_nvidia_demo.py +29 -28
- examples/lightrag_ollama_age_demo.py +67 -49
- examples/lightrag_ollama_demo.py +61 -44
- examples/lightrag_ollama_gremlin_demo.py +63 -49
- examples/lightrag_ollama_neo4j_milvus_mongo_demo.py +60 -26
- examples/lightrag_openai_compatible_demo.py +20 -12
- examples/lightrag_openai_compatible_demo_embedding_cache.py +25 -16
- examples/lightrag_openai_compatible_stream_demo.py +43 -29
- examples/lightrag_openai_demo.py +39 -25
- examples/lightrag_openai_mongodb_graph_demo.py +34 -9
- examples/lightrag_openai_neo4j_milvus_redis_demo.py +47 -16
- examples/lightrag_oracle_demo.py +40 -32
- examples/lightrag_siliconcloud_demo.py +39 -25
- examples/lightrag_tidb_demo.py +32 -24
- examples/lightrag_zhipu_demo.py +49 -35
- examples/lightrag_zhipu_postgres_demo.py +11 -3
- examples/query_keyword_separation_example.py +21 -13
- examples/test.py +43 -28
- examples/test_chromadb.py +31 -22
- examples/test_faiss.py +13 -5
- examples/test_neo4j.py +45 -31
- examples/test_split_by_character.ipynb +27 -10
- examples/vram_management_demo.py +67 -53
- reproduce/Step_1.py +17 -2
- reproduce/Step_1_openai_compatible.py +23 -8
- run_with_gunicorn.py +0 -203
.gitignore
CHANGED
@@ -57,7 +57,7 @@ ignore_this.txt
|
|
57 |
*.ignore.*
|
58 |
|
59 |
# Project-specific files
|
60 |
-
dickens
|
61 |
book.txt
|
62 |
lightrag-dev/
|
63 |
gui/
|
|
|
57 |
*.ignore.*
|
58 |
|
59 |
# Project-specific files
|
60 |
+
dickens*/
|
61 |
book.txt
|
62 |
lightrag-dev/
|
63 |
gui/
|
README.md
CHANGED
@@ -102,33 +102,47 @@ Use the below Python snippet (in a script) to initialize LightRAG and perform qu
|
|
102 |
|
103 |
```python
|
104 |
import os
|
|
|
105 |
from lightrag import LightRAG, QueryParam
|
106 |
from lightrag.llm.openai import gpt_4o_mini_complete, gpt_4o_complete, openai_embed
|
|
|
107 |
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
113 |
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
#
|
126 |
-
mode="
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
```
|
133 |
|
134 |
### Query Param
|
@@ -190,15 +204,21 @@ async def embedding_func(texts: list[str]) -> np.ndarray:
|
|
190 |
base_url="https://api.upstage.ai/v1/solar"
|
191 |
)
|
192 |
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
|
|
|
|
200 |
)
|
201 |
-
|
|
|
|
|
|
|
|
|
202 |
```
|
203 |
</details>
|
204 |
|
@@ -210,10 +230,6 @@ rag = LightRAG(
|
|
210 |
See `lightrag_hf_demo.py`
|
211 |
|
212 |
```python
|
213 |
-
from lightrag.llm import hf_model_complete, hf_embed
|
214 |
-
from transformers import AutoModel, AutoTokenizer
|
215 |
-
from lightrag.utils import EmbeddingFunc
|
216 |
-
|
217 |
# Initialize LightRAG with Hugging Face model
|
218 |
rag = LightRAG(
|
219 |
working_dir=WORKING_DIR,
|
@@ -242,9 +258,6 @@ If you want to use Ollama models, you need to pull model you plan to use and emb
|
|
242 |
Then you only need to set LightRAG as follows:
|
243 |
|
244 |
```python
|
245 |
-
from lightrag.llm.ollama import ollama_model_complete, ollama_embed
|
246 |
-
from lightrag.utils import EmbeddingFunc
|
247 |
-
|
248 |
# Initialize LightRAG with Ollama model
|
249 |
rag = LightRAG(
|
250 |
working_dir=WORKING_DIR,
|
@@ -325,20 +338,58 @@ LightRAG supports integration with LlamaIndex.
|
|
325 |
|
326 |
```python
|
327 |
# Using LlamaIndex with direct OpenAI access
|
|
|
328 |
from lightrag import LightRAG
|
329 |
from lightrag.llm.llama_index_impl import llama_index_complete_if_cache, llama_index_embed
|
330 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
331 |
from llama_index.llms.openai import OpenAI
|
|
|
332 |
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
342 |
```
|
343 |
|
344 |
#### For detailed documentation and examples, see:
|
@@ -353,11 +404,6 @@ rag = LightRAG(
|
|
353 |
LightRAG now supports multi-turn dialogue through the conversation history feature. Here's how to use it:
|
354 |
|
355 |
```python
|
356 |
-
from lightrag import LightRAG, QueryParam
|
357 |
-
|
358 |
-
# Initialize LightRAG
|
359 |
-
rag = LightRAG(working_dir=WORKING_DIR)
|
360 |
-
|
361 |
# Create conversation history
|
362 |
conversation_history = [
|
363 |
{"role": "user", "content": "What is the main character's attitude towards Christmas?"},
|
@@ -387,11 +433,6 @@ response = rag.query(
|
|
387 |
LightRAG now supports custom prompts for fine-tuned control over the system's behavior. Here's how to use it:
|
388 |
|
389 |
```python
|
390 |
-
from lightrag import LightRAG, QueryParam
|
391 |
-
|
392 |
-
# Initialize LightRAG
|
393 |
-
rag = LightRAG(working_dir=WORKING_DIR)
|
394 |
-
|
395 |
# Create query parameters
|
396 |
query_param = QueryParam(
|
397 |
mode="hybrid", # or other mode: "local", "global", "hybrid", "mix" and "naive"
|
@@ -456,16 +497,6 @@ rag.query_with_separate_keyword_extraction(
|
|
456 |
<summary> <b>Insert Custom KG</b> </summary>
|
457 |
|
458 |
```python
|
459 |
-
rag = LightRAG(
|
460 |
-
working_dir=WORKING_DIR,
|
461 |
-
llm_model_func=llm_model_func,
|
462 |
-
embedding_func=EmbeddingFunc(
|
463 |
-
embedding_dim=embedding_dimension,
|
464 |
-
max_token_size=8192,
|
465 |
-
func=embedding_func,
|
466 |
-
),
|
467 |
-
)
|
468 |
-
|
469 |
custom_kg = {
|
470 |
"entities": [
|
471 |
{
|
@@ -534,6 +565,7 @@ rag = LightRAG(
|
|
534 |
"insert_batch_size": 20 # Process 20 documents per batch
|
535 |
}
|
536 |
)
|
|
|
537 |
rag.insert(["TEXT1", "TEXT2", "TEXT3", ...]) # Documents will be processed in batches of 20
|
538 |
```
|
539 |
|
@@ -560,27 +592,6 @@ rag.insert(["TEXT1", "TEXT2",...], ids=["ID_FOR_TEXT1", "ID_FOR_TEXT2"])
|
|
560 |
|
561 |
</details>
|
562 |
|
563 |
-
<details>
|
564 |
-
<summary><b>Incremental Insert</b></summary>
|
565 |
-
|
566 |
-
```python
|
567 |
-
# Incremental Insert: Insert new documents into an existing LightRAG instance
|
568 |
-
rag = LightRAG(
|
569 |
-
working_dir=WORKING_DIR,
|
570 |
-
llm_model_func=llm_model_func,
|
571 |
-
embedding_func=EmbeddingFunc(
|
572 |
-
embedding_dim=embedding_dimension,
|
573 |
-
max_token_size=8192,
|
574 |
-
func=embedding_func,
|
575 |
-
),
|
576 |
-
)
|
577 |
-
|
578 |
-
with open("./newText.txt") as f:
|
579 |
-
rag.insert(f.read())
|
580 |
-
```
|
581 |
-
|
582 |
-
</details>
|
583 |
-
|
584 |
<details>
|
585 |
<summary><b>Insert using Pipeline</b></summary>
|
586 |
|
@@ -592,6 +603,7 @@ And using a routine to process news documents.
|
|
592 |
|
593 |
```python
|
594 |
rag = LightRAG(..)
|
|
|
595 |
await rag.apipeline_enqueue_documents(input)
|
596 |
# Your routine in loop
|
597 |
await rag.apipeline_process_enqueue_documents(input)
|
@@ -633,8 +645,6 @@ export NEO4J_PASSWORD="password"
|
|
633 |
|
634 |
# Note: Default settings use NetworkX
|
635 |
# Initialize LightRAG with Neo4J implementation.
|
636 |
-
WORKING_DIR = "./local_neo4jWorkDir"
|
637 |
-
|
638 |
rag = LightRAG(
|
639 |
working_dir=WORKING_DIR,
|
640 |
llm_model_func=gpt_4o_mini_complete, # Use gpt_4o_mini_complete LLM model
|
@@ -706,26 +716,26 @@ You can also install `faiss-gpu` if you have GPU support.
|
|
706 |
|
707 |
- Here we are using `sentence-transformers` but you can also use `OpenAIEmbedding` model with `3072` dimensions.
|
708 |
|
709 |
-
```
|
710 |
async def embedding_func(texts: list[str]) -> np.ndarray:
|
711 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
712 |
embeddings = model.encode(texts, convert_to_numpy=True)
|
713 |
return embeddings
|
714 |
|
715 |
# Initialize LightRAG with the LLM model function and embedding function
|
716 |
-
|
717 |
-
|
718 |
-
|
719 |
-
|
720 |
-
|
721 |
-
|
722 |
-
|
723 |
-
|
724 |
-
|
725 |
-
|
726 |
-
|
727 |
-
|
728 |
-
|
729 |
```
|
730 |
|
731 |
</details>
|
@@ -733,17 +743,6 @@ async def embedding_func(texts: list[str]) -> np.ndarray:
|
|
733 |
## Delete
|
734 |
|
735 |
```python
|
736 |
-
|
737 |
-
rag = LightRAG(
|
738 |
-
working_dir=WORKING_DIR,
|
739 |
-
llm_model_func=llm_model_func,
|
740 |
-
embedding_func=EmbeddingFunc(
|
741 |
-
embedding_dim=embedding_dimension,
|
742 |
-
max_token_size=8192,
|
743 |
-
func=embedding_func,
|
744 |
-
),
|
745 |
-
)
|
746 |
-
|
747 |
# Delete Entity: Deleting entities by their names
|
748 |
rag.delete_by_entity("Project Gutenberg")
|
749 |
|
|
|
102 |
|
103 |
```python
|
104 |
import os
|
105 |
+
import asyncio
|
106 |
from lightrag import LightRAG, QueryParam
|
107 |
from lightrag.llm.openai import gpt_4o_mini_complete, gpt_4o_complete, openai_embed
|
108 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
109 |
|
110 |
+
async def initialize_rag():
|
111 |
+
rag = LightRAG(
|
112 |
+
working_dir="your/path",
|
113 |
+
embedding_func=openai_embed,
|
114 |
+
llm_model_func=gpt_4o_mini_complete
|
115 |
+
)
|
116 |
|
117 |
+
await rag.initialize_storages()
|
118 |
+
await initialize_pipeline_status()
|
119 |
+
|
120 |
+
return rag
|
121 |
+
|
122 |
+
def main():
|
123 |
+
# Initialize RAG instance
|
124 |
+
rag = asyncio.run(initialize_rag())
|
125 |
+
# Insert text
|
126 |
+
rag.insert("Your text")
|
127 |
+
|
128 |
+
# Perform naive search
|
129 |
+
mode="naive"
|
130 |
+
# Perform local search
|
131 |
+
mode="local"
|
132 |
+
# Perform global search
|
133 |
+
mode="global"
|
134 |
+
# Perform hybrid search
|
135 |
+
mode="hybrid"
|
136 |
+
# Mix mode Integrates knowledge graph and vector retrieval.
|
137 |
+
mode="mix"
|
138 |
+
|
139 |
+
rag.query(
|
140 |
+
"What are the top themes in this story?",
|
141 |
+
param=QueryParam(mode=mode)
|
142 |
+
)
|
143 |
+
|
144 |
+
if __name__ == "__main__":
|
145 |
+
main()
|
146 |
```
|
147 |
|
148 |
### Query Param
|
|
|
204 |
base_url="https://api.upstage.ai/v1/solar"
|
205 |
)
|
206 |
|
207 |
+
async def initialize_rag():
|
208 |
+
rag = LightRAG(
|
209 |
+
working_dir=WORKING_DIR,
|
210 |
+
llm_model_func=llm_model_func,
|
211 |
+
embedding_func=EmbeddingFunc(
|
212 |
+
embedding_dim=4096,
|
213 |
+
max_token_size=8192,
|
214 |
+
func=embedding_func
|
215 |
+
)
|
216 |
)
|
217 |
+
|
218 |
+
await rag.initialize_storages()
|
219 |
+
await initialize_pipeline_status()
|
220 |
+
|
221 |
+
return rag
|
222 |
```
|
223 |
</details>
|
224 |
|
|
|
230 |
See `lightrag_hf_demo.py`
|
231 |
|
232 |
```python
|
|
|
|
|
|
|
|
|
233 |
# Initialize LightRAG with Hugging Face model
|
234 |
rag = LightRAG(
|
235 |
working_dir=WORKING_DIR,
|
|
|
258 |
Then you only need to set LightRAG as follows:
|
259 |
|
260 |
```python
|
|
|
|
|
|
|
261 |
# Initialize LightRAG with Ollama model
|
262 |
rag = LightRAG(
|
263 |
working_dir=WORKING_DIR,
|
|
|
338 |
|
339 |
```python
|
340 |
# Using LlamaIndex with direct OpenAI access
|
341 |
+
import asyncio
|
342 |
from lightrag import LightRAG
|
343 |
from lightrag.llm.llama_index_impl import llama_index_complete_if_cache, llama_index_embed
|
344 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
345 |
from llama_index.llms.openai import OpenAI
|
346 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
347 |
|
348 |
+
async def initialize_rag():
|
349 |
+
rag = LightRAG(
|
350 |
+
working_dir="your/path",
|
351 |
+
llm_model_func=llama_index_complete_if_cache, # LlamaIndex-compatible completion function
|
352 |
+
embedding_func=EmbeddingFunc( # LlamaIndex-compatible embedding function
|
353 |
+
embedding_dim=1536,
|
354 |
+
max_token_size=8192,
|
355 |
+
func=lambda texts: llama_index_embed(texts, embed_model=embed_model)
|
356 |
+
),
|
357 |
+
)
|
358 |
+
|
359 |
+
await rag.initialize_storages()
|
360 |
+
await initialize_pipeline_status()
|
361 |
+
|
362 |
+
return rag
|
363 |
+
|
364 |
+
def main():
|
365 |
+
# Initialize RAG instance
|
366 |
+
rag = asyncio.run(initialize_rag())
|
367 |
+
|
368 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
369 |
+
rag.insert(f.read())
|
370 |
+
|
371 |
+
# Perform naive search
|
372 |
+
print(
|
373 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
374 |
+
)
|
375 |
+
|
376 |
+
# Perform local search
|
377 |
+
print(
|
378 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
379 |
+
)
|
380 |
+
|
381 |
+
# Perform global search
|
382 |
+
print(
|
383 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
384 |
+
)
|
385 |
+
|
386 |
+
# Perform hybrid search
|
387 |
+
print(
|
388 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
389 |
+
)
|
390 |
+
|
391 |
+
if __name__ == "__main__":
|
392 |
+
main()
|
393 |
```
|
394 |
|
395 |
#### For detailed documentation and examples, see:
|
|
|
404 |
LightRAG now supports multi-turn dialogue through the conversation history feature. Here's how to use it:
|
405 |
|
406 |
```python
|
|
|
|
|
|
|
|
|
|
|
407 |
# Create conversation history
|
408 |
conversation_history = [
|
409 |
{"role": "user", "content": "What is the main character's attitude towards Christmas?"},
|
|
|
433 |
LightRAG now supports custom prompts for fine-tuned control over the system's behavior. Here's how to use it:
|
434 |
|
435 |
```python
|
|
|
|
|
|
|
|
|
|
|
436 |
# Create query parameters
|
437 |
query_param = QueryParam(
|
438 |
mode="hybrid", # or other mode: "local", "global", "hybrid", "mix" and "naive"
|
|
|
497 |
<summary> <b>Insert Custom KG</b> </summary>
|
498 |
|
499 |
```python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
500 |
custom_kg = {
|
501 |
"entities": [
|
502 |
{
|
|
|
565 |
"insert_batch_size": 20 # Process 20 documents per batch
|
566 |
}
|
567 |
)
|
568 |
+
|
569 |
rag.insert(["TEXT1", "TEXT2", "TEXT3", ...]) # Documents will be processed in batches of 20
|
570 |
```
|
571 |
|
|
|
592 |
|
593 |
</details>
|
594 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
595 |
<details>
|
596 |
<summary><b>Insert using Pipeline</b></summary>
|
597 |
|
|
|
603 |
|
604 |
```python
|
605 |
rag = LightRAG(..)
|
606 |
+
|
607 |
await rag.apipeline_enqueue_documents(input)
|
608 |
# Your routine in loop
|
609 |
await rag.apipeline_process_enqueue_documents(input)
|
|
|
645 |
|
646 |
# Note: Default settings use NetworkX
|
647 |
# Initialize LightRAG with Neo4J implementation.
|
|
|
|
|
648 |
rag = LightRAG(
|
649 |
working_dir=WORKING_DIR,
|
650 |
llm_model_func=gpt_4o_mini_complete, # Use gpt_4o_mini_complete LLM model
|
|
|
716 |
|
717 |
- Here we are using `sentence-transformers` but you can also use `OpenAIEmbedding` model with `3072` dimensions.
|
718 |
|
719 |
+
```python
|
720 |
async def embedding_func(texts: list[str]) -> np.ndarray:
|
721 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
722 |
embeddings = model.encode(texts, convert_to_numpy=True)
|
723 |
return embeddings
|
724 |
|
725 |
# Initialize LightRAG with the LLM model function and embedding function
|
726 |
+
rag = LightRAG(
|
727 |
+
working_dir=WORKING_DIR,
|
728 |
+
llm_model_func=llm_model_func,
|
729 |
+
embedding_func=EmbeddingFunc(
|
730 |
+
embedding_dim=384,
|
731 |
+
max_token_size=8192,
|
732 |
+
func=embedding_func,
|
733 |
+
),
|
734 |
+
vector_storage="FaissVectorDBStorage",
|
735 |
+
vector_db_storage_cls_kwargs={
|
736 |
+
"cosine_better_than_threshold": 0.3 # Your desired threshold
|
737 |
+
}
|
738 |
+
)
|
739 |
```
|
740 |
|
741 |
</details>
|
|
|
743 |
## Delete
|
744 |
|
745 |
```python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
746 |
# Delete Entity: Deleting entities by their names
|
747 |
rag.delete_by_entity("Project Gutenberg")
|
748 |
|
examples/copy_llm_cache_to_another_storage.py
CHANGED
@@ -10,7 +10,7 @@ import os
|
|
10 |
from dotenv import load_dotenv
|
11 |
|
12 |
from lightrag.kg.postgres_impl import PostgreSQLDB, PGKVStorage
|
13 |
-
from lightrag.
|
14 |
from lightrag.namespace import NameSpace
|
15 |
|
16 |
load_dotenv()
|
|
|
10 |
from dotenv import load_dotenv
|
11 |
|
12 |
from lightrag.kg.postgres_impl import PostgreSQLDB, PGKVStorage
|
13 |
+
from lightrag.kg.json_kv_impl import JsonKVStorage
|
14 |
from lightrag.namespace import NameSpace
|
15 |
|
16 |
load_dotenv()
|
examples/lightrag_api_ollama_demo.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
from fastapi import FastAPI, HTTPException, File, UploadFile
|
|
|
2 |
from pydantic import BaseModel
|
3 |
import os
|
4 |
from lightrag import LightRAG, QueryParam
|
@@ -8,12 +9,12 @@ from typing import Optional
|
|
8 |
import asyncio
|
9 |
import nest_asyncio
|
10 |
import aiofiles
|
|
|
11 |
|
12 |
# Apply nest_asyncio to solve event loop issues
|
13 |
nest_asyncio.apply()
|
14 |
|
15 |
DEFAULT_RAG_DIR = "index_default"
|
16 |
-
app = FastAPI(title="LightRAG API", description="API for RAG operations")
|
17 |
|
18 |
DEFAULT_INPUT_FILE = "book.txt"
|
19 |
INPUT_FILE = os.environ.get("INPUT_FILE", f"{DEFAULT_INPUT_FILE}")
|
@@ -28,23 +29,41 @@ if not os.path.exists(WORKING_DIR):
|
|
28 |
os.mkdir(WORKING_DIR)
|
29 |
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
43 |
),
|
44 |
-
)
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
# Data models
|
49 |
class QueryRequest(BaseModel):
|
50 |
query: str
|
|
|
1 |
from fastapi import FastAPI, HTTPException, File, UploadFile
|
2 |
+
from contextlib import asynccontextmanager
|
3 |
from pydantic import BaseModel
|
4 |
import os
|
5 |
from lightrag import LightRAG, QueryParam
|
|
|
9 |
import asyncio
|
10 |
import nest_asyncio
|
11 |
import aiofiles
|
12 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
13 |
|
14 |
# Apply nest_asyncio to solve event loop issues
|
15 |
nest_asyncio.apply()
|
16 |
|
17 |
DEFAULT_RAG_DIR = "index_default"
|
|
|
18 |
|
19 |
DEFAULT_INPUT_FILE = "book.txt"
|
20 |
INPUT_FILE = os.environ.get("INPUT_FILE", f"{DEFAULT_INPUT_FILE}")
|
|
|
29 |
os.mkdir(WORKING_DIR)
|
30 |
|
31 |
|
32 |
+
async def init():
|
33 |
+
rag = LightRAG(
|
34 |
+
working_dir=WORKING_DIR,
|
35 |
+
llm_model_func=ollama_model_complete,
|
36 |
+
llm_model_name="gemma2:9b",
|
37 |
+
llm_model_max_async=4,
|
38 |
+
llm_model_max_token_size=8192,
|
39 |
+
llm_model_kwargs={"host": "http://localhost:11434", "options": {"num_ctx": 8192}},
|
40 |
+
embedding_func=EmbeddingFunc(
|
41 |
+
embedding_dim=768,
|
42 |
+
max_token_size=8192,
|
43 |
+
func=lambda texts: ollama_embed(
|
44 |
+
texts, embed_model="nomic-embed-text", host="http://localhost:11434"
|
45 |
+
),
|
46 |
),
|
47 |
+
)
|
48 |
+
|
49 |
+
# Add initialization code
|
50 |
+
await rag.initialize_storages()
|
51 |
+
await initialize_pipeline_status()
|
52 |
+
|
53 |
+
return rag
|
54 |
|
55 |
|
56 |
+
@asynccontextmanager
|
57 |
+
async def lifespan(app: FastAPI):
|
58 |
+
global rag
|
59 |
+
rag = await init()
|
60 |
+
print("done!")
|
61 |
+
yield
|
62 |
+
|
63 |
+
|
64 |
+
app = FastAPI(
|
65 |
+
title="LightRAG API", description="API for RAG operations", lifespan=lifespan
|
66 |
+
)
|
67 |
# Data models
|
68 |
class QueryRequest(BaseModel):
|
69 |
query: str
|
examples/lightrag_api_openai_compatible_demo.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
from fastapi import FastAPI, HTTPException, File, UploadFile
|
|
|
2 |
from pydantic import BaseModel
|
3 |
import os
|
4 |
from lightrag import LightRAG, QueryParam
|
@@ -8,6 +9,7 @@ import numpy as np
|
|
8 |
from typing import Optional
|
9 |
import asyncio
|
10 |
import nest_asyncio
|
|
|
11 |
|
12 |
# Apply nest_asyncio to solve event loop issues
|
13 |
nest_asyncio.apply()
|
@@ -71,16 +73,35 @@ async def get_embedding_dim():
|
|
71 |
|
72 |
|
73 |
# Initialize RAG instance
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
# Data models
|
86 |
|
|
|
1 |
from fastapi import FastAPI, HTTPException, File, UploadFile
|
2 |
+
from contextlib import asynccontextmanager
|
3 |
from pydantic import BaseModel
|
4 |
import os
|
5 |
from lightrag import LightRAG, QueryParam
|
|
|
9 |
from typing import Optional
|
10 |
import asyncio
|
11 |
import nest_asyncio
|
12 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
13 |
|
14 |
# Apply nest_asyncio to solve event loop issues
|
15 |
nest_asyncio.apply()
|
|
|
73 |
|
74 |
|
75 |
# Initialize RAG instance
|
76 |
+
async def init():
|
77 |
+
embedding_dimension = await get_embedding_dim()
|
78 |
+
|
79 |
+
rag = LightRAG(
|
80 |
+
working_dir=WORKING_DIR,
|
81 |
+
llm_model_func=llm_model_func,
|
82 |
+
embedding_func=EmbeddingFunc(
|
83 |
+
embedding_dim=embedding_dimension,
|
84 |
+
max_token_size=EMBEDDING_MAX_TOKEN_SIZE,
|
85 |
+
func=embedding_func,
|
86 |
+
),
|
87 |
+
)
|
88 |
+
|
89 |
+
await rag.initialize_storages()
|
90 |
+
await initialize_pipeline_status()
|
91 |
+
|
92 |
+
return rag
|
93 |
|
94 |
+
@asynccontextmanager
|
95 |
+
async def lifespan(app: FastAPI):
|
96 |
+
global rag
|
97 |
+
rag = await init()
|
98 |
+
print("done!")
|
99 |
+
yield
|
100 |
+
|
101 |
+
|
102 |
+
app = FastAPI(
|
103 |
+
title="LightRAG API", description="API for RAG operations", lifespan=lifespan
|
104 |
+
)
|
105 |
|
106 |
# Data models
|
107 |
|
examples/lightrag_api_openai_compatible_demo_simplified.py
DELETED
@@ -1,101 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
from lightrag import LightRAG, QueryParam
|
3 |
-
from lightrag.llm.openai import openai_complete_if_cache, openai_embed
|
4 |
-
from lightrag.utils import EmbeddingFunc
|
5 |
-
import numpy as np
|
6 |
-
import asyncio
|
7 |
-
import nest_asyncio
|
8 |
-
|
9 |
-
# Apply nest_asyncio to solve event loop issues
|
10 |
-
nest_asyncio.apply()
|
11 |
-
|
12 |
-
DEFAULT_RAG_DIR = "index_default"
|
13 |
-
|
14 |
-
# Configure working directory
|
15 |
-
WORKING_DIR = os.environ.get("RAG_DIR", f"{DEFAULT_RAG_DIR}")
|
16 |
-
print(f"WORKING_DIR: {WORKING_DIR}")
|
17 |
-
LLM_MODEL = os.environ.get("LLM_MODEL", "gpt-4o-mini")
|
18 |
-
print(f"LLM_MODEL: {LLM_MODEL}")
|
19 |
-
EMBEDDING_MODEL = os.environ.get("EMBEDDING_MODEL", "text-embedding-3-small")
|
20 |
-
print(f"EMBEDDING_MODEL: {EMBEDDING_MODEL}")
|
21 |
-
EMBEDDING_MAX_TOKEN_SIZE = int(os.environ.get("EMBEDDING_MAX_TOKEN_SIZE", 8192))
|
22 |
-
print(f"EMBEDDING_MAX_TOKEN_SIZE: {EMBEDDING_MAX_TOKEN_SIZE}")
|
23 |
-
BASE_URL = os.environ.get("BASE_URL", "https://api.openai.com/v1")
|
24 |
-
print(f"BASE_URL: {BASE_URL}")
|
25 |
-
API_KEY = os.environ.get("API_KEY", "xxxxxxxx")
|
26 |
-
print(f"API_KEY: {API_KEY}")
|
27 |
-
|
28 |
-
if not os.path.exists(WORKING_DIR):
|
29 |
-
os.mkdir(WORKING_DIR)
|
30 |
-
|
31 |
-
|
32 |
-
# LLM model function
|
33 |
-
|
34 |
-
|
35 |
-
async def llm_model_func(
|
36 |
-
prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
|
37 |
-
) -> str:
|
38 |
-
return await openai_complete_if_cache(
|
39 |
-
model=LLM_MODEL,
|
40 |
-
prompt=prompt,
|
41 |
-
system_prompt=system_prompt,
|
42 |
-
history_messages=history_messages,
|
43 |
-
base_url=BASE_URL,
|
44 |
-
api_key=API_KEY,
|
45 |
-
**kwargs,
|
46 |
-
)
|
47 |
-
|
48 |
-
|
49 |
-
# Embedding function
|
50 |
-
|
51 |
-
|
52 |
-
async def embedding_func(texts: list[str]) -> np.ndarray:
|
53 |
-
return await openai_embed(
|
54 |
-
texts=texts,
|
55 |
-
model=EMBEDDING_MODEL,
|
56 |
-
base_url=BASE_URL,
|
57 |
-
api_key=API_KEY,
|
58 |
-
)
|
59 |
-
|
60 |
-
|
61 |
-
async def get_embedding_dim():
|
62 |
-
test_text = ["This is a test sentence."]
|
63 |
-
embedding = await embedding_func(test_text)
|
64 |
-
embedding_dim = embedding.shape[1]
|
65 |
-
print(f"{embedding_dim=}")
|
66 |
-
return embedding_dim
|
67 |
-
|
68 |
-
|
69 |
-
# Initialize RAG instance
|
70 |
-
rag = LightRAG(
|
71 |
-
working_dir=WORKING_DIR,
|
72 |
-
llm_model_func=llm_model_func,
|
73 |
-
embedding_func=EmbeddingFunc(
|
74 |
-
embedding_dim=asyncio.run(get_embedding_dim()),
|
75 |
-
max_token_size=EMBEDDING_MAX_TOKEN_SIZE,
|
76 |
-
func=embedding_func,
|
77 |
-
),
|
78 |
-
)
|
79 |
-
|
80 |
-
with open("./book.txt", "r", encoding="utf-8") as f:
|
81 |
-
rag.insert(f.read())
|
82 |
-
|
83 |
-
# Perform naive search
|
84 |
-
print(
|
85 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
86 |
-
)
|
87 |
-
|
88 |
-
# Perform local search
|
89 |
-
print(
|
90 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
91 |
-
)
|
92 |
-
|
93 |
-
# Perform global search
|
94 |
-
print(
|
95 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
96 |
-
)
|
97 |
-
|
98 |
-
# Perform hybrid search
|
99 |
-
print(
|
100 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
101 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/lightrag_api_oracle_demo.py
CHANGED
@@ -16,6 +16,7 @@ from lightrag import LightRAG, QueryParam
|
|
16 |
from lightrag.llm.openai import openai_complete_if_cache, openai_embed
|
17 |
from lightrag.utils import EmbeddingFunc
|
18 |
import numpy as np
|
|
|
19 |
|
20 |
|
21 |
print(os.getcwd())
|
@@ -113,6 +114,9 @@ async def init():
|
|
113 |
vector_storage="OracleVectorDBStorage",
|
114 |
)
|
115 |
|
|
|
|
|
|
|
116 |
return rag
|
117 |
|
118 |
|
|
|
16 |
from lightrag.llm.openai import openai_complete_if_cache, openai_embed
|
17 |
from lightrag.utils import EmbeddingFunc
|
18 |
import numpy as np
|
19 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
20 |
|
21 |
|
22 |
print(os.getcwd())
|
|
|
114 |
vector_storage="OracleVectorDBStorage",
|
115 |
)
|
116 |
|
117 |
+
await rag.initialize_storages()
|
118 |
+
await initialize_pipeline_status()
|
119 |
+
|
120 |
return rag
|
121 |
|
122 |
|
examples/lightrag_azure_openai_demo.py
CHANGED
@@ -6,6 +6,7 @@ import numpy as np
|
|
6 |
from dotenv import load_dotenv
|
7 |
import logging
|
8 |
from openai import AzureOpenAI
|
|
|
9 |
|
10 |
logging.basicConfig(level=logging.INFO)
|
11 |
|
@@ -90,6 +91,9 @@ rag = LightRAG(
|
|
90 |
),
|
91 |
)
|
92 |
|
|
|
|
|
|
|
93 |
book1 = open("./book_1.txt", encoding="utf-8")
|
94 |
book2 = open("./book_2.txt", encoding="utf-8")
|
95 |
|
|
|
6 |
from dotenv import load_dotenv
|
7 |
import logging
|
8 |
from openai import AzureOpenAI
|
9 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
10 |
|
11 |
logging.basicConfig(level=logging.INFO)
|
12 |
|
|
|
91 |
),
|
92 |
)
|
93 |
|
94 |
+
rag.initialize_storages()
|
95 |
+
initialize_pipeline_status()
|
96 |
+
|
97 |
book1 = open("./book_1.txt", encoding="utf-8")
|
98 |
book2 = open("./book_2.txt", encoding="utf-8")
|
99 |
|
examples/lightrag_bedrock_demo.py
CHANGED
@@ -8,6 +8,12 @@ import logging
|
|
8 |
from lightrag import LightRAG, QueryParam
|
9 |
from lightrag.llm.bedrock import bedrock_complete, bedrock_embed
|
10 |
from lightrag.utils import EmbeddingFunc
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
logging.getLogger("aiobotocore").setLevel(logging.WARNING)
|
13 |
|
@@ -15,22 +21,31 @@ WORKING_DIR = "./dickens"
|
|
15 |
if not os.path.exists(WORKING_DIR):
|
16 |
os.mkdir(WORKING_DIR)
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
)
|
26 |
-
|
27 |
-
with open("./book.txt", "r", encoding="utf-8") as f:
|
28 |
-
rag.insert(f.read())
|
29 |
-
|
30 |
-
for mode in ["naive", "local", "global", "hybrid"]:
|
31 |
-
print("\n+-" + "-" * len(mode) + "-+")
|
32 |
-
print(f"| {mode.capitalize()} |")
|
33 |
-
print("+-" + "-" * len(mode) + "-+\n")
|
34 |
-
print(
|
35 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode=mode))
|
36 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
from lightrag import LightRAG, QueryParam
|
9 |
from lightrag.llm.bedrock import bedrock_complete, bedrock_embed
|
10 |
from lightrag.utils import EmbeddingFunc
|
11 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
12 |
+
|
13 |
+
import asyncio
|
14 |
+
import nest_asyncio
|
15 |
+
|
16 |
+
nest_asyncio.apply()
|
17 |
|
18 |
logging.getLogger("aiobotocore").setLevel(logging.WARNING)
|
19 |
|
|
|
21 |
if not os.path.exists(WORKING_DIR):
|
22 |
os.mkdir(WORKING_DIR)
|
23 |
|
24 |
+
async def initialize_rag():
|
25 |
+
rag = LightRAG(
|
26 |
+
working_dir=WORKING_DIR,
|
27 |
+
llm_model_func=bedrock_complete,
|
28 |
+
llm_model_name="Anthropic Claude 3 Haiku // Amazon Bedrock",
|
29 |
+
embedding_func=EmbeddingFunc(
|
30 |
+
embedding_dim=1024, max_token_size=8192, func=bedrock_embed
|
31 |
+
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
)
|
33 |
+
|
34 |
+
await rag.initialize_storages()
|
35 |
+
await initialize_pipeline_status()
|
36 |
+
|
37 |
+
return rag
|
38 |
+
|
39 |
+
def main():
|
40 |
+
rag = asyncio.run(initialize_rag())
|
41 |
+
|
42 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
43 |
+
rag.insert(f.read())
|
44 |
+
|
45 |
+
for mode in ["naive", "local", "global", "hybrid"]:
|
46 |
+
print("\n+-" + "-" * len(mode) + "-+")
|
47 |
+
print(f"| {mode.capitalize()} |")
|
48 |
+
print("+-" + "-" * len(mode) + "-+\n")
|
49 |
+
print(
|
50 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode=mode))
|
51 |
+
)
|
examples/lightrag_gemini_demo.py
CHANGED
@@ -8,6 +8,12 @@ from dotenv import load_dotenv
|
|
8 |
from lightrag.utils import EmbeddingFunc
|
9 |
from lightrag import LightRAG, QueryParam
|
10 |
from sentence_transformers import SentenceTransformer
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
load_dotenv()
|
13 |
gemini_api_key = os.getenv("GEMINI_API_KEY")
|
@@ -60,25 +66,37 @@ async def embedding_func(texts: list[str]) -> np.ndarray:
|
|
60 |
return embeddings
|
61 |
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
76 |
|
77 |
-
rag.insert(text)
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
-
response
|
80 |
-
query="What is the main theme of the story?",
|
81 |
-
param=QueryParam(mode="hybrid", top_k=5, response_type="single line"),
|
82 |
-
)
|
83 |
|
84 |
-
|
|
|
|
8 |
from lightrag.utils import EmbeddingFunc
|
9 |
from lightrag import LightRAG, QueryParam
|
10 |
from sentence_transformers import SentenceTransformer
|
11 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
12 |
+
|
13 |
+
import asyncio
|
14 |
+
import nest_asyncio
|
15 |
+
# Apply nest_asyncio to solve event loop issues
|
16 |
+
nest_asyncio.apply()
|
17 |
|
18 |
load_dotenv()
|
19 |
gemini_api_key = os.getenv("GEMINI_API_KEY")
|
|
|
66 |
return embeddings
|
67 |
|
68 |
|
69 |
+
async def initialize_rag():
|
70 |
+
rag = LightRAG(
|
71 |
+
working_dir=WORKING_DIR,
|
72 |
+
llm_model_func=llm_model_func,
|
73 |
+
embedding_func=EmbeddingFunc(
|
74 |
+
embedding_dim=384,
|
75 |
+
max_token_size=8192,
|
76 |
+
func=embedding_func,
|
77 |
+
),
|
78 |
+
)
|
79 |
+
|
80 |
+
await rag.initialize_storages()
|
81 |
+
await initialize_pipeline_status()
|
82 |
+
|
83 |
+
return rag
|
84 |
|
85 |
+
def main():
|
86 |
+
# Initialize RAG instance
|
87 |
+
rag = asyncio.run(initialize_rag())
|
88 |
+
file_path = "story.txt"
|
89 |
+
with open(file_path, "r") as file:
|
90 |
+
text = file.read()
|
91 |
|
92 |
+
rag.insert(text)
|
93 |
+
|
94 |
+
response = rag.query(
|
95 |
+
query="What is the main theme of the story?",
|
96 |
+
param=QueryParam(mode="hybrid", top_k=5, response_type="single line"),
|
97 |
+
)
|
98 |
|
99 |
+
print(response)
|
|
|
|
|
|
|
100 |
|
101 |
+
if __name__ == "__main__":
|
102 |
+
main()
|
examples/lightrag_hf_demo.py
CHANGED
@@ -4,51 +4,68 @@ from lightrag import LightRAG, QueryParam
|
|
4 |
from lightrag.llm.hf import hf_model_complete, hf_embed
|
5 |
from lightrag.utils import EmbeddingFunc
|
6 |
from transformers import AutoModel, AutoTokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
WORKING_DIR = "./dickens"
|
9 |
|
10 |
if not os.path.exists(WORKING_DIR):
|
11 |
os.mkdir(WORKING_DIR)
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
texts
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
27 |
),
|
28 |
),
|
29 |
-
)
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
|
|
|
|
32 |
|
33 |
-
|
34 |
-
|
|
|
|
|
35 |
|
36 |
-
# Perform
|
37 |
-
print(
|
38 |
-
|
39 |
-
)
|
40 |
|
41 |
-
# Perform
|
42 |
-
print(
|
43 |
-
|
44 |
-
)
|
45 |
|
46 |
-
# Perform
|
47 |
-
print(
|
48 |
-
|
49 |
-
)
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
54 |
-
)
|
|
|
4 |
from lightrag.llm.hf import hf_model_complete, hf_embed
|
5 |
from lightrag.utils import EmbeddingFunc
|
6 |
from transformers import AutoModel, AutoTokenizer
|
7 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
8 |
+
|
9 |
+
import asyncio
|
10 |
+
import nest_asyncio
|
11 |
+
|
12 |
+
nest_asyncio.apply()
|
13 |
|
14 |
WORKING_DIR = "./dickens"
|
15 |
|
16 |
if not os.path.exists(WORKING_DIR):
|
17 |
os.mkdir(WORKING_DIR)
|
18 |
|
19 |
+
async def initialize_rag():
|
20 |
+
rag = LightRAG(
|
21 |
+
working_dir=WORKING_DIR,
|
22 |
+
llm_model_func=hf_model_complete,
|
23 |
+
llm_model_name="meta-llama/Llama-3.1-8B-Instruct",
|
24 |
+
embedding_func=EmbeddingFunc(
|
25 |
+
embedding_dim=384,
|
26 |
+
max_token_size=5000,
|
27 |
+
func=lambda texts: hf_embed(
|
28 |
+
texts,
|
29 |
+
tokenizer=AutoTokenizer.from_pretrained(
|
30 |
+
"sentence-transformers/all-MiniLM-L6-v2"
|
31 |
+
),
|
32 |
+
embed_model=AutoModel.from_pretrained(
|
33 |
+
"sentence-transformers/all-MiniLM-L6-v2"
|
34 |
+
),
|
35 |
),
|
36 |
),
|
37 |
+
)
|
38 |
+
|
39 |
+
await rag.initialize_storages()
|
40 |
+
await initialize_pipeline_status()
|
41 |
+
|
42 |
+
return rag
|
43 |
+
|
44 |
+
def main():
|
45 |
+
rag = asyncio.run(initialize_rag())
|
46 |
|
47 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
48 |
+
rag.insert(f.read())
|
49 |
|
50 |
+
# Perform naive search
|
51 |
+
print(
|
52 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
53 |
+
)
|
54 |
|
55 |
+
# Perform local search
|
56 |
+
print(
|
57 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
58 |
+
)
|
59 |
|
60 |
+
# Perform global search
|
61 |
+
print(
|
62 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
63 |
+
)
|
64 |
|
65 |
+
# Perform hybrid search
|
66 |
+
print(
|
67 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
68 |
+
)
|
69 |
|
70 |
+
if __name__ == "__main__":
|
71 |
+
main()
|
|
|
|
examples/lightrag_jinaai_demo.py
DELETED
@@ -1,115 +0,0 @@
|
|
1 |
-
import numpy as np
|
2 |
-
from lightrag import LightRAG, QueryParam
|
3 |
-
from lightrag.utils import EmbeddingFunc
|
4 |
-
from lightrag.llm.jina import jina_embed
|
5 |
-
from lightrag.llm.openai import openai_complete_if_cache
|
6 |
-
import os
|
7 |
-
import asyncio
|
8 |
-
|
9 |
-
|
10 |
-
async def embedding_func(texts: list[str]) -> np.ndarray:
|
11 |
-
return await jina_embed(texts, api_key="YourJinaAPIKey")
|
12 |
-
|
13 |
-
|
14 |
-
WORKING_DIR = "./dickens"
|
15 |
-
|
16 |
-
if not os.path.exists(WORKING_DIR):
|
17 |
-
os.mkdir(WORKING_DIR)
|
18 |
-
|
19 |
-
|
20 |
-
async def llm_model_func(
|
21 |
-
prompt, system_prompt=None, history_messages=[], **kwargs
|
22 |
-
) -> str:
|
23 |
-
return await openai_complete_if_cache(
|
24 |
-
"solar-mini",
|
25 |
-
prompt,
|
26 |
-
system_prompt=system_prompt,
|
27 |
-
history_messages=history_messages,
|
28 |
-
api_key=os.getenv("UPSTAGE_API_KEY"),
|
29 |
-
base_url="https://api.upstage.ai/v1/solar",
|
30 |
-
**kwargs,
|
31 |
-
)
|
32 |
-
|
33 |
-
|
34 |
-
rag = LightRAG(
|
35 |
-
working_dir=WORKING_DIR,
|
36 |
-
llm_model_func=llm_model_func,
|
37 |
-
embedding_func=EmbeddingFunc(
|
38 |
-
embedding_dim=1024, max_token_size=8192, func=embedding_func
|
39 |
-
),
|
40 |
-
)
|
41 |
-
|
42 |
-
|
43 |
-
async def lightraginsert(file_path, semaphore):
|
44 |
-
async with semaphore:
|
45 |
-
try:
|
46 |
-
with open(file_path, "r", encoding="utf-8") as f:
|
47 |
-
content = f.read()
|
48 |
-
except UnicodeDecodeError:
|
49 |
-
# If UTF-8 decoding fails, try other encodings
|
50 |
-
with open(file_path, "r", encoding="gbk") as f:
|
51 |
-
content = f.read()
|
52 |
-
await rag.ainsert(content)
|
53 |
-
|
54 |
-
|
55 |
-
async def process_files(directory, concurrency_limit):
|
56 |
-
semaphore = asyncio.Semaphore(concurrency_limit)
|
57 |
-
tasks = []
|
58 |
-
for root, dirs, files in os.walk(directory):
|
59 |
-
for f in files:
|
60 |
-
file_path = os.path.join(root, f)
|
61 |
-
if f.startswith("."):
|
62 |
-
continue
|
63 |
-
tasks.append(lightraginsert(file_path, semaphore))
|
64 |
-
await asyncio.gather(*tasks)
|
65 |
-
|
66 |
-
|
67 |
-
async def main():
|
68 |
-
try:
|
69 |
-
rag = LightRAG(
|
70 |
-
working_dir=WORKING_DIR,
|
71 |
-
llm_model_func=llm_model_func,
|
72 |
-
embedding_func=EmbeddingFunc(
|
73 |
-
embedding_dim=1024,
|
74 |
-
max_token_size=8192,
|
75 |
-
func=embedding_func,
|
76 |
-
),
|
77 |
-
)
|
78 |
-
|
79 |
-
asyncio.run(process_files(WORKING_DIR, concurrency_limit=4))
|
80 |
-
|
81 |
-
# Perform naive search
|
82 |
-
print(
|
83 |
-
await rag.aquery(
|
84 |
-
"What are the top themes in this story?", param=QueryParam(mode="naive")
|
85 |
-
)
|
86 |
-
)
|
87 |
-
|
88 |
-
# Perform local search
|
89 |
-
print(
|
90 |
-
await rag.aquery(
|
91 |
-
"What are the top themes in this story?", param=QueryParam(mode="local")
|
92 |
-
)
|
93 |
-
)
|
94 |
-
|
95 |
-
# Perform global search
|
96 |
-
print(
|
97 |
-
await rag.aquery(
|
98 |
-
"What are the top themes in this story?",
|
99 |
-
param=QueryParam(mode="global"),
|
100 |
-
)
|
101 |
-
)
|
102 |
-
|
103 |
-
# Perform hybrid search
|
104 |
-
print(
|
105 |
-
await rag.aquery(
|
106 |
-
"What are the top themes in this story?",
|
107 |
-
param=QueryParam(mode="hybrid"),
|
108 |
-
)
|
109 |
-
)
|
110 |
-
except Exception as e:
|
111 |
-
print(f"An error occurred: {e}")
|
112 |
-
|
113 |
-
|
114 |
-
if __name__ == "__main__":
|
115 |
-
asyncio.run(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/lightrag_llamaindex_direct_demo.py
CHANGED
@@ -8,6 +8,11 @@ from lightrag.utils import EmbeddingFunc
|
|
8 |
from llama_index.llms.openai import OpenAI
|
9 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
10 |
import asyncio
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Configure working directory
|
13 |
WORKING_DIR = "./index_default"
|
@@ -76,38 +81,53 @@ async def get_embedding_dim():
|
|
76 |
return embedding_dim
|
77 |
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
rag.
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
)
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
)
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
from llama_index.llms.openai import OpenAI
|
9 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
10 |
import asyncio
|
11 |
+
import nest_asyncio
|
12 |
+
|
13 |
+
nest_asyncio.apply()
|
14 |
+
|
15 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
16 |
|
17 |
# Configure working directory
|
18 |
WORKING_DIR = "./index_default"
|
|
|
81 |
return embedding_dim
|
82 |
|
83 |
|
84 |
+
async def initialize_rag():
|
85 |
+
embedding_dimension = await get_embedding_dim()
|
86 |
+
|
87 |
+
rag = LightRAG(
|
88 |
+
working_dir=WORKING_DIR,
|
89 |
+
llm_model_func=llm_model_func,
|
90 |
+
embedding_func=EmbeddingFunc(
|
91 |
+
embedding_dim=embedding_dimension,
|
92 |
+
max_token_size=EMBEDDING_MAX_TOKEN_SIZE,
|
93 |
+
func=embedding_func,
|
94 |
+
),
|
95 |
+
)
|
96 |
+
|
97 |
+
await rag.initialize_storages()
|
98 |
+
await initialize_pipeline_status()
|
99 |
+
|
100 |
+
return rag
|
101 |
+
|
102 |
+
|
103 |
+
def main():
|
104 |
+
# Initialize RAG instance
|
105 |
+
rag = asyncio.run(initialize_rag())
|
106 |
+
|
107 |
+
# Insert example text
|
108 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
109 |
+
rag.insert(f.read())
|
110 |
+
|
111 |
+
# Test different query modes
|
112 |
+
print("\nNaive Search:")
|
113 |
+
print(
|
114 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
115 |
+
)
|
116 |
+
|
117 |
+
print("\nLocal Search:")
|
118 |
+
print(
|
119 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
120 |
+
)
|
121 |
+
|
122 |
+
print("\nGlobal Search:")
|
123 |
+
print(
|
124 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
125 |
+
)
|
126 |
+
|
127 |
+
print("\nHybrid Search:")
|
128 |
+
print(
|
129 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
130 |
+
)
|
131 |
+
|
132 |
+
if __name__ == "__main__":
|
133 |
+
main()
|
examples/lightrag_llamaindex_litellm_demo.py
CHANGED
@@ -8,6 +8,11 @@ from lightrag.utils import EmbeddingFunc
|
|
8 |
from llama_index.llms.litellm import LiteLLM
|
9 |
from llama_index.embeddings.litellm import LiteLLMEmbedding
|
10 |
import asyncio
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Configure working directory
|
13 |
WORKING_DIR = "./index_default"
|
@@ -79,38 +84,53 @@ async def get_embedding_dim():
|
|
79 |
return embedding_dim
|
80 |
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
rag.
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
)
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
)
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
from llama_index.llms.litellm import LiteLLM
|
9 |
from llama_index.embeddings.litellm import LiteLLMEmbedding
|
10 |
import asyncio
|
11 |
+
import nest_asyncio
|
12 |
+
|
13 |
+
nest_asyncio.apply()
|
14 |
+
|
15 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
16 |
|
17 |
# Configure working directory
|
18 |
WORKING_DIR = "./index_default"
|
|
|
84 |
return embedding_dim
|
85 |
|
86 |
|
87 |
+
async def initialize_rag():
|
88 |
+
embedding_dimension = await get_embedding_dim()
|
89 |
+
|
90 |
+
rag = LightRAG(
|
91 |
+
working_dir=WORKING_DIR,
|
92 |
+
llm_model_func=llm_model_func,
|
93 |
+
embedding_func=EmbeddingFunc(
|
94 |
+
embedding_dim=embedding_dimension,
|
95 |
+
max_token_size=EMBEDDING_MAX_TOKEN_SIZE,
|
96 |
+
func=embedding_func,
|
97 |
+
),
|
98 |
+
)
|
99 |
+
|
100 |
+
await rag.initialize_storages()
|
101 |
+
await initialize_pipeline_status()
|
102 |
+
|
103 |
+
return rag
|
104 |
+
|
105 |
+
|
106 |
+
def main():
|
107 |
+
# Initialize RAG instance
|
108 |
+
rag = asyncio.run(initialize_rag())
|
109 |
+
|
110 |
+
# Insert example text
|
111 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
112 |
+
rag.insert(f.read())
|
113 |
+
|
114 |
+
# Test different query modes
|
115 |
+
print("\nNaive Search:")
|
116 |
+
print(
|
117 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
118 |
+
)
|
119 |
+
|
120 |
+
print("\nLocal Search:")
|
121 |
+
print(
|
122 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
123 |
+
)
|
124 |
+
|
125 |
+
print("\nGlobal Search:")
|
126 |
+
print(
|
127 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
128 |
+
)
|
129 |
+
|
130 |
+
print("\nHybrid Search:")
|
131 |
+
print(
|
132 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
133 |
+
)
|
134 |
+
|
135 |
+
if __name__ == "__main__":
|
136 |
+
main()
|
examples/lightrag_lmdeploy_demo.py
CHANGED
@@ -5,6 +5,12 @@ from lightrag.llm.lmdeploy import lmdeploy_model_if_cache
|
|
5 |
from lightrag.llm.hf import hf_embed
|
6 |
from lightrag.utils import EmbeddingFunc
|
7 |
from transformers import AutoModel, AutoTokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
WORKING_DIR = "./dickens"
|
10 |
|
@@ -35,46 +41,59 @@ async def lmdeploy_model_complete(
|
|
35 |
**kwargs,
|
36 |
)
|
37 |
|
38 |
-
|
39 |
-
rag = LightRAG(
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
53 |
),
|
54 |
),
|
55 |
-
)
|
56 |
-
)
|
57 |
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
|
|
61 |
|
62 |
-
#
|
63 |
-
|
64 |
-
|
65 |
-
)
|
66 |
|
67 |
-
#
|
68 |
-
print(
|
69 |
-
|
70 |
-
)
|
|
|
71 |
|
72 |
-
|
73 |
-
print(
|
74 |
-
|
75 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
80 |
-
)
|
|
|
5 |
from lightrag.llm.hf import hf_embed
|
6 |
from lightrag.utils import EmbeddingFunc
|
7 |
from transformers import AutoModel, AutoTokenizer
|
8 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
9 |
+
|
10 |
+
import asyncio
|
11 |
+
import nest_asyncio
|
12 |
+
|
13 |
+
nest_asyncio.apply()
|
14 |
|
15 |
WORKING_DIR = "./dickens"
|
16 |
|
|
|
41 |
**kwargs,
|
42 |
)
|
43 |
|
44 |
+
async def initialize_rag():
|
45 |
+
rag = LightRAG(
|
46 |
+
working_dir=WORKING_DIR,
|
47 |
+
llm_model_func=lmdeploy_model_complete,
|
48 |
+
llm_model_name="meta-llama/Llama-3.1-8B-Instruct", # please use definite path for local model
|
49 |
+
embedding_func=EmbeddingFunc(
|
50 |
+
embedding_dim=384,
|
51 |
+
max_token_size=5000,
|
52 |
+
func=lambda texts: hf_embed(
|
53 |
+
texts,
|
54 |
+
tokenizer=AutoTokenizer.from_pretrained(
|
55 |
+
"sentence-transformers/all-MiniLM-L6-v2"
|
56 |
+
),
|
57 |
+
embed_model=AutoModel.from_pretrained(
|
58 |
+
"sentence-transformers/all-MiniLM-L6-v2"
|
59 |
+
),
|
60 |
),
|
61 |
),
|
62 |
+
)
|
|
|
63 |
|
64 |
+
await rag.initialize_storages()
|
65 |
+
await initialize_pipeline_status()
|
66 |
+
|
67 |
+
return rag
|
68 |
|
69 |
+
def main():
|
70 |
+
# Initialize RAG instance
|
71 |
+
rag = asyncio.run(initialize_rag())
|
72 |
|
73 |
+
# Insert example text
|
74 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
75 |
+
rag.insert(f.read())
|
|
|
76 |
|
77 |
+
# Test different query modes
|
78 |
+
print("\nNaive Search:")
|
79 |
+
print(
|
80 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
81 |
+
)
|
82 |
|
83 |
+
print("\nLocal Search:")
|
84 |
+
print(
|
85 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
86 |
+
)
|
87 |
+
|
88 |
+
print("\nGlobal Search:")
|
89 |
+
print(
|
90 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
91 |
+
)
|
92 |
+
|
93 |
+
print("\nHybrid Search:")
|
94 |
+
print(
|
95 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
96 |
+
)
|
97 |
|
98 |
+
if __name__ == "__main__":
|
99 |
+
main()
|
|
|
|
examples/lightrag_nvidia_demo.py
CHANGED
@@ -1,5 +1,9 @@
|
|
1 |
import os
|
2 |
import asyncio
|
|
|
|
|
|
|
|
|
3 |
from lightrag import LightRAG, QueryParam
|
4 |
from lightrag.llm import (
|
5 |
openai_complete_if_cache,
|
@@ -7,10 +11,12 @@ from lightrag.llm import (
|
|
7 |
)
|
8 |
from lightrag.utils import EmbeddingFunc
|
9 |
import numpy as np
|
|
|
10 |
|
11 |
# for custom llm_model_func
|
12 |
from lightrag.utils import locate_json_string_body_from_string
|
13 |
|
|
|
14 |
WORKING_DIR = "./dickens"
|
15 |
|
16 |
if not os.path.exists(WORKING_DIR):
|
@@ -91,42 +97,37 @@ async def test_funcs():
|
|
91 |
|
92 |
# asyncio.run(test_funcs())
|
93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
|
|
|
|
|
|
|
|
95 |
async def main():
|
96 |
try:
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
# lightRAG class during indexing
|
101 |
-
rag = LightRAG(
|
102 |
-
working_dir=WORKING_DIR,
|
103 |
-
llm_model_func=llm_model_func,
|
104 |
-
# llm_model_name="meta/llama3-70b-instruct", #un comment if
|
105 |
-
embedding_func=EmbeddingFunc(
|
106 |
-
embedding_dim=embedding_dimension,
|
107 |
-
max_token_size=512, # maximum token size, somehow it's still exceed maximum number of token
|
108 |
-
# so truncate (trunc) parameter on embedding_func will handle it and try to examine the tokenizer used in LightRAG
|
109 |
-
# so you can adjust to be able to fit the NVIDIA model (future work)
|
110 |
-
func=indexing_embedding_func,
|
111 |
-
),
|
112 |
-
)
|
113 |
|
114 |
# reading file
|
115 |
with open("./book.txt", "r", encoding="utf-8") as f:
|
116 |
await rag.ainsert(f.read())
|
117 |
|
118 |
-
# redefine rag to change embedding into query type
|
119 |
-
rag = LightRAG(
|
120 |
-
working_dir=WORKING_DIR,
|
121 |
-
llm_model_func=llm_model_func,
|
122 |
-
# llm_model_name="meta/llama3-70b-instruct", #un comment if
|
123 |
-
embedding_func=EmbeddingFunc(
|
124 |
-
embedding_dim=embedding_dimension,
|
125 |
-
max_token_size=512,
|
126 |
-
func=query_embedding_func,
|
127 |
-
),
|
128 |
-
)
|
129 |
-
|
130 |
# Perform naive search
|
131 |
print("==============Naive===============")
|
132 |
print(
|
|
|
1 |
import os
|
2 |
import asyncio
|
3 |
+
import nest_asyncio
|
4 |
+
|
5 |
+
nest_asyncio.apply()
|
6 |
+
|
7 |
from lightrag import LightRAG, QueryParam
|
8 |
from lightrag.llm import (
|
9 |
openai_complete_if_cache,
|
|
|
11 |
)
|
12 |
from lightrag.utils import EmbeddingFunc
|
13 |
import numpy as np
|
14 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
15 |
|
16 |
# for custom llm_model_func
|
17 |
from lightrag.utils import locate_json_string_body_from_string
|
18 |
|
19 |
+
|
20 |
WORKING_DIR = "./dickens"
|
21 |
|
22 |
if not os.path.exists(WORKING_DIR):
|
|
|
97 |
|
98 |
# asyncio.run(test_funcs())
|
99 |
|
100 |
+
async def initialize_rag():
|
101 |
+
embedding_dimension = await get_embedding_dim()
|
102 |
+
print(f"Detected embedding dimension: {embedding_dimension}")
|
103 |
+
|
104 |
+
# lightRAG class during indexing
|
105 |
+
rag = LightRAG(
|
106 |
+
working_dir=WORKING_DIR,
|
107 |
+
llm_model_func=llm_model_func,
|
108 |
+
# llm_model_name="meta/llama3-70b-instruct", #un comment if
|
109 |
+
embedding_func=EmbeddingFunc(
|
110 |
+
embedding_dim=embedding_dimension,
|
111 |
+
max_token_size=512, # maximum token size, somehow it's still exceed maximum number of token
|
112 |
+
# so truncate (trunc) parameter on embedding_func will handle it and try to examine the tokenizer used in LightRAG
|
113 |
+
# so you can adjust to be able to fit the NVIDIA model (future work)
|
114 |
+
func=indexing_embedding_func,
|
115 |
+
),
|
116 |
+
)
|
117 |
|
118 |
+
await rag.initialize_storages()
|
119 |
+
await initialize_pipeline_status()
|
120 |
+
|
121 |
+
return rag
|
122 |
async def main():
|
123 |
try:
|
124 |
+
# Initialize RAG instance
|
125 |
+
rag = asyncio.run(initialize_rag())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
# reading file
|
128 |
with open("./book.txt", "r", encoding="utf-8") as f:
|
129 |
await rag.ainsert(f.read())
|
130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
# Perform naive search
|
132 |
print("==============Naive===============")
|
133 |
print(
|
examples/lightrag_ollama_age_demo.py
CHANGED
@@ -1,4 +1,8 @@
|
|
1 |
import asyncio
|
|
|
|
|
|
|
|
|
2 |
import inspect
|
3 |
import logging
|
4 |
import os
|
@@ -6,6 +10,7 @@ import os
|
|
6 |
from lightrag import LightRAG, QueryParam
|
7 |
from lightrag.llm.ollama import ollama_embed, ollama_model_complete
|
8 |
from lightrag.utils import EmbeddingFunc
|
|
|
9 |
|
10 |
WORKING_DIR = "./dickens_age"
|
11 |
|
@@ -22,59 +27,72 @@ os.environ["AGE_POSTGRES_HOST"] = "localhost"
|
|
22 |
os.environ["AGE_POSTGRES_PORT"] = "5455"
|
23 |
os.environ["AGE_GRAPH_NAME"] = "dickens"
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
37 |
),
|
38 |
-
|
39 |
-
|
40 |
-
)
|
41 |
-
|
42 |
-
with open("./book.txt", "r", encoding="utf-8") as f:
|
43 |
-
rag.insert(f.read())
|
44 |
-
|
45 |
-
# Perform naive search
|
46 |
-
print(
|
47 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
48 |
-
)
|
49 |
-
|
50 |
-
# Perform local search
|
51 |
-
print(
|
52 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
53 |
-
)
|
54 |
-
|
55 |
-
# Perform global search
|
56 |
-
print(
|
57 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
58 |
-
)
|
59 |
-
|
60 |
-
# Perform hybrid search
|
61 |
-
print(
|
62 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
63 |
-
)
|
64 |
-
|
65 |
-
# stream response
|
66 |
-
resp = rag.query(
|
67 |
-
"What are the top themes in this story?",
|
68 |
-
param=QueryParam(mode="hybrid", stream=True),
|
69 |
-
)
|
70 |
|
|
|
|
|
|
|
|
|
71 |
|
72 |
async def print_stream(stream):
|
73 |
async for chunk in stream:
|
74 |
print(chunk, end="", flush=True)
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
asyncio.run(
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import asyncio
|
2 |
+
import nest_asyncio
|
3 |
+
|
4 |
+
nest_asyncio.apply()
|
5 |
+
|
6 |
import inspect
|
7 |
import logging
|
8 |
import os
|
|
|
10 |
from lightrag import LightRAG, QueryParam
|
11 |
from lightrag.llm.ollama import ollama_embed, ollama_model_complete
|
12 |
from lightrag.utils import EmbeddingFunc
|
13 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
14 |
|
15 |
WORKING_DIR = "./dickens_age"
|
16 |
|
|
|
27 |
os.environ["AGE_POSTGRES_PORT"] = "5455"
|
28 |
os.environ["AGE_GRAPH_NAME"] = "dickens"
|
29 |
|
30 |
+
async def initialize_rag():
|
31 |
+
rag = LightRAG(
|
32 |
+
working_dir=WORKING_DIR,
|
33 |
+
llm_model_func=ollama_model_complete,
|
34 |
+
llm_model_name="llama3.1:8b",
|
35 |
+
llm_model_max_async=4,
|
36 |
+
llm_model_max_token_size=32768,
|
37 |
+
llm_model_kwargs={"host": "http://localhost:11434", "options": {"num_ctx": 32768}},
|
38 |
+
embedding_func=EmbeddingFunc(
|
39 |
+
embedding_dim=768,
|
40 |
+
max_token_size=8192,
|
41 |
+
func=lambda texts: ollama_embed(
|
42 |
+
texts, embed_model="nomic-embed-text", host="http://localhost:11434"
|
43 |
+
),
|
44 |
),
|
45 |
+
graph_storage="AGEStorage",
|
46 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
await rag.initialize_storages()
|
49 |
+
await initialize_pipeline_status()
|
50 |
+
|
51 |
+
return rag
|
52 |
|
53 |
async def print_stream(stream):
|
54 |
async for chunk in stream:
|
55 |
print(chunk, end="", flush=True)
|
56 |
|
57 |
+
def main():
|
58 |
+
# Initialize RAG instance
|
59 |
+
rag = asyncio.run(initialize_rag())
|
60 |
+
|
61 |
+
# Insert example text
|
62 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
63 |
+
rag.insert(f.read())
|
64 |
+
|
65 |
+
# Test different query modes
|
66 |
+
print("\nNaive Search:")
|
67 |
+
print(
|
68 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
69 |
+
)
|
70 |
+
|
71 |
+
print("\nLocal Search:")
|
72 |
+
print(
|
73 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
74 |
+
)
|
75 |
+
|
76 |
+
print("\nGlobal Search:")
|
77 |
+
print(
|
78 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
79 |
+
)
|
80 |
+
|
81 |
+
print("\nHybrid Search:")
|
82 |
+
print(
|
83 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
84 |
+
)
|
85 |
+
|
86 |
+
# stream response
|
87 |
+
resp = rag.query(
|
88 |
+
"What are the top themes in this story?",
|
89 |
+
param=QueryParam(mode="hybrid", stream=True),
|
90 |
+
)
|
91 |
+
|
92 |
+
if inspect.isasyncgen(resp):
|
93 |
+
asyncio.run(print_stream(resp))
|
94 |
+
else:
|
95 |
+
print(resp)
|
96 |
+
|
97 |
+
if __name__ == "__main__":
|
98 |
+
main()
|
examples/lightrag_ollama_demo.py
CHANGED
@@ -1,10 +1,14 @@
|
|
1 |
import asyncio
|
|
|
|
|
|
|
2 |
import os
|
3 |
import inspect
|
4 |
import logging
|
5 |
from lightrag import LightRAG, QueryParam
|
6 |
from lightrag.llm.ollama import ollama_model_complete, ollama_embed
|
7 |
from lightrag.utils import EmbeddingFunc
|
|
|
8 |
|
9 |
WORKING_DIR = "./dickens"
|
10 |
|
@@ -13,58 +17,71 @@ logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)
|
|
13 |
if not os.path.exists(WORKING_DIR):
|
14 |
os.mkdir(WORKING_DIR)
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
28 |
),
|
29 |
-
)
|
30 |
-
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
|
|
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
rag
|
38 |
-
)
|
39 |
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
)
|
44 |
|
45 |
-
#
|
46 |
-
print(
|
47 |
-
|
48 |
-
)
|
|
|
49 |
|
50 |
-
|
51 |
-
print(
|
52 |
-
|
53 |
-
)
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
)
|
60 |
|
|
|
|
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
65 |
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
if
|
68 |
-
|
69 |
-
else:
|
70 |
-
print(resp)
|
|
|
1 |
import asyncio
|
2 |
+
import nest_asyncio
|
3 |
+
|
4 |
+
nest_asyncio.apply()
|
5 |
import os
|
6 |
import inspect
|
7 |
import logging
|
8 |
from lightrag import LightRAG, QueryParam
|
9 |
from lightrag.llm.ollama import ollama_model_complete, ollama_embed
|
10 |
from lightrag.utils import EmbeddingFunc
|
11 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
12 |
|
13 |
WORKING_DIR = "./dickens"
|
14 |
|
|
|
17 |
if not os.path.exists(WORKING_DIR):
|
18 |
os.mkdir(WORKING_DIR)
|
19 |
|
20 |
+
async def initialize_rag():
|
21 |
+
rag = LightRAG(
|
22 |
+
working_dir=WORKING_DIR,
|
23 |
+
llm_model_func=ollama_model_complete,
|
24 |
+
llm_model_name="gemma2:2b",
|
25 |
+
llm_model_max_async=4,
|
26 |
+
llm_model_max_token_size=32768,
|
27 |
+
llm_model_kwargs={"host": "http://localhost:11434", "options": {"num_ctx": 32768}},
|
28 |
+
embedding_func=EmbeddingFunc(
|
29 |
+
embedding_dim=768,
|
30 |
+
max_token_size=8192,
|
31 |
+
func=lambda texts: ollama_embed(
|
32 |
+
texts, embed_model="nomic-embed-text", host="http://localhost:11434"
|
33 |
+
),
|
34 |
),
|
35 |
+
)
|
36 |
+
|
37 |
+
await rag.initialize_storages()
|
38 |
+
await initialize_pipeline_status()
|
39 |
+
|
40 |
+
return rag
|
41 |
|
42 |
+
async def print_stream(stream):
|
43 |
+
async for chunk in stream:
|
44 |
+
print(chunk, end="", flush=True)
|
45 |
|
46 |
+
def main():
|
47 |
+
# Initialize RAG instance
|
48 |
+
rag = asyncio.run(initialize_rag())
|
|
|
49 |
|
50 |
+
# Insert example text
|
51 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
52 |
+
rag.insert(f.read())
|
|
|
53 |
|
54 |
+
# Test different query modes
|
55 |
+
print("\nNaive Search:")
|
56 |
+
print(
|
57 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
58 |
+
)
|
59 |
|
60 |
+
print("\nLocal Search:")
|
61 |
+
print(
|
62 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
63 |
+
)
|
64 |
|
65 |
+
print("\nGlobal Search:")
|
66 |
+
print(
|
67 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
68 |
+
)
|
|
|
69 |
|
70 |
+
print("\nHybrid Search:")
|
71 |
+
print(
|
72 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
73 |
+
)
|
74 |
|
75 |
+
# stream response
|
76 |
+
resp = rag.query(
|
77 |
+
"What are the top themes in this story?",
|
78 |
+
param=QueryParam(mode="hybrid", stream=True),
|
79 |
+
)
|
80 |
|
81 |
+
if inspect.isasyncgen(resp):
|
82 |
+
asyncio.run(print_stream(resp))
|
83 |
+
else:
|
84 |
+
print(resp)
|
85 |
|
86 |
+
if __name__ == "__main__":
|
87 |
+
main()
|
|
|
|
examples/lightrag_ollama_gremlin_demo.py
CHANGED
@@ -12,6 +12,7 @@ import os
|
|
12 |
from lightrag import LightRAG, QueryParam
|
13 |
from lightrag.llm.ollama import ollama_embed, ollama_model_complete
|
14 |
from lightrag.utils import EmbeddingFunc
|
|
|
15 |
|
16 |
WORKING_DIR = "./dickens_gremlin"
|
17 |
|
@@ -31,59 +32,72 @@ os.environ["GREMLIN_TRAVERSE_SOURCE"] = "g"
|
|
31 |
os.environ["GREMLIN_USER"] = ""
|
32 |
os.environ["GREMLIN_PASSWORD"] = ""
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
46 |
),
|
47 |
-
|
48 |
-
|
49 |
-
)
|
50 |
-
|
51 |
-
with open("./book.txt", "r", encoding="utf-8") as f:
|
52 |
-
rag.insert(f.read())
|
53 |
-
|
54 |
-
# Perform naive search
|
55 |
-
print(
|
56 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
57 |
-
)
|
58 |
-
|
59 |
-
# Perform local search
|
60 |
-
print(
|
61 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
62 |
-
)
|
63 |
-
|
64 |
-
# Perform global search
|
65 |
-
print(
|
66 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
67 |
-
)
|
68 |
-
|
69 |
-
# Perform hybrid search
|
70 |
-
print(
|
71 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
72 |
-
)
|
73 |
-
|
74 |
-
# stream response
|
75 |
-
resp = rag.query(
|
76 |
-
"What are the top themes in this story?",
|
77 |
-
param=QueryParam(mode="hybrid", stream=True),
|
78 |
-
)
|
79 |
|
|
|
|
|
|
|
|
|
80 |
|
81 |
async def print_stream(stream):
|
82 |
async for chunk in stream:
|
83 |
print(chunk, end="", flush=True)
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
asyncio.run(
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
from lightrag import LightRAG, QueryParam
|
13 |
from lightrag.llm.ollama import ollama_embed, ollama_model_complete
|
14 |
from lightrag.utils import EmbeddingFunc
|
15 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
16 |
|
17 |
WORKING_DIR = "./dickens_gremlin"
|
18 |
|
|
|
32 |
os.environ["GREMLIN_USER"] = ""
|
33 |
os.environ["GREMLIN_PASSWORD"] = ""
|
34 |
|
35 |
+
async def initialize_rag():
|
36 |
+
rag = LightRAG(
|
37 |
+
working_dir=WORKING_DIR,
|
38 |
+
llm_model_func=ollama_model_complete,
|
39 |
+
llm_model_name="llama3.1:8b",
|
40 |
+
llm_model_max_async=4,
|
41 |
+
llm_model_max_token_size=32768,
|
42 |
+
llm_model_kwargs={"host": "http://localhost:11434", "options": {"num_ctx": 32768}},
|
43 |
+
embedding_func=EmbeddingFunc(
|
44 |
+
embedding_dim=768,
|
45 |
+
max_token_size=8192,
|
46 |
+
func=lambda texts: ollama_embed(
|
47 |
+
texts, embed_model="nomic-embed-text", host="http://localhost:11434"
|
48 |
+
),
|
49 |
),
|
50 |
+
graph_storage="GremlinStorage",
|
51 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
await rag.initialize_storages()
|
54 |
+
await initialize_pipeline_status()
|
55 |
+
|
56 |
+
return rag
|
57 |
|
58 |
async def print_stream(stream):
|
59 |
async for chunk in stream:
|
60 |
print(chunk, end="", flush=True)
|
61 |
|
62 |
+
def main():
|
63 |
+
# Initialize RAG instance
|
64 |
+
rag = asyncio.run(initialize_rag())
|
65 |
+
|
66 |
+
# Insert example text
|
67 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
68 |
+
rag.insert(f.read())
|
69 |
+
|
70 |
+
# Test different query modes
|
71 |
+
print("\nNaive Search:")
|
72 |
+
print(
|
73 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
74 |
+
)
|
75 |
+
|
76 |
+
print("\nLocal Search:")
|
77 |
+
print(
|
78 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
79 |
+
)
|
80 |
+
|
81 |
+
print("\nGlobal Search:")
|
82 |
+
print(
|
83 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
84 |
+
)
|
85 |
+
|
86 |
+
print("\nHybrid Search:")
|
87 |
+
print(
|
88 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
89 |
+
)
|
90 |
+
|
91 |
+
# stream response
|
92 |
+
resp = rag.query(
|
93 |
+
"What are the top themes in this story?",
|
94 |
+
param=QueryParam(mode="hybrid", stream=True),
|
95 |
+
)
|
96 |
+
|
97 |
+
if inspect.isasyncgen(resp):
|
98 |
+
asyncio.run(print_stream(resp))
|
99 |
+
else:
|
100 |
+
print(resp)
|
101 |
+
|
102 |
+
if __name__ == "__main__":
|
103 |
+
main()
|
examples/lightrag_ollama_neo4j_milvus_mongo_demo.py
CHANGED
@@ -2,6 +2,11 @@ import os
|
|
2 |
from lightrag import LightRAG, QueryParam
|
3 |
from lightrag.llm.ollama import ollama_model_complete, ollama_embed
|
4 |
from lightrag.utils import EmbeddingFunc
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# WorkingDir
|
7 |
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
@@ -27,30 +32,59 @@ os.environ["MILVUS_USER"] = "root"
|
|
27 |
os.environ["MILVUS_PASSWORD"] = "root"
|
28 |
os.environ["MILVUS_DB_NAME"] = "lightrag"
|
29 |
|
30 |
-
|
31 |
-
rag = LightRAG(
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
43 |
),
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
rag
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from lightrag import LightRAG, QueryParam
|
3 |
from lightrag.llm.ollama import ollama_model_complete, ollama_embed
|
4 |
from lightrag.utils import EmbeddingFunc
|
5 |
+
import asyncio
|
6 |
+
import nest_asyncio
|
7 |
+
|
8 |
+
nest_asyncio.apply()
|
9 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
10 |
|
11 |
# WorkingDir
|
12 |
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
32 |
os.environ["MILVUS_PASSWORD"] = "root"
|
33 |
os.environ["MILVUS_DB_NAME"] = "lightrag"
|
34 |
|
35 |
+
async def initialize_rag():
|
36 |
+
rag = LightRAG(
|
37 |
+
working_dir=WORKING_DIR,
|
38 |
+
llm_model_func=ollama_model_complete,
|
39 |
+
llm_model_name="qwen2.5:14b",
|
40 |
+
llm_model_max_async=4,
|
41 |
+
llm_model_max_token_size=32768,
|
42 |
+
llm_model_kwargs={"host": "http://127.0.0.1:11434", "options": {"num_ctx": 32768}},
|
43 |
+
embedding_func=EmbeddingFunc(
|
44 |
+
embedding_dim=1024,
|
45 |
+
max_token_size=8192,
|
46 |
+
func=lambda texts: ollama_embed(
|
47 |
+
texts=texts, embed_model="bge-m3:latest", host="http://127.0.0.1:11434"
|
48 |
+
),
|
49 |
),
|
50 |
+
kv_storage="MongoKVStorage",
|
51 |
+
graph_storage="Neo4JStorage",
|
52 |
+
vector_storage="MilvusVectorDBStorage",
|
53 |
+
)
|
54 |
+
|
55 |
+
await rag.initialize_storages()
|
56 |
+
await initialize_pipeline_status()
|
57 |
+
|
58 |
+
return rag
|
59 |
+
|
60 |
+
def main():
|
61 |
+
# Initialize RAG instance
|
62 |
+
rag = asyncio.run(initialize_rag())
|
63 |
+
|
64 |
+
# Insert example text
|
65 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
66 |
+
rag.insert(f.read())
|
67 |
+
|
68 |
+
# Test different query modes
|
69 |
+
print("\nNaive Search:")
|
70 |
+
print(
|
71 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
72 |
+
)
|
73 |
+
|
74 |
+
print("\nLocal Search:")
|
75 |
+
print(
|
76 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
77 |
+
)
|
78 |
+
|
79 |
+
print("\nGlobal Search:")
|
80 |
+
print(
|
81 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
82 |
+
)
|
83 |
+
|
84 |
+
print("\nHybrid Search:")
|
85 |
+
print(
|
86 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
87 |
+
)
|
88 |
+
|
89 |
+
if __name__ == "__main__":
|
90 |
+
main()
|
examples/lightrag_openai_compatible_demo.py
CHANGED
@@ -4,6 +4,7 @@ from lightrag import LightRAG, QueryParam
|
|
4 |
from lightrag.llm.openai import openai_complete_if_cache, openai_embed
|
5 |
from lightrag.utils import EmbeddingFunc
|
6 |
import numpy as np
|
|
|
7 |
|
8 |
WORKING_DIR = "./dickens"
|
9 |
|
@@ -52,21 +53,28 @@ async def test_funcs():
|
|
52 |
|
53 |
# asyncio.run(test_funcs())
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
|
|
56 |
async def main():
|
57 |
try:
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
rag = LightRAG(
|
62 |
-
working_dir=WORKING_DIR,
|
63 |
-
llm_model_func=llm_model_func,
|
64 |
-
embedding_func=EmbeddingFunc(
|
65 |
-
embedding_dim=embedding_dimension,
|
66 |
-
max_token_size=8192,
|
67 |
-
func=embedding_func,
|
68 |
-
),
|
69 |
-
)
|
70 |
|
71 |
with open("./book.txt", "r", encoding="utf-8") as f:
|
72 |
await rag.ainsert(f.read())
|
|
|
4 |
from lightrag.llm.openai import openai_complete_if_cache, openai_embed
|
5 |
from lightrag.utils import EmbeddingFunc
|
6 |
import numpy as np
|
7 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
8 |
|
9 |
WORKING_DIR = "./dickens"
|
10 |
|
|
|
53 |
|
54 |
# asyncio.run(test_funcs())
|
55 |
|
56 |
+
async def initialize_rag():
|
57 |
+
embedding_dimension = await get_embedding_dim()
|
58 |
+
print(f"Detected embedding dimension: {embedding_dimension}")
|
59 |
+
|
60 |
+
rag = LightRAG(
|
61 |
+
working_dir=WORKING_DIR,
|
62 |
+
llm_model_func=llm_model_func,
|
63 |
+
embedding_func=EmbeddingFunc(
|
64 |
+
embedding_dim=embedding_dimension,
|
65 |
+
max_token_size=8192,
|
66 |
+
func=embedding_func,
|
67 |
+
),
|
68 |
+
)
|
69 |
+
|
70 |
+
await rag.initialize_storages()
|
71 |
+
await initialize_pipeline_status()
|
72 |
|
73 |
+
return rag
|
74 |
async def main():
|
75 |
try:
|
76 |
+
# Initialize RAG instance
|
77 |
+
rag = asyncio.run(initialize_rag())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
with open("./book.txt", "r", encoding="utf-8") as f:
|
80 |
await rag.ainsert(f.read())
|
examples/lightrag_openai_compatible_demo_embedding_cache.py
CHANGED
@@ -4,6 +4,7 @@ from lightrag import LightRAG, QueryParam
|
|
4 |
from lightrag.llm.openai import openai_complete_if_cache, openai_embed
|
5 |
from lightrag.utils import EmbeddingFunc
|
6 |
import numpy as np
|
|
|
7 |
|
8 |
WORKING_DIR = "./dickens"
|
9 |
|
@@ -52,25 +53,33 @@ async def test_funcs():
|
|
52 |
|
53 |
# asyncio.run(test_funcs())
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
async def main():
|
57 |
try:
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
rag = LightRAG(
|
62 |
-
working_dir=WORKING_DIR,
|
63 |
-
embedding_cache_config={
|
64 |
-
"enabled": True,
|
65 |
-
"similarity_threshold": 0.90,
|
66 |
-
},
|
67 |
-
llm_model_func=llm_model_func,
|
68 |
-
embedding_func=EmbeddingFunc(
|
69 |
-
embedding_dim=embedding_dimension,
|
70 |
-
max_token_size=8192,
|
71 |
-
func=embedding_func,
|
72 |
-
),
|
73 |
-
)
|
74 |
|
75 |
with open("./book.txt", "r", encoding="utf-8") as f:
|
76 |
await rag.ainsert(f.read())
|
|
|
4 |
from lightrag.llm.openai import openai_complete_if_cache, openai_embed
|
5 |
from lightrag.utils import EmbeddingFunc
|
6 |
import numpy as np
|
7 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
8 |
|
9 |
WORKING_DIR = "./dickens"
|
10 |
|
|
|
53 |
|
54 |
# asyncio.run(test_funcs())
|
55 |
|
56 |
+
async def initialize_rag():
|
57 |
+
embedding_dimension = await get_embedding_dim()
|
58 |
+
print(f"Detected embedding dimension: {embedding_dimension}")
|
59 |
+
|
60 |
+
rag = LightRAG(
|
61 |
+
working_dir=WORKING_DIR,
|
62 |
+
embedding_cache_config={
|
63 |
+
"enabled": True,
|
64 |
+
"similarity_threshold": 0.90,
|
65 |
+
},
|
66 |
+
llm_model_func=llm_model_func,
|
67 |
+
embedding_func=EmbeddingFunc(
|
68 |
+
embedding_dim=embedding_dimension,
|
69 |
+
max_token_size=8192,
|
70 |
+
func=embedding_func,
|
71 |
+
),
|
72 |
+
)
|
73 |
+
|
74 |
+
await rag.initialize_storages()
|
75 |
+
await initialize_pipeline_status()
|
76 |
+
|
77 |
+
return rag
|
78 |
|
79 |
async def main():
|
80 |
try:
|
81 |
+
# Initialize RAG instance
|
82 |
+
rag = asyncio.run(initialize_rag())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
with open("./book.txt", "r", encoding="utf-8") as f:
|
85 |
await rag.ainsert(f.read())
|
examples/lightrag_openai_compatible_stream_demo.py
CHANGED
@@ -1,9 +1,11 @@
|
|
1 |
import inspect
|
2 |
import os
|
|
|
3 |
from lightrag import LightRAG
|
4 |
from lightrag.llm import openai_complete, openai_embed
|
5 |
from lightrag.utils import EmbeddingFunc, always_get_an_event_loop
|
6 |
from lightrag import QueryParam
|
|
|
7 |
|
8 |
# WorkingDir
|
9 |
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
@@ -13,42 +15,54 @@ if not os.path.exists(WORKING_DIR):
|
|
13 |
print(f"WorkingDir: {WORKING_DIR}")
|
14 |
|
15 |
api_key = "empty"
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
31 |
),
|
32 |
-
)
|
33 |
-
)
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
resp = rag.query(
|
39 |
-
"What are the top themes in this story?",
|
40 |
-
param=QueryParam(mode="hybrid", stream=True),
|
41 |
-
)
|
42 |
|
|
|
43 |
|
44 |
async def print_stream(stream):
|
45 |
async for chunk in stream:
|
46 |
if chunk:
|
47 |
print(chunk, end="", flush=True)
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
loop = always_get_an_event_loop()
|
51 |
-
if inspect.isasyncgen(resp):
|
52 |
-
loop.run_until_complete(print_stream(resp))
|
53 |
-
else:
|
54 |
-
print(resp)
|
|
|
1 |
import inspect
|
2 |
import os
|
3 |
+
import asyncio
|
4 |
from lightrag import LightRAG
|
5 |
from lightrag.llm import openai_complete, openai_embed
|
6 |
from lightrag.utils import EmbeddingFunc, always_get_an_event_loop
|
7 |
from lightrag import QueryParam
|
8 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
9 |
|
10 |
# WorkingDir
|
11 |
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
15 |
print(f"WorkingDir: {WORKING_DIR}")
|
16 |
|
17 |
api_key = "empty"
|
18 |
+
async def initialize_rag():
|
19 |
+
rag = LightRAG(
|
20 |
+
working_dir=WORKING_DIR,
|
21 |
+
llm_model_func=openai_complete,
|
22 |
+
llm_model_name="qwen2.5-14b-instruct@4bit",
|
23 |
+
llm_model_max_async=4,
|
24 |
+
llm_model_max_token_size=32768,
|
25 |
+
llm_model_kwargs={"base_url": "http://127.0.0.1:1234/v1", "api_key": api_key},
|
26 |
+
embedding_func=EmbeddingFunc(
|
27 |
+
embedding_dim=1024,
|
28 |
+
max_token_size=8192,
|
29 |
+
func=lambda texts: openai_embed(
|
30 |
+
texts=texts,
|
31 |
+
model="text-embedding-bge-m3",
|
32 |
+
base_url="http://127.0.0.1:1234/v1",
|
33 |
+
api_key=api_key,
|
34 |
+
),
|
35 |
),
|
36 |
+
)
|
|
|
37 |
|
38 |
+
await rag.initialize_storages()
|
39 |
+
await initialize_pipeline_status()
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
return rag
|
42 |
|
43 |
async def print_stream(stream):
|
44 |
async for chunk in stream:
|
45 |
if chunk:
|
46 |
print(chunk, end="", flush=True)
|
47 |
|
48 |
+
def main():
|
49 |
+
# Initialize RAG instance
|
50 |
+
rag = asyncio.run(initialize_rag())
|
51 |
+
|
52 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
53 |
+
rag.insert(f.read())
|
54 |
+
|
55 |
+
resp = rag.query(
|
56 |
+
"What are the top themes in this story?",
|
57 |
+
param=QueryParam(mode="hybrid", stream=True),
|
58 |
+
)
|
59 |
+
|
60 |
+
loop = always_get_an_event_loop()
|
61 |
+
if inspect.isasyncgen(resp):
|
62 |
+
loop.run_until_complete(print_stream(resp))
|
63 |
+
else:
|
64 |
+
print(resp)
|
65 |
+
|
66 |
+
if __name__ == "__main__":
|
67 |
+
main()
|
68 |
|
|
|
|
|
|
|
|
|
|
examples/lightrag_openai_demo.py
CHANGED
@@ -1,40 +1,54 @@
|
|
1 |
import os
|
2 |
-
|
3 |
from lightrag import LightRAG, QueryParam
|
4 |
from lightrag.llm.openai import gpt_4o_mini_complete, openai_embed
|
|
|
5 |
|
6 |
WORKING_DIR = "./dickens"
|
7 |
|
8 |
if not os.path.exists(WORKING_DIR):
|
9 |
os.mkdir(WORKING_DIR)
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
|
|
|
|
21 |
|
22 |
-
# Perform
|
23 |
-
print(
|
24 |
-
|
25 |
-
)
|
26 |
|
27 |
-
# Perform
|
28 |
-
print(
|
29 |
-
|
30 |
-
)
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
35 |
-
)
|
36 |
|
37 |
-
# Perform hybrid search
|
38 |
-
print(
|
39 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
40 |
-
)
|
|
|
1 |
import os
|
2 |
+
import asyncio
|
3 |
from lightrag import LightRAG, QueryParam
|
4 |
from lightrag.llm.openai import gpt_4o_mini_complete, openai_embed
|
5 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
6 |
|
7 |
WORKING_DIR = "./dickens"
|
8 |
|
9 |
if not os.path.exists(WORKING_DIR):
|
10 |
os.mkdir(WORKING_DIR)
|
11 |
|
12 |
+
async def initialize_rag():
|
13 |
+
rag = LightRAG(
|
14 |
+
working_dir=WORKING_DIR,
|
15 |
+
embedding_func=openai_embed,
|
16 |
+
llm_model_func=gpt_4o_mini_complete,
|
17 |
+
# llm_model_func=gpt_4o_complete
|
18 |
+
)
|
19 |
+
|
20 |
+
await rag.initialize_storages()
|
21 |
+
await initialize_pipeline_status()
|
22 |
+
|
23 |
+
return rag
|
24 |
+
|
25 |
+
def main():
|
26 |
+
# Initialize RAG instance
|
27 |
+
rag = asyncio.run(initialize_rag())
|
28 |
+
|
29 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
30 |
+
rag.insert(f.read())
|
31 |
|
32 |
+
# Perform naive search
|
33 |
+
print(
|
34 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
35 |
+
)
|
36 |
|
37 |
+
# Perform local search
|
38 |
+
print(
|
39 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
40 |
+
)
|
41 |
|
42 |
+
# Perform global search
|
43 |
+
print(
|
44 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
45 |
+
)
|
46 |
|
47 |
+
# Perform hybrid search
|
48 |
+
print(
|
49 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
50 |
+
)
|
51 |
|
52 |
+
if __name__ == "__main__":
|
53 |
+
main()
|
|
|
|
|
54 |
|
|
|
|
|
|
|
|
examples/lightrag_openai_mongodb_graph_demo.py
CHANGED
@@ -4,6 +4,7 @@ from lightrag import LightRAG, QueryParam
|
|
4 |
from lightrag.llm.openai import gpt_4o_mini_complete, openai_embed
|
5 |
from lightrag.utils import EmbeddingFunc
|
6 |
import numpy as np
|
|
|
7 |
|
8 |
#########
|
9 |
# Uncomment the below two lines if running in a jupyter notebook to handle the async nature of rag.insert()
|
@@ -52,7 +53,7 @@ async def create_embedding_function_instance():
|
|
52 |
async def initialize_rag():
|
53 |
embedding_func_instance = await create_embedding_function_instance()
|
54 |
|
55 |
-
|
56 |
working_dir=WORKING_DIR,
|
57 |
llm_model_func=gpt_4o_mini_complete,
|
58 |
embedding_func=embedding_func_instance,
|
@@ -60,14 +61,38 @@ async def initialize_rag():
|
|
60 |
log_level="DEBUG",
|
61 |
)
|
62 |
|
|
|
|
|
63 |
|
64 |
-
|
65 |
-
rag = asyncio.run(initialize_rag())
|
66 |
|
67 |
-
with open("book.txt", "r", encoding="utf-8") as f:
|
68 |
-
rag.insert(f.read())
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
rag
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from lightrag.llm.openai import gpt_4o_mini_complete, openai_embed
|
5 |
from lightrag.utils import EmbeddingFunc
|
6 |
import numpy as np
|
7 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
8 |
|
9 |
#########
|
10 |
# Uncomment the below two lines if running in a jupyter notebook to handle the async nature of rag.insert()
|
|
|
53 |
async def initialize_rag():
|
54 |
embedding_func_instance = await create_embedding_function_instance()
|
55 |
|
56 |
+
rag = LightRAG(
|
57 |
working_dir=WORKING_DIR,
|
58 |
llm_model_func=gpt_4o_mini_complete,
|
59 |
embedding_func=embedding_func_instance,
|
|
|
61 |
log_level="DEBUG",
|
62 |
)
|
63 |
|
64 |
+
await rag.initialize_storages()
|
65 |
+
await initialize_pipeline_status()
|
66 |
|
67 |
+
return rag
|
|
|
68 |
|
|
|
|
|
69 |
|
70 |
+
def main():
|
71 |
+
# Initialize RAG instance
|
72 |
+
rag = asyncio.run(initialize_rag())
|
73 |
+
|
74 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
75 |
+
rag.insert(f.read())
|
76 |
+
|
77 |
+
# Perform naive search
|
78 |
+
print(
|
79 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
80 |
+
)
|
81 |
+
|
82 |
+
# Perform local search
|
83 |
+
print(
|
84 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
85 |
+
)
|
86 |
+
|
87 |
+
# Perform global search
|
88 |
+
print(
|
89 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
90 |
+
)
|
91 |
+
|
92 |
+
# Perform hybrid search
|
93 |
+
print(
|
94 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
95 |
+
)
|
96 |
+
|
97 |
+
if __name__ == "__main__":
|
98 |
+
main()
|
examples/lightrag_openai_neo4j_milvus_redis_demo.py
CHANGED
@@ -1,7 +1,9 @@
|
|
1 |
import os
|
|
|
2 |
from lightrag import LightRAG, QueryParam
|
3 |
from lightrag.llm.ollama import ollama_embed, openai_complete_if_cache
|
4 |
from lightrag.utils import EmbeddingFunc
|
|
|
5 |
|
6 |
# WorkingDir
|
7 |
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
@@ -48,23 +50,52 @@ embedding_func = EmbeddingFunc(
|
|
48 |
texts, embed_model="shaw/dmeta-embedding-zh", host="http://117.50.173.35:11434"
|
49 |
),
|
50 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
rag
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
)
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
-
|
|
|
|
1 |
import os
|
2 |
+
import asyncio
|
3 |
from lightrag import LightRAG, QueryParam
|
4 |
from lightrag.llm.ollama import ollama_embed, openai_complete_if_cache
|
5 |
from lightrag.utils import EmbeddingFunc
|
6 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
7 |
|
8 |
# WorkingDir
|
9 |
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
50 |
texts, embed_model="shaw/dmeta-embedding-zh", host="http://117.50.173.35:11434"
|
51 |
),
|
52 |
)
|
53 |
+
async def initialize_rag():
|
54 |
+
rag = LightRAG(
|
55 |
+
working_dir=WORKING_DIR,
|
56 |
+
llm_model_func=llm_model_func,
|
57 |
+
llm_model_max_token_size=32768,
|
58 |
+
embedding_func=embedding_func,
|
59 |
+
chunk_token_size=512,
|
60 |
+
chunk_overlap_token_size=256,
|
61 |
+
kv_storage="RedisKVStorage",
|
62 |
+
graph_storage="Neo4JStorage",
|
63 |
+
vector_storage="MilvusVectorDBStorage",
|
64 |
+
doc_status_storage="RedisKVStorage",
|
65 |
+
)
|
66 |
|
67 |
+
await rag.initialize_storages()
|
68 |
+
await initialize_pipeline_status()
|
69 |
+
|
70 |
+
return rag
|
71 |
+
|
72 |
+
|
73 |
+
def main():
|
74 |
+
# Initialize RAG instance
|
75 |
+
rag = asyncio.run(initialize_rag())
|
76 |
+
|
77 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
78 |
+
rag.insert(f.read())
|
79 |
|
80 |
+
# Perform naive search
|
81 |
+
print(
|
82 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
83 |
+
)
|
84 |
+
|
85 |
+
# Perform local search
|
86 |
+
print(
|
87 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
88 |
+
)
|
89 |
|
90 |
+
# Perform global search
|
91 |
+
print(
|
92 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
93 |
+
)
|
94 |
+
|
95 |
+
# Perform hybrid search
|
96 |
+
print(
|
97 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
98 |
+
)
|
99 |
|
100 |
+
if __name__ == "__main__":
|
101 |
+
main()
|
examples/lightrag_oracle_demo.py
CHANGED
@@ -6,6 +6,7 @@ from lightrag import LightRAG, QueryParam
|
|
6 |
from lightrag.llm.openai import openai_complete_if_cache, openai_embed
|
7 |
from lightrag.utils import EmbeddingFunc
|
8 |
import numpy as np
|
|
|
9 |
|
10 |
print(os.getcwd())
|
11 |
script_directory = Path(__file__).resolve().parent.parent
|
@@ -63,41 +64,48 @@ async def get_embedding_dim():
|
|
63 |
embedding_dim = embedding.shape[1]
|
64 |
return embedding_dim
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
async def main():
|
68 |
try:
|
69 |
-
#
|
70 |
-
|
71 |
-
print(f"Detected embedding dimension: {embedding_dimension}")
|
72 |
-
|
73 |
-
# Initialize LightRAG
|
74 |
-
# We use Oracle DB as the KV/vector/graph storage
|
75 |
-
# You can add `addon_params={"example_number": 1, "language": "Simplfied Chinese"}` to control the prompt
|
76 |
-
rag = LightRAG(
|
77 |
-
# log_level="DEBUG",
|
78 |
-
working_dir=WORKING_DIR,
|
79 |
-
entity_extract_max_gleaning=1,
|
80 |
-
enable_llm_cache=True,
|
81 |
-
enable_llm_cache_for_entity_extract=True,
|
82 |
-
embedding_cache_config=None, # {"enabled": True,"similarity_threshold": 0.90},
|
83 |
-
chunk_token_size=CHUNK_TOKEN_SIZE,
|
84 |
-
llm_model_max_token_size=MAX_TOKENS,
|
85 |
-
llm_model_func=llm_model_func,
|
86 |
-
embedding_func=EmbeddingFunc(
|
87 |
-
embedding_dim=embedding_dimension,
|
88 |
-
max_token_size=500,
|
89 |
-
func=embedding_func,
|
90 |
-
),
|
91 |
-
graph_storage="OracleGraphStorage",
|
92 |
-
kv_storage="OracleKVStorage",
|
93 |
-
vector_storage="OracleVectorDBStorage",
|
94 |
-
addon_params={
|
95 |
-
"example_number": 1,
|
96 |
-
"language": "Simplfied Chinese",
|
97 |
-
"entity_types": ["organization", "person", "geo", "event"],
|
98 |
-
"insert_batch_size": 2,
|
99 |
-
},
|
100 |
-
)
|
101 |
|
102 |
# Extract and Insert into LightRAG storage
|
103 |
with open(WORKING_DIR + "/docs.txt", "r", encoding="utf-8") as f:
|
|
|
6 |
from lightrag.llm.openai import openai_complete_if_cache, openai_embed
|
7 |
from lightrag.utils import EmbeddingFunc
|
8 |
import numpy as np
|
9 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
10 |
|
11 |
print(os.getcwd())
|
12 |
script_directory = Path(__file__).resolve().parent.parent
|
|
|
64 |
embedding_dim = embedding.shape[1]
|
65 |
return embedding_dim
|
66 |
|
67 |
+
async def initialize_rag():
|
68 |
+
# Detect embedding dimension
|
69 |
+
embedding_dimension = await get_embedding_dim()
|
70 |
+
print(f"Detected embedding dimension: {embedding_dimension}")
|
71 |
+
|
72 |
+
# Initialize LightRAG
|
73 |
+
# We use Oracle DB as the KV/vector/graph storage
|
74 |
+
# You can add `addon_params={"example_number": 1, "language": "Simplfied Chinese"}` to control the prompt
|
75 |
+
rag = LightRAG(
|
76 |
+
# log_level="DEBUG",
|
77 |
+
working_dir=WORKING_DIR,
|
78 |
+
entity_extract_max_gleaning=1,
|
79 |
+
enable_llm_cache=True,
|
80 |
+
enable_llm_cache_for_entity_extract=True,
|
81 |
+
embedding_cache_config=None, # {"enabled": True,"similarity_threshold": 0.90},
|
82 |
+
chunk_token_size=CHUNK_TOKEN_SIZE,
|
83 |
+
llm_model_max_token_size=MAX_TOKENS,
|
84 |
+
llm_model_func=llm_model_func,
|
85 |
+
embedding_func=EmbeddingFunc(
|
86 |
+
embedding_dim=embedding_dimension,
|
87 |
+
max_token_size=500,
|
88 |
+
func=embedding_func,
|
89 |
+
),
|
90 |
+
graph_storage="OracleGraphStorage",
|
91 |
+
kv_storage="OracleKVStorage",
|
92 |
+
vector_storage="OracleVectorDBStorage",
|
93 |
+
addon_params={
|
94 |
+
"example_number": 1,
|
95 |
+
"language": "Simplfied Chinese",
|
96 |
+
"entity_types": ["organization", "person", "geo", "event"],
|
97 |
+
"insert_batch_size": 2,
|
98 |
+
},
|
99 |
+
)
|
100 |
+
await rag.initialize_storages()
|
101 |
+
await initialize_pipeline_status()
|
102 |
+
|
103 |
+
return rag
|
104 |
|
105 |
async def main():
|
106 |
try:
|
107 |
+
# Initialize RAG instance
|
108 |
+
rag = asyncio.run(initialize_rag())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
# Extract and Insert into LightRAG storage
|
111 |
with open(WORKING_DIR + "/docs.txt", "r", encoding="utf-8") as f:
|
examples/lightrag_siliconcloud_demo.py
CHANGED
@@ -5,6 +5,7 @@ from lightrag.llm.openai import openai_complete_if_cache
|
|
5 |
from lightrag.llm.siliconcloud import siliconcloud_embedding
|
6 |
from lightrag.utils import EmbeddingFunc
|
7 |
import numpy as np
|
|
|
8 |
|
9 |
WORKING_DIR = "./dickens"
|
10 |
|
@@ -46,35 +47,48 @@ async def test_funcs():
|
|
46 |
|
47 |
asyncio.run(test_funcs())
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
rag
|
51 |
-
working_dir=WORKING_DIR,
|
52 |
-
llm_model_func=llm_model_func,
|
53 |
-
embedding_func=EmbeddingFunc(
|
54 |
-
embedding_dim=768, max_token_size=512, func=embedding_func
|
55 |
-
),
|
56 |
-
)
|
57 |
|
58 |
|
59 |
-
|
60 |
-
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
65 |
-
)
|
66 |
|
67 |
-
# Perform
|
68 |
-
print(
|
69 |
-
|
70 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
75 |
-
)
|
76 |
|
77 |
-
# Perform hybrid search
|
78 |
-
print(
|
79 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
80 |
-
)
|
|
|
5 |
from lightrag.llm.siliconcloud import siliconcloud_embedding
|
6 |
from lightrag.utils import EmbeddingFunc
|
7 |
import numpy as np
|
8 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
9 |
|
10 |
WORKING_DIR = "./dickens"
|
11 |
|
|
|
47 |
|
48 |
asyncio.run(test_funcs())
|
49 |
|
50 |
+
async def initialize_rag():
|
51 |
+
rag = LightRAG(
|
52 |
+
working_dir=WORKING_DIR,
|
53 |
+
llm_model_func=llm_model_func,
|
54 |
+
embedding_func=EmbeddingFunc(
|
55 |
+
embedding_dim=768, max_token_size=512, func=embedding_func
|
56 |
+
),
|
57 |
+
)
|
58 |
+
|
59 |
+
await rag.initialize_storages()
|
60 |
+
await initialize_pipeline_status()
|
61 |
|
62 |
+
return rag
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
|
65 |
+
def main():
|
66 |
+
# Initialize RAG instance
|
67 |
+
rag = asyncio.run(initialize_rag())
|
68 |
|
69 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
70 |
+
rag.insert(f.read())
|
|
|
|
|
71 |
|
72 |
+
# Perform naive search
|
73 |
+
print(
|
74 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
75 |
+
)
|
76 |
+
|
77 |
+
# Perform local search
|
78 |
+
print(
|
79 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
80 |
+
)
|
81 |
+
|
82 |
+
# Perform global search
|
83 |
+
print(
|
84 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
85 |
+
)
|
86 |
+
|
87 |
+
# Perform hybrid search
|
88 |
+
print(
|
89 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
90 |
+
)
|
91 |
|
92 |
+
if __name__ == "__main__":
|
93 |
+
main()
|
|
|
|
|
94 |
|
|
|
|
|
|
|
|
examples/lightrag_tidb_demo.py
CHANGED
@@ -6,6 +6,7 @@ import numpy as np
|
|
6 |
from lightrag import LightRAG, QueryParam
|
7 |
from lightrag.llm import siliconcloud_embedding, openai_complete_if_cache
|
8 |
from lightrag.utils import EmbeddingFunc
|
|
|
9 |
|
10 |
WORKING_DIR = "./dickens"
|
11 |
|
@@ -54,33 +55,40 @@ async def get_embedding_dim():
|
|
54 |
embedding_dim = embedding.shape[1]
|
55 |
return embedding_dim
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
async def main():
|
59 |
try:
|
60 |
-
#
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
# We use TiDB DB as the KV/vector
|
66 |
-
rag = LightRAG(
|
67 |
-
enable_llm_cache=False,
|
68 |
-
working_dir=WORKING_DIR,
|
69 |
-
chunk_token_size=512,
|
70 |
-
llm_model_func=llm_model_func,
|
71 |
-
embedding_func=EmbeddingFunc(
|
72 |
-
embedding_dim=embedding_dimension,
|
73 |
-
max_token_size=512,
|
74 |
-
func=embedding_func,
|
75 |
-
),
|
76 |
-
kv_storage="TiDBKVStorage",
|
77 |
-
vector_storage="TiDBVectorDBStorage",
|
78 |
-
graph_storage="TiDBGraphStorage",
|
79 |
-
)
|
80 |
-
|
81 |
-
# Extract and Insert into LightRAG storage
|
82 |
-
with open("./dickens/demo.txt", "r", encoding="utf-8") as f:
|
83 |
-
await rag.ainsert(f.read())
|
84 |
|
85 |
# Perform search in different modes
|
86 |
modes = ["naive", "local", "global", "hybrid"]
|
|
|
6 |
from lightrag import LightRAG, QueryParam
|
7 |
from lightrag.llm import siliconcloud_embedding, openai_complete_if_cache
|
8 |
from lightrag.utils import EmbeddingFunc
|
9 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
10 |
|
11 |
WORKING_DIR = "./dickens"
|
12 |
|
|
|
55 |
embedding_dim = embedding.shape[1]
|
56 |
return embedding_dim
|
57 |
|
58 |
+
async def initialize_rag():
|
59 |
+
# Detect embedding dimension
|
60 |
+
embedding_dimension = await get_embedding_dim()
|
61 |
+
print(f"Detected embedding dimension: {embedding_dimension}")
|
62 |
+
|
63 |
+
# Initialize LightRAG
|
64 |
+
# We use TiDB DB as the KV/vector
|
65 |
+
rag = LightRAG(
|
66 |
+
enable_llm_cache=False,
|
67 |
+
working_dir=WORKING_DIR,
|
68 |
+
chunk_token_size=512,
|
69 |
+
llm_model_func=llm_model_func,
|
70 |
+
embedding_func=EmbeddingFunc(
|
71 |
+
embedding_dim=embedding_dimension,
|
72 |
+
max_token_size=512,
|
73 |
+
func=embedding_func,
|
74 |
+
),
|
75 |
+
kv_storage="TiDBKVStorage",
|
76 |
+
vector_storage="TiDBVectorDBStorage",
|
77 |
+
graph_storage="TiDBGraphStorage",
|
78 |
+
)
|
79 |
+
|
80 |
+
await rag.initialize_storages()
|
81 |
+
await initialize_pipeline_status()
|
82 |
+
|
83 |
+
return rag
|
84 |
|
85 |
async def main():
|
86 |
try:
|
87 |
+
# Initialize RAG instance
|
88 |
+
rag = asyncio.run(initialize_rag())
|
89 |
+
|
90 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
91 |
+
rag.insert(f.read())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
# Perform search in different modes
|
94 |
modes = ["naive", "local", "global", "hybrid"]
|
examples/lightrag_zhipu_demo.py
CHANGED
@@ -1,10 +1,12 @@
|
|
1 |
import os
|
2 |
import logging
|
|
|
3 |
|
4 |
|
5 |
from lightrag import LightRAG, QueryParam
|
6 |
from lightrag.llm.zhipu import zhipu_complete, zhipu_embedding
|
7 |
from lightrag.utils import EmbeddingFunc
|
|
|
8 |
|
9 |
WORKING_DIR = "./dickens"
|
10 |
|
@@ -17,39 +19,51 @@ api_key = os.environ.get("ZHIPUAI_API_KEY")
|
|
17 |
if api_key is None:
|
18 |
raise Exception("Please set ZHIPU_API_KEY in your environment")
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
rag
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
)
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
)
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
)
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
)
|
|
|
1 |
import os
|
2 |
import logging
|
3 |
+
import asyncio
|
4 |
|
5 |
|
6 |
from lightrag import LightRAG, QueryParam
|
7 |
from lightrag.llm.zhipu import zhipu_complete, zhipu_embedding
|
8 |
from lightrag.utils import EmbeddingFunc
|
9 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
10 |
|
11 |
WORKING_DIR = "./dickens"
|
12 |
|
|
|
19 |
if api_key is None:
|
20 |
raise Exception("Please set ZHIPU_API_KEY in your environment")
|
21 |
|
22 |
+
async def initialize_rag():
|
23 |
+
rag = LightRAG(
|
24 |
+
working_dir=WORKING_DIR,
|
25 |
+
llm_model_func=zhipu_complete,
|
26 |
+
llm_model_name="glm-4-flashx", # Using the most cost/performance balance model, but you can change it here.
|
27 |
+
llm_model_max_async=4,
|
28 |
+
llm_model_max_token_size=32768,
|
29 |
+
embedding_func=EmbeddingFunc(
|
30 |
+
embedding_dim=2048, # Zhipu embedding-3 dimension
|
31 |
+
max_token_size=8192,
|
32 |
+
func=lambda texts: zhipu_embedding(texts),
|
33 |
+
),
|
34 |
+
)
|
35 |
|
36 |
+
await rag.initialize_storages()
|
37 |
+
await initialize_pipeline_status()
|
38 |
+
|
39 |
+
return rag
|
40 |
+
|
41 |
+
def main():
|
42 |
+
# Initialize RAG instance
|
43 |
+
rag = asyncio.run(initialize_rag())
|
44 |
+
|
45 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
46 |
+
rag.insert(f.read())
|
47 |
+
|
48 |
+
# Perform naive search
|
49 |
+
print(
|
50 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
51 |
+
)
|
52 |
+
|
53 |
+
# Perform local search
|
54 |
+
print(
|
55 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
56 |
+
)
|
57 |
+
|
58 |
+
# Perform global search
|
59 |
+
print(
|
60 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
61 |
+
)
|
62 |
+
|
63 |
+
# Perform hybrid search
|
64 |
+
print(
|
65 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
66 |
+
)
|
67 |
+
|
68 |
+
if __name__ == "__main__":
|
69 |
+
main()
|
|
examples/lightrag_zhipu_postgres_demo.py
CHANGED
@@ -8,6 +8,7 @@ from lightrag import LightRAG, QueryParam
|
|
8 |
from lightrag.llm.zhipu import zhipu_complete
|
9 |
from lightrag.llm.ollama import ollama_embedding
|
10 |
from lightrag.utils import EmbeddingFunc
|
|
|
11 |
|
12 |
load_dotenv()
|
13 |
ROOT_DIR = os.environ.get("ROOT_DIR")
|
@@ -27,8 +28,7 @@ os.environ["POSTGRES_USER"] = "rag"
|
|
27 |
os.environ["POSTGRES_PASSWORD"] = "rag"
|
28 |
os.environ["POSTGRES_DATABASE"] = "rag"
|
29 |
|
30 |
-
|
31 |
-
async def main():
|
32 |
rag = LightRAG(
|
33 |
working_dir=WORKING_DIR,
|
34 |
llm_model_func=zhipu_complete,
|
@@ -50,9 +50,17 @@ async def main():
|
|
50 |
auto_manage_storages_states=False,
|
51 |
)
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
# add embedding_func for graph database, it's deleted in commit 5661d76860436f7bf5aef2e50d9ee4a59660146c
|
54 |
rag.chunk_entity_relation_graph.embedding_func = rag.embedding_func
|
55 |
-
await rag.initialize_storages()
|
56 |
|
57 |
with open(f"{ROOT_DIR}/book.txt", "r", encoding="utf-8") as f:
|
58 |
await rag.ainsert(f.read())
|
|
|
8 |
from lightrag.llm.zhipu import zhipu_complete
|
9 |
from lightrag.llm.ollama import ollama_embedding
|
10 |
from lightrag.utils import EmbeddingFunc
|
11 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
12 |
|
13 |
load_dotenv()
|
14 |
ROOT_DIR = os.environ.get("ROOT_DIR")
|
|
|
28 |
os.environ["POSTGRES_PASSWORD"] = "rag"
|
29 |
os.environ["POSTGRES_DATABASE"] = "rag"
|
30 |
|
31 |
+
async def initialize_rag():
|
|
|
32 |
rag = LightRAG(
|
33 |
working_dir=WORKING_DIR,
|
34 |
llm_model_func=zhipu_complete,
|
|
|
50 |
auto_manage_storages_states=False,
|
51 |
)
|
52 |
|
53 |
+
await rag.initialize_storages()
|
54 |
+
await initialize_pipeline_status()
|
55 |
+
|
56 |
+
return rag
|
57 |
+
|
58 |
+
async def main():
|
59 |
+
# Initialize RAG instance
|
60 |
+
rag = asyncio.run(initialize_rag())
|
61 |
+
|
62 |
# add embedding_func for graph database, it's deleted in commit 5661d76860436f7bf5aef2e50d9ee4a59660146c
|
63 |
rag.chunk_entity_relation_graph.embedding_func = rag.embedding_func
|
|
|
64 |
|
65 |
with open(f"{ROOT_DIR}/book.txt", "r", encoding="utf-8") as f:
|
66 |
await rag.ainsert(f.read())
|
examples/query_keyword_separation_example.py
CHANGED
@@ -6,6 +6,7 @@ import numpy as np
|
|
6 |
from dotenv import load_dotenv
|
7 |
import logging
|
8 |
from openai import AzureOpenAI
|
|
|
9 |
|
10 |
logging.basicConfig(level=logging.INFO)
|
11 |
|
@@ -79,25 +80,32 @@ async def test_funcs():
|
|
79 |
asyncio.run(test_funcs())
|
80 |
|
81 |
embedding_dimension = 3072
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
-
rag
|
84 |
-
|
85 |
-
llm_model_func=llm_model_func,
|
86 |
-
embedding_func=EmbeddingFunc(
|
87 |
-
embedding_dim=embedding_dimension,
|
88 |
-
max_token_size=8192,
|
89 |
-
func=embedding_func,
|
90 |
-
),
|
91 |
-
)
|
92 |
-
|
93 |
-
book1 = open("./book_1.txt", encoding="utf-8")
|
94 |
-
book2 = open("./book_2.txt", encoding="utf-8")
|
95 |
|
96 |
-
rag
|
97 |
|
98 |
|
99 |
# Example function demonstrating the new query_with_separate_keyword_extraction usage
|
100 |
async def run_example():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
query = "What are the top themes in this story?"
|
102 |
prompt = "Please simplify the response for a young audience."
|
103 |
|
|
|
6 |
from dotenv import load_dotenv
|
7 |
import logging
|
8 |
from openai import AzureOpenAI
|
9 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
10 |
|
11 |
logging.basicConfig(level=logging.INFO)
|
12 |
|
|
|
80 |
asyncio.run(test_funcs())
|
81 |
|
82 |
embedding_dimension = 3072
|
83 |
+
async def initialize_rag():
|
84 |
+
rag = LightRAG(
|
85 |
+
working_dir=WORKING_DIR,
|
86 |
+
llm_model_func=llm_model_func,
|
87 |
+
embedding_func=EmbeddingFunc(
|
88 |
+
embedding_dim=embedding_dimension,
|
89 |
+
max_token_size=8192,
|
90 |
+
func=embedding_func,
|
91 |
+
),
|
92 |
+
)
|
93 |
|
94 |
+
await rag.initialize_storages()
|
95 |
+
await initialize_pipeline_status()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
+
return rag
|
98 |
|
99 |
|
100 |
# Example function demonstrating the new query_with_separate_keyword_extraction usage
|
101 |
async def run_example():
|
102 |
+
# Initialize RAG instance
|
103 |
+
rag = asyncio.run(initialize_rag())
|
104 |
+
|
105 |
+
book1 = open("./book_1.txt", encoding="utf-8")
|
106 |
+
book2 = open("./book_2.txt", encoding="utf-8")
|
107 |
+
|
108 |
+
rag.insert([book1.read(), book2.read()])
|
109 |
query = "What are the top themes in this story?"
|
110 |
prompt = "Please simplify the response for a young audience."
|
111 |
|
examples/test.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import os
|
2 |
from lightrag import LightRAG, QueryParam
|
3 |
from lightrag.llm.openai import gpt_4o_mini_complete
|
|
|
4 |
#########
|
5 |
# Uncomment the below two lines if running in a jupyter notebook to handle the async nature of rag.insert()
|
6 |
# import nest_asyncio
|
@@ -12,31 +13,45 @@ WORKING_DIR = "./dickens"
|
|
12 |
if not os.path.exists(WORKING_DIR):
|
13 |
os.mkdir(WORKING_DIR)
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
rag.
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
#
|
30 |
-
|
31 |
-
|
32 |
-
)
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
)
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
from lightrag import LightRAG, QueryParam
|
3 |
from lightrag.llm.openai import gpt_4o_mini_complete
|
4 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
5 |
#########
|
6 |
# Uncomment the below two lines if running in a jupyter notebook to handle the async nature of rag.insert()
|
7 |
# import nest_asyncio
|
|
|
13 |
if not os.path.exists(WORKING_DIR):
|
14 |
os.mkdir(WORKING_DIR)
|
15 |
|
16 |
+
async def initialize_rag():
|
17 |
+
rag = LightRAG(
|
18 |
+
working_dir=WORKING_DIR,
|
19 |
+
llm_model_func=gpt_4o_mini_complete, # Use gpt_4o_mini_complete LLM model
|
20 |
+
# llm_model_func=gpt_4o_complete # Optionally, use a stronger model
|
21 |
+
)
|
22 |
+
|
23 |
+
await rag.initialize_storages()
|
24 |
+
await initialize_pipeline_status()
|
25 |
+
|
26 |
+
return rag
|
27 |
+
|
28 |
+
|
29 |
+
def main():
|
30 |
+
# Initialize RAG instance
|
31 |
+
rag = asyncio.run(initialize_rag())
|
32 |
+
|
33 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
34 |
+
rag.insert(f.read())
|
35 |
+
|
36 |
+
# Perform naive search
|
37 |
+
print(
|
38 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
39 |
+
)
|
40 |
+
|
41 |
+
# Perform local search
|
42 |
+
print(
|
43 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
44 |
+
)
|
45 |
+
|
46 |
+
# Perform global search
|
47 |
+
print(
|
48 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
49 |
+
)
|
50 |
+
|
51 |
+
# Perform hybrid search
|
52 |
+
print(
|
53 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
54 |
+
)
|
55 |
+
|
56 |
+
if __name__ == "__main__":
|
57 |
+
main()
|
examples/test_chromadb.py
CHANGED
@@ -4,6 +4,7 @@ from lightrag import LightRAG, QueryParam
|
|
4 |
from lightrag.llm.openai import gpt_4o_mini_complete, openai_embed
|
5 |
from lightrag.utils import EmbeddingFunc
|
6 |
import numpy as np
|
|
|
7 |
|
8 |
#########
|
9 |
# Uncomment the below two lines if running in a jupyter notebook to handle the async nature of rag.insert()
|
@@ -67,7 +68,7 @@ async def create_embedding_function_instance():
|
|
67 |
async def initialize_rag():
|
68 |
embedding_func_instance = await create_embedding_function_instance()
|
69 |
if CHROMADB_USE_LOCAL_PERSISTENT:
|
70 |
-
|
71 |
working_dir=WORKING_DIR,
|
72 |
llm_model_func=gpt_4o_mini_complete,
|
73 |
embedding_func=embedding_func_instance,
|
@@ -87,7 +88,7 @@ async def initialize_rag():
|
|
87 |
},
|
88 |
)
|
89 |
else:
|
90 |
-
|
91 |
working_dir=WORKING_DIR,
|
92 |
llm_model_func=gpt_4o_mini_complete,
|
93 |
embedding_func=embedding_func_instance,
|
@@ -112,28 +113,36 @@ async def initialize_rag():
|
|
112 |
)
|
113 |
|
114 |
|
115 |
-
|
116 |
-
|
117 |
|
118 |
-
|
119 |
-
# rag.insert(f.read())
|
120 |
|
121 |
-
#
|
122 |
-
|
123 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
124 |
-
)
|
125 |
|
126 |
-
|
127 |
-
|
128 |
-
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
129 |
-
)
|
130 |
|
131 |
-
# Perform
|
132 |
-
print(
|
133 |
-
|
134 |
-
)
|
135 |
|
136 |
-
# Perform
|
137 |
-
print(
|
138 |
-
|
139 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from lightrag.llm.openai import gpt_4o_mini_complete, openai_embed
|
5 |
from lightrag.utils import EmbeddingFunc
|
6 |
import numpy as np
|
7 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
8 |
|
9 |
#########
|
10 |
# Uncomment the below two lines if running in a jupyter notebook to handle the async nature of rag.insert()
|
|
|
68 |
async def initialize_rag():
|
69 |
embedding_func_instance = await create_embedding_function_instance()
|
70 |
if CHROMADB_USE_LOCAL_PERSISTENT:
|
71 |
+
rag = LightRAG(
|
72 |
working_dir=WORKING_DIR,
|
73 |
llm_model_func=gpt_4o_mini_complete,
|
74 |
embedding_func=embedding_func_instance,
|
|
|
88 |
},
|
89 |
)
|
90 |
else:
|
91 |
+
rag = LightRAG(
|
92 |
working_dir=WORKING_DIR,
|
93 |
llm_model_func=gpt_4o_mini_complete,
|
94 |
embedding_func=embedding_func_instance,
|
|
|
113 |
)
|
114 |
|
115 |
|
116 |
+
await rag.initialize_storages()
|
117 |
+
await initialize_pipeline_status()
|
118 |
|
119 |
+
return rag
|
|
|
120 |
|
121 |
+
# Initialize RAG instance
|
122 |
+
rag = asyncio.run(initialize_rag())
|
|
|
|
|
123 |
|
124 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
125 |
+
rag.insert(f.read())
|
|
|
|
|
126 |
|
127 |
+
# Perform naive search
|
128 |
+
print(
|
129 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
130 |
+
)
|
131 |
|
132 |
+
# Perform local search
|
133 |
+
print(
|
134 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
135 |
+
)
|
136 |
+
|
137 |
+
# Perform global search
|
138 |
+
print(
|
139 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
140 |
+
)
|
141 |
+
|
142 |
+
# Perform hybrid search
|
143 |
+
print(
|
144 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
145 |
+
)
|
146 |
+
|
147 |
+
if __name__ == "__main__":
|
148 |
+
main()
|
examples/test_faiss.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
import logging
|
|
|
3 |
import numpy as np
|
4 |
|
5 |
from dotenv import load_dotenv
|
@@ -8,7 +9,9 @@ from sentence_transformers import SentenceTransformer
|
|
8 |
from openai import AzureOpenAI
|
9 |
from lightrag import LightRAG, QueryParam
|
10 |
from lightrag.utils import EmbeddingFunc
|
|
|
11 |
|
|
|
12 |
# Configure Logging
|
13 |
logging.basicConfig(level=logging.INFO)
|
14 |
|
@@ -55,11 +58,7 @@ async def embedding_func(texts: list[str]) -> np.ndarray:
|
|
55 |
embeddings = model.encode(texts, convert_to_numpy=True)
|
56 |
return embeddings
|
57 |
|
58 |
-
|
59 |
-
def main():
|
60 |
-
WORKING_DIR = "./dickens"
|
61 |
-
|
62 |
-
# Initialize LightRAG with the LLM model function and embedding function
|
63 |
rag = LightRAG(
|
64 |
working_dir=WORKING_DIR,
|
65 |
llm_model_func=llm_model_func,
|
@@ -74,6 +73,15 @@ def main():
|
|
74 |
},
|
75 |
)
|
76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
# Insert the custom chunks into LightRAG
|
78 |
book1 = open("./book_1.txt", encoding="utf-8")
|
79 |
book2 = open("./book_2.txt", encoding="utf-8")
|
|
|
1 |
import os
|
2 |
import logging
|
3 |
+
import asyncio
|
4 |
import numpy as np
|
5 |
|
6 |
from dotenv import load_dotenv
|
|
|
9 |
from openai import AzureOpenAI
|
10 |
from lightrag import LightRAG, QueryParam
|
11 |
from lightrag.utils import EmbeddingFunc
|
12 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
13 |
|
14 |
+
WORKING_DIR = "./dickens"
|
15 |
# Configure Logging
|
16 |
logging.basicConfig(level=logging.INFO)
|
17 |
|
|
|
58 |
embeddings = model.encode(texts, convert_to_numpy=True)
|
59 |
return embeddings
|
60 |
|
61 |
+
async def initialize_rag():
|
|
|
|
|
|
|
|
|
62 |
rag = LightRAG(
|
63 |
working_dir=WORKING_DIR,
|
64 |
llm_model_func=llm_model_func,
|
|
|
73 |
},
|
74 |
)
|
75 |
|
76 |
+
await rag.initialize_storages()
|
77 |
+
await initialize_pipeline_status()
|
78 |
+
|
79 |
+
return rag
|
80 |
+
|
81 |
+
def main():
|
82 |
+
|
83 |
+
# Initialize RAG instance
|
84 |
+
rag = asyncio.run(initialize_rag())
|
85 |
# Insert the custom chunks into LightRAG
|
86 |
book1 = open("./book_1.txt", encoding="utf-8")
|
87 |
book2 = open("./book_2.txt", encoding="utf-8")
|
examples/test_neo4j.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
import os
|
|
|
2 |
from lightrag import LightRAG, QueryParam
|
3 |
from lightrag.llm.openai import gpt_4o_mini_complete
|
4 |
-
|
5 |
|
6 |
#########
|
7 |
# Uncomment the below two lines if running in a jupyter notebook to handle the async nature of rag.insert()
|
@@ -14,33 +15,46 @@ WORKING_DIR = "./local_neo4jWorkDir"
|
|
14 |
if not os.path.exists(WORKING_DIR):
|
15 |
os.mkdir(WORKING_DIR)
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
rag.
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
)
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
)
|
37 |
-
|
38 |
-
# Perform
|
39 |
-
print(
|
40 |
-
|
41 |
-
)
|
42 |
-
|
43 |
-
# Perform
|
44 |
-
print(
|
45 |
-
|
46 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import asyncio
|
3 |
from lightrag import LightRAG, QueryParam
|
4 |
from lightrag.llm.openai import gpt_4o_mini_complete
|
5 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
6 |
|
7 |
#########
|
8 |
# Uncomment the below two lines if running in a jupyter notebook to handle the async nature of rag.insert()
|
|
|
15 |
if not os.path.exists(WORKING_DIR):
|
16 |
os.mkdir(WORKING_DIR)
|
17 |
|
18 |
+
async def initialize_rag():
|
19 |
+
rag = LightRAG(
|
20 |
+
working_dir=WORKING_DIR,
|
21 |
+
llm_model_func=gpt_4o_mini_complete, # Use gpt_4o_mini_complete LLM model
|
22 |
+
graph_storage="Neo4JStorage",
|
23 |
+
log_level="INFO",
|
24 |
+
# llm_model_func=gpt_4o_complete # Optionally, use a stronger model
|
25 |
+
)
|
26 |
+
|
27 |
+
await rag.initialize_storages()
|
28 |
+
await initialize_pipeline_status()
|
29 |
+
|
30 |
+
return rag
|
31 |
+
|
32 |
+
def main():
|
33 |
+
# Initialize RAG instance
|
34 |
+
rag = asyncio.run(initialize_rag())
|
35 |
+
|
36 |
+
with open("./book.txt", "r", encoding="utf-8") as f:
|
37 |
+
rag.insert(f.read())
|
38 |
+
|
39 |
+
# Perform naive search
|
40 |
+
print(
|
41 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="naive"))
|
42 |
+
)
|
43 |
+
|
44 |
+
# Perform local search
|
45 |
+
print(
|
46 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="local"))
|
47 |
+
)
|
48 |
+
|
49 |
+
# Perform global search
|
50 |
+
print(
|
51 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="global"))
|
52 |
+
)
|
53 |
+
|
54 |
+
# Perform hybrid search
|
55 |
+
print(
|
56 |
+
rag.query("What are the top themes in this story?", param=QueryParam(mode="hybrid"))
|
57 |
+
)
|
58 |
+
|
59 |
+
if __name__ == "__main__":
|
60 |
+
main()
|
examples/test_split_by_character.ipynb
CHANGED
@@ -18,6 +18,7 @@
|
|
18 |
"from lightrag import LightRAG, QueryParam\n",
|
19 |
"from lightrag.llm.openai import openai_complete_if_cache, openai_embed\n",
|
20 |
"from lightrag.utils import EmbeddingFunc\n",
|
|
|
21 |
"import nest_asyncio"
|
22 |
]
|
23 |
},
|
@@ -25,7 +26,9 @@
|
|
25 |
"cell_type": "markdown",
|
26 |
"id": "dd17956ec322b361",
|
27 |
"metadata": {},
|
28 |
-
"source":
|
|
|
|
|
29 |
},
|
30 |
{
|
31 |
"cell_type": "code",
|
@@ -109,14 +112,26 @@
|
|
109 |
}
|
110 |
],
|
111 |
"source": [
|
112 |
-
"
|
113 |
-
"
|
114 |
-
"
|
115 |
-
"
|
116 |
-
"
|
117 |
-
"
|
118 |
-
"
|
119 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
]
|
121 |
},
|
122 |
{
|
@@ -908,7 +923,9 @@
|
|
908 |
"cell_type": "markdown",
|
909 |
"id": "4e5bfad24cb721a8",
|
910 |
"metadata": {},
|
911 |
-
"source":
|
|
|
|
|
912 |
},
|
913 |
{
|
914 |
"cell_type": "code",
|
|
|
18 |
"from lightrag import LightRAG, QueryParam\n",
|
19 |
"from lightrag.llm.openai import openai_complete_if_cache, openai_embed\n",
|
20 |
"from lightrag.utils import EmbeddingFunc\n",
|
21 |
+
"from lightrag.kg.shared_storage import initialize_pipeline_status\n",
|
22 |
"import nest_asyncio"
|
23 |
]
|
24 |
},
|
|
|
26 |
"cell_type": "markdown",
|
27 |
"id": "dd17956ec322b361",
|
28 |
"metadata": {},
|
29 |
+
"source": [
|
30 |
+
"#### split by character"
|
31 |
+
]
|
32 |
},
|
33 |
{
|
34 |
"cell_type": "code",
|
|
|
112 |
}
|
113 |
],
|
114 |
"source": [
|
115 |
+
"import asyncio\n",
|
116 |
+
"import nest_asyncio\n",
|
117 |
+
"\n",
|
118 |
+
"nest_asyncio.apply()\n",
|
119 |
+
"\n",
|
120 |
+
"async def initialize_rag():\n",
|
121 |
+
" rag = LightRAG(\n",
|
122 |
+
" working_dir=WORKING_DIR,\n",
|
123 |
+
" llm_model_func=llm_model_func,\n",
|
124 |
+
" embedding_func=EmbeddingFunc(\n",
|
125 |
+
" embedding_dim=4096, max_token_size=8192, func=embedding_func\n",
|
126 |
+
" ),\n",
|
127 |
+
" chunk_token_size=512,\n",
|
128 |
+
" )\n",
|
129 |
+
" await rag.initialize_storages()\n",
|
130 |
+
" await initialize_pipeline_status()\n",
|
131 |
+
"\n",
|
132 |
+
" return rag\n",
|
133 |
+
"\n",
|
134 |
+
"rag = asyncio.run(initialize_rag())"
|
135 |
]
|
136 |
},
|
137 |
{
|
|
|
923 |
"cell_type": "markdown",
|
924 |
"id": "4e5bfad24cb721a8",
|
925 |
"metadata": {},
|
926 |
+
"source": [
|
927 |
+
"#### split by character only"
|
928 |
+
]
|
929 |
},
|
930 |
{
|
931 |
"cell_type": "code",
|
examples/vram_management_demo.py
CHANGED
@@ -1,8 +1,10 @@
|
|
1 |
import os
|
2 |
import time
|
|
|
3 |
from lightrag import LightRAG, QueryParam
|
4 |
from lightrag.llm.ollama import ollama_model_complete, ollama_embed
|
5 |
from lightrag.utils import EmbeddingFunc
|
|
|
6 |
|
7 |
# Working directory and the directory path for text files
|
8 |
WORKING_DIR = "./dickens"
|
@@ -12,17 +14,22 @@ TEXT_FILES_DIR = "/llm/mt"
|
|
12 |
if not os.path.exists(WORKING_DIR):
|
13 |
os.mkdir(WORKING_DIR)
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
)
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# Read all .txt files from the TEXT_FILES_DIR directory
|
28 |
texts = []
|
@@ -47,58 +54,65 @@ def insert_texts_with_retry(rag, texts, retries=3, delay=5):
|
|
47 |
raise RuntimeError("Failed to insert texts after multiple retries.")
|
48 |
|
49 |
|
50 |
-
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
# Perform different types of queries and handle potential errors
|
53 |
-
try:
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
57 |
)
|
58 |
-
|
59 |
-
|
60 |
-
print(f"Error performing naive search: {e}")
|
61 |
|
62 |
-
try:
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
66 |
)
|
67 |
-
|
68 |
-
|
69 |
-
print(f"Error performing local search: {e}")
|
70 |
|
71 |
-
try:
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
75 |
)
|
76 |
-
|
77 |
-
|
78 |
-
print(f"Error performing global search: {e}")
|
79 |
|
80 |
-
try:
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
84 |
)
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
|
|
|
|
|
|
|
89 |
|
90 |
-
# Function to clear VRAM resources
|
91 |
-
def clear_vram():
|
92 |
-
os.system("sudo nvidia-smi --gpu-reset")
|
93 |
|
|
|
|
|
|
|
94 |
|
95 |
-
|
96 |
-
|
97 |
-
start_time
|
|
|
|
|
|
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
if current_time - start_time > clear_vram_interval:
|
102 |
-
clear_vram()
|
103 |
-
start_time = current_time
|
104 |
-
time.sleep(60) # Check the time every minute
|
|
|
1 |
import os
|
2 |
import time
|
3 |
+
import asyncio
|
4 |
from lightrag import LightRAG, QueryParam
|
5 |
from lightrag.llm.ollama import ollama_model_complete, ollama_embed
|
6 |
from lightrag.utils import EmbeddingFunc
|
7 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
8 |
|
9 |
# Working directory and the directory path for text files
|
10 |
WORKING_DIR = "./dickens"
|
|
|
14 |
if not os.path.exists(WORKING_DIR):
|
15 |
os.mkdir(WORKING_DIR)
|
16 |
|
17 |
+
async def initialize_rag():
|
18 |
+
# Initialize LightRAG
|
19 |
+
rag = LightRAG(
|
20 |
+
working_dir=WORKING_DIR,
|
21 |
+
llm_model_func=ollama_model_complete,
|
22 |
+
llm_model_name="qwen2.5:3b-instruct-max-context",
|
23 |
+
embedding_func=EmbeddingFunc(
|
24 |
+
embedding_dim=768,
|
25 |
+
max_token_size=8192,
|
26 |
+
func=lambda texts: ollama_embed(texts, embed_model="nomic-embed-text"),
|
27 |
+
),
|
28 |
+
)
|
29 |
+
await rag.initialize_storages()
|
30 |
+
await initialize_pipeline_status()
|
31 |
+
|
32 |
+
return rag
|
33 |
|
34 |
# Read all .txt files from the TEXT_FILES_DIR directory
|
35 |
texts = []
|
|
|
54 |
raise RuntimeError("Failed to insert texts after multiple retries.")
|
55 |
|
56 |
|
57 |
+
def main():
|
58 |
+
# Initialize RAG instance
|
59 |
+
rag = asyncio.run(initialize_rag())
|
60 |
+
|
61 |
+
insert_texts_with_retry(rag, texts)
|
62 |
|
63 |
+
# Perform different types of queries and handle potential errors
|
64 |
+
try:
|
65 |
+
print(
|
66 |
+
rag.query(
|
67 |
+
"What are the top themes in this story?", param=QueryParam(mode="naive")
|
68 |
+
)
|
69 |
)
|
70 |
+
except Exception as e:
|
71 |
+
print(f"Error performing naive search: {e}")
|
|
|
72 |
|
73 |
+
try:
|
74 |
+
print(
|
75 |
+
rag.query(
|
76 |
+
"What are the top themes in this story?", param=QueryParam(mode="local")
|
77 |
+
)
|
78 |
)
|
79 |
+
except Exception as e:
|
80 |
+
print(f"Error performing local search: {e}")
|
|
|
81 |
|
82 |
+
try:
|
83 |
+
print(
|
84 |
+
rag.query(
|
85 |
+
"What are the top themes in this story?", param=QueryParam(mode="global")
|
86 |
+
)
|
87 |
)
|
88 |
+
except Exception as e:
|
89 |
+
print(f"Error performing global search: {e}")
|
|
|
90 |
|
91 |
+
try:
|
92 |
+
print(
|
93 |
+
rag.query(
|
94 |
+
"What are the top themes in this story?", param=QueryParam(mode="hybrid")
|
95 |
+
)
|
96 |
)
|
97 |
+
except Exception as e:
|
98 |
+
print(f"Error performing hybrid search: {e}")
|
99 |
+
|
100 |
|
101 |
+
# Function to clear VRAM resources
|
102 |
+
def clear_vram():
|
103 |
+
os.system("sudo nvidia-smi --gpu-reset")
|
104 |
|
|
|
|
|
|
|
105 |
|
106 |
+
# Regularly clear VRAM to prevent overflow
|
107 |
+
clear_vram_interval = 3600 # Clear once every hour
|
108 |
+
start_time = time.time()
|
109 |
|
110 |
+
while True:
|
111 |
+
current_time = time.time()
|
112 |
+
if current_time - start_time > clear_vram_interval:
|
113 |
+
clear_vram()
|
114 |
+
start_time = current_time
|
115 |
+
time.sleep(60) # Check the time every minute
|
116 |
|
117 |
+
if __name__ == "__main__":
|
118 |
+
main()
|
|
|
|
|
|
|
|
reproduce/Step_1.py
CHANGED
@@ -1,8 +1,10 @@
|
|
1 |
import os
|
2 |
import json
|
3 |
import time
|
|
|
4 |
|
5 |
from lightrag import LightRAG
|
|
|
6 |
|
7 |
|
8 |
def insert_text(rag, file_path):
|
@@ -29,6 +31,19 @@ WORKING_DIR = f"../{cls}"
|
|
29 |
if not os.path.exists(WORKING_DIR):
|
30 |
os.mkdir(WORKING_DIR)
|
31 |
|
32 |
-
|
|
|
33 |
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import json
|
3 |
import time
|
4 |
+
import asyncio
|
5 |
|
6 |
from lightrag import LightRAG
|
7 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
8 |
|
9 |
|
10 |
def insert_text(rag, file_path):
|
|
|
31 |
if not os.path.exists(WORKING_DIR):
|
32 |
os.mkdir(WORKING_DIR)
|
33 |
|
34 |
+
async def initialize_rag():
|
35 |
+
rag = LightRAG(working_dir=WORKING_DIR)
|
36 |
|
37 |
+
await rag.initialize_storages()
|
38 |
+
await initialize_pipeline_status()
|
39 |
+
|
40 |
+
return rag
|
41 |
+
|
42 |
+
def main():
|
43 |
+
# Initialize RAG instance
|
44 |
+
rag = asyncio.run(initialize_rag())
|
45 |
+
insert_text(rag, f"../datasets/unique_contexts/{cls}_unique_contexts.json")
|
46 |
+
|
47 |
+
|
48 |
+
if __name__ == "__main__":
|
49 |
+
main()
|
reproduce/Step_1_openai_compatible.py
CHANGED
@@ -1,11 +1,13 @@
|
|
1 |
import os
|
2 |
import json
|
3 |
import time
|
|
|
4 |
import numpy as np
|
5 |
|
6 |
from lightrag import LightRAG
|
7 |
from lightrag.utils import EmbeddingFunc
|
8 |
from lightrag.llm.openai import openai_complete_if_cache, openai_embed
|
|
|
9 |
|
10 |
|
11 |
## For Upstage API
|
@@ -60,12 +62,25 @@ WORKING_DIR = f"../{cls}"
|
|
60 |
if not os.path.exists(WORKING_DIR):
|
61 |
os.mkdir(WORKING_DIR)
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
|
|
|
|
1 |
import os
|
2 |
import json
|
3 |
import time
|
4 |
+
import asyncio
|
5 |
import numpy as np
|
6 |
|
7 |
from lightrag import LightRAG
|
8 |
from lightrag.utils import EmbeddingFunc
|
9 |
from lightrag.llm.openai import openai_complete_if_cache, openai_embed
|
10 |
+
from lightrag.kg.shared_storage import initialize_pipeline_status
|
11 |
|
12 |
|
13 |
## For Upstage API
|
|
|
62 |
if not os.path.exists(WORKING_DIR):
|
63 |
os.mkdir(WORKING_DIR)
|
64 |
|
65 |
+
async def initialize_rag():
|
66 |
+
rag = LightRAG(
|
67 |
+
working_dir=WORKING_DIR,
|
68 |
+
llm_model_func=llm_model_func,
|
69 |
+
embedding_func=EmbeddingFunc(
|
70 |
+
embedding_dim=4096, max_token_size=8192, func=embedding_func
|
71 |
+
),
|
72 |
+
)
|
73 |
+
|
74 |
+
await rag.initialize_storages()
|
75 |
+
await initialize_pipeline_status()
|
76 |
+
|
77 |
+
return rag
|
78 |
+
|
79 |
+
def main():
|
80 |
+
# Initialize RAG instance
|
81 |
+
rag = asyncio.run(initialize_rag())
|
82 |
+
insert_text(rag, f"../datasets/unique_contexts/{cls}_unique_contexts.json")
|
83 |
+
|
84 |
|
85 |
+
if __name__ == "__main__":
|
86 |
+
main()
|
run_with_gunicorn.py
DELETED
@@ -1,203 +0,0 @@
|
|
1 |
-
#!/usr/bin/env python
|
2 |
-
"""
|
3 |
-
Start LightRAG server with Gunicorn
|
4 |
-
"""
|
5 |
-
|
6 |
-
import os
|
7 |
-
import sys
|
8 |
-
import signal
|
9 |
-
import pipmaster as pm
|
10 |
-
from lightrag.api.utils_api import parse_args, display_splash_screen
|
11 |
-
from lightrag.kg.shared_storage import initialize_share_data, finalize_share_data
|
12 |
-
|
13 |
-
|
14 |
-
def check_and_install_dependencies():
|
15 |
-
"""Check and install required dependencies"""
|
16 |
-
required_packages = [
|
17 |
-
"gunicorn",
|
18 |
-
"tiktoken",
|
19 |
-
"psutil",
|
20 |
-
# Add other required packages here
|
21 |
-
]
|
22 |
-
|
23 |
-
for package in required_packages:
|
24 |
-
if not pm.is_installed(package):
|
25 |
-
print(f"Installing {package}...")
|
26 |
-
pm.install(package)
|
27 |
-
print(f"{package} installed successfully")
|
28 |
-
|
29 |
-
|
30 |
-
# Signal handler for graceful shutdown
|
31 |
-
def signal_handler(sig, frame):
|
32 |
-
print("\n\n" + "=" * 80)
|
33 |
-
print("RECEIVED TERMINATION SIGNAL")
|
34 |
-
print(f"Process ID: {os.getpid()}")
|
35 |
-
print("=" * 80 + "\n")
|
36 |
-
|
37 |
-
# Release shared resources
|
38 |
-
finalize_share_data()
|
39 |
-
|
40 |
-
# Exit with success status
|
41 |
-
sys.exit(0)
|
42 |
-
|
43 |
-
|
44 |
-
def main():
|
45 |
-
# Check and install dependencies
|
46 |
-
check_and_install_dependencies()
|
47 |
-
|
48 |
-
# Register signal handlers for graceful shutdown
|
49 |
-
signal.signal(signal.SIGINT, signal_handler) # Ctrl+C
|
50 |
-
signal.signal(signal.SIGTERM, signal_handler) # kill command
|
51 |
-
|
52 |
-
# Parse all arguments using parse_args
|
53 |
-
args = parse_args(is_uvicorn_mode=False)
|
54 |
-
|
55 |
-
# Display startup information
|
56 |
-
display_splash_screen(args)
|
57 |
-
|
58 |
-
print("🚀 Starting LightRAG with Gunicorn")
|
59 |
-
print(f"🔄 Worker management: Gunicorn (workers={args.workers})")
|
60 |
-
print("🔍 Preloading app: Enabled")
|
61 |
-
print("📝 Note: Using Gunicorn's preload feature for shared data initialization")
|
62 |
-
print("\n\n" + "=" * 80)
|
63 |
-
print("MAIN PROCESS INITIALIZATION")
|
64 |
-
print(f"Process ID: {os.getpid()}")
|
65 |
-
print(f"Workers setting: {args.workers}")
|
66 |
-
print("=" * 80 + "\n")
|
67 |
-
|
68 |
-
# Import Gunicorn's StandaloneApplication
|
69 |
-
from gunicorn.app.base import BaseApplication
|
70 |
-
|
71 |
-
# Define a custom application class that loads our config
|
72 |
-
class GunicornApp(BaseApplication):
|
73 |
-
def __init__(self, app, options=None):
|
74 |
-
self.options = options or {}
|
75 |
-
self.application = app
|
76 |
-
super().__init__()
|
77 |
-
|
78 |
-
def load_config(self):
|
79 |
-
# Define valid Gunicorn configuration options
|
80 |
-
valid_options = {
|
81 |
-
"bind",
|
82 |
-
"workers",
|
83 |
-
"worker_class",
|
84 |
-
"timeout",
|
85 |
-
"keepalive",
|
86 |
-
"preload_app",
|
87 |
-
"errorlog",
|
88 |
-
"accesslog",
|
89 |
-
"loglevel",
|
90 |
-
"certfile",
|
91 |
-
"keyfile",
|
92 |
-
"limit_request_line",
|
93 |
-
"limit_request_fields",
|
94 |
-
"limit_request_field_size",
|
95 |
-
"graceful_timeout",
|
96 |
-
"max_requests",
|
97 |
-
"max_requests_jitter",
|
98 |
-
}
|
99 |
-
|
100 |
-
# Special hooks that need to be set separately
|
101 |
-
special_hooks = {
|
102 |
-
"on_starting",
|
103 |
-
"on_reload",
|
104 |
-
"on_exit",
|
105 |
-
"pre_fork",
|
106 |
-
"post_fork",
|
107 |
-
"pre_exec",
|
108 |
-
"pre_request",
|
109 |
-
"post_request",
|
110 |
-
"worker_init",
|
111 |
-
"worker_exit",
|
112 |
-
"nworkers_changed",
|
113 |
-
"child_exit",
|
114 |
-
}
|
115 |
-
|
116 |
-
# Import and configure the gunicorn_config module
|
117 |
-
import gunicorn_config
|
118 |
-
|
119 |
-
# Set configuration variables in gunicorn_config, prioritizing command line arguments
|
120 |
-
gunicorn_config.workers = (
|
121 |
-
args.workers if args.workers else int(os.getenv("WORKERS", 1))
|
122 |
-
)
|
123 |
-
|
124 |
-
# Bind configuration prioritizes command line arguments
|
125 |
-
host = args.host if args.host != "0.0.0.0" else os.getenv("HOST", "0.0.0.0")
|
126 |
-
port = args.port if args.port != 9621 else int(os.getenv("PORT", 9621))
|
127 |
-
gunicorn_config.bind = f"{host}:{port}"
|
128 |
-
|
129 |
-
# Log level configuration prioritizes command line arguments
|
130 |
-
gunicorn_config.loglevel = (
|
131 |
-
args.log_level.lower()
|
132 |
-
if args.log_level
|
133 |
-
else os.getenv("LOG_LEVEL", "info")
|
134 |
-
)
|
135 |
-
|
136 |
-
# Timeout configuration prioritizes command line arguments
|
137 |
-
gunicorn_config.timeout = (
|
138 |
-
args.timeout if args.timeout else int(os.getenv("TIMEOUT", 150))
|
139 |
-
)
|
140 |
-
|
141 |
-
# Keepalive configuration
|
142 |
-
gunicorn_config.keepalive = int(os.getenv("KEEPALIVE", 5))
|
143 |
-
|
144 |
-
# SSL configuration prioritizes command line arguments
|
145 |
-
if args.ssl or os.getenv("SSL", "").lower() in (
|
146 |
-
"true",
|
147 |
-
"1",
|
148 |
-
"yes",
|
149 |
-
"t",
|
150 |
-
"on",
|
151 |
-
):
|
152 |
-
gunicorn_config.certfile = (
|
153 |
-
args.ssl_certfile
|
154 |
-
if args.ssl_certfile
|
155 |
-
else os.getenv("SSL_CERTFILE")
|
156 |
-
)
|
157 |
-
gunicorn_config.keyfile = (
|
158 |
-
args.ssl_keyfile if args.ssl_keyfile else os.getenv("SSL_KEYFILE")
|
159 |
-
)
|
160 |
-
|
161 |
-
# Set configuration options from the module
|
162 |
-
for key in dir(gunicorn_config):
|
163 |
-
if key in valid_options:
|
164 |
-
value = getattr(gunicorn_config, key)
|
165 |
-
# Skip functions like on_starting and None values
|
166 |
-
if not callable(value) and value is not None:
|
167 |
-
self.cfg.set(key, value)
|
168 |
-
# Set special hooks
|
169 |
-
elif key in special_hooks:
|
170 |
-
value = getattr(gunicorn_config, key)
|
171 |
-
if callable(value):
|
172 |
-
self.cfg.set(key, value)
|
173 |
-
|
174 |
-
if hasattr(gunicorn_config, "logconfig_dict"):
|
175 |
-
self.cfg.set(
|
176 |
-
"logconfig_dict", getattr(gunicorn_config, "logconfig_dict")
|
177 |
-
)
|
178 |
-
|
179 |
-
def load(self):
|
180 |
-
# Import the application
|
181 |
-
from lightrag.api.lightrag_server import get_application
|
182 |
-
|
183 |
-
return get_application(args)
|
184 |
-
|
185 |
-
# Create the application
|
186 |
-
app = GunicornApp("")
|
187 |
-
|
188 |
-
# Force workers to be an integer and greater than 1 for multi-process mode
|
189 |
-
workers_count = int(args.workers)
|
190 |
-
if workers_count > 1:
|
191 |
-
# Set a flag to indicate we're in the main process
|
192 |
-
os.environ["LIGHTRAG_MAIN_PROCESS"] = "1"
|
193 |
-
initialize_share_data(workers_count)
|
194 |
-
else:
|
195 |
-
initialize_share_data(1)
|
196 |
-
|
197 |
-
# Run the application
|
198 |
-
print("\nStarting Gunicorn with direct Python API...")
|
199 |
-
app.run()
|
200 |
-
|
201 |
-
|
202 |
-
if __name__ == "__main__":
|
203 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|