You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.7 KiB
90 lines
2.7 KiB
2 weeks ago
|
import asyncio
|
||
|
import inspect
|
||
|
import logging
|
||
|
import os
|
||
|
|
||
|
from lightrag.kg.shared_storage import initialize_pipeline_status
|
||
|
from lightrag.utils import EmbeddingFunc
|
||
|
|
||
|
from Config.Config import LLM_MODEL_NAME, EMBED_DIM, EMBED_MAX_TOKEN_SIZE
|
||
|
from Util.LightRagUtil import configure_logging, initialize_rag, print_stream, llm_model_func, embedding_func
|
||
|
from lightrag import QueryParam, LightRAG
|
||
|
|
||
|
# 在程序开始时添加以下配置
|
||
|
logging.basicConfig(
|
||
|
level=logging.INFO, # 设置日志级别为INFO
|
||
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||
|
)
|
||
|
|
||
|
# 或者如果你想更详细地控制日志输出
|
||
|
logger = logging.getLogger('lightrag')
|
||
|
logger.setLevel(logging.INFO)
|
||
|
handler = logging.StreamHandler()
|
||
|
handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
|
||
|
logger.addHandler(handler)
|
||
|
|
||
|
ROOT_DIR = '.'
|
||
|
WORKING_DIR = f"{ROOT_DIR}/dickens-pg"
|
||
|
|
||
|
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)
|
||
|
|
||
|
if not os.path.exists(WORKING_DIR):
|
||
|
os.mkdir(WORKING_DIR)
|
||
|
|
||
|
# AGE
|
||
|
os.environ["AGE_GRAPH_NAME"] = "dickens"
|
||
|
os.environ["POSTGRES_HOST"] = "10.10.14.208"
|
||
|
os.environ["POSTGRES_PORT"] = "5432"
|
||
|
os.environ["POSTGRES_USER"] = "postgres"
|
||
|
os.environ["POSTGRES_PASSWORD"] = "postgres"
|
||
|
os.environ["POSTGRES_DATABASE"] = "rag"
|
||
|
|
||
|
|
||
|
async def initialize_rag():
|
||
|
rag = LightRAG(
|
||
|
working_dir=WORKING_DIR,
|
||
|
llm_model_func=llm_model_func,
|
||
|
llm_model_name=LLM_MODEL_NAME,
|
||
|
llm_model_max_async=4,
|
||
|
llm_model_max_token_size=32768,
|
||
|
enable_llm_cache_for_entity_extract=True,
|
||
|
embedding_func=EmbeddingFunc(
|
||
|
embedding_dim=EMBED_DIM,
|
||
|
max_token_size=EMBED_MAX_TOKEN_SIZE,
|
||
|
func=embedding_func
|
||
|
),
|
||
|
kv_storage="PGKVStorage",
|
||
|
doc_status_storage="PGDocStatusStorage",
|
||
|
graph_storage="PGGraphStorage",
|
||
|
vector_storage="PGVectorStorage",
|
||
|
auto_manage_storages_states=False,
|
||
|
)
|
||
|
|
||
|
await rag.initialize_storages()
|
||
|
await initialize_pipeline_status()
|
||
|
|
||
|
return rag
|
||
|
|
||
|
async def main():
|
||
|
try:
|
||
|
rag = await initialize_rag()
|
||
|
resp = await rag.aquery(
|
||
|
"What are the top themes in this story",
|
||
|
param=QueryParam(mode="hybrid", stream=True),
|
||
|
# hybrid naive
|
||
|
)
|
||
|
if inspect.isasyncgen(resp):
|
||
|
await print_stream(resp)
|
||
|
else:
|
||
|
print(resp)
|
||
|
except Exception as e:
|
||
|
print(f"An error occurred: {e}")
|
||
|
finally:
|
||
|
if rag:
|
||
|
await rag.finalize_storages()
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
configure_logging()
|
||
|
asyncio.run(main())
|