Refactor code formatting and update requirements for improved clarity and consistency
Browse files- .gitignore +1 -1
- api/.env.aoi.example +1 -1
- api/azure_openai_lightrag_server.py +15 -9
- api/requirements.txt +9 -9
- examples/.env.oai.example +1 -1
- lightrag/llm.py +1 -1
.gitignore
CHANGED
|
@@ -18,4 +18,4 @@ gui/
|
|
| 18 |
.env
|
| 19 |
venv/
|
| 20 |
examples/input/
|
| 21 |
-
examples/output/
|
|
|
|
| 18 |
.env
|
| 19 |
venv/
|
| 20 |
examples/input/
|
| 21 |
+
examples/output/
|
api/.env.aoi.example
CHANGED
|
@@ -4,4 +4,4 @@ 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
|
|
|
|
| 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
|
api/azure_openai_lightrag_server.py
CHANGED
|
@@ -4,7 +4,10 @@ import asyncio
|
|
| 4 |
import logging
|
| 5 |
import argparse
|
| 6 |
from lightrag import LightRAG, QueryParam
|
| 7 |
-
from lightrag.llm import
|
|
|
|
|
|
|
|
|
|
| 8 |
from lightrag.utils import EmbeddingFunc
|
| 9 |
from typing import Optional, List
|
| 10 |
from enum import Enum
|
|
@@ -28,6 +31,7 @@ AZURE_OPENAI_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
|
|
| 28 |
AZURE_EMBEDDING_DEPLOYMENT = os.getenv("AZURE_EMBEDDING_DEPLOYMENT")
|
| 29 |
AZURE_EMBEDDING_API_VERSION = os.getenv("AZURE_EMBEDDING_API_VERSION")
|
| 30 |
|
|
|
|
| 31 |
def parse_args():
|
| 32 |
parser = argparse.ArgumentParser(
|
| 33 |
description="LightRAG FastAPI Server with OpenAI integration"
|
|
@@ -132,7 +136,7 @@ class SearchMode(str, Enum):
|
|
| 132 |
class QueryRequest(BaseModel):
|
| 133 |
query: str
|
| 134 |
mode: SearchMode = SearchMode.hybrid
|
| 135 |
-
#stream: bool = False
|
| 136 |
|
| 137 |
|
| 138 |
class QueryResponse(BaseModel):
|
|
@@ -205,10 +209,11 @@ def create_app(args):
|
|
| 205 |
embedding_func=EmbeddingFunc(
|
| 206 |
embedding_dim=embedding_dim,
|
| 207 |
max_token_size=args.max_embed_tokens,
|
| 208 |
-
func=lambda texts: azure_openai_embedding(
|
|
|
|
|
|
|
| 209 |
),
|
| 210 |
)
|
| 211 |
-
|
| 212 |
|
| 213 |
@app.on_event("startup")
|
| 214 |
async def startup_event():
|
|
@@ -266,9 +271,7 @@ def create_app(args):
|
|
| 266 |
if os.path.exists(cachefile):
|
| 267 |
with open(cachefile, "w") as f:
|
| 268 |
f.write("{}")
|
| 269 |
-
return {
|
| 270 |
-
"status": "success"
|
| 271 |
-
}
|
| 272 |
except Exception as e:
|
| 273 |
raise HTTPException(status_code=500, detail=str(e))
|
| 274 |
|
|
@@ -319,15 +322,17 @@ def create_app(args):
|
|
| 319 |
param=QueryParam(mode=request.mode, stream=True),
|
| 320 |
)
|
| 321 |
if inspect.isasyncgen(response):
|
|
|
|
| 322 |
async def stream_generator():
|
| 323 |
async for chunk in response:
|
| 324 |
yield json.dumps({"data": chunk}) + "\n"
|
| 325 |
|
| 326 |
-
return StreamingResponse(
|
|
|
|
|
|
|
| 327 |
else:
|
| 328 |
return QueryResponse(response=response)
|
| 329 |
|
| 330 |
-
|
| 331 |
except Exception as e:
|
| 332 |
raise HTTPException(status_code=500, detail=str(e))
|
| 333 |
|
|
@@ -433,5 +438,6 @@ def create_app(args):
|
|
| 433 |
if __name__ == "__main__":
|
| 434 |
args = parse_args()
|
| 435 |
import uvicorn
|
|
|
|
| 436 |
app = create_app(args)
|
| 437 |
uvicorn.run(app, host=args.host, port=args.port)
|
|
|
|
| 4 |
import logging
|
| 5 |
import argparse
|
| 6 |
from lightrag import LightRAG, QueryParam
|
| 7 |
+
from lightrag.llm import (
|
| 8 |
+
azure_openai_complete_if_cache,
|
| 9 |
+
azure_openai_embedding,
|
| 10 |
+
)
|
| 11 |
from lightrag.utils import EmbeddingFunc
|
| 12 |
from typing import Optional, List
|
| 13 |
from enum import Enum
|
|
|
|
| 31 |
AZURE_EMBEDDING_DEPLOYMENT = os.getenv("AZURE_EMBEDDING_DEPLOYMENT")
|
| 32 |
AZURE_EMBEDDING_API_VERSION = os.getenv("AZURE_EMBEDDING_API_VERSION")
|
| 33 |
|
| 34 |
+
|
| 35 |
def parse_args():
|
| 36 |
parser = argparse.ArgumentParser(
|
| 37 |
description="LightRAG FastAPI Server with OpenAI integration"
|
|
|
|
| 136 |
class QueryRequest(BaseModel):
|
| 137 |
query: str
|
| 138 |
mode: SearchMode = SearchMode.hybrid
|
| 139 |
+
# stream: bool = False
|
| 140 |
|
| 141 |
|
| 142 |
class QueryResponse(BaseModel):
|
|
|
|
| 209 |
embedding_func=EmbeddingFunc(
|
| 210 |
embedding_dim=embedding_dim,
|
| 211 |
max_token_size=args.max_embed_tokens,
|
| 212 |
+
func=lambda texts: azure_openai_embedding(
|
| 213 |
+
texts, model=args.embedding_model
|
| 214 |
+
),
|
| 215 |
),
|
| 216 |
)
|
|
|
|
| 217 |
|
| 218 |
@app.on_event("startup")
|
| 219 |
async def startup_event():
|
|
|
|
| 271 |
if os.path.exists(cachefile):
|
| 272 |
with open(cachefile, "w") as f:
|
| 273 |
f.write("{}")
|
| 274 |
+
return {"status": "success"}
|
|
|
|
|
|
|
| 275 |
except Exception as e:
|
| 276 |
raise HTTPException(status_code=500, detail=str(e))
|
| 277 |
|
|
|
|
| 322 |
param=QueryParam(mode=request.mode, stream=True),
|
| 323 |
)
|
| 324 |
if inspect.isasyncgen(response):
|
| 325 |
+
|
| 326 |
async def stream_generator():
|
| 327 |
async for chunk in response:
|
| 328 |
yield json.dumps({"data": chunk}) + "\n"
|
| 329 |
|
| 330 |
+
return StreamingResponse(
|
| 331 |
+
stream_generator(), media_type="application/json"
|
| 332 |
+
)
|
| 333 |
else:
|
| 334 |
return QueryResponse(response=response)
|
| 335 |
|
|
|
|
| 336 |
except Exception as e:
|
| 337 |
raise HTTPException(status_code=500, detail=str(e))
|
| 338 |
|
|
|
|
| 438 |
if __name__ == "__main__":
|
| 439 |
args = parse_args()
|
| 440 |
import uvicorn
|
| 441 |
+
|
| 442 |
app = create_app(args)
|
| 443 |
uvicorn.run(app, host=args.host, port=args.port)
|
api/requirements.txt
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
|
|
| 1 |
ascii_colors
|
| 2 |
fastapi
|
| 3 |
-
python-multipart
|
| 4 |
-
uvicorn
|
| 5 |
-
nest_asyncio
|
| 6 |
lightrag-hku
|
| 7 |
-
|
| 8 |
-
|
| 9 |
numpy
|
| 10 |
ollama
|
| 11 |
-
torch
|
| 12 |
openai
|
|
|
|
|
|
|
| 13 |
tenacity
|
| 14 |
-
transformers
|
| 15 |
tiktoken
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
aioboto3
|
| 2 |
ascii_colors
|
| 3 |
fastapi
|
|
|
|
|
|
|
|
|
|
| 4 |
lightrag-hku
|
| 5 |
+
nano_vectordb
|
| 6 |
+
nest_asyncio
|
| 7 |
numpy
|
| 8 |
ollama
|
|
|
|
| 9 |
openai
|
| 10 |
+
python-dotenv
|
| 11 |
+
python-multipart
|
| 12 |
tenacity
|
|
|
|
| 13 |
tiktoken
|
| 14 |
+
torch
|
| 15 |
+
tqdm
|
| 16 |
+
transformers
|
| 17 |
+
uvicorn
|
examples/.env.oai.example
CHANGED
|
@@ -4,4 +4,4 @@ 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
|
|
|
|
| 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
|
lightrag/llm.py
CHANGED
|
@@ -148,7 +148,7 @@ async def azure_openai_complete_if_cache(
|
|
| 148 |
response = await openai_async_client.chat.completions.create(
|
| 149 |
model=model, messages=messages, **kwargs
|
| 150 |
)
|
| 151 |
-
|
| 152 |
if hasattr(response, "__aiter__"):
|
| 153 |
|
| 154 |
async def inner():
|
|
|
|
| 148 |
response = await openai_async_client.chat.completions.create(
|
| 149 |
model=model, messages=messages, **kwargs
|
| 150 |
)
|
| 151 |
+
|
| 152 |
if hasattr(response, "__aiter__"):
|
| 153 |
|
| 154 |
async def inner():
|