Spaces:
Runtime error
Runtime error
root commited on
Commit ·
73386d5
1
Parent(s): 3692eb0
add_function_pmids_retrieval_and_ref_ench_sent
Browse filesFiles changed (9)
- README.md+5-3
- app.py+30-11
- applocal.pyAdded+663
- config-bak.ini+1
- config-mod_down-bak.iniAdded+64
- huixiangdou/service/findarticles.py+75-16
- huixiangdou/service/llm_server_hybrid.py+4
- huixiangdou/service/worker.py+30-18
- run.sh+2-2
13 unmodified lines14151617181961 unmodified lines818283848586878 unmodified lines9697989910010110267 unmodified lines17017117217317417517613 unmodified lines- 整一个帮我写综述的Agent,希望他能完成文献内容的收集,文本分类和总结,科学事实对比,撰写综述等功能- 计划用到RAG, function calling等技术- 还在不断摸索中,欢迎大佬指导!## 流程图基本上就是在上海AIlab的茴香豆上面改的 这里主要讲解使用流程 架构和茴香豆一样 [茴香豆架构](https://github.com/InternLM/HuixiangDou/blob/main/docs/architecture_zh.md)61 unmodified linescd MedicalReviewAgentpip install -r requirements.txt```huggingface-cli下载模型```bashcd /root && mkdir models8 unmodified lines```bashconda activate ReviewAgentcd MedicalReviewAgentpython3 app.py```gradio在本地7860端口运行67 unmodified lines## 感谢1. [茴香豆](https://github.com/InternLM/HuixiangDou)2. [E-utilities](https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pmc&id=PMCID)3. [Ragflow](https://github.com/infiniflow/ragflow/blob/main/README_zh.md)4. [Advanced RAG pipeline](https://medium.aiplanet.com/evaluating-naive-rag-and-advanced-rag-pipeline-using-langchain-v-0-1-0-and-ragas-17d24e74e5cf)13 unmodified lines1415161718192061 unmodified lines828384858687888 unmodified lines97989910010110210310467 unmodified lines17217317417517617717813 unmodified lines- 整一个帮我写综述的Agent,希望他能完成文献内容的收集,文本分类和总结,科学事实对比,撰写综述等功能- 计划用到RAG, function calling等技术- 还在不断摸索中,欢迎大佬指导!- [huggingface 体验链接](https://huggingface.co/spaces/Yijun-Yang/ReadReview/), zeroGPUs 比较吝啬 我把本地推理给阉割了 不要用本地模型哈 用API 用本地模型会报错## 流程图基本上就是在上海AIlab的茴香豆上面改的 这里主要讲解使用流程 架构和茴香豆一样 [茴香豆架构](https://github.com/InternLM/HuixiangDou/blob/main/docs/architecture_zh.md)61 unmodified linescd MedicalReviewAgentpip install -r requirements.txt```huggingface-cli下载模型(optional, 第一次调用的时候hf会下载,但是可能有墙)```bashcd /root && mkdir models8 unmodified lines```bashconda activate ReviewAgentcd MedicalReviewAgentpython3 app.py --model_downloaded True # 如果已经在/root/models下载了模型 这个参数会换一个配置文件,里面的modelpath是本地路径不是hf的仓库路径 自己显卡跑跑用这个python3 app.py # 如果不打算用本地/root/models储存的模型 这是hf的spaces的构建配置```gradio在本地7860端口运行67 unmodified lines## 感谢1. [茴香豆](https://github.com/InternLM/HuixiangDou)2. [E-utilities](https://www.ncbi.nlm.nih.gov/books/NBK25499/)3. [Ragflow](https://github.com/infiniflow/ragflow/blob/main/README_zh.md)4. [Advanced RAG pipeline](https://medium.aiplanet.com/evaluating-naive-rag-and-advanced-rag-pipeline-using-langchain-v-0-1-0-and-ragas-17d24e74e5cf)
41 unmodified lines4243444546477 unmodified lines55565758596019 unmodified lines808182838485864 unmodified lines91929394959691 unmodified lines18818919019119219319419519619719813 unmodified lines212213214215216217218181 unmodified lines40040140240340440540640728 unmodified lines43643743843944044144244344444544644744829 unmodified lines478479480481482483109 unmodified lines59359459559659759859920 unmodified lines62062162262362462562662741 unmodified linesaction='store_true',default=True,help='Auto deploy required Hybrid LLM Service.')args = parser.parse_args()return args7 unmodified linesinteractive=True,visible=True),gr.Textbox(label="您的API",lines = 1,interactive=True,visible=True),gr.Dropdown([],label="选择模型",interactive=True,visible=True)]19 unmodified lines}return gr.Dropdown(choices= model_choices[remote_company])def update_remote_config(remote_ornot,remote_company = None,api = None,model = None):with open(CONFIG_PATH, encoding='utf8') as f:config = pytoml.load(f)4 unmodified linesconfig['llm']['enable_remote'] = 1config['llm']['server']['remote_type'] = remote_companyconfig['llm']['server']['remote_api_key'] = apiconfig['llm']['server']['remote_llm_model'] = modelelse:config['llm']['enable_local'] = 191 unmodified linesreturn filesdef generate_articles_repo(keywords:str,retmax:int):keys= [k.strip() for k in keywords.split('\n')]repodir, _, _ = get_ready('repo_work')articelfinder = ArticleRetrieval(keywords = keys,repo_dir = repodir,retmax = retmax)articelfinder.initiallize()13 unmodified linesdef update_repo():keys,len,retmax,pdflen = update_repo_info()if keys:newinfo = f"搜索得到文献:\n 关键词:{keys}\n 文献数量:{len}\n 获取上限:{retmax}\n\n上传文献:\n 数量:{pdflen}"else:if pdflen:181 unmodified lineslogger.info(f'{code}, {query}, {reply}, {references}')urls = getpmcurls(references)mds = '\n'.join([f'[{ref}]({url})' for ref,url in zip(references,urls)])return reply, gr.Markdown(label="参考文献",value = mds)def main_interface():with gr.Blocks() as demo:28 unmodified linesremote_company = gr.Dropdown(["kimi", "deepseek", "zhipuai",'gpt'],label="选择大模型提供商",interactive=False,visible=False)api = gr.Textbox(label="您的API",lines = 1,interactive=False,visible=False)model = gr.Dropdown([],label="选择模型",interactive=False,visible=False)confirm_button = gr.Button("保存配置")remote_ornot.change(update_remote_buttons, inputs=[remote_ornot],outputs=[apimd,remote_company,api,model])remote_company.change(udate_model_dropdown, inputs=[remote_company],outputs=[model])confirm_button.click(update_remote_config, inputs=[remote_ornot,remote_company,api,model],outputs=[confirm_button])with gr.Tab("文献查找+数据库生成"):29 unmodified lineswith gr.Row(equal_height=True):with gr.Column(scale=1):input_keys = gr.Textbox(label="感兴趣的关键词",lines = 5)retmax = gr.Slider(minimum=0,109 unmodified linesquery = gr.Textbox(label="想写什么")write_button = gr.Button("写综述")output_text = gr.Textbox(label="看看",lines=10)output_references = gr.Markdown(label="参考文献")update_options.click(update_chunksize_dropdown,20 unmodified lines# start serviceif __name__ == '__main__':args = parse_args()# copy config from config-bakshutil.copy('config-bak.ini', args.config_path) # yyjCONFIG_PATH = args.config_pathif args.standalone is True:41 unmodified lines424344454647484950517 unmodified lines596061626364656619 unmodified lines868788899091924 unmodified lines97989910010110210391 unmodified lines19519619719819920020120220320420520620720820921021113 unmodified lines225226227228229230231181 unmodified lines41341441541641741841942028 unmodified lines44945045145245345445545645745845946046146229 unmodified lines492493494495496497498109 unmodified lines60860961061161261361420 unmodified lines63563663763863964064164264364464564641 unmodified linesaction='store_true',default=True,help='Auto deploy required Hybrid LLM Service.')parser.add_argument("--model_downloaded",type=bool,default=False,help="If the model has been downloaded in the root/models folder. Default is False.")args = parser.parse_args()return args7 unmodified linesinteractive=True,visible=True),gr.Textbox(label="您的API",lines = 1,interactive=True,visible=True),gr.Textbox(label="base url",lines = 1,interactive=True,visible=True),gr.Dropdown([],label="选择模型",interactive=True,visible=True)]19 unmodified lines}return gr.Dropdown(choices= model_choices[remote_company])def update_remote_config(remote_ornot,remote_company = None,api = None,baseurl = None, model = None):with open(CONFIG_PATH, encoding='utf8') as f:config = pytoml.load(f)4 unmodified linesconfig['llm']['enable_remote'] = 1config['llm']['server']['remote_type'] = remote_companyconfig['llm']['server']['remote_api_key'] = apiconfig['llm']['server']['remote_base_url'] = baseurlconfig['llm']['server']['remote_llm_model'] = modelelse:config['llm']['enable_local'] = 191 unmodified linesreturn filesdef generate_articles_repo(strings:str,retmax:int):string = [k.strip() for k in strings.split('\n')]pmids = [k for k in string if k.isdigit()]keys = [k for k in string if not k.isdigit()]repodir, _, _ = get_ready('repo_work')articelfinder = ArticleRetrieval(keywords = keys,pmids = pmids,repo_dir = repodir,retmax = retmax)articelfinder.initiallize()13 unmodified linesdef update_repo():keys,len,retmax,pdflen = update_repo_info()if keys or len:newinfo = f"搜索得到文献:\n 关键词:{keys}\n 文献数量:{len}\n 获取上限:{retmax}\n\n上传文献:\n 数量:{pdflen}"else:if pdflen:181 unmodified lineslogger.info(f'{code}, {query}, {reply}, {references}')urls = getpmcurls(references)mds = '\n\n'.join([f'[{ref}]({url})' for ref,url in zip(references,urls)])return gr.Markdown(label="看看",value = reply,line_breaks=True) , gr.Markdown(label="参考文献",value = mds,line_breaks=True)def main_interface():with gr.Blocks() as demo:28 unmodified linesremote_company = gr.Dropdown(["kimi", "deepseek", "zhipuai",'gpt'],label="选择大模型提供商",interactive=False,visible=False)api = gr.Textbox(label="您的API",lines = 1,interactive=False,visible=False)baseurl = gr.Textbox(label="base url",lines = 1,interactive=False,visible=False)model = gr.Dropdown([],label="选择模型",interactive=False,visible=False)confirm_button = gr.Button("保存配置")remote_ornot.change(update_remote_buttons, inputs=[remote_ornot],outputs=[apimd,remote_company,api,baseurl,model])remote_company.change(udate_model_dropdown, inputs=[remote_company],outputs=[model])confirm_button.click(update_remote_config, inputs=[remote_ornot,remote_company,api,baseurl,model],outputs=[confirm_button])with gr.Tab("文献查找+数据库生成"):29 unmodified lineswith gr.Row(equal_height=True):with gr.Column(scale=1):input_keys = gr.Textbox(label="感兴趣的关键词",value = "输入关键词或者PMID, 换行分隔",lines = 5)retmax = gr.Slider(minimum=0,109 unmodified linesquery = gr.Textbox(label="想写什么")write_button = gr.Button("写综述")output_text = gr.Markdown(label="看看")output_references = gr.Markdown(label="参考文献")update_options.click(update_chunksize_dropdown,20 unmodified lines# start serviceif __name__ == '__main__':args = parse_args()# copy config from config-bakif args.model_downloaded:shutil.copy('config-mod_down-bak.ini', args.config_path) # yyjelse:shutil.copy('config-bak.ini', args.config_path) # yyjCONFIG_PATH = args.config_pathif args.standalone is True:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663import argparseimport jsonimport timeimport osimport globimport randomimport shutilfrom enum import Enumfrom threading import Threadfrom multiprocessing import Process, Value
import gradio as grimport pytomlfrom loguru import logger# import spaces
from huixiangdou.service import Worker, llm_serve, ArticleRetrieval, CacheRetriever, FeatureStore, FileOperation
class PARAM_CODE(Enum): """Parameter code.""" SUCCESS = 0 FAILED = 1 ERROR = 2
def parse_args(): """Parse args.""" parser = argparse.ArgumentParser(description='Worker.') parser.add_argument('--work_dir', type=str, default='workdir', help='Working directory.') parser.add_argument('--repo_dir', type=str, default='repodir', help='Repository directory.') parser.add_argument( '--config_path', default='config.ini', type=str, help='Worker configuration path. Default value is config.ini') parser.add_argument('--standalone', action='store_true', default=True, help='Auto deploy required Hybrid LLM Service.') parser.add_argument("--model_downloaded", type=bool, default=False, help="If the model has been downloaded in the root/models folder. Default is False.") args = parser.parse_args() return args
def update_remote_buttons(remote): if remote: return [ gr.Markdown("[如何配置API]('https://github.com/jabberwockyang/MedicalReviewAgent/blob/main/README.md')", visible=True), gr.Dropdown(["kimi", "deepseek", "zhipuai",'gpt'], label="选择大模型提供商", interactive=True,visible=True), gr.Textbox(label="您的API",lines = 1, interactive=True,visible=True), gr.Textbox(label="base url",lines = 1, interactive=True,visible=True), gr.Dropdown([],label="选择模型", interactive=True,visible=True) ] else: return [ gr.Markdown("[如何配置API]('https://github.com/jabberwockyang/MedicalReviewAgent/blob/main/README.md')", visible=False), gr.Dropdown(["kimi", "deepseek", "zhipuai",'gpt'], label="选择大模型提供商", interactive=False,visible=False), gr.Textbox(label="您的API",lines = 1, interactive=False,visible=False), gr.Dropdown([],label="选择模型", interactive=False,visible=False) ]
def udate_model_dropdown(remote_company): model_choices = { 'kimi': ['moonshot-v1-128k'], 'deepseek': ['deepseek-chat'], 'zhipuai': ['glm-4'], 'gpt': ['gpt-4-32k-0613','gpt-3.5-turbo'] } return gr.Dropdown(choices= model_choices[remote_company])
def update_remote_config(remote_ornot,remote_company = None,api = None,baseurl = None, model = None): with open(CONFIG_PATH, encoding='utf8') as f: config = pytoml.load(f) if remote_ornot: if remote_company == None or api == None or model == None: raise ValueError('remote_company, api, model not provided') config['llm']['enable_local'] = 0 config['llm']['enable_remote'] = 1 config['llm']['server']['remote_type'] = remote_company config['llm']['server']['remote_api_key'] = api config['llm']['server']['remote_base_url'] = baseurl config['llm']['server']['remote_llm_model'] = model else: config['llm']['enable_local'] = 1 config['llm']['enable_remote'] = 0 with open(CONFIG_PATH, 'w') as f: pytoml.dump(config, f) return gr.Button("配置已保存")
# @spaces.GPU(duration=360)def get_ready(query:str,chunksize=None,k=None): with open(CONFIG_PATH, encoding='utf8') as f: config = pytoml.load(f) workdir = config['feature_store']['work_dir'] repodir = config['feature_store']['repo_dir']
if query == 'repo_work': # no need to return assistant return repodir, workdir, config theme = '' try: with open(os.path.join(config['feature_store']['repo_dir'],'config.json'), 'r') as f: repo_config = json.load(f) theme = ' '.join(repo_config['keywords']) except: pass
if query == 'annotation': if not chunksize or not k: raise ValueError('chunksize or k not provided') chunkdir = os.path.join(workdir, f'chunksize_{chunksize}') clusterdir = os.path.join(chunkdir, 'cluster_features', f'cluster_features_{k}') assistant = Worker(work_dir=chunkdir, config_path=CONFIG_PATH,language='en') samples_json = os.path.join(clusterdir,'samples.json') with open(samples_json, 'r') as f: samples = json.load(f) f.close() return clusterdir, samples, assistant, theme elif query == 'inspiration': if not chunksize or not k: raise ValueError('chunksize or k not provided') chunkdir = os.path.join(workdir, f'chunksize_{chunksize}') clusterdir = os.path.join(chunkdir, 'cluster_features', f'cluster_features_{k}') assistant = Worker(work_dir=chunkdir, config_path=CONFIG_PATH,language='en') annofile = os.path.join(clusterdir,'annotation.jsonl') with open(annofile, 'r') as f: annoresult = f.readlines()
f.close() annoresult = [json.loads(obj) for obj in annoresult] return clusterdir, annoresult, assistant, theme elif query == 'summarize': # no need for params k if not chunksize: raise ValueError('chunksize not provided') chunkdir = os.path.join(workdir, f'chunksize_{chunksize}') assistant = Worker(work_dir=chunkdir, config_path=CONFIG_PATH,language='en') return assistant,theme
else: raise ValueError('query not recognized') def update_repo_info(): with open(CONFIG_PATH, encoding='utf8') as f: config = pytoml.load(f) repodir = config['feature_store']['repo_dir'] if os.path.exists(repodir): pdffiles = glob.glob(os.path.join(repodir, '*.pdf')) number_of_pdf = len(pdffiles) if os.path.exists(os.path.join(repodir,'config.json')): with open(os.path.join(repodir,'config.json'), 'r') as f: repo_config = json.load(f)
keywords = repo_config['keywords'] length = repo_config['len'] retmax = repo_config['retmax']
return keywords,length,retmax,number_of_pdf else: return None,None,None,number_of_pdf else: return None,None,None,None def upload_file(files): repodir, workdir, _ = get_ready('repo_work') if not os.path.exists(repodir): os.makedirs(repodir)
for file in files: destination_path = os.path.join(repodir, os.path.basename(file.name))
shutil.copy(file.name, destination_path)
return files
def generate_articles_repo(strings:str,retmax:int): string = [k.strip() for k in strings.split('\n')]
pmids = [k for k in string if k.isdigit()] keys = [k for k in string if not k.isdigit()] repodir, _, _ = get_ready('repo_work')
articelfinder = ArticleRetrieval(keywords = keys, pmids = pmids, repo_dir = repodir, retmax = retmax) articelfinder.initiallize() return update_repo()
def delete_articles_repo(): # 在这里运行生成数据库的函数 repodir, workdir, _ = get_ready('repo_work') if os.path.exists(repodir): shutil.rmtree(repodir) if os.path.exists(workdir): shutil.rmtree(workdir)
return gr.Textbox(label="文献库概况",lines =3, value = '文献库和相关数据库已删除', visible = True)
def update_repo(): keys,len,retmax,pdflen = update_repo_info() if keys or len: newinfo = f"搜索得到文献:\n 关键词:{keys}\n 文献数量:{len}\n 获取上限:{retmax}\n\n上传文献:\n 数量:{pdflen}" else: if pdflen: newinfo = f'搜索得到文献:无\n上传文献:\n 数量:{pdflen}' else: newinfo = '目前还没有文献库'
return gr.Textbox(label="文献库概况",lines =1, value = newinfo, visible = True)
def update_database_info(): with open(CONFIG_PATH, encoding='utf8') as f: config = pytoml.load(f) workdir = config['feature_store']['work_dir'] chunkdirs = glob.glob(os.path.join(workdir, 'chunksize_*')) chunkdirs.sort() list_of_chunksize = [int(chunkdir.split('_')[-1]) for chunkdir in chunkdirs] # print(list_of_chunksize) jsonobj = {} for chunkdir in chunkdirs: k_dir = glob.glob(os.path.join(chunkdir, 'cluster_features','cluster_features_*')) k_dir.sort() list_of_k = [int(k.split('_')[-1]) for k in k_dir] jsonobj[int(chunkdir.split('_')[-1])] = list_of_k
new_options = [f"chunksize:{chunksize}, k:{k}" for chunksize in list_of_chunksize for k in jsonobj[chunksize]] return new_options, jsonobj
# @spaces.GPU(duration=360)def generate_database(chunksize:int,nclusters:str|list[str]): # 在这里运行生成数据库的函数 repodir, workdir, _ = get_ready('repo_work') if not os.path.exists(repodir): return gr.Textbox(label="数据库已生成",value = '请先生成文献库',visible = True) nclusters = [int(i) for i in nclusters] # 文献库和数据库的覆盖删除逻辑待定 # 理论上 文献库只能生成一次 所以每次生成文献库都要删除之前的文献库和数据库 # 数据库可以根据文献库多次生成 暂不做删除 目前没有节省算力的逻辑 重复计算后覆盖 以后优化 # 不同的chunksize和nclusters会放在不同的文件夹下 不会互相覆盖 # if os.path.exists(workdir): # shutil.rmtree(workdir)
cache = CacheRetriever(config_path=CONFIG_PATH) fs_init = FeatureStore(embeddings=cache.embeddings, reranker=cache.reranker, chunk_size=chunksize, n_clusters=nclusters, config_path=CONFIG_PATH)
# walk all files in repo dir file_opr = FileOperation() files = file_opr.scan_dir(repo_dir=repodir) fs_init.initialize(files=files, work_dir=workdir,file_opr=file_opr) file_opr.summarize(files) del fs_init cache.pop('default') texts, _ = update_database_info() return gr.Textbox(label="数据库概况",value = '\n'.join(texts) ,visible = True)
def delete_database(): _, workdir, _ = get_ready('repo_work') if os.path.exists(workdir): shutil.rmtree(workdir) return gr.Textbox(label="数据库概况",lines =3,value = '数据库已删除',visible = True)
def update_database_textbox(): texts, _ = update_database_info() if texts == []: return gr.Textbox(label="数据库概况",value = '目前还没有数据库',visible = True) else: return gr.Textbox(label="数据库概况",value = '\n'.join(texts),visible = True)
def update_chunksize_dropdown(): _, jsonobj = update_database_info() return gr.Dropdown(choices= jsonobj.keys())
def update_ncluster_dropdown(chunksize:int): _, jsonobj = update_database_info() nclusters = jsonobj[chunksize] return gr.Dropdown(choices= nclusters)
# @spaces.GPU(duration=360)def annotation(n,chunksize:int,nclusters:int,remote_ornot:bool): ''' use llm to annotate cluster n: percentage of clusters to annotate ''' query = 'annotation' if remote_ornot: backend = 'remote' else: backend = 'local'
clusterdir, samples, assistant, theme = get_ready('annotation',chunksize,nclusters) new_obj_list = [] n = round(n * len(samples.keys())) for cluster_no in random.sample(samples.keys(), n): chunk = '\n'.join(samples[cluster_no]['samples'][:10])
code, reply, cluster_no = assistant.annotate_cluster( theme = theme, cluster_no=cluster_no, chunk=chunk, history=[], groupname='', backend=backend) references = f"cluster_no: {cluster_no}" new_obj = { 'cluster_no': cluster_no, 'chunk': chunk, 'annotation': reply } new_obj_list.append(new_obj) logger.info(f'{code}, {query}, {reply}, {references}')
with open(os.path.join(clusterdir, 'annotation.jsonl'), 'a') as f: json.dump(new_obj, f, ensure_ascii=False) f.write('\n') return '\n\n'.join([obj['annotation'] for obj in new_obj_list])
# @spaces.GPU(duration=360)def inspiration(annotation:str,chunksize:int,nclusters:int,remote_ornot:bool): query = 'inspiration' if remote_ornot: backend = 'remote' else: backend = 'local' clusterdir, annoresult, assistant, theme = get_ready('inspiration',chunksize,nclusters) new_obj_list = []
if annotation is not None: # if the user wants to get inspiration from specific clusters only annoresult = [obj for obj in annoresult if obj['annotation'] in [txt.strip() for txt in annotation.split('\n')]] for index in random.sample(range(len(annoresult)), min(5, len(annoresult))): cluster_no = annoresult[index]['cluster_no'] chunks = annoresult[index]['annotation'] code, reply = assistant.getinspiration( theme = theme, annotations = chunks, history=[], groupname='',backend=backend) new_obj = { 'inspiration': reply, 'cluster_no': cluster_no } new_obj_list.append(new_obj) logger.info(f'{code}, {query}, {cluster_no},{reply}')
with open(os.path.join(clusterdir, 'inspiration.jsonl'), 'a') as f: json.dump(new_obj, f, ensure_ascii=False) with open(os.path.join(clusterdir, 'inspiration.txt'), 'a') as f: f.write(f'{reply}\n') return '\n\n'.join(list(set([obj['inspiration'] for obj in new_obj_list])))
def getpmcurls(references): urls = [] for ref in references: if ref.startswith('PMC'): refid = ref.replace('.txt','') urls.append(f'https://www.ncbi.nlm.nih.gov/pmc/articles/{refid}/') else: urls.append(ref) return urls # @spaces.GPU(duration=360)def summarize_text(query,chunksize:int,remote_ornot:bool): if remote_ornot: backend = 'remote' else: backend = 'local' assistant,_ = get_ready('summarize',chunksize=chunksize,k=None) code, reply, references = assistant.generate(query=query, history=[], groupname='',backend = backend) logger.info(f'{code}, {query}, {reply}, {references}') urls = getpmcurls(references) mds = '\n\n'.join([f'[{ref}]({url})' for ref,url in zip(references,urls)]) return gr.Markdown(label="看看",value = reply,line_breaks=True) , gr.Markdown(label="参考文献",value = mds,line_breaks=True)
def main_interface(): with gr.Blocks() as demo: with gr.Row(): gr.Markdown( """ # 医学文献综述助手 (又名 不想看文献) """ )
with gr.Tab("模型服务配置"): gr.Markdown(""" #### 配置模型服务 🛠️
1. **是否使用远程大模型** - 勾选此项,如果你想使用远程的大模型服务。 - 如果不勾选,将默认使用本地模型服务。
2. **API配置** - 配置大模型提供商和API,确保模型服务能够正常运行。 - 提供商选择:kimi、deepseek、zhipuai、gpt。 - 输入您的API密钥和选择对应模型。 - 点击“保存配置”按钮以保存您的设置。
📝 **备注**:请参考[如何使用]('https://github.com/jabberwockyang/MedicalReviewAgent/blob/main/README.md')获取更多信息。
""")
remote_ornot = gr.Checkbox(label="是否使用远程大模型") with gr.Accordion("API配置", open=True): apimd = gr.Markdown("[如何配置API]('https://github.com/jabberwockyang/MedicalReviewAgent/blob/main/README.md')",visible=False) remote_company = gr.Dropdown(["kimi", "deepseek", "zhipuai",'gpt'], label="选择大模型提供商",interactive=False,visible=False) api = gr.Textbox(label="您的API",lines = 1,interactive=False,visible=False) baseurl = gr.Textbox(label="base url",lines = 1,interactive=False,visible=False) model = gr.Dropdown([],label="选择模型",interactive=False,visible=False) confirm_button = gr.Button("保存配置")
remote_ornot.change(update_remote_buttons, inputs=[remote_ornot],outputs=[apimd,remote_company,api,baseurl,model]) remote_company.change(udate_model_dropdown, inputs=[remote_company],outputs=[model]) confirm_button.click(update_remote_config, inputs=[remote_ornot,remote_company,api,baseurl,model],outputs=[confirm_button])
with gr.Tab("文献查找+数据库生成"): gr.Markdown("""#### 查找文献 📚
1. **输入关键词批量PubMed PMC文献** - 在“感兴趣的关键词”框中输入您感兴趣的关键词,每行一个。 - 设置查找数量(0-1000)。 - 点击“搜索PubMed PMC”按钮进行文献查找。
2. **上传PDF** - 通过“上传PDF”按钮上传您已有的PDF文献文件。
3. **更新文献库情况 删除文献库** - 点击“更新文献库情况”按钮,查看当前文献库的概况。 - 如果需要重置或删除现有文献库,点击“删除文献库”按钮。
#### 生成数据库 🗂️
1. **设置数据库构建参数 生成数据库** - 选择块大小(Chunk Size)和聚类数(Number of Clusters)。 - 提供选项用于选择合适的块大小和聚类数。 - 点击“生成数据库”按钮开始数据库生成过程。
2. **更新数据库情况 删除数据库** - 点击“更新数据库情况”按钮,查看当前数据库的概况。 - 点击“删除数据库”按钮移除现有数据库。
📝 **备注**:请参考[如何选择数据库构建参数]('https://github.com/jabberwockyang/MedicalReviewAgent/tree/main')获取更多信息。""") with gr.Row(equal_height=True): with gr.Column(scale=1): input_keys = gr.Textbox(label="感兴趣的关键词", value = "输入关键词或者PMID, 换行分隔", lines = 5) retmax = gr.Slider( minimum=0, maximum=1000, value=500, interactive=True, label="查多少", ) generate_repo_button = gr.Button("搜索PubMed PMC") with gr.Column(scale=2): file_output = gr.File(scale=2) upload_button = gr.UploadButton("上传PDF", file_types=[".pdf",".csv",".doc"], file_count="multiple",scale=0) with gr.Row(equal_height=True): with gr.Column(scale=0): delete_repo_button = gr.Button("删除文献库") update_repo_button = gr.Button("更新文献库情况") with gr.Column(scale=2):
repo_summary =gr.Textbox(label= '文献库概况', value="目前还没有文献库")
generate_repo_button.click(generate_articles_repo, inputs=[input_keys,retmax], outputs = [repo_summary]) delete_repo_button.click(delete_articles_repo, inputs=None, outputs = repo_summary) update_repo_button.click(update_repo, inputs=None, outputs = repo_summary) upload_button.upload(upload_file, upload_button, file_output) with gr.Accordion("数据库构建参数", open=True): gr.Markdown("[如何选择数据库构建参数]('https://github.com/jabberwockyang/MedicalReviewAgent/tree/main')") chunksize = gr.Slider(label="Chunk Size", info= 'How long you want the chunk to be?', minimum=128, maximum=4096,value=1024,step=1, interactive=True) ncluster = gr.CheckboxGroup(["10", "20", "50", '100','200','500','1000'], # default=["20", "50", '100'], label="Number of Clusters", info="How many Clusters you want to generate")
with gr.Row(): gene_database_button = gr.Button("生成数据库") delete_database_button = gr.Button("删除数据库") update_database_button = gr.Button("更新数据库情况")
database_summary = gr.Textbox(label="数据库概况",lines = 1,value="目前还没有数据库")
gene_database_button.click(generate_database, inputs=[chunksize,ncluster], outputs = database_summary) update_database_button.click(update_database_textbox,inputs=None, outputs = [database_summary]) delete_database_button.click(delete_database, inputs=None, outputs = database_summary) with gr.Tab("写综述"): gr.Markdown("""#### 写综述 ✍️
1. **更新数据库情况** - 点击“更新数据库情况”按钮,确保使用最新的数据库信息。
2. **选择块大小和聚类数** - 从下拉菜单中选择合适的块大小和聚类数。
3. **抽样标注文章聚类** - 设置抽样标注比例(0-1)。 - 点击“抽样标注文章聚类”按钮开始标注过程。
4. **获取灵感** - 如果不知道写什么,点击“获取灵感”按钮。 - 系统将基于标注的文章聚类提供相应的综述子问题。
5. **写综述** - 输入您想写的内容或主题。 - 点击“写综述”按钮,生成综述文本。
6. **查看生成结果** - 生成的综述文本将显示在“看看”文本框中。 - 参考文献将显示在“参考文献”框中。
📝 **备注**:可以尝试不同的参数进行标注和灵感获取,有助于提高综述的质量和相关性。 """)
with gr.Accordion("聚类标注相关参数", open=True): with gr.Row(): update_options = gr.Button("更新数据库情况", scale=0) chunksize = gr.Dropdown([], label="选择块大小", scale=0) nclusters = gr.Dropdown([], label="选择聚类数", scale=0) ntoread = gr.Slider( minimum=0,maximum=1,value=0.5, interactive=True, label="抽样标注比例", )
annotation_button = gr.Button("抽样标注文章聚类") annotation_output = gr.Textbox(label="文章聚类标注/片段摘要", lines = 5, interactive= True, show_copy_button=True) inspiration_button = gr.Button("获取灵感") inspiration_output = gr.Textbox(label="灵光一现", lines = 5, show_copy_button=True)
query = gr.Textbox(label="想写什么") write_button = gr.Button("写综述") output_text = gr.Markdown(label="看看") output_references = gr.Markdown(label="参考文献") update_options.click(update_chunksize_dropdown, outputs=[chunksize]) chunksize.change(update_ncluster_dropdown, inputs=[chunksize], outputs= [nclusters]) annotation_button.click(annotation, inputs = [ntoread, chunksize, nclusters,remote_ornot], outputs=[annotation_output]) inspiration_button.click(inspiration, inputs= [annotation_output, chunksize, nclusters,remote_ornot], outputs=[inspiration_output]) write_button.click(summarize_text, inputs=[query, chunksize,remote_ornot], outputs =[output_text,output_references])
demo.launch(share=False, server_name='0.0.0.0', debug=True,show_error=True,allowed_paths=['img_0.jpg']) # start serviceif __name__ == '__main__': args = parse_args() # copy config from config-bak if args.model_downloaded: shutil.copy('config-mod_down-bak.ini', args.config_path) # yyj else: shutil.copy('config-bak.ini', args.config_path) # yyj
CONFIG_PATH = args.config_path
if args.standalone is True: # hybrid llm serve server_ready = Value('i', 0) server_process = Process(target=llm_serve, args=(args.config_path, server_ready)) server_process.start() while True: if server_ready.value == 0: logger.info('waiting for server to be ready..') time.sleep(3) elif server_ready.value == 1: break else: logger.error('start local LLM server failed, quit.') raise Exception('local LLM path') logger.info('Hybrid LLM Server start.')
main_interface()22 unmodified lines23242526272822 unmodified lineslocal_llm_bind_port = 8888remote_type = ""remote_api_key = ""remote_llm_max_text_length = 32000remote_llm_model = ""rpm = 50022 unmodified lines2324252627282922 unmodified lineslocal_llm_bind_port = 8888remote_type = ""remote_api_key = ""remote_base_url = ""remote_llm_max_text_length = 32000remote_llm_model = ""rpm = 500
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364[feature_store]reject_throttle = 0embedding_model_path = "/root/models/bce-embedding-base_v1"reranker_model_path = "/root/models/bce-reranker-base_v1"repo_dir = "repodir"work_dir = "workdir"n_clusters = [20, 50]chunk_size = 1024
[web_search]x_api_key = "${YOUR-API-KEY}"domain_partial_order = ["openai.com", "pytorch.org", "readthedocs.io", "nvidia.com", "stackoverflow.com", "juejin.cn", "zhuanlan.zhihu.com", "www.cnblogs.com"]save_dir = "logs/web_search_result"
[llm]enable_local = 1enable_remote = 1client_url = "http://127.0.0.1:8888/inference"
[llm.server]local_llm_path = "/root/models/Qwen1.5-7B-Chat"local_llm_max_text_length = 32000local_llm_bind_port = 8888remote_type = ""remote_api_key = ""remote_base_url = ""remote_llm_max_text_length = 32000remote_llm_model = ""rpm = 500
[worker]enable_sg_search = 0save_path = "logs/work.txt"
[worker.time]start = "00:00:00"end = "23:59:59"has_weekday = 1
[sg_search]binary_src_path = "/usr/local/bin/src"src_access_token = "${YOUR-SRC-ACCESS-TOKEN}"
[sg_search.opencompass]github_repo_id = "open-compass/opencompass"introduction = "用于评测大型语言模型(LLM). 它提供了完整的开源可复现的评测框架,支持大语言模型、多模态模型的一站式评测,基于分布式技术,对大参数量模型亦能实现高效评测。评测方向汇总为知识、语言、理解、推理、考试五大能力维度,整合集纳了超过70个评测数据集,合计提供了超过40万个模型评测问题,并提供长文本、安全、代码3类大模型特色技术能力评测。"
[sg_search.lmdeploy]github_repo_id = "internlm/lmdeploy"introduction = "lmdeploy 是一个用于压缩、部署和服务 LLM(Large Language Model)的工具包。是一个服务端场景下,transformer 结构 LLM 部署工具,支持 GPU 服务端部署,速度有保障,支持 Tensor Parallel,多并发优化,功能全面,包括模型转换、缓存历史会话的 cache feature 等. 它还提供了 WebUI、命令行和 gRPC 客户端接入。"
[frontend]type = "none"webhook_url = "https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxxxxxx"message_process_policy = "immediate"
[frontend.lark_group]app_id = "cli_a53a34dcb778500e"app_secret = "2ajhg1ixSvlNm1bJkH4tJhPfTCsGGHT1"encrypt_key = "abc"verification_token = "def"
[frontend.wechat_personal]bind_port = 95279 unmodified lines1011121314151617181920212223242526272829303132333435363736 unmodified lines74757677787980811 unmodified line838485868788891 unmodified line9192939495969798991001011029 unmodified linesclass ArticleRetrieval:def __init__(self,keywords: list,repo_dir = 'repodir',retmax = 500):self.keywords = keywordsself.repo_dir = repo_dirself.retmax = retmax## 通过PMC数据库检索文章def search_pmc(self):base_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"params = {"db": "pmc","term": '+'.join(self.keywords),"retmax": self.retmax}response = requests.get(base_url, params=params)root = ET.fromstring(response.content)pmc_ids = [id_element.text for id_element in root.findall('.//Id')]print(f"Found {len(pmc_ids)} articles.")self.pmc_ids = pmc_idsreturn pmc_ids# 解析XML文件def _get_all_text(self, element):36 unmodified linesif full_text.strip() == '':continueelse:logger.info(full_text[:1000])with open(os.path.join(self.repo_dir,f'PMC{id}.txt'), 'w') as f:f.write(full_text)self.success += 11 unmodified lineconfig = {'keywords': self.keywords,'repo_dir': self.repo_dir,'pmc_ids': self.pmc_ids,'len': self.success,'retmax': self.retmax}1 unmodified linejson.dump(config, f, indent=4, ensure_ascii=False)def initiallize(self):self.search_pmc()self.fetch_full_text()self.save_config()if __name__ == '__main__':if os.path.exists('repodir'):shutil.rmtree('repodir')articelfinder = ArticleRetrieval(keywords = ['covid-19'],repo_dir = 'repodir',retmax = 5)articelfinder.initiallize()9 unmodified lines1011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697036 unmodified lines1071081091101111121131141 unmodified line1161171181191201211221231241251261271 unmodified line1291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601619 unmodified linesclass ArticleRetrieval:def __init__(self,keywords: list = [],pmids: list = [],repo_dir = 'repodir',retmax = 500):if keywords is [] and pmids is []:raise ValueError("Either keywords or pmids must be provided.")self.keywords = keywordsself.pmids = pmidsself.repo_dir = repo_dirself.retmax = retmaxself.pmc_ids = []def esummary_pmc(self):base_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?"params = {"db": "pubmed","id": ','.join(self.pmids),# "retmax": self.retmax}response = requests.get(base_url, params=params)root = ET.fromstring(response.content)results = []for docsum in root.findall('DocSum'):pmcid = Noneid_value = docsum.find('Id').textfor item in docsum.findall('Item'):if item.attrib.get('Name') == 'ArticleIds':for id_item in item.findall('Item'):if id_item.attrib.get('Name') == 'pmc':pmcid = id_item.textbreakif pmcid:results.append((id_value, pmcid))self.esummary = resultsself.pmc_ids = [r[1] for r in results]## 通过Pubmed数据库检索文章def esearch_pmc(self):base_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"params = {"db": "pubmed","term": '+'.join(self.keywords),"retmax": self.retmax}response = requests.get(base_url, params=params)root = ET.fromstring(response.content)idlist = root.find('.//IdList')pmids = [id_element.text for id_element in idlist.findall('.//Id')]print(f"Found {len(pmids)} articles for keywords {self.keywords}.")self.pmids.extend(pmids)# 解析XML文件def _get_all_text(self, element):36 unmodified linesif full_text.strip() == '':continueelse:logger.info(full_text[:500])with open(os.path.join(self.repo_dir,f'{id}.txt'), 'w') as f:f.write(full_text)self.success += 11 unmodified lineconfig = {'keywords': self.keywords,'repo_dir': self.repo_dir,'result': [{'pmid': r[0],'pmcid': r[1]} for r in self.esummary],'len': self.success,'retmax': self.retmax}1 unmodified linejson.dump(config, f, indent=4, ensure_ascii=False)def initiallize(self):if self.keywords !=[]:print(self.keywords)self.esearch_pmc() # get pmids from pubmed database using keywordsself.esummary_pmc() # get pmc ids from pubmed database using pmidsself.fetch_full_text() # get full text from pmc database using pmc idsself.save_config() # save config fileif __name__ == '__main__':if os.path.exists('repodir'):shutil.rmtree('repodir')strings = """3694432438453907383004323865145338398096382558853803554738734498"""string = [k.strip() for k in strings.split('\n')]pmids = [k for k in string if k.isdigit()]print(pmids)keys = [k for k in string if not k.isdigit() and k != '']print(keys)articelfinder = ArticleRetrieval(keywords = keys,pmids = pmids,repo_dir = 'repodir',retmax = 5)articelfinder.initiallize()
201 unmodified lines202203204205206207137 unmodified lines345346347348349350201 unmodified linesself.remote_model = self.server_config['remote_llm_model']self.remote_max_length = self.server_config['remote_llm_max_text_length']self.api_key = self.server_config['remote_api_key']def call_puyu(self, prompt, history):self.reload_config()137 unmodified linesif base_url is not None:client = OpenAI(api_key=self.server_config['remote_api_key'],base_url=base_url)else:client = OpenAI(api_key=self.server_config['remote_api_key'])201 unmodified lines202203204205206207208137 unmodified lines346347348349350351352353354201 unmodified linesself.remote_model = self.server_config['remote_llm_model']self.remote_max_length = self.server_config['remote_llm_max_text_length']self.api_key = self.server_config['remote_api_key']self.base_url = self.server_config['remote_base_url']def call_puyu(self, prompt, history):self.reload_config()137 unmodified linesif base_url is not None:client = OpenAI(api_key=self.server_config['remote_api_key'],base_url=base_url)elif self.base_url != '':client = OpenAI(api_key=self.server_config['remote_api_key'],base_url=self.base_url)else:client = OpenAI(api_key=self.server_config['remote_api_key'])
16 unmodified lines17181920212262 unmodified lines85868788899091113 unmodified lines2052062072082092102117 unmodified lines21922022122222322422522639 unmodified lines26626726826927027127227327427527627727822 unmodified lines3013023033043053063073083093103112 unmodified lines31431531631731831932032132232316 unmodified linesfrom .web_search import WebSearchclass Worker:"""The Worker class orchestrates the logic of handling user queries,generating responses and managing several aspects of a chat assistant. It62 unmodified linesself.SECURITY_TEMAPLTE = 'Evaluate whether the following sentence involves prohibited content such as politics, insult, pornography, terror, religion, cyber violence, racial discrimination, etc., rate it on a scale of 0-10, do not explain, just give the score. The scoring standard is as follows: any violation directly gets 10 points; completely unrelated gets 0 points. Give the score, no explanation: "{}"' # noqa E501self.PERPLESITY_TEMPLATE = 'Question: {} Answer: {}\nRead the dialogue above, does the answer express that they don\'t know? The more comprehensive the answer, the lower the score. Rate it on a scale of 0-10, no explanation, just give the score.\nThe scoring standard is as follows: an accurate answer to the question gets 0 points; a detailed answer gets 1 point; knowing some answers but having uncertain information gets 8 points; knowing a small part of the answer but recommends seeking help from others gets 9 points; not knowing any of the answers and directly recommending asking others for help gets 10 points. Just give the score, no explanation.' # noqa E501self.SUMMARIZE_TEMPLATE = '"{}" \n Read the content above carefully, summarize it in a short and powerful way.' # noqa E501self.GENERATE_TEMPLATE = 'Background Information: "{}"\n Question: "{}"\n Please read the reference material carefully and answer the question.' # noqa E501self.ANNOTATE_CLUSTER = 'these are chunklized sentences from different papers about{}, they are clustered by similarity, the following is 10 samples from one of the cluster: "{}"\n Please tag the cluster in one breif sentence.'self.INSPIRATION_TEMPLATE = 'Given the following summary of the articles content about {0} {1}, give some idea or sub-questions of the review about {0}, one question is sufficient.' # noqa E501113 unmodified linestracker=tracker,throttle=5,default=10,backend=backend):context += chunkcontext += '\n\n'refs.append(ref)refs = list(set(refs))7 unmodified linesbackend=backend,history=history)tracker.log('feature store doc', [chunk, response])return ErrorCode.SUCCESS, response, refs# try:# references = []# web_context = ''39 unmodified lines# except Exception as e:# logger.error(e)if response is not None and len(response) > 0:prompt = self.PERPLESITY_TEMPLATE.format(query, response)if self.single_judge(prompt=prompt,tracker=tracker,throttle=10,default=0,backend = backend):reborn_code = ErrorCode.BAD_ANSWER# if self.config['worker']['enable_sg_search']:# if reborn_code == ErrorCode.BAD_ANSWER or reborn_code == ErrorCode.NO_SEARCH_RESULT: # noqa E50122 unmodified lines# default=0):# return ErrorCode.BAD_ANSWER, response, referencesif response is not None and len(response) >= 800:# reply too long, summarize itresponse = self.llm.generate_response(prompt=self.SUMMARIZE_TEMPLATE.format(response),backend=backend)# if len(response) > 0 and self.single_judge(# self.SECURITY_TEMAPLTE.format(response),2 unmodified lines# default=0):# return ErrorCode.SECURITY, response, referencesif reborn_code != ErrorCode.SUCCESS:return reborn_code, response, referencesreturn ErrorCode.SUCCESS, response, referencesdef annotate_cluster(self,theme, cluster_no, chunk, history, groupname,backend):"""Annotates a cluster of questions based on the user query and16 unmodified lines171819202122232425262728293062 unmodified lines93949596979899113 unmodified lines2132142152162172182197 unmodified lines22722822923023123223323423523623723823939 unmodified lines27928028128228328428528628728828929029122 unmodified lines3143153163173183193203213223233242 unmodified lines32732832933033133233333433516 unmodified linesfrom .web_search import WebSearchdef convertid2url(text):# Regular expression to find all PMC referencespattern = r"\[PMC(\d+)\]"# Function to replace each match with a URL linkreplacement = lambda match: f"[PMC{match.group(1)}](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC{match.group(1)}/)"# Replace all occurrences in the textformatted_text = re.sub(pattern, replacement, text)return formatted_textclass Worker:"""The Worker class orchestrates the logic of handling user queries,generating responses and managing several aspects of a chat assistant. It62 unmodified linesself.SECURITY_TEMAPLTE = 'Evaluate whether the following sentence involves prohibited content such as politics, insult, pornography, terror, religion, cyber violence, racial discrimination, etc., rate it on a scale of 0-10, do not explain, just give the score. The scoring standard is as follows: any violation directly gets 10 points; completely unrelated gets 0 points. Give the score, no explanation: "{}"' # noqa E501self.PERPLESITY_TEMPLATE = 'Question: {} Answer: {}\nRead the dialogue above, does the answer express that they don\'t know? The more comprehensive the answer, the lower the score. Rate it on a scale of 0-10, no explanation, just give the score.\nThe scoring standard is as follows: an accurate answer to the question gets 0 points; a detailed answer gets 1 point; knowing some answers but having uncertain information gets 8 points; knowing a small part of the answer but recommends seeking help from others gets 9 points; not knowing any of the answers and directly recommending asking others for help gets 10 points. Just give the score, no explanation.' # noqa E501self.SUMMARIZE_TEMPLATE = '"{}" \n Read the content above carefully, summarize it in a short and powerful way.' # noqa E501self.GENERATE_TEMPLATE = 'Background Information: "{}"\n Question: "{}"\n Please read the reference material carefully and answer the question. with reference id at the end of the corresponding content for example: Primary determinants of the therapeutic approach are age, comorbidities, and diagnostic molecular profile [PMC9958584]' # noqa E501self.ANNOTATE_CLUSTER = 'these are chunklized sentences from different papers about{}, they are clustered by similarity, the following is 10 samples from one of the cluster: "{}"\n Please tag the cluster in one breif sentence.'self.INSPIRATION_TEMPLATE = 'Given the following summary of the articles content about {0} {1}, give some idea or sub-questions of the review about {0}, one question is sufficient.' # noqa E501113 unmodified linestracker=tracker,throttle=5,default=10,backend=backend):context += f"reference: {ref} content: {chunk}"context += '\n\n'refs.append(ref)refs = list(set(refs))7 unmodified linesbackend=backend,history=history)tracker.log('feature store doc', [chunk, response])response = convertid2url(response)return ErrorCode.SUCCESS, response, refselse:return ErrorCode.NO_SEARCH_RESULT, response, references# try:# references = []# web_context = ''39 unmodified lines# except Exception as e:# logger.error(e)# if response is not None and len(response) > 0:# prompt = self.PERPLESITY_TEMPLATE.format(query, response)# if self.single_judge(prompt=prompt,# tracker=tracker,# throttle=10,# default=0,backend = backend):# reborn_code = ErrorCode.BAD_ANSWER# if self.config['worker']['enable_sg_search']:# if reborn_code == ErrorCode.BAD_ANSWER or reborn_code == ErrorCode.NO_SEARCH_RESULT: # noqa E50122 unmodified lines# default=0):# return ErrorCode.BAD_ANSWER, response, references# if response is not None and len(response) >= 800:# # reply too long, summarize it# response = self.llm.generate_response(# prompt=self.SUMMARIZE_TEMPLATE.format(response),# backend=backend)# if len(response) > 0 and self.single_judge(# self.SECURITY_TEMAPLTE.format(response),2 unmodified lines# default=0):# return ErrorCode.SECURITY, response, references# if reborn_code != ErrorCode.SUCCESS:# return reborn_code, response, references# return ErrorCode.SUCCESS, response, referencesdef annotate_cluster(self,theme, cluster_no, chunk, history, groupname,backend):"""Annotates a cluster of questions based on the user query and
1234567# 云端运行conda activate ReviewAgentcd MedicalReviewAgentpython3 app.py# 本地映射端口ssh -CNg -L 7860:127.0.0.1:7860 root@43.133.72.2161234567# 云端运行conda activate ReviewAgentcd MedicalReviewAgentpython3 applocal.py --model_downloaded True# 本地映射端口ssh -CNg -L 7860:127.0.0.1:7860 root@youripaddress
README.md+5-3
app.py+30-11
applocal.pyAdded+663
config-bak.ini+1
config-mod_down-bak.iniAdded+64
huixiangdou/service/findarticles.py+75-16
huixiangdou/service/llm_server_hybrid.py+4
huixiangdou/service/worker.py+30-18
run.sh+2-2