Remove deprecated demo code
Browse files- examples/lightrag_ollama_age_demo.py +0 -113
- examples/lightrag_siliconcloud_demo.py +0 -103
- examples/lightrag_siliconcloud_track_token_demo.py +0 -110
- examples/lightrag_tidb_demo.py +0 -116
- examples/lightrag_tongyi_openai_demo.py +0 -136
- examples/lightrag_zhipu_demo.py +0 -80
- examples/lightrag_zhipu_postgres_demo.py +0 -109
examples/lightrag_ollama_age_demo.py
DELETED
@@ -1,113 +0,0 @@
|
|
1 |
-
import asyncio
|
2 |
-
import nest_asyncio
|
3 |
-
|
4 |
-
import inspect
|
5 |
-
import logging
|
6 |
-
import os
|
7 |
-
|
8 |
-
from lightrag import LightRAG, QueryParam
|
9 |
-
from lightrag.llm.ollama import ollama_embed, ollama_model_complete
|
10 |
-
from lightrag.utils import EmbeddingFunc
|
11 |
-
from lightrag.kg.shared_storage import initialize_pipeline_status
|
12 |
-
|
13 |
-
nest_asyncio.apply()
|
14 |
-
|
15 |
-
WORKING_DIR = "./dickens_age"
|
16 |
-
|
17 |
-
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)
|
18 |
-
|
19 |
-
if not os.path.exists(WORKING_DIR):
|
20 |
-
os.mkdir(WORKING_DIR)
|
21 |
-
|
22 |
-
# AGE
|
23 |
-
os.environ["AGE_POSTGRES_DB"] = "postgresDB"
|
24 |
-
os.environ["AGE_POSTGRES_USER"] = "postgresUser"
|
25 |
-
os.environ["AGE_POSTGRES_PASSWORD"] = "postgresPW"
|
26 |
-
os.environ["AGE_POSTGRES_HOST"] = "localhost"
|
27 |
-
os.environ["AGE_POSTGRES_PORT"] = "5455"
|
28 |
-
os.environ["AGE_GRAPH_NAME"] = "dickens"
|
29 |
-
|
30 |
-
|
31 |
-
async def initialize_rag():
|
32 |
-
rag = LightRAG(
|
33 |
-
working_dir=WORKING_DIR,
|
34 |
-
llm_model_func=ollama_model_complete,
|
35 |
-
llm_model_name="llama3.1:8b",
|
36 |
-
llm_model_max_async=4,
|
37 |
-
llm_model_max_token_size=32768,
|
38 |
-
llm_model_kwargs={
|
39 |
-
"host": "http://localhost:11434",
|
40 |
-
"options": {"num_ctx": 32768},
|
41 |
-
},
|
42 |
-
embedding_func=EmbeddingFunc(
|
43 |
-
embedding_dim=768,
|
44 |
-
max_token_size=8192,
|
45 |
-
func=lambda texts: ollama_embed(
|
46 |
-
texts, embed_model="nomic-embed-text", host="http://localhost:11434"
|
47 |
-
),
|
48 |
-
),
|
49 |
-
graph_storage="AGEStorage",
|
50 |
-
)
|
51 |
-
|
52 |
-
await rag.initialize_storages()
|
53 |
-
await initialize_pipeline_status()
|
54 |
-
|
55 |
-
return rag
|
56 |
-
|
57 |
-
|
58 |
-
async def print_stream(stream):
|
59 |
-
async for chunk in stream:
|
60 |
-
print(chunk, end="", flush=True)
|
61 |
-
|
62 |
-
|
63 |
-
def main():
|
64 |
-
# Initialize RAG instance
|
65 |
-
rag = asyncio.run(initialize_rag())
|
66 |
-
|
67 |
-
# Insert example text
|
68 |
-
with open("./book.txt", "r", encoding="utf-8") as f:
|
69 |
-
rag.insert(f.read())
|
70 |
-
|
71 |
-
# Test different query modes
|
72 |
-
print("\nNaive Search:")
|
73 |
-
print(
|
74 |
-
rag.query(
|
75 |
-
"What are the top themes in this story?", param=QueryParam(mode="naive")
|
76 |
-
)
|
77 |
-
)
|
78 |
-
|
79 |
-
print("\nLocal Search:")
|
80 |
-
print(
|
81 |
-
rag.query(
|
82 |
-
"What are the top themes in this story?", param=QueryParam(mode="local")
|
83 |
-
)
|
84 |
-
)
|
85 |
-
|
86 |
-
print("\nGlobal Search:")
|
87 |
-
print(
|
88 |
-
rag.query(
|
89 |
-
"What are the top themes in this story?", param=QueryParam(mode="global")
|
90 |
-
)
|
91 |
-
)
|
92 |
-
|
93 |
-
print("\nHybrid Search:")
|
94 |
-
print(
|
95 |
-
rag.query(
|
96 |
-
"What are the top themes in this story?", param=QueryParam(mode="hybrid")
|
97 |
-
)
|
98 |
-
)
|
99 |
-
|
100 |
-
# stream response
|
101 |
-
resp = rag.query(
|
102 |
-
"What are the top themes in this story?",
|
103 |
-
param=QueryParam(mode="hybrid", stream=True),
|
104 |
-
)
|
105 |
-
|
106 |
-
if inspect.isasyncgen(resp):
|
107 |
-
asyncio.run(print_stream(resp))
|
108 |
-
else:
|
109 |
-
print(resp)
|
110 |
-
|
111 |
-
|
112 |
-
if __name__ == "__main__":
|
113 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/lightrag_siliconcloud_demo.py
DELETED
@@ -1,103 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import asyncio
|
3 |
-
from lightrag import LightRAG, QueryParam
|
4 |
-
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 |
-
from lightrag.kg.shared_storage import initialize_pipeline_status
|
9 |
-
|
10 |
-
WORKING_DIR = "./dickens"
|
11 |
-
|
12 |
-
if not os.path.exists(WORKING_DIR):
|
13 |
-
os.mkdir(WORKING_DIR)
|
14 |
-
|
15 |
-
|
16 |
-
async def llm_model_func(
|
17 |
-
prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
|
18 |
-
) -> str:
|
19 |
-
return await openai_complete_if_cache(
|
20 |
-
"Qwen/Qwen2.5-7B-Instruct",
|
21 |
-
prompt,
|
22 |
-
system_prompt=system_prompt,
|
23 |
-
history_messages=history_messages,
|
24 |
-
api_key=os.getenv("SILICONFLOW_API_KEY"),
|
25 |
-
base_url="https://api.siliconflow.cn/v1/",
|
26 |
-
**kwargs,
|
27 |
-
)
|
28 |
-
|
29 |
-
|
30 |
-
async def embedding_func(texts: list[str]) -> np.ndarray:
|
31 |
-
return await siliconcloud_embedding(
|
32 |
-
texts,
|
33 |
-
model="netease-youdao/bce-embedding-base_v1",
|
34 |
-
api_key=os.getenv("SILICONFLOW_API_KEY"),
|
35 |
-
max_token_size=512,
|
36 |
-
)
|
37 |
-
|
38 |
-
|
39 |
-
# function test
|
40 |
-
async def test_funcs():
|
41 |
-
result = await llm_model_func("How are you?")
|
42 |
-
print("llm_model_func: ", result)
|
43 |
-
|
44 |
-
result = await embedding_func(["How are you?"])
|
45 |
-
print("embedding_func: ", result)
|
46 |
-
|
47 |
-
|
48 |
-
asyncio.run(test_funcs())
|
49 |
-
|
50 |
-
|
51 |
-
async def initialize_rag():
|
52 |
-
rag = LightRAG(
|
53 |
-
working_dir=WORKING_DIR,
|
54 |
-
llm_model_func=llm_model_func,
|
55 |
-
embedding_func=EmbeddingFunc(
|
56 |
-
embedding_dim=768, max_token_size=512, func=embedding_func
|
57 |
-
),
|
58 |
-
)
|
59 |
-
|
60 |
-
await rag.initialize_storages()
|
61 |
-
await initialize_pipeline_status()
|
62 |
-
|
63 |
-
return rag
|
64 |
-
|
65 |
-
|
66 |
-
def main():
|
67 |
-
# Initialize RAG instance
|
68 |
-
rag = asyncio.run(initialize_rag())
|
69 |
-
|
70 |
-
with open("./book.txt", "r", encoding="utf-8") as f:
|
71 |
-
rag.insert(f.read())
|
72 |
-
|
73 |
-
# Perform naive search
|
74 |
-
print(
|
75 |
-
rag.query(
|
76 |
-
"What are the top themes in this story?", param=QueryParam(mode="naive")
|
77 |
-
)
|
78 |
-
)
|
79 |
-
|
80 |
-
# Perform local search
|
81 |
-
print(
|
82 |
-
rag.query(
|
83 |
-
"What are the top themes in this story?", param=QueryParam(mode="local")
|
84 |
-
)
|
85 |
-
)
|
86 |
-
|
87 |
-
# Perform global search
|
88 |
-
print(
|
89 |
-
rag.query(
|
90 |
-
"What are the top themes in this story?", param=QueryParam(mode="global")
|
91 |
-
)
|
92 |
-
)
|
93 |
-
|
94 |
-
# Perform hybrid search
|
95 |
-
print(
|
96 |
-
rag.query(
|
97 |
-
"What are the top themes in this story?", param=QueryParam(mode="hybrid")
|
98 |
-
)
|
99 |
-
)
|
100 |
-
|
101 |
-
|
102 |
-
if __name__ == "__main__":
|
103 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/lightrag_siliconcloud_track_token_demo.py
DELETED
@@ -1,110 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import asyncio
|
3 |
-
from lightrag import LightRAG, QueryParam
|
4 |
-
from lightrag.llm.openai import openai_complete_if_cache
|
5 |
-
from lightrag.llm.siliconcloud import siliconcloud_embedding
|
6 |
-
from lightrag.utils import EmbeddingFunc
|
7 |
-
from lightrag.utils import TokenTracker
|
8 |
-
import numpy as np
|
9 |
-
from lightrag.kg.shared_storage import initialize_pipeline_status
|
10 |
-
from dotenv import load_dotenv
|
11 |
-
|
12 |
-
load_dotenv()
|
13 |
-
|
14 |
-
token_tracker = TokenTracker()
|
15 |
-
WORKING_DIR = "./dickens"
|
16 |
-
|
17 |
-
if not os.path.exists(WORKING_DIR):
|
18 |
-
os.mkdir(WORKING_DIR)
|
19 |
-
|
20 |
-
|
21 |
-
async def llm_model_func(
|
22 |
-
prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
|
23 |
-
) -> str:
|
24 |
-
return await openai_complete_if_cache(
|
25 |
-
"Qwen/Qwen2.5-7B-Instruct",
|
26 |
-
prompt,
|
27 |
-
system_prompt=system_prompt,
|
28 |
-
history_messages=history_messages,
|
29 |
-
api_key=os.getenv("SILICONFLOW_API_KEY"),
|
30 |
-
base_url="https://api.siliconflow.cn/v1/",
|
31 |
-
token_tracker=token_tracker,
|
32 |
-
**kwargs,
|
33 |
-
)
|
34 |
-
|
35 |
-
|
36 |
-
async def embedding_func(texts: list[str]) -> np.ndarray:
|
37 |
-
return await siliconcloud_embedding(
|
38 |
-
texts,
|
39 |
-
model="BAAI/bge-m3",
|
40 |
-
api_key=os.getenv("SILICONFLOW_API_KEY"),
|
41 |
-
max_token_size=512,
|
42 |
-
)
|
43 |
-
|
44 |
-
|
45 |
-
# function test
|
46 |
-
async def test_funcs():
|
47 |
-
# Context Manager Method
|
48 |
-
with token_tracker:
|
49 |
-
result = await llm_model_func("How are you?")
|
50 |
-
print("llm_model_func: ", result)
|
51 |
-
|
52 |
-
|
53 |
-
asyncio.run(test_funcs())
|
54 |
-
|
55 |
-
|
56 |
-
async def initialize_rag():
|
57 |
-
rag = LightRAG(
|
58 |
-
working_dir=WORKING_DIR,
|
59 |
-
llm_model_func=llm_model_func,
|
60 |
-
embedding_func=EmbeddingFunc(
|
61 |
-
embedding_dim=1024, max_token_size=512, func=embedding_func
|
62 |
-
),
|
63 |
-
)
|
64 |
-
|
65 |
-
await rag.initialize_storages()
|
66 |
-
await initialize_pipeline_status()
|
67 |
-
|
68 |
-
return rag
|
69 |
-
|
70 |
-
|
71 |
-
def main():
|
72 |
-
# Initialize RAG instance
|
73 |
-
rag = asyncio.run(initialize_rag())
|
74 |
-
|
75 |
-
# Reset tracker before processing queries
|
76 |
-
token_tracker.reset()
|
77 |
-
|
78 |
-
with open("./book.txt", "r", encoding="utf-8") as f:
|
79 |
-
rag.insert(f.read())
|
80 |
-
|
81 |
-
print(
|
82 |
-
rag.query(
|
83 |
-
"What are the top themes in this story?", param=QueryParam(mode="naive")
|
84 |
-
)
|
85 |
-
)
|
86 |
-
|
87 |
-
print(
|
88 |
-
rag.query(
|
89 |
-
"What are the top themes in this story?", param=QueryParam(mode="local")
|
90 |
-
)
|
91 |
-
)
|
92 |
-
|
93 |
-
print(
|
94 |
-
rag.query(
|
95 |
-
"What are the top themes in this story?", param=QueryParam(mode="global")
|
96 |
-
)
|
97 |
-
)
|
98 |
-
|
99 |
-
print(
|
100 |
-
rag.query(
|
101 |
-
"What are the top themes in this story?", param=QueryParam(mode="hybrid")
|
102 |
-
)
|
103 |
-
)
|
104 |
-
|
105 |
-
# Display final token usage after main query
|
106 |
-
print("Token usage:", token_tracker.get_usage())
|
107 |
-
|
108 |
-
|
109 |
-
if __name__ == "__main__":
|
110 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/lightrag_tidb_demo.py
DELETED
@@ -1,116 +0,0 @@
|
|
1 |
-
###########################################
|
2 |
-
# TiDB storage implementation is deprecated
|
3 |
-
###########################################
|
4 |
-
|
5 |
-
import asyncio
|
6 |
-
import os
|
7 |
-
|
8 |
-
import numpy as np
|
9 |
-
|
10 |
-
from lightrag import LightRAG, QueryParam
|
11 |
-
from lightrag.llm import siliconcloud_embedding, openai_complete_if_cache
|
12 |
-
from lightrag.utils import EmbeddingFunc
|
13 |
-
from lightrag.kg.shared_storage import initialize_pipeline_status
|
14 |
-
|
15 |
-
WORKING_DIR = "./dickens"
|
16 |
-
|
17 |
-
# We use SiliconCloud API to call LLM on Oracle Cloud
|
18 |
-
# More docs here https://docs.siliconflow.cn/introduction
|
19 |
-
BASE_URL = "https://api.siliconflow.cn/v1/"
|
20 |
-
APIKEY = ""
|
21 |
-
CHATMODEL = ""
|
22 |
-
EMBEDMODEL = ""
|
23 |
-
|
24 |
-
os.environ["TIDB_HOST"] = ""
|
25 |
-
os.environ["TIDB_PORT"] = ""
|
26 |
-
os.environ["TIDB_USER"] = ""
|
27 |
-
os.environ["TIDB_PASSWORD"] = ""
|
28 |
-
os.environ["TIDB_DATABASE"] = "lightrag"
|
29 |
-
|
30 |
-
if not os.path.exists(WORKING_DIR):
|
31 |
-
os.mkdir(WORKING_DIR)
|
32 |
-
|
33 |
-
|
34 |
-
async def llm_model_func(
|
35 |
-
prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
|
36 |
-
) -> str:
|
37 |
-
return await openai_complete_if_cache(
|
38 |
-
CHATMODEL,
|
39 |
-
prompt,
|
40 |
-
system_prompt=system_prompt,
|
41 |
-
history_messages=history_messages,
|
42 |
-
api_key=APIKEY,
|
43 |
-
base_url=BASE_URL,
|
44 |
-
**kwargs,
|
45 |
-
)
|
46 |
-
|
47 |
-
|
48 |
-
async def embedding_func(texts: list[str]) -> np.ndarray:
|
49 |
-
return await siliconcloud_embedding(
|
50 |
-
texts,
|
51 |
-
# model=EMBEDMODEL,
|
52 |
-
api_key=APIKEY,
|
53 |
-
)
|
54 |
-
|
55 |
-
|
56 |
-
async def get_embedding_dim():
|
57 |
-
test_text = ["This is a test sentence."]
|
58 |
-
embedding = await embedding_func(test_text)
|
59 |
-
embedding_dim = embedding.shape[1]
|
60 |
-
return embedding_dim
|
61 |
-
|
62 |
-
|
63 |
-
async def initialize_rag():
|
64 |
-
# Detect embedding dimension
|
65 |
-
embedding_dimension = await get_embedding_dim()
|
66 |
-
print(f"Detected embedding dimension: {embedding_dimension}")
|
67 |
-
|
68 |
-
# Initialize LightRAG
|
69 |
-
# We use TiDB DB as the KV/vector
|
70 |
-
rag = LightRAG(
|
71 |
-
enable_llm_cache=False,
|
72 |
-
working_dir=WORKING_DIR,
|
73 |
-
chunk_token_size=512,
|
74 |
-
llm_model_func=llm_model_func,
|
75 |
-
embedding_func=EmbeddingFunc(
|
76 |
-
embedding_dim=embedding_dimension,
|
77 |
-
max_token_size=512,
|
78 |
-
func=embedding_func,
|
79 |
-
),
|
80 |
-
kv_storage="TiDBKVStorage",
|
81 |
-
vector_storage="TiDBVectorDBStorage",
|
82 |
-
graph_storage="TiDBGraphStorage",
|
83 |
-
)
|
84 |
-
|
85 |
-
await rag.initialize_storages()
|
86 |
-
await initialize_pipeline_status()
|
87 |
-
|
88 |
-
return rag
|
89 |
-
|
90 |
-
|
91 |
-
async def main():
|
92 |
-
try:
|
93 |
-
# Initialize RAG instance
|
94 |
-
rag = await initialize_rag()
|
95 |
-
|
96 |
-
with open("./book.txt", "r", encoding="utf-8") as f:
|
97 |
-
rag.insert(f.read())
|
98 |
-
|
99 |
-
# Perform search in different modes
|
100 |
-
modes = ["naive", "local", "global", "hybrid"]
|
101 |
-
for mode in modes:
|
102 |
-
print("=" * 20, mode, "=" * 20)
|
103 |
-
print(
|
104 |
-
await rag.aquery(
|
105 |
-
"What are the top themes in this story?",
|
106 |
-
param=QueryParam(mode=mode),
|
107 |
-
)
|
108 |
-
)
|
109 |
-
print("-" * 100, "\n")
|
110 |
-
|
111 |
-
except Exception as e:
|
112 |
-
print(f"An error occurred: {e}")
|
113 |
-
|
114 |
-
|
115 |
-
if __name__ == "__main__":
|
116 |
-
asyncio.run(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/lightrag_tongyi_openai_demo.py
DELETED
@@ -1,136 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import asyncio
|
3 |
-
from lightrag import LightRAG, QueryParam
|
4 |
-
from lightrag.utils import EmbeddingFunc
|
5 |
-
import numpy as np
|
6 |
-
from dotenv import load_dotenv
|
7 |
-
import logging
|
8 |
-
from openai import OpenAI
|
9 |
-
from lightrag.kg.shared_storage import initialize_pipeline_status
|
10 |
-
|
11 |
-
logging.basicConfig(level=logging.INFO)
|
12 |
-
|
13 |
-
load_dotenv()
|
14 |
-
|
15 |
-
LLM_MODEL = os.environ.get("LLM_MODEL", "qwen-turbo-latest")
|
16 |
-
LLM_BINDING_HOST = "https://dashscope.aliyuncs.com/compatible-mode/v1"
|
17 |
-
LLM_BINDING_API_KEY = os.getenv("LLM_BINDING_API_KEY")
|
18 |
-
|
19 |
-
EMBEDDING_MODEL = os.environ.get("EMBEDDING_MODEL", "text-embedding-v3")
|
20 |
-
EMBEDDING_BINDING_HOST = os.getenv("EMBEDDING_BINDING_HOST", LLM_BINDING_HOST)
|
21 |
-
EMBEDDING_BINDING_API_KEY = os.getenv("EMBEDDING_BINDING_API_KEY", LLM_BINDING_API_KEY)
|
22 |
-
EMBEDDING_DIM = int(os.environ.get("EMBEDDING_DIM", 1024))
|
23 |
-
EMBEDDING_MAX_TOKEN_SIZE = int(os.environ.get("EMBEDDING_MAX_TOKEN_SIZE", 8192))
|
24 |
-
EMBEDDING_MAX_BATCH_SIZE = int(os.environ.get("EMBEDDING_MAX_BATCH_SIZE", 10))
|
25 |
-
|
26 |
-
print(f"LLM_MODEL: {LLM_MODEL}")
|
27 |
-
print(f"EMBEDDING_MODEL: {EMBEDDING_MODEL}")
|
28 |
-
|
29 |
-
WORKING_DIR = "./dickens"
|
30 |
-
|
31 |
-
if os.path.exists(WORKING_DIR):
|
32 |
-
import shutil
|
33 |
-
|
34 |
-
shutil.rmtree(WORKING_DIR)
|
35 |
-
|
36 |
-
os.mkdir(WORKING_DIR)
|
37 |
-
|
38 |
-
|
39 |
-
async def llm_model_func(
|
40 |
-
prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
|
41 |
-
) -> str:
|
42 |
-
client = OpenAI(
|
43 |
-
api_key=LLM_BINDING_API_KEY,
|
44 |
-
base_url=LLM_BINDING_HOST,
|
45 |
-
)
|
46 |
-
|
47 |
-
messages = []
|
48 |
-
if system_prompt:
|
49 |
-
messages.append({"role": "system", "content": system_prompt})
|
50 |
-
if history_messages:
|
51 |
-
messages.extend(history_messages)
|
52 |
-
messages.append({"role": "user", "content": prompt})
|
53 |
-
|
54 |
-
chat_completion = client.chat.completions.create(
|
55 |
-
model=LLM_MODEL,
|
56 |
-
messages=messages,
|
57 |
-
temperature=kwargs.get("temperature", 0),
|
58 |
-
top_p=kwargs.get("top_p", 1),
|
59 |
-
n=kwargs.get("n", 1),
|
60 |
-
extra_body={"enable_thinking": False},
|
61 |
-
)
|
62 |
-
return chat_completion.choices[0].message.content
|
63 |
-
|
64 |
-
|
65 |
-
async def embedding_func(texts: list[str]) -> np.ndarray:
|
66 |
-
client = OpenAI(
|
67 |
-
api_key=EMBEDDING_BINDING_API_KEY,
|
68 |
-
base_url=EMBEDDING_BINDING_HOST,
|
69 |
-
)
|
70 |
-
|
71 |
-
print("##### embedding: texts: %d #####" % len(texts))
|
72 |
-
max_batch_size = EMBEDDING_MAX_BATCH_SIZE
|
73 |
-
embeddings = []
|
74 |
-
for i in range(0, len(texts), max_batch_size):
|
75 |
-
batch = texts[i : i + max_batch_size]
|
76 |
-
embedding = client.embeddings.create(model=EMBEDDING_MODEL, input=batch)
|
77 |
-
embeddings += [item.embedding for item in embedding.data]
|
78 |
-
|
79 |
-
return np.array(embeddings)
|
80 |
-
|
81 |
-
|
82 |
-
async def test_funcs():
|
83 |
-
result = await llm_model_func("How are you?")
|
84 |
-
print("Resposta do llm_model_func: ", result)
|
85 |
-
|
86 |
-
result = await embedding_func(["How are you?"])
|
87 |
-
print("Resultado do embedding_func: ", result.shape)
|
88 |
-
print("Dimensão da embedding: ", result.shape[1])
|
89 |
-
|
90 |
-
|
91 |
-
asyncio.run(test_funcs())
|
92 |
-
|
93 |
-
|
94 |
-
async def initialize_rag():
|
95 |
-
rag = LightRAG(
|
96 |
-
working_dir=WORKING_DIR,
|
97 |
-
llm_model_func=llm_model_func,
|
98 |
-
embedding_func=EmbeddingFunc(
|
99 |
-
embedding_dim=EMBEDDING_DIM,
|
100 |
-
max_token_size=EMBEDDING_MAX_TOKEN_SIZE,
|
101 |
-
func=embedding_func,
|
102 |
-
),
|
103 |
-
)
|
104 |
-
|
105 |
-
await rag.initialize_storages()
|
106 |
-
await initialize_pipeline_status()
|
107 |
-
|
108 |
-
return rag
|
109 |
-
|
110 |
-
|
111 |
-
def main():
|
112 |
-
rag = asyncio.run(initialize_rag())
|
113 |
-
|
114 |
-
with open("./book.txt", "r", encoding="utf-8") as f:
|
115 |
-
rag.insert(f.read())
|
116 |
-
|
117 |
-
query_text = "What are the main themes?"
|
118 |
-
|
119 |
-
print("Result (Naive):")
|
120 |
-
print(rag.query(query_text, param=QueryParam(mode="naive")))
|
121 |
-
|
122 |
-
print("\nResult (Local):")
|
123 |
-
print(rag.query(query_text, param=QueryParam(mode="local")))
|
124 |
-
|
125 |
-
print("\nResult (Global):")
|
126 |
-
print(rag.query(query_text, param=QueryParam(mode="global")))
|
127 |
-
|
128 |
-
print("\nResult (Hybrid):")
|
129 |
-
print(rag.query(query_text, param=QueryParam(mode="hybrid")))
|
130 |
-
|
131 |
-
print("\nResult (mix):")
|
132 |
-
print(rag.query(query_text, param=QueryParam(mode="mix")))
|
133 |
-
|
134 |
-
|
135 |
-
if __name__ == "__main__":
|
136 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/lightrag_zhipu_demo.py
DELETED
@@ -1,80 +0,0 @@
|
|
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 |
-
|
13 |
-
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)
|
14 |
-
|
15 |
-
if not os.path.exists(WORKING_DIR):
|
16 |
-
os.mkdir(WORKING_DIR)
|
17 |
-
|
18 |
-
api_key = os.environ.get("ZHIPUAI_API_KEY")
|
19 |
-
if api_key is None:
|
20 |
-
raise Exception("Please set ZHIPU_API_KEY in your environment")
|
21 |
-
|
22 |
-
|
23 |
-
async def initialize_rag():
|
24 |
-
rag = LightRAG(
|
25 |
-
working_dir=WORKING_DIR,
|
26 |
-
llm_model_func=zhipu_complete,
|
27 |
-
llm_model_name="glm-4-flashx", # Using the most cost/performance balance model, but you can change it here.
|
28 |
-
llm_model_max_async=4,
|
29 |
-
llm_model_max_token_size=32768,
|
30 |
-
embedding_func=EmbeddingFunc(
|
31 |
-
embedding_dim=2048, # Zhipu embedding-3 dimension
|
32 |
-
max_token_size=8192,
|
33 |
-
func=lambda texts: zhipu_embedding(texts),
|
34 |
-
),
|
35 |
-
)
|
36 |
-
|
37 |
-
await rag.initialize_storages()
|
38 |
-
await initialize_pipeline_status()
|
39 |
-
|
40 |
-
return rag
|
41 |
-
|
42 |
-
|
43 |
-
def main():
|
44 |
-
# Initialize RAG instance
|
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(
|
53 |
-
"What are the top themes in this story?", param=QueryParam(mode="naive")
|
54 |
-
)
|
55 |
-
)
|
56 |
-
|
57 |
-
# Perform local search
|
58 |
-
print(
|
59 |
-
rag.query(
|
60 |
-
"What are the top themes in this story?", param=QueryParam(mode="local")
|
61 |
-
)
|
62 |
-
)
|
63 |
-
|
64 |
-
# Perform global search
|
65 |
-
print(
|
66 |
-
rag.query(
|
67 |
-
"What are the top themes in this story?", param=QueryParam(mode="global")
|
68 |
-
)
|
69 |
-
)
|
70 |
-
|
71 |
-
# Perform hybrid search
|
72 |
-
print(
|
73 |
-
rag.query(
|
74 |
-
"What are the top themes in this story?", param=QueryParam(mode="hybrid")
|
75 |
-
)
|
76 |
-
)
|
77 |
-
|
78 |
-
|
79 |
-
if __name__ == "__main__":
|
80 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/lightrag_zhipu_postgres_demo.py
DELETED
@@ -1,109 +0,0 @@
|
|
1 |
-
import asyncio
|
2 |
-
import logging
|
3 |
-
import os
|
4 |
-
import time
|
5 |
-
from dotenv import load_dotenv
|
6 |
-
|
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 |
-
from lightrag.kg.shared_storage import initialize_pipeline_status
|
12 |
-
|
13 |
-
load_dotenv()
|
14 |
-
ROOT_DIR = os.environ.get("ROOT_DIR")
|
15 |
-
WORKING_DIR = f"{ROOT_DIR}/dickens-pg"
|
16 |
-
|
17 |
-
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)
|
18 |
-
|
19 |
-
if not os.path.exists(WORKING_DIR):
|
20 |
-
os.mkdir(WORKING_DIR)
|
21 |
-
|
22 |
-
# AGE
|
23 |
-
os.environ["AGE_GRAPH_NAME"] = "dickens"
|
24 |
-
|
25 |
-
os.environ["POSTGRES_HOST"] = "localhost"
|
26 |
-
os.environ["POSTGRES_PORT"] = "15432"
|
27 |
-
os.environ["POSTGRES_USER"] = "rag"
|
28 |
-
os.environ["POSTGRES_PASSWORD"] = "rag"
|
29 |
-
os.environ["POSTGRES_DATABASE"] = "rag"
|
30 |
-
|
31 |
-
|
32 |
-
async def initialize_rag():
|
33 |
-
rag = LightRAG(
|
34 |
-
working_dir=WORKING_DIR,
|
35 |
-
llm_model_func=zhipu_complete,
|
36 |
-
llm_model_name="glm-4-flashx",
|
37 |
-
llm_model_max_async=4,
|
38 |
-
llm_model_max_token_size=32768,
|
39 |
-
enable_llm_cache_for_entity_extract=True,
|
40 |
-
embedding_func=EmbeddingFunc(
|
41 |
-
embedding_dim=1024,
|
42 |
-
max_token_size=8192,
|
43 |
-
func=lambda texts: ollama_embedding(
|
44 |
-
texts, embed_model="bge-m3", host="http://localhost:11434"
|
45 |
-
),
|
46 |
-
),
|
47 |
-
kv_storage="PGKVStorage",
|
48 |
-
doc_status_storage="PGDocStatusStorage",
|
49 |
-
graph_storage="PGGraphStorage",
|
50 |
-
vector_storage="PGVectorStorage",
|
51 |
-
auto_manage_storages_states=False,
|
52 |
-
)
|
53 |
-
|
54 |
-
await rag.initialize_storages()
|
55 |
-
await initialize_pipeline_status()
|
56 |
-
|
57 |
-
return rag
|
58 |
-
|
59 |
-
|
60 |
-
async def main():
|
61 |
-
# Initialize RAG instance
|
62 |
-
rag = await initialize_rag()
|
63 |
-
|
64 |
-
# add embedding_func for graph database, it's deleted in commit 5661d76860436f7bf5aef2e50d9ee4a59660146c
|
65 |
-
rag.chunk_entity_relation_graph.embedding_func = rag.embedding_func
|
66 |
-
|
67 |
-
with open(f"{ROOT_DIR}/book.txt", "r", encoding="utf-8") as f:
|
68 |
-
await rag.ainsert(f.read())
|
69 |
-
|
70 |
-
print("==== Trying to test the rag queries ====")
|
71 |
-
print("**** Start Naive Query ****")
|
72 |
-
start_time = time.time()
|
73 |
-
# Perform naive search
|
74 |
-
print(
|
75 |
-
await rag.aquery(
|
76 |
-
"What are the top themes in this story?", param=QueryParam(mode="naive")
|
77 |
-
)
|
78 |
-
)
|
79 |
-
print(f"Naive Query Time: {time.time() - start_time} seconds")
|
80 |
-
# Perform local search
|
81 |
-
print("**** Start Local Query ****")
|
82 |
-
start_time = time.time()
|
83 |
-
print(
|
84 |
-
await rag.aquery(
|
85 |
-
"What are the top themes in this story?", param=QueryParam(mode="local")
|
86 |
-
)
|
87 |
-
)
|
88 |
-
print(f"Local Query Time: {time.time() - start_time} seconds")
|
89 |
-
# Perform global search
|
90 |
-
print("**** Start Global Query ****")
|
91 |
-
start_time = time.time()
|
92 |
-
print(
|
93 |
-
await rag.aquery(
|
94 |
-
"What are the top themes in this story?", param=QueryParam(mode="global")
|
95 |
-
)
|
96 |
-
)
|
97 |
-
print(f"Global Query Time: {time.time() - start_time}")
|
98 |
-
# Perform hybrid search
|
99 |
-
print("**** Start Hybrid Query ****")
|
100 |
-
print(
|
101 |
-
await rag.aquery(
|
102 |
-
"What are the top themes in this story?", param=QueryParam(mode="hybrid")
|
103 |
-
)
|
104 |
-
)
|
105 |
-
print(f"Hybrid Query Time: {time.time() - start_time} seconds")
|
106 |
-
|
107 |
-
|
108 |
-
if __name__ == "__main__":
|
109 |
-
asyncio.run(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|