新增单元测试
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
|
||||
import os,json,asyncio,nest_asyncio
|
||||
from typing import Dict
|
||||
from app.engine import get_chat_engine
|
||||
from app.observability import init_observability
|
||||
from app.settings import init_settings
|
||||
|
||||
init_settings()
|
||||
init_observability()
|
||||
nest_asyncio.apply()
|
||||
|
||||
# 开始答题
|
||||
async def run_answer(prjflag:str,title:str,content:Dict[str,str], output_file:str):
|
||||
results = []
|
||||
for question,answer in content.items():
|
||||
param = {'prjFalg':prjflag}
|
||||
chat_engine = get_chat_engine(params = param)
|
||||
response = await chat_engine.astream_chat(question)
|
||||
|
||||
final_response = ''
|
||||
async for token in response.async_response_gen():
|
||||
final_response += token
|
||||
|
||||
if final_response!=' ':
|
||||
content_str = final_response
|
||||
else:
|
||||
content_str = "<无回答>"
|
||||
|
||||
result_dict = {
|
||||
"问题": question,
|
||||
"答案": answer,
|
||||
"回答": content_str
|
||||
}
|
||||
results.append(result_dict)
|
||||
|
||||
outInfo = {f'{title}':results}
|
||||
with open(output_file, 'a', encoding='utf-8') as f:
|
||||
f.write(json.dumps(outInfo, ensure_ascii=False, indent=4))
|
||||
f.write(',\n')
|
||||
|
||||
# 主异步函数
|
||||
async def excute(prjflag:str,filePath:str,outFilePath:str):
|
||||
with open(filePath, 'r', encoding='utf-8') as f:
|
||||
data:dict= json.load(f)
|
||||
for title, items in data.items():
|
||||
content = {}
|
||||
for index, item in enumerate(items, start=1):
|
||||
question = item['question']
|
||||
answer = item['answer']
|
||||
content[question] = answer
|
||||
await run_answer(prjflag,title,content,outFilePath)
|
||||
|
||||
async def main():
|
||||
que_Dir = os.path.join(os.getcwd(),f'unit_test\\Quetions')
|
||||
ans_dir = os.path.join(os.getcwd(),f'unit_test\\Answers')
|
||||
que_files = [f for f in os.listdir(que_Dir) if f.endswith('.json')]
|
||||
for que_file in que_files:
|
||||
prjflag = os.path.splitext(que_file)[0]
|
||||
filePath =os.path.join(que_Dir,que_file)
|
||||
os.makedirs(ans_dir,exist_ok = True)
|
||||
output_file_path = os.path.join(ans_dir,f'{que_file}')
|
||||
await excute(prjflag,filePath,output_file_path)
|
||||
|
||||
# 运行主协程
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user