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.

53 lines
2.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import asyncio
import inspect
import os
from Config.Config import NEO4J_URI, NEO4J_USERNAME, NEO4J_PASSWORD
from Util.LightRagUtil import configure_logging, initialize_rag, print_stream
from lightrag import QueryParam
# 化学
data = [
# {"NAME": "Chemistry", "Q": "硝酸光照分解的化学反应方程式是什么", "ChineseName": "化学"},
{"NAME": "Chemistry", "Q": "氢气与氧气燃烧的现象", "ChineseName": "化学"},
{"NAME": "Math", "Q": "氧化铁与硝酸的化学反应方程式是什么", "ChineseName": "数学"},
{"NAME": "Chinese", "Q": "氧化铁与硝酸的化学反应方程式是什么", "ChineseName": "语文"}]
# 准备查询的科目
KEMU = "Chemistry"
# 查找索引号
idx = [i for i, d in enumerate(data) if d["NAME"] == KEMU][0]
async def main():
# 设置Neo4j连接参数
os.environ["NEO4J_URI"] = NEO4J_URI
os.environ["NEO4J_USERNAME"] = NEO4J_USERNAME
os.environ["NEO4J_PASSWORD"] = NEO4J_PASSWORD
try:
user_prompt = "\n 1、资料中提供化学反应方程式的一定要严格按提供的Latex公式输出绝对不允许对Latex公式进行修改 "
user_prompt = user_prompt + "\n 2、如果资料中提供了图片的一定要严格按照原文提供图片输出不允许省略或不输出"
user_prompt = user_prompt + "\n 3、资料中提到的知识内容需要判断是否与本次问题相关不相关的绝对不要输出"
rag = await initialize_rag('Topic/' + data[idx]["NAME"],graph_storage="Neo4JStorage") # 加上使用Neo4JStorage
resp = await rag.aquery(
data[idx]["Q"],
param=QueryParam(mode="hybrid", stream=True, user_prompt=user_prompt),
# 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())