yangdx commited on
Commit
e2584a3
·
1 Parent(s): 62a507f

Remove api demo (reference to LightRAG Server instead)

Browse files
examples/lightrag_api_ollama_demo.py DELETED
@@ -1,188 +0,0 @@
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
6
- from lightrag.llm.ollama import ollama_embed, ollama_model_complete
7
- from lightrag.utils import EmbeddingFunc
8
- from typing import Optional
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}")
21
- print(f"INPUT_FILE: {INPUT_FILE}")
22
-
23
- # Configure working directory
24
- WORKING_DIR = os.environ.get("RAG_DIR", f"{DEFAULT_RAG_DIR}")
25
- print(f"WORKING_DIR: {WORKING_DIR}")
26
-
27
-
28
- if not os.path.exists(WORKING_DIR):
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={
40
- "host": "http://localhost:11434",
41
- "options": {"num_ctx": 8192},
42
- },
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
- )
51
-
52
- # Add initialization code
53
- await rag.initialize_storages()
54
- await initialize_pipeline_status()
55
-
56
- return rag
57
-
58
-
59
- @asynccontextmanager
60
- async def lifespan(app: FastAPI):
61
- global rag
62
- rag = await init()
63
- print("done!")
64
- yield
65
-
66
-
67
- app = FastAPI(
68
- title="LightRAG API", description="API for RAG operations", lifespan=lifespan
69
- )
70
-
71
-
72
- # Data models
73
- class QueryRequest(BaseModel):
74
- query: str
75
- mode: str = "hybrid"
76
- only_need_context: bool = False
77
-
78
-
79
- class InsertRequest(BaseModel):
80
- text: str
81
-
82
-
83
- class Response(BaseModel):
84
- status: str
85
- data: Optional[str] = None
86
- message: Optional[str] = None
87
-
88
-
89
- # API routes
90
- @app.post("/query", response_model=Response)
91
- async def query_endpoint(request: QueryRequest):
92
- try:
93
- loop = asyncio.get_event_loop()
94
- result = await loop.run_in_executor(
95
- None,
96
- lambda: rag.query(
97
- request.query,
98
- param=QueryParam(
99
- mode=request.mode, only_need_context=request.only_need_context
100
- ),
101
- ),
102
- )
103
- return Response(status="success", data=result)
104
- except Exception as e:
105
- raise HTTPException(status_code=500, detail=str(e))
106
-
107
-
108
- # insert by text
109
- @app.post("/insert", response_model=Response)
110
- async def insert_endpoint(request: InsertRequest):
111
- try:
112
- loop = asyncio.get_event_loop()
113
- await loop.run_in_executor(None, lambda: rag.insert(request.text))
114
- return Response(status="success", message="Text inserted successfully")
115
- except Exception as e:
116
- raise HTTPException(status_code=500, detail=str(e))
117
-
118
-
119
- # insert by file in payload
120
- @app.post("/insert_file", response_model=Response)
121
- async def insert_file(file: UploadFile = File(...)):
122
- try:
123
- file_content = await file.read()
124
- # Read file content
125
- try:
126
- content = file_content.decode("utf-8")
127
- except UnicodeDecodeError:
128
- # If UTF-8 decoding fails, try other encodings
129
- content = file_content.decode("gbk")
130
- # Insert file content
131
- loop = asyncio.get_event_loop()
132
- await loop.run_in_executor(None, lambda: rag.insert(content))
133
-
134
- return Response(
135
- status="success",
136
- message=f"File content from {file.filename} inserted successfully",
137
- )
138
- except Exception as e:
139
- raise HTTPException(status_code=500, detail=str(e))
140
-
141
-
142
- # insert by local default file
143
- @app.post("/insert_default_file", response_model=Response)
144
- @app.get("/insert_default_file", response_model=Response)
145
- async def insert_default_file():
146
- try:
147
- # Read file content from book.txt
148
- async with aiofiles.open(INPUT_FILE, "r", encoding="utf-8") as file:
149
- content = await file.read()
150
- print(f"read input file {INPUT_FILE} successfully")
151
- # Insert file content
152
- loop = asyncio.get_event_loop()
153
- await loop.run_in_executor(None, lambda: rag.insert(content))
154
-
155
- return Response(
156
- status="success",
157
- message=f"File content from {INPUT_FILE} inserted successfully",
158
- )
159
- except Exception as e:
160
- raise HTTPException(status_code=500, detail=str(e))
161
-
162
-
163
- @app.get("/health")
164
- async def health_check():
165
- return {"status": "healthy"}
166
-
167
-
168
- if __name__ == "__main__":
169
- import uvicorn
170
-
171
- uvicorn.run(app, host="0.0.0.0", port=8020)
172
-
173
- # Usage example
174
- # To run the server, use the following command in your terminal:
175
- # python lightrag_api_openai_compatible_demo.py
176
-
177
- # Example requests:
178
- # 1. Query:
179
- # curl -X POST "http://127.0.0.1:8020/query" -H "Content-Type: application/json" -d '{"query": "your query here", "mode": "hybrid"}'
180
-
181
- # 2. Insert text:
182
- # curl -X POST "http://127.0.0.1:8020/insert" -H "Content-Type: application/json" -d '{"text": "your text here"}'
183
-
184
- # 3. Insert file:
185
- # curl -X POST "http://127.0.0.1:8020/insert_file" -H "Content-Type: multipart/form-data" -F "file=@path/to/your/file.txt"
186
-
187
- # 4. Health check:
188
- # curl -X GET "http://127.0.0.1:8020/health"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
examples/lightrag_api_openai_compatible_demo.py DELETED
@@ -1,204 +0,0 @@
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
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 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()
16
-
17
- DEFAULT_RAG_DIR = "index_default"
18
- app = FastAPI(title="LightRAG API", description="API for RAG operations")
19
-
20
- # Configure working directory
21
- WORKING_DIR = os.environ.get("RAG_DIR", f"{DEFAULT_RAG_DIR}")
22
- print(f"WORKING_DIR: {WORKING_DIR}")
23
- LLM_MODEL = os.environ.get("LLM_MODEL", "gpt-4o-mini")
24
- print(f"LLM_MODEL: {LLM_MODEL}")
25
- EMBEDDING_MODEL = os.environ.get("EMBEDDING_MODEL", "text-embedding-3-large")
26
- print(f"EMBEDDING_MODEL: {EMBEDDING_MODEL}")
27
- EMBEDDING_MAX_TOKEN_SIZE = int(os.environ.get("EMBEDDING_MAX_TOKEN_SIZE", 8192))
28
- print(f"EMBEDDING_MAX_TOKEN_SIZE: {EMBEDDING_MAX_TOKEN_SIZE}")
29
- BASE_URL = os.environ.get("BASE_URL", "https://api.openai.com/v1")
30
- print(f"BASE_URL: {BASE_URL}")
31
- API_KEY = os.environ.get("API_KEY", "xxxxxxxx")
32
- print(f"API_KEY: {API_KEY}")
33
-
34
- if not os.path.exists(WORKING_DIR):
35
- os.mkdir(WORKING_DIR)
36
-
37
-
38
- # LLM model function
39
-
40
-
41
- async def llm_model_func(
42
- prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
43
- ) -> str:
44
- return await openai_complete_if_cache(
45
- model=LLM_MODEL,
46
- prompt=prompt,
47
- system_prompt=system_prompt,
48
- history_messages=history_messages,
49
- base_url=BASE_URL,
50
- api_key=API_KEY,
51
- **kwargs,
52
- )
53
-
54
-
55
- # Embedding function
56
-
57
-
58
- async def embedding_func(texts: list[str]) -> np.ndarray:
59
- return await openai_embed(
60
- texts=texts,
61
- model=EMBEDDING_MODEL,
62
- base_url=BASE_URL,
63
- api_key=API_KEY,
64
- )
65
-
66
-
67
- async def get_embedding_dim():
68
- test_text = ["This is a test sentence."]
69
- embedding = await embedding_func(test_text)
70
- embedding_dim = embedding.shape[1]
71
- print(f"{embedding_dim=}")
72
- return embedding_dim
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
-
95
- @asynccontextmanager
96
- async def lifespan(app: FastAPI):
97
- global rag
98
- rag = await init()
99
- print("done!")
100
- yield
101
-
102
-
103
- app = FastAPI(
104
- title="LightRAG API", description="API for RAG operations", lifespan=lifespan
105
- )
106
-
107
- # Data models
108
-
109
-
110
- class QueryRequest(BaseModel):
111
- query: str
112
- mode: str = "hybrid"
113
- only_need_context: bool = False
114
-
115
-
116
- class InsertRequest(BaseModel):
117
- text: str
118
-
119
-
120
- class Response(BaseModel):
121
- status: str
122
- data: Optional[str] = None
123
- message: Optional[str] = None
124
-
125
-
126
- # API routes
127
-
128
-
129
- @app.post("/query", response_model=Response)
130
- async def query_endpoint(request: QueryRequest):
131
- try:
132
- loop = asyncio.get_event_loop()
133
- result = await loop.run_in_executor(
134
- None,
135
- lambda: rag.query(
136
- request.query,
137
- param=QueryParam(
138
- mode=request.mode, only_need_context=request.only_need_context
139
- ),
140
- ),
141
- )
142
- return Response(status="success", data=result)
143
- except Exception as e:
144
- raise HTTPException(status_code=500, detail=str(e))
145
-
146
-
147
- @app.post("/insert", response_model=Response)
148
- async def insert_endpoint(request: InsertRequest):
149
- try:
150
- loop = asyncio.get_event_loop()
151
- await loop.run_in_executor(None, lambda: rag.insert(request.text))
152
- return Response(status="success", message="Text inserted successfully")
153
- except Exception as e:
154
- raise HTTPException(status_code=500, detail=str(e))
155
-
156
-
157
- @app.post("/insert_file", response_model=Response)
158
- async def insert_file(file: UploadFile = File(...)):
159
- try:
160
- file_content = await file.read()
161
- # Read file content
162
- try:
163
- content = file_content.decode("utf-8")
164
- except UnicodeDecodeError:
165
- # If UTF-8 decoding fails, try other encodings
166
- content = file_content.decode("gbk")
167
- # Insert file content
168
- loop = asyncio.get_event_loop()
169
- await loop.run_in_executor(None, lambda: rag.insert(content))
170
-
171
- return Response(
172
- status="success",
173
- message=f"File content from {file.filename} inserted successfully",
174
- )
175
- except Exception as e:
176
- raise HTTPException(status_code=500, detail=str(e))
177
-
178
-
179
- @app.get("/health")
180
- async def health_check():
181
- return {"status": "healthy"}
182
-
183
-
184
- if __name__ == "__main__":
185
- import uvicorn
186
-
187
- uvicorn.run(app, host="0.0.0.0", port=8020)
188
-
189
- # Usage example
190
- # To run the server, use the following command in your terminal:
191
- # python lightrag_api_openai_compatible_demo.py
192
-
193
- # Example requests:
194
- # 1. Query:
195
- # curl -X POST "http://127.0.0.1:8020/query" -H "Content-Type: application/json" -d '{"query": "your query here", "mode": "hybrid"}'
196
-
197
- # 2. Insert text:
198
- # curl -X POST "http://127.0.0.1:8020/insert" -H "Content-Type: application/json" -d '{"text": "your text here"}'
199
-
200
- # 3. Insert file:
201
- # curl -X POST "http://127.0.0.1:8020/insert_file" -H "Content-Type: multipart/form-data" -F "file=@path/to/your/file.txt"
202
-
203
- # 4. Health check:
204
- # curl -X GET "http://127.0.0.1:8020/health"