Remove buggy examplesfiles
Browse files- examples/.env.oai.example +0 -7
- examples/batch_eval.py +0 -108
- examples/generate_query.py +0 -55
- examples/get_all_edges_nx.py +0 -40
- examples/openai_README.md +0 -114
- examples/openai_README_zh.md +0 -115
- examples/test.py +0 -68
- examples/test_chromadb.py +0 -158
- examples/test_faiss.py +0 -108
- examples/test_neo4j.py +0 -71
- examples/test_postgres.py +0 -51
- examples/vram_management_demo.py +0 -121
examples/.env.oai.example
DELETED
@@ -1,7 +0,0 @@
|
|
1 |
-
AZURE_OPENAI_API_VERSION=2024-08-01-preview
|
2 |
-
AZURE_OPENAI_DEPLOYMENT=gpt-4o
|
3 |
-
AZURE_OPENAI_API_KEY=myapikey
|
4 |
-
AZURE_OPENAI_ENDPOINT=https://myendpoint.openai.azure.com
|
5 |
-
|
6 |
-
AZURE_EMBEDDING_DEPLOYMENT=text-embedding-3-large
|
7 |
-
AZURE_EMBEDDING_API_VERSION=2023-05-15
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/batch_eval.py
DELETED
@@ -1,108 +0,0 @@
|
|
1 |
-
import re
|
2 |
-
import json
|
3 |
-
import jsonlines
|
4 |
-
|
5 |
-
from openai import OpenAI
|
6 |
-
|
7 |
-
|
8 |
-
def batch_eval(query_file, result1_file, result2_file, output_file_path):
|
9 |
-
client = OpenAI()
|
10 |
-
|
11 |
-
with open(query_file, "r") as f:
|
12 |
-
data = f.read()
|
13 |
-
|
14 |
-
queries = re.findall(r"- Question \d+: (.+)", data)
|
15 |
-
|
16 |
-
with open(result1_file, "r") as f:
|
17 |
-
answers1 = json.load(f)
|
18 |
-
answers1 = [i["result"] for i in answers1]
|
19 |
-
|
20 |
-
with open(result2_file, "r") as f:
|
21 |
-
answers2 = json.load(f)
|
22 |
-
answers2 = [i["result"] for i in answers2]
|
23 |
-
|
24 |
-
requests = []
|
25 |
-
for i, (query, answer1, answer2) in enumerate(zip(queries, answers1, answers2)):
|
26 |
-
sys_prompt = """
|
27 |
-
---Role---
|
28 |
-
You are an expert tasked with evaluating two answers to the same question based on three criteria: **Comprehensiveness**, **Diversity**, and **Empowerment**.
|
29 |
-
"""
|
30 |
-
|
31 |
-
prompt = f"""
|
32 |
-
You will evaluate two answers to the same question based on three criteria: **Comprehensiveness**, **Diversity**, and **Empowerment**.
|
33 |
-
|
34 |
-
- **Comprehensiveness**: How much detail does the answer provide to cover all aspects and details of the question?
|
35 |
-
- **Diversity**: How varied and rich is the answer in providing different perspectives and insights on the question?
|
36 |
-
- **Empowerment**: How well does the answer help the reader understand and make informed judgments about the topic?
|
37 |
-
|
38 |
-
For each criterion, choose the better answer (either Answer 1 or Answer 2) and explain why. Then, select an overall winner based on these three categories.
|
39 |
-
|
40 |
-
Here is the question:
|
41 |
-
{query}
|
42 |
-
|
43 |
-
Here are the two answers:
|
44 |
-
|
45 |
-
**Answer 1:**
|
46 |
-
{answer1}
|
47 |
-
|
48 |
-
**Answer 2:**
|
49 |
-
{answer2}
|
50 |
-
|
51 |
-
Evaluate both answers using the three criteria listed above and provide detailed explanations for each criterion.
|
52 |
-
|
53 |
-
Output your evaluation in the following JSON format:
|
54 |
-
|
55 |
-
{{
|
56 |
-
"Comprehensiveness": {{
|
57 |
-
"Winner": "[Answer 1 or Answer 2]",
|
58 |
-
"Explanation": "[Provide explanation here]"
|
59 |
-
}},
|
60 |
-
"Empowerment": {{
|
61 |
-
"Winner": "[Answer 1 or Answer 2]",
|
62 |
-
"Explanation": "[Provide explanation here]"
|
63 |
-
}},
|
64 |
-
"Overall Winner": {{
|
65 |
-
"Winner": "[Answer 1 or Answer 2]",
|
66 |
-
"Explanation": "[Summarize why this answer is the overall winner based on the three criteria]"
|
67 |
-
}}
|
68 |
-
}}
|
69 |
-
"""
|
70 |
-
|
71 |
-
request_data = {
|
72 |
-
"custom_id": f"request-{i+1}",
|
73 |
-
"method": "POST",
|
74 |
-
"url": "/v1/chat/completions",
|
75 |
-
"body": {
|
76 |
-
"model": "gpt-4o-mini",
|
77 |
-
"messages": [
|
78 |
-
{"role": "system", "content": sys_prompt},
|
79 |
-
{"role": "user", "content": prompt},
|
80 |
-
],
|
81 |
-
},
|
82 |
-
}
|
83 |
-
|
84 |
-
requests.append(request_data)
|
85 |
-
|
86 |
-
with jsonlines.open(output_file_path, mode="w") as writer:
|
87 |
-
for request in requests:
|
88 |
-
writer.write(request)
|
89 |
-
|
90 |
-
print(f"Batch API requests written to {output_file_path}")
|
91 |
-
|
92 |
-
batch_input_file = client.files.create(
|
93 |
-
file=open(output_file_path, "rb"), purpose="batch"
|
94 |
-
)
|
95 |
-
batch_input_file_id = batch_input_file.id
|
96 |
-
|
97 |
-
batch = client.batches.create(
|
98 |
-
input_file_id=batch_input_file_id,
|
99 |
-
endpoint="/v1/chat/completions",
|
100 |
-
completion_window="24h",
|
101 |
-
metadata={"description": "nightly eval job"},
|
102 |
-
)
|
103 |
-
|
104 |
-
print(f"Batch {batch.id} has been created.")
|
105 |
-
|
106 |
-
|
107 |
-
if __name__ == "__main__":
|
108 |
-
batch_eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/generate_query.py
DELETED
@@ -1,55 +0,0 @@
|
|
1 |
-
from openai import OpenAI
|
2 |
-
|
3 |
-
# os.environ["OPENAI_API_KEY"] = ""
|
4 |
-
|
5 |
-
|
6 |
-
def openai_complete_if_cache(
|
7 |
-
model="gpt-4o-mini", prompt=None, system_prompt=None, history_messages=[], **kwargs
|
8 |
-
) -> str:
|
9 |
-
openai_client = OpenAI()
|
10 |
-
|
11 |
-
messages = []
|
12 |
-
if system_prompt:
|
13 |
-
messages.append({"role": "system", "content": system_prompt})
|
14 |
-
messages.extend(history_messages)
|
15 |
-
messages.append({"role": "user", "content": prompt})
|
16 |
-
|
17 |
-
response = openai_client.chat.completions.create(
|
18 |
-
model=model, messages=messages, **kwargs
|
19 |
-
)
|
20 |
-
return response.choices[0].message.content
|
21 |
-
|
22 |
-
|
23 |
-
if __name__ == "__main__":
|
24 |
-
description = ""
|
25 |
-
prompt = f"""
|
26 |
-
Given the following description of a dataset:
|
27 |
-
|
28 |
-
{description}
|
29 |
-
|
30 |
-
Please identify 5 potential users who would engage with this dataset. For each user, list 5 tasks they would perform with this dataset. Then, for each (user, task) combination, generate 5 questions that require a high-level understanding of the entire dataset.
|
31 |
-
|
32 |
-
Output the results in the following structure:
|
33 |
-
- User 1: [user description]
|
34 |
-
- Task 1: [task description]
|
35 |
-
- Question 1:
|
36 |
-
- Question 2:
|
37 |
-
- Question 3:
|
38 |
-
- Question 4:
|
39 |
-
- Question 5:
|
40 |
-
- Task 2: [task description]
|
41 |
-
...
|
42 |
-
- Task 5: [task description]
|
43 |
-
- User 2: [user description]
|
44 |
-
...
|
45 |
-
- User 5: [user description]
|
46 |
-
...
|
47 |
-
"""
|
48 |
-
|
49 |
-
result = openai_complete_if_cache(model="gpt-4o-mini", prompt=prompt)
|
50 |
-
|
51 |
-
file_path = "./queries.txt"
|
52 |
-
with open(file_path, "w") as file:
|
53 |
-
file.write(result)
|
54 |
-
|
55 |
-
print(f"Queries written to {file_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/get_all_edges_nx.py
DELETED
@@ -1,40 +0,0 @@
|
|
1 |
-
import networkx as nx
|
2 |
-
|
3 |
-
G = nx.read_graphml("./dickensTestEmbedcall/graph_chunk_entity_relation.graphml")
|
4 |
-
|
5 |
-
|
6 |
-
def get_all_edges_and_nodes(G):
|
7 |
-
# Get all edges and their properties
|
8 |
-
edges_with_properties = []
|
9 |
-
for u, v, data in G.edges(data=True):
|
10 |
-
edges_with_properties.append(
|
11 |
-
{
|
12 |
-
"start": u,
|
13 |
-
"end": v,
|
14 |
-
"label": data.get(
|
15 |
-
"label", ""
|
16 |
-
), # Assuming 'label' is used for edge type
|
17 |
-
"properties": data,
|
18 |
-
"start_node_properties": G.nodes[u],
|
19 |
-
"end_node_properties": G.nodes[v],
|
20 |
-
}
|
21 |
-
)
|
22 |
-
|
23 |
-
return edges_with_properties
|
24 |
-
|
25 |
-
|
26 |
-
# Example usage
|
27 |
-
if __name__ == "__main__":
|
28 |
-
# Assume G is your NetworkX graph loaded from Neo4j
|
29 |
-
|
30 |
-
all_edges = get_all_edges_and_nodes(G)
|
31 |
-
|
32 |
-
# Print all edges and node properties
|
33 |
-
for edge in all_edges:
|
34 |
-
print(f"Edge Label: {edge['label']}")
|
35 |
-
print(f"Edge Properties: {edge['properties']}")
|
36 |
-
print(f"Start Node: {edge['start']}")
|
37 |
-
print(f"Start Node Properties: {edge['start_node_properties']}")
|
38 |
-
print(f"End Node: {edge['end']}")
|
39 |
-
print(f"End Node Properties: {edge['end_node_properties']}")
|
40 |
-
print("---")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/openai_README.md
DELETED
@@ -1,114 +0,0 @@
|
|
1 |
-
|
2 |
-
## API Server Implementation
|
3 |
-
|
4 |
-
LightRAG also provides a FastAPI-based server implementation for RESTful API access to RAG operations. This allows you to run LightRAG as a service and interact with it through HTTP requests.
|
5 |
-
|
6 |
-
### Setting up the API Server
|
7 |
-
<details>
|
8 |
-
<summary>Click to expand setup instructions</summary>
|
9 |
-
|
10 |
-
1. First, ensure you have the required dependencies:
|
11 |
-
```bash
|
12 |
-
pip install fastapi uvicorn pydantic
|
13 |
-
```
|
14 |
-
|
15 |
-
2. Set up your environment variables:
|
16 |
-
```bash
|
17 |
-
export RAG_DIR="your_index_directory" # Optional: Defaults to "index_default"
|
18 |
-
export OPENAI_BASE_URL="Your OpenAI API base URL" # Optional: Defaults to "https://api.openai.com/v1"
|
19 |
-
export OPENAI_API_KEY="Your OpenAI API key" # Required
|
20 |
-
export LLM_MODEL="Your LLM model" # Optional: Defaults to "gpt-4o-mini"
|
21 |
-
export EMBEDDING_MODEL="Your embedding model" # Optional: Defaults to "text-embedding-3-large"
|
22 |
-
```
|
23 |
-
|
24 |
-
3. Run the API server:
|
25 |
-
```bash
|
26 |
-
python examples/lightrag_api_openai_compatible_demo.py
|
27 |
-
```
|
28 |
-
|
29 |
-
The server will start on `http://0.0.0.0:8020`.
|
30 |
-
</details>
|
31 |
-
|
32 |
-
### API Endpoints
|
33 |
-
|
34 |
-
The API server provides the following endpoints:
|
35 |
-
|
36 |
-
#### 1. Query Endpoint
|
37 |
-
<details>
|
38 |
-
<summary>Click to view Query endpoint details</summary>
|
39 |
-
|
40 |
-
- **URL:** `/query`
|
41 |
-
- **Method:** POST
|
42 |
-
- **Body:**
|
43 |
-
```json
|
44 |
-
{
|
45 |
-
"query": "Your question here",
|
46 |
-
"mode": "hybrid", // Can be "naive", "local", "global", or "hybrid"
|
47 |
-
"only_need_context": true // Optional: Defaults to false, if true, only the referenced context will be returned, otherwise the llm answer will be returned
|
48 |
-
}
|
49 |
-
```
|
50 |
-
- **Example:**
|
51 |
-
```bash
|
52 |
-
curl -X POST "http://127.0.0.1:8020/query" \
|
53 |
-
-H "Content-Type: application/json" \
|
54 |
-
-d '{"query": "What are the main themes?", "mode": "hybrid"}'
|
55 |
-
```
|
56 |
-
</details>
|
57 |
-
|
58 |
-
#### 2. Insert Text Endpoint
|
59 |
-
<details>
|
60 |
-
<summary>Click to view Insert Text endpoint details</summary>
|
61 |
-
|
62 |
-
- **URL:** `/insert`
|
63 |
-
- **Method:** POST
|
64 |
-
- **Body:**
|
65 |
-
```json
|
66 |
-
{
|
67 |
-
"text": "Your text content here"
|
68 |
-
}
|
69 |
-
```
|
70 |
-
- **Example:**
|
71 |
-
```bash
|
72 |
-
curl -X POST "http://127.0.0.1:8020/insert" \
|
73 |
-
-H "Content-Type: application/json" \
|
74 |
-
-d '{"text": "Content to be inserted into RAG"}'
|
75 |
-
```
|
76 |
-
</details>
|
77 |
-
|
78 |
-
#### 3. Insert File Endpoint
|
79 |
-
<details>
|
80 |
-
<summary>Click to view Insert File endpoint details</summary>
|
81 |
-
|
82 |
-
- **URL:** `/insert_file`
|
83 |
-
- **Method:** POST
|
84 |
-
- **Body:**
|
85 |
-
```json
|
86 |
-
{
|
87 |
-
"file_path": "path/to/your/file.txt"
|
88 |
-
}
|
89 |
-
```
|
90 |
-
- **Example:**
|
91 |
-
```bash
|
92 |
-
curl -X POST "http://127.0.0.1:8020/insert_file" \
|
93 |
-
-H "Content-Type: application/json" \
|
94 |
-
-d '{"file_path": "./book.txt"}'
|
95 |
-
```
|
96 |
-
</details>
|
97 |
-
|
98 |
-
#### 4. Health Check Endpoint
|
99 |
-
<details>
|
100 |
-
<summary>Click to view Health Check endpoint details</summary>
|
101 |
-
|
102 |
-
- **URL:** `/health`
|
103 |
-
- **Method:** GET
|
104 |
-
- **Example:**
|
105 |
-
```bash
|
106 |
-
curl -X GET "http://127.0.0.1:8020/health"
|
107 |
-
```
|
108 |
-
</details>
|
109 |
-
|
110 |
-
### Configuration
|
111 |
-
|
112 |
-
The API server can be configured using environment variables:
|
113 |
-
- `RAG_DIR`: Directory for storing the RAG index (default: "index_default")
|
114 |
-
- API keys and base URLs should be configured in the code for your specific LLM and embedding model providers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/openai_README_zh.md
DELETED
@@ -1,115 +0,0 @@
|
|
1 |
-
|
2 |
-
## API 服务器实现
|
3 |
-
|
4 |
-
LightRAG also provides a FastAPI-based server implementation for RESTful API access to RAG operations. This allows you to run LightRAG as a service and interact with it through HTTP requests.
|
5 |
-
LightRAG 还提供基于 FastAPI 的服务器实现,用于对 RAG 操作进行 RESTful API 访问。这允许您将 LightRAG 作为服务运行并通过 HTTP 请求与其交互。
|
6 |
-
|
7 |
-
### 设置 API 服务器
|
8 |
-
<details>
|
9 |
-
<summary>单击展开设置说明</summary>
|
10 |
-
|
11 |
-
1. 首先,确保您具有所需的依赖项:
|
12 |
-
```bash
|
13 |
-
pip install fastapi uvicorn pydantic
|
14 |
-
```
|
15 |
-
|
16 |
-
2. 设置您的环境变量:
|
17 |
-
```bash
|
18 |
-
export RAG_DIR="your_index_directory" # Optional: Defaults to "index_default"
|
19 |
-
export OPENAI_BASE_URL="Your OpenAI API base URL" # Optional: Defaults to "https://api.openai.com/v1"
|
20 |
-
export OPENAI_API_KEY="Your OpenAI API key" # Required
|
21 |
-
export LLM_MODEL="Your LLM model" # Optional: Defaults to "gpt-4o-mini"
|
22 |
-
export EMBEDDING_MODEL="Your embedding model" # Optional: Defaults to "text-embedding-3-large"
|
23 |
-
```
|
24 |
-
|
25 |
-
3. 运行API服务器:
|
26 |
-
```bash
|
27 |
-
python examples/lightrag_api_openai_compatible_demo.py
|
28 |
-
```
|
29 |
-
|
30 |
-
服务器将启动于 `http://0.0.0.0:8020`.
|
31 |
-
</details>
|
32 |
-
|
33 |
-
### API端点
|
34 |
-
|
35 |
-
API服务器提供以下端点:
|
36 |
-
|
37 |
-
#### 1. 查询端点
|
38 |
-
<details>
|
39 |
-
<summary>点击查看查询端点详情</summary>
|
40 |
-
|
41 |
-
- **URL:** `/query`
|
42 |
-
- **Method:** POST
|
43 |
-
- **Body:**
|
44 |
-
```json
|
45 |
-
{
|
46 |
-
"query": "Your question here",
|
47 |
-
"mode": "hybrid", // Can be "naive", "local", "global", or "hybrid"
|
48 |
-
"only_need_context": true // Optional: Defaults to false, if true, only the referenced context will be returned, otherwise the llm answer will be returned
|
49 |
-
}
|
50 |
-
```
|
51 |
-
- **Example:**
|
52 |
-
```bash
|
53 |
-
curl -X POST "http://127.0.0.1:8020/query" \
|
54 |
-
-H "Content-Type: application/json" \
|
55 |
-
-d '{"query": "What are the main themes?", "mode": "hybrid"}'
|
56 |
-
```
|
57 |
-
</details>
|
58 |
-
|
59 |
-
#### 2. 插入文本端点
|
60 |
-
<details>
|
61 |
-
<summary>单击可查看插入文本端点详细信息</summary>
|
62 |
-
|
63 |
-
- **URL:** `/insert`
|
64 |
-
- **Method:** POST
|
65 |
-
- **Body:**
|
66 |
-
```json
|
67 |
-
{
|
68 |
-
"text": "Your text content here"
|
69 |
-
}
|
70 |
-
```
|
71 |
-
- **Example:**
|
72 |
-
```bash
|
73 |
-
curl -X POST "http://127.0.0.1:8020/insert" \
|
74 |
-
-H "Content-Type: application/json" \
|
75 |
-
-d '{"text": "Content to be inserted into RAG"}'
|
76 |
-
```
|
77 |
-
</details>
|
78 |
-
|
79 |
-
#### 3. 插入文件端点
|
80 |
-
<details>
|
81 |
-
<summary>单击查看插入文件端点详细信息</summary>
|
82 |
-
|
83 |
-
- **URL:** `/insert_file`
|
84 |
-
- **Method:** POST
|
85 |
-
- **Body:**
|
86 |
-
```json
|
87 |
-
{
|
88 |
-
"file_path": "path/to/your/file.txt"
|
89 |
-
}
|
90 |
-
```
|
91 |
-
- **Example:**
|
92 |
-
```bash
|
93 |
-
curl -X POST "http://127.0.0.1:8020/insert_file" \
|
94 |
-
-H "Content-Type: application/json" \
|
95 |
-
-d '{"file_path": "./book.txt"}'
|
96 |
-
```
|
97 |
-
</details>
|
98 |
-
|
99 |
-
#### 4. 健康检查端点
|
100 |
-
<details>
|
101 |
-
<summary>点击查看健康检查端点详细信息</summary>
|
102 |
-
|
103 |
-
- **URL:** `/health`
|
104 |
-
- **Method:** GET
|
105 |
-
- **Example:**
|
106 |
-
```bash
|
107 |
-
curl -X GET "http://127.0.0.1:8020/health"
|
108 |
-
```
|
109 |
-
</details>
|
110 |
-
|
111 |
-
### 配置
|
112 |
-
|
113 |
-
可以使用环境变量配置API服务器:
|
114 |
-
- `RAG_DIR`: 存放RAG索引的目录 (default: "index_default")
|
115 |
-
- 应在代码中为您的特定 LLM 和嵌入模型提供商配置 API 密钥和基本 URL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/test.py
DELETED
@@ -1,68 +0,0 @@
|
|
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 |
-
# Uncomment the below two lines if running in a jupyter notebook to handle the async nature of rag.insert()
|
8 |
-
# import nest_asyncio
|
9 |
-
# nest_asyncio.apply()
|
10 |
-
#########
|
11 |
-
|
12 |
-
WORKING_DIR = "./dickens"
|
13 |
-
|
14 |
-
if not os.path.exists(WORKING_DIR):
|
15 |
-
os.mkdir(WORKING_DIR)
|
16 |
-
|
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 |
-
# llm_model_func=gpt_4o_complete # Optionally, use a stronger model
|
23 |
-
)
|
24 |
-
|
25 |
-
await rag.initialize_storages()
|
26 |
-
await initialize_pipeline_status()
|
27 |
-
|
28 |
-
return rag
|
29 |
-
|
30 |
-
|
31 |
-
def main():
|
32 |
-
# Initialize RAG instance
|
33 |
-
rag = asyncio.run(initialize_rag())
|
34 |
-
|
35 |
-
with open("./book.txt", "r", encoding="utf-8") as f:
|
36 |
-
rag.insert(f.read())
|
37 |
-
|
38 |
-
# Perform naive search
|
39 |
-
print(
|
40 |
-
rag.query(
|
41 |
-
"What are the top themes in this story?", param=QueryParam(mode="naive")
|
42 |
-
)
|
43 |
-
)
|
44 |
-
|
45 |
-
# Perform local search
|
46 |
-
print(
|
47 |
-
rag.query(
|
48 |
-
"What are the top themes in this story?", param=QueryParam(mode="local")
|
49 |
-
)
|
50 |
-
)
|
51 |
-
|
52 |
-
# Perform global search
|
53 |
-
print(
|
54 |
-
rag.query(
|
55 |
-
"What are the top themes in this story?", param=QueryParam(mode="global")
|
56 |
-
)
|
57 |
-
)
|
58 |
-
|
59 |
-
# Perform hybrid search
|
60 |
-
print(
|
61 |
-
rag.query(
|
62 |
-
"What are the top themes in this story?", param=QueryParam(mode="hybrid")
|
63 |
-
)
|
64 |
-
)
|
65 |
-
|
66 |
-
|
67 |
-
if __name__ == "__main__":
|
68 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/test_chromadb.py
DELETED
@@ -1,158 +0,0 @@
|
|
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.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()
|
11 |
-
# import nest_asyncio
|
12 |
-
# nest_asyncio.apply()
|
13 |
-
#########
|
14 |
-
WORKING_DIR = "./chromadb_test_dir"
|
15 |
-
if not os.path.exists(WORKING_DIR):
|
16 |
-
os.mkdir(WORKING_DIR)
|
17 |
-
|
18 |
-
# ChromaDB Configuration
|
19 |
-
CHROMADB_USE_LOCAL_PERSISTENT = False
|
20 |
-
# Local PersistentClient Configuration
|
21 |
-
CHROMADB_LOCAL_PATH = os.environ.get(
|
22 |
-
"CHROMADB_LOCAL_PATH", os.path.join(WORKING_DIR, "chroma_data")
|
23 |
-
)
|
24 |
-
# Remote HttpClient Configuration
|
25 |
-
CHROMADB_HOST = os.environ.get("CHROMADB_HOST", "localhost")
|
26 |
-
CHROMADB_PORT = int(os.environ.get("CHROMADB_PORT", 8000))
|
27 |
-
CHROMADB_AUTH_TOKEN = os.environ.get("CHROMADB_AUTH_TOKEN", "secret-token")
|
28 |
-
CHROMADB_AUTH_PROVIDER = os.environ.get(
|
29 |
-
"CHROMADB_AUTH_PROVIDER", "chromadb.auth.token_authn.TokenAuthClientProvider"
|
30 |
-
)
|
31 |
-
CHROMADB_AUTH_HEADER = os.environ.get("CHROMADB_AUTH_HEADER", "X-Chroma-Token")
|
32 |
-
|
33 |
-
# Embedding Configuration and Functions
|
34 |
-
EMBEDDING_MODEL = os.environ.get("EMBEDDING_MODEL", "text-embedding-3-large")
|
35 |
-
EMBEDDING_MAX_TOKEN_SIZE = int(os.environ.get("EMBEDDING_MAX_TOKEN_SIZE", 8192))
|
36 |
-
|
37 |
-
# ChromaDB requires knowing the dimension of embeddings upfront when
|
38 |
-
# creating a collection. The embedding dimension is model-specific
|
39 |
-
# (e.g. text-embedding-3-large uses 3072 dimensions)
|
40 |
-
# we dynamically determine it by running a test embedding
|
41 |
-
# and then pass it to the ChromaDBStorage class
|
42 |
-
|
43 |
-
|
44 |
-
async def embedding_func(texts: list[str]) -> np.ndarray:
|
45 |
-
return await openai_embed(
|
46 |
-
texts,
|
47 |
-
model=EMBEDDING_MODEL,
|
48 |
-
)
|
49 |
-
|
50 |
-
|
51 |
-
async def get_embedding_dimension():
|
52 |
-
test_text = ["This is a test sentence."]
|
53 |
-
embedding = await embedding_func(test_text)
|
54 |
-
return embedding.shape[1]
|
55 |
-
|
56 |
-
|
57 |
-
async def create_embedding_function_instance():
|
58 |
-
# Get embedding dimension
|
59 |
-
embedding_dimension = await get_embedding_dimension()
|
60 |
-
# Create embedding function instance
|
61 |
-
return EmbeddingFunc(
|
62 |
-
embedding_dim=embedding_dimension,
|
63 |
-
max_token_size=EMBEDDING_MAX_TOKEN_SIZE,
|
64 |
-
func=embedding_func,
|
65 |
-
)
|
66 |
-
|
67 |
-
|
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,
|
75 |
-
vector_storage="ChromaVectorDBStorage",
|
76 |
-
log_level="DEBUG",
|
77 |
-
embedding_batch_num=32,
|
78 |
-
vector_db_storage_cls_kwargs={
|
79 |
-
"local_path": CHROMADB_LOCAL_PATH,
|
80 |
-
"collection_settings": {
|
81 |
-
"hnsw:space": "cosine",
|
82 |
-
"hnsw:construction_ef": 128,
|
83 |
-
"hnsw:search_ef": 128,
|
84 |
-
"hnsw:M": 16,
|
85 |
-
"hnsw:batch_size": 100,
|
86 |
-
"hnsw:sync_threshold": 1000,
|
87 |
-
},
|
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,
|
95 |
-
vector_storage="ChromaVectorDBStorage",
|
96 |
-
log_level="DEBUG",
|
97 |
-
embedding_batch_num=32,
|
98 |
-
vector_db_storage_cls_kwargs={
|
99 |
-
"host": CHROMADB_HOST,
|
100 |
-
"port": CHROMADB_PORT,
|
101 |
-
"auth_token": CHROMADB_AUTH_TOKEN,
|
102 |
-
"auth_provider": CHROMADB_AUTH_PROVIDER,
|
103 |
-
"auth_header_name": CHROMADB_AUTH_HEADER,
|
104 |
-
"collection_settings": {
|
105 |
-
"hnsw:space": "cosine",
|
106 |
-
"hnsw:construction_ef": 128,
|
107 |
-
"hnsw:search_ef": 128,
|
108 |
-
"hnsw:M": 16,
|
109 |
-
"hnsw:batch_size": 100,
|
110 |
-
"hnsw:sync_threshold": 1000,
|
111 |
-
},
|
112 |
-
},
|
113 |
-
)
|
114 |
-
|
115 |
-
await rag.initialize_storages()
|
116 |
-
await initialize_pipeline_status()
|
117 |
-
|
118 |
-
return rag
|
119 |
-
|
120 |
-
|
121 |
-
def main():
|
122 |
-
# Initialize RAG instance
|
123 |
-
rag = asyncio.run(initialize_rag())
|
124 |
-
|
125 |
-
with open("./book.txt", "r", encoding="utf-8") as f:
|
126 |
-
rag.insert(f.read())
|
127 |
-
|
128 |
-
# Perform naive search
|
129 |
-
print(
|
130 |
-
rag.query(
|
131 |
-
"What are the top themes in this story?", param=QueryParam(mode="naive")
|
132 |
-
)
|
133 |
-
)
|
134 |
-
|
135 |
-
# Perform local search
|
136 |
-
print(
|
137 |
-
rag.query(
|
138 |
-
"What are the top themes in this story?", param=QueryParam(mode="local")
|
139 |
-
)
|
140 |
-
)
|
141 |
-
|
142 |
-
# Perform global search
|
143 |
-
print(
|
144 |
-
rag.query(
|
145 |
-
"What are the top themes in this story?", param=QueryParam(mode="global")
|
146 |
-
)
|
147 |
-
)
|
148 |
-
|
149 |
-
# Perform hybrid search
|
150 |
-
print(
|
151 |
-
rag.query(
|
152 |
-
"What are the top themes in this story?", param=QueryParam(mode="hybrid")
|
153 |
-
)
|
154 |
-
)
|
155 |
-
|
156 |
-
|
157 |
-
if __name__ == "__main__":
|
158 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/test_faiss.py
DELETED
@@ -1,108 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import logging
|
3 |
-
import asyncio
|
4 |
-
import numpy as np
|
5 |
-
|
6 |
-
from dotenv import load_dotenv
|
7 |
-
from sentence_transformers import SentenceTransformer
|
8 |
-
|
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 |
-
|
18 |
-
# Load environment variables from .env file
|
19 |
-
load_dotenv()
|
20 |
-
AZURE_OPENAI_API_VERSION = os.getenv("AZURE_OPENAI_API_VERSION")
|
21 |
-
AZURE_OPENAI_DEPLOYMENT = os.getenv("AZURE_OPENAI_DEPLOYMENT")
|
22 |
-
AZURE_OPENAI_API_KEY = os.getenv("AZURE_OPENAI_API_KEY")
|
23 |
-
AZURE_OPENAI_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
|
24 |
-
|
25 |
-
|
26 |
-
async def llm_model_func(
|
27 |
-
prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
|
28 |
-
) -> str:
|
29 |
-
# Create a client for AzureOpenAI
|
30 |
-
client = AzureOpenAI(
|
31 |
-
api_key=AZURE_OPENAI_API_KEY,
|
32 |
-
api_version=AZURE_OPENAI_API_VERSION,
|
33 |
-
azure_endpoint=AZURE_OPENAI_ENDPOINT,
|
34 |
-
)
|
35 |
-
|
36 |
-
# Build the messages list for the conversation
|
37 |
-
messages = []
|
38 |
-
if system_prompt:
|
39 |
-
messages.append({"role": "system", "content": system_prompt})
|
40 |
-
if history_messages:
|
41 |
-
messages.extend(history_messages)
|
42 |
-
messages.append({"role": "user", "content": prompt})
|
43 |
-
|
44 |
-
# Call the LLM
|
45 |
-
chat_completion = client.chat.completions.create(
|
46 |
-
model=AZURE_OPENAI_DEPLOYMENT,
|
47 |
-
messages=messages,
|
48 |
-
temperature=kwargs.get("temperature", 0),
|
49 |
-
top_p=kwargs.get("top_p", 1),
|
50 |
-
n=kwargs.get("n", 1),
|
51 |
-
)
|
52 |
-
|
53 |
-
return chat_completion.choices[0].message.content
|
54 |
-
|
55 |
-
|
56 |
-
async def embedding_func(texts: list[str]) -> np.ndarray:
|
57 |
-
model = SentenceTransformer("all-MiniLM-L6-v2")
|
58 |
-
embeddings = model.encode(texts, convert_to_numpy=True)
|
59 |
-
return embeddings
|
60 |
-
|
61 |
-
|
62 |
-
async def initialize_rag():
|
63 |
-
rag = LightRAG(
|
64 |
-
working_dir=WORKING_DIR,
|
65 |
-
llm_model_func=llm_model_func,
|
66 |
-
embedding_func=EmbeddingFunc(
|
67 |
-
embedding_dim=384,
|
68 |
-
max_token_size=8192,
|
69 |
-
func=embedding_func,
|
70 |
-
),
|
71 |
-
vector_storage="FaissVectorDBStorage",
|
72 |
-
vector_db_storage_cls_kwargs={
|
73 |
-
"cosine_better_than_threshold": 0.2 # Your desired threshold
|
74 |
-
},
|
75 |
-
)
|
76 |
-
|
77 |
-
await rag.initialize_storages()
|
78 |
-
await initialize_pipeline_status()
|
79 |
-
|
80 |
-
return rag
|
81 |
-
|
82 |
-
|
83 |
-
def main():
|
84 |
-
# Initialize RAG instance
|
85 |
-
rag = asyncio.run(initialize_rag())
|
86 |
-
# Insert the custom chunks into LightRAG
|
87 |
-
book1 = open("./book_1.txt", encoding="utf-8")
|
88 |
-
book2 = open("./book_2.txt", encoding="utf-8")
|
89 |
-
|
90 |
-
rag.insert([book1.read(), book2.read()])
|
91 |
-
|
92 |
-
query_text = "What are the main themes?"
|
93 |
-
|
94 |
-
print("Result (Naive):")
|
95 |
-
print(rag.query(query_text, param=QueryParam(mode="naive")))
|
96 |
-
|
97 |
-
print("\nResult (Local):")
|
98 |
-
print(rag.query(query_text, param=QueryParam(mode="local")))
|
99 |
-
|
100 |
-
print("\nResult (Global):")
|
101 |
-
print(rag.query(query_text, param=QueryParam(mode="global")))
|
102 |
-
|
103 |
-
print("\nResult (Hybrid):")
|
104 |
-
print(rag.query(query_text, param=QueryParam(mode="hybrid")))
|
105 |
-
|
106 |
-
|
107 |
-
if __name__ == "__main__":
|
108 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/test_neo4j.py
DELETED
@@ -1,71 +0,0 @@
|
|
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()
|
9 |
-
# import nest_asyncio
|
10 |
-
# nest_asyncio.apply()
|
11 |
-
#########
|
12 |
-
|
13 |
-
WORKING_DIR = "./local_neo4jWorkDir"
|
14 |
-
|
15 |
-
if not os.path.exists(WORKING_DIR):
|
16 |
-
os.mkdir(WORKING_DIR)
|
17 |
-
|
18 |
-
|
19 |
-
async def initialize_rag():
|
20 |
-
rag = LightRAG(
|
21 |
-
working_dir=WORKING_DIR,
|
22 |
-
llm_model_func=gpt_4o_mini_complete, # Use gpt_4o_mini_complete LLM model
|
23 |
-
graph_storage="Neo4JStorage",
|
24 |
-
log_level="INFO",
|
25 |
-
# llm_model_func=gpt_4o_complete # Optionally, use a stronger model
|
26 |
-
)
|
27 |
-
|
28 |
-
await rag.initialize_storages()
|
29 |
-
await initialize_pipeline_status()
|
30 |
-
|
31 |
-
return rag
|
32 |
-
|
33 |
-
|
34 |
-
def main():
|
35 |
-
# Initialize RAG instance
|
36 |
-
rag = asyncio.run(initialize_rag())
|
37 |
-
|
38 |
-
with open("./book.txt", "r", encoding="utf-8") as f:
|
39 |
-
rag.insert(f.read())
|
40 |
-
|
41 |
-
# Perform naive search
|
42 |
-
print(
|
43 |
-
rag.query(
|
44 |
-
"What are the top themes in this story?", param=QueryParam(mode="naive")
|
45 |
-
)
|
46 |
-
)
|
47 |
-
|
48 |
-
# Perform local search
|
49 |
-
print(
|
50 |
-
rag.query(
|
51 |
-
"What are the top themes in this story?", param=QueryParam(mode="local")
|
52 |
-
)
|
53 |
-
)
|
54 |
-
|
55 |
-
# Perform global search
|
56 |
-
print(
|
57 |
-
rag.query(
|
58 |
-
"What are the top themes in this story?", param=QueryParam(mode="global")
|
59 |
-
)
|
60 |
-
)
|
61 |
-
|
62 |
-
# Perform hybrid search
|
63 |
-
print(
|
64 |
-
rag.query(
|
65 |
-
"What are the top themes in this story?", param=QueryParam(mode="hybrid")
|
66 |
-
)
|
67 |
-
)
|
68 |
-
|
69 |
-
|
70 |
-
if __name__ == "__main__":
|
71 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/test_postgres.py
DELETED
@@ -1,51 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import asyncio
|
3 |
-
from lightrag.kg.postgres_impl import PGGraphStorage
|
4 |
-
from lightrag.llm.ollama import ollama_embedding
|
5 |
-
from lightrag.utils import EmbeddingFunc
|
6 |
-
|
7 |
-
#########
|
8 |
-
# Uncomment the below two lines if running in a jupyter notebook to handle the async nature of rag.insert()
|
9 |
-
# import nest_asyncio
|
10 |
-
# nest_asyncio.apply()
|
11 |
-
#########
|
12 |
-
|
13 |
-
WORKING_DIR = "./local_neo4jWorkDir"
|
14 |
-
|
15 |
-
if not os.path.exists(WORKING_DIR):
|
16 |
-
os.mkdir(WORKING_DIR)
|
17 |
-
|
18 |
-
# AGE
|
19 |
-
os.environ["AGE_GRAPH_NAME"] = "dickens"
|
20 |
-
|
21 |
-
os.environ["POSTGRES_HOST"] = "localhost"
|
22 |
-
os.environ["POSTGRES_PORT"] = "15432"
|
23 |
-
os.environ["POSTGRES_USER"] = "rag"
|
24 |
-
os.environ["POSTGRES_PASSWORD"] = "rag"
|
25 |
-
os.environ["POSTGRES_DATABASE"] = "rag"
|
26 |
-
|
27 |
-
|
28 |
-
async def main():
|
29 |
-
graph_db = PGGraphStorage(
|
30 |
-
namespace="dickens",
|
31 |
-
embedding_func=EmbeddingFunc(
|
32 |
-
embedding_dim=1024,
|
33 |
-
max_token_size=8192,
|
34 |
-
func=lambda texts: ollama_embedding(
|
35 |
-
texts, embed_model="bge-m3", host="http://localhost:11434"
|
36 |
-
),
|
37 |
-
),
|
38 |
-
global_config={},
|
39 |
-
)
|
40 |
-
await graph_db.initialize()
|
41 |
-
labels = await graph_db.get_all_labels()
|
42 |
-
print("all labels", labels)
|
43 |
-
|
44 |
-
res = await graph_db.get_knowledge_graph("FEZZIWIG")
|
45 |
-
print("knowledge graphs", res)
|
46 |
-
|
47 |
-
await graph_db.finalize()
|
48 |
-
|
49 |
-
|
50 |
-
if __name__ == "__main__":
|
51 |
-
asyncio.run(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
examples/vram_management_demo.py
DELETED
@@ -1,121 +0,0 @@
|
|
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"
|
11 |
-
TEXT_FILES_DIR = "/llm/mt"
|
12 |
-
|
13 |
-
# Create the working directory if it doesn't exist
|
14 |
-
if not os.path.exists(WORKING_DIR):
|
15 |
-
os.mkdir(WORKING_DIR)
|
16 |
-
|
17 |
-
|
18 |
-
async def initialize_rag():
|
19 |
-
# Initialize LightRAG
|
20 |
-
rag = LightRAG(
|
21 |
-
working_dir=WORKING_DIR,
|
22 |
-
llm_model_func=ollama_model_complete,
|
23 |
-
llm_model_name="qwen2.5:3b-instruct-max-context",
|
24 |
-
embedding_func=EmbeddingFunc(
|
25 |
-
embedding_dim=768,
|
26 |
-
max_token_size=8192,
|
27 |
-
func=lambda texts: ollama_embed(texts, embed_model="nomic-embed-text"),
|
28 |
-
),
|
29 |
-
)
|
30 |
-
await rag.initialize_storages()
|
31 |
-
await initialize_pipeline_status()
|
32 |
-
|
33 |
-
return rag
|
34 |
-
|
35 |
-
|
36 |
-
# Read all .txt files from the TEXT_FILES_DIR directory
|
37 |
-
texts = []
|
38 |
-
for filename in os.listdir(TEXT_FILES_DIR):
|
39 |
-
if filename.endswith(".txt"):
|
40 |
-
file_path = os.path.join(TEXT_FILES_DIR, filename)
|
41 |
-
with open(file_path, "r", encoding="utf-8") as file:
|
42 |
-
texts.append(file.read())
|
43 |
-
|
44 |
-
|
45 |
-
# Batch insert texts into LightRAG with a retry mechanism
|
46 |
-
def insert_texts_with_retry(rag, texts, retries=3, delay=5):
|
47 |
-
for _ in range(retries):
|
48 |
-
try:
|
49 |
-
rag.insert(texts)
|
50 |
-
return
|
51 |
-
except Exception as e:
|
52 |
-
print(
|
53 |
-
f"Error occurred during insertion: {e}. Retrying in {delay} seconds..."
|
54 |
-
)
|
55 |
-
time.sleep(delay)
|
56 |
-
raise RuntimeError("Failed to insert texts after multiple retries.")
|
57 |
-
|
58 |
-
|
59 |
-
def main():
|
60 |
-
# Initialize RAG instance
|
61 |
-
rag = asyncio.run(initialize_rag())
|
62 |
-
|
63 |
-
insert_texts_with_retry(rag, texts)
|
64 |
-
|
65 |
-
# Perform different types of queries and handle potential errors
|
66 |
-
try:
|
67 |
-
print(
|
68 |
-
rag.query(
|
69 |
-
"What are the top themes in this story?", param=QueryParam(mode="naive")
|
70 |
-
)
|
71 |
-
)
|
72 |
-
except Exception as e:
|
73 |
-
print(f"Error performing naive search: {e}")
|
74 |
-
|
75 |
-
try:
|
76 |
-
print(
|
77 |
-
rag.query(
|
78 |
-
"What are the top themes in this story?", param=QueryParam(mode="local")
|
79 |
-
)
|
80 |
-
)
|
81 |
-
except Exception as e:
|
82 |
-
print(f"Error performing local search: {e}")
|
83 |
-
|
84 |
-
try:
|
85 |
-
print(
|
86 |
-
rag.query(
|
87 |
-
"What are the top themes in this story?",
|
88 |
-
param=QueryParam(mode="global"),
|
89 |
-
)
|
90 |
-
)
|
91 |
-
except Exception as e:
|
92 |
-
print(f"Error performing global search: {e}")
|
93 |
-
|
94 |
-
try:
|
95 |
-
print(
|
96 |
-
rag.query(
|
97 |
-
"What are the top themes in this story?",
|
98 |
-
param=QueryParam(mode="hybrid"),
|
99 |
-
)
|
100 |
-
)
|
101 |
-
except Exception as e:
|
102 |
-
print(f"Error performing hybrid search: {e}")
|
103 |
-
|
104 |
-
# Function to clear VRAM resources
|
105 |
-
def clear_vram():
|
106 |
-
os.system("sudo nvidia-smi --gpu-reset")
|
107 |
-
|
108 |
-
# Regularly clear VRAM to prevent overflow
|
109 |
-
clear_vram_interval = 3600 # Clear once every hour
|
110 |
-
start_time = time.time()
|
111 |
-
|
112 |
-
while True:
|
113 |
-
current_time = time.time()
|
114 |
-
if current_time - start_time > clear_vram_interval:
|
115 |
-
clear_vram()
|
116 |
-
start_time = current_time
|
117 |
-
time.sleep(60) # Check the time every minute
|
118 |
-
|
119 |
-
|
120 |
-
if __name__ == "__main__":
|
121 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|