yangdx
commited on
Commit
·
ca6e075
1
Parent(s):
28e64e8
Add log support for OpenAI demo
Browse files
examples/lightrag_openai_demo.py
CHANGED
@@ -1,11 +1,76 @@
|
|
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 |
|
@@ -61,4 +126,6 @@ async def main():
|
|
61 |
|
62 |
|
63 |
if __name__ == "__main__":
|
|
|
|
|
64 |
asyncio.run(main())
|
|
|
1 |
import os
|
2 |
import asyncio
|
3 |
+
import logging
|
4 |
+
import logging.config
|
5 |
from lightrag import LightRAG, QueryParam
|
6 |
from lightrag.llm.openai import gpt_4o_mini_complete, openai_embed
|
7 |
from lightrag.kg.shared_storage import initialize_pipeline_status
|
8 |
+
from lightrag.utils import logger, set_verbose_debug
|
9 |
|
10 |
WORKING_DIR = "./dickens"
|
11 |
|
12 |
+
def configure_logging():
|
13 |
+
"""Configure logging for the application"""
|
14 |
+
|
15 |
+
# Reset any existing handlers to ensure clean configuration
|
16 |
+
for logger_name in ["uvicorn", "uvicorn.access", "uvicorn.error", "lightrag"]:
|
17 |
+
logger_instance = logging.getLogger(logger_name)
|
18 |
+
logger_instance.handlers = []
|
19 |
+
logger_instance.filters = []
|
20 |
+
|
21 |
+
# Get log directory path from environment variable or use current directory
|
22 |
+
log_dir = os.getenv("LOG_DIR", os.getcwd())
|
23 |
+
log_file_path = os.path.abspath(os.path.join(log_dir, "lightrag_demo.log"))
|
24 |
+
|
25 |
+
print(f"\nLightRAG demo log file: {log_file_path}\n")
|
26 |
+
os.makedirs(os.path.dirname(log_dir), exist_ok=True)
|
27 |
+
|
28 |
+
# Get log file max size and backup count from environment variables
|
29 |
+
log_max_bytes = int(os.getenv("LOG_MAX_BYTES", 10485760)) # Default 10MB
|
30 |
+
log_backup_count = int(os.getenv("LOG_BACKUP_COUNT", 5)) # Default 5 backups
|
31 |
+
|
32 |
+
logging.config.dictConfig(
|
33 |
+
{
|
34 |
+
"version": 1,
|
35 |
+
"disable_existing_loggers": False,
|
36 |
+
"formatters": {
|
37 |
+
"default": {
|
38 |
+
"format": "%(levelname)s: %(message)s",
|
39 |
+
},
|
40 |
+
"detailed": {
|
41 |
+
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
42 |
+
},
|
43 |
+
},
|
44 |
+
"handlers": {
|
45 |
+
"console": {
|
46 |
+
"formatter": "default",
|
47 |
+
"class": "logging.StreamHandler",
|
48 |
+
"stream": "ext://sys.stderr",
|
49 |
+
},
|
50 |
+
"file": {
|
51 |
+
"formatter": "detailed",
|
52 |
+
"class": "logging.handlers.RotatingFileHandler",
|
53 |
+
"filename": log_file_path,
|
54 |
+
"maxBytes": log_max_bytes,
|
55 |
+
"backupCount": log_backup_count,
|
56 |
+
"encoding": "utf-8",
|
57 |
+
},
|
58 |
+
},
|
59 |
+
"loggers": {
|
60 |
+
"lightrag": {
|
61 |
+
"handlers": ["console", "file"],
|
62 |
+
"level": "INFO",
|
63 |
+
"propagate": False,
|
64 |
+
},
|
65 |
+
},
|
66 |
+
}
|
67 |
+
)
|
68 |
+
|
69 |
+
# Set the logger level to INFO
|
70 |
+
logger.setLevel(logging.INFO)
|
71 |
+
# Enable verbose debug if needed
|
72 |
+
set_verbose_debug(os.getenv("VERBOSE_DEBUG", "false").lower() == "true")
|
73 |
+
|
74 |
if not os.path.exists(WORKING_DIR):
|
75 |
os.mkdir(WORKING_DIR)
|
76 |
|
|
|
126 |
|
127 |
|
128 |
if __name__ == "__main__":
|
129 |
+
# Configure logging before running the main function
|
130 |
+
configure_logging()
|
131 |
asyncio.run(main())
|