| 364 | |
| 365 | |
| 366 | def build_tool(config) -> Tool: |
| 367 | tool = Tool( |
| 368 | "Meta Analysis Plugin", |
| 369 | description="Analyzing literatures", |
| 370 | name_for_model="Meta Analysis", |
| 371 | description_for_model="Plugin for searching and analyzing literatures. All input should be a json like {'input': 'some input'}. Please use the provided questions and search step by step.", |
| 372 | logo_url="https://your-app-url.com/.well-known/logo.png", |
| 373 | contact_email="hello@contact.com", |
| 374 | legal_info_url="hello@legal.com", |
| 375 | ) |
| 376 | QRY = 'https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/' |
| 377 | |
| 378 | @tool.get("/search_literature") |
| 379 | def search_literature( topic: str, maxnum: int, term: str ): |
| 380 | """search for the given topic literatures in the database and return the path of literatures file and the number of literatures. the searching term should be key words in the topic (2-5 words). the number of literatures will be less than maxnum (recommend 30) """ |
| 381 | topic = topic.replace('AND', '').replace('OR', '') |
| 382 | if len(topic)<4: |
| 383 | term = topic |
| 384 | else: |
| 385 | term = term.replace('AND', '').replace('OR', '') |
| 386 | if len(term.split(' '))>4: |
| 387 | term = None |
| 388 | retmax = min(maxnum,50) |
| 389 | if retmax==1: |
| 390 | newdocs, sims = initial(topic, term, 1) |
| 391 | else: |
| 392 | docs, sims = initial(topic, term) |
| 393 | srts = np.argsort(-sims) |
| 394 | kys = list(docs.keys()) |
| 395 | newdocs = {} |
| 396 | for sr in srts: |
| 397 | if sims[sr]<0.72 and len(newdocs)>0: |
| 398 | break |
| 399 | if len(newdocs)>=retmax: |
| 400 | break |
| 401 | newdocs[kys[sr]] = docs[kys[sr]] |
| 402 | pickle.dump(newdocs, open('searchdoc_'+topic.replace(' ', '_')[:50]+'.pkl', 'wb')) |
| 403 | js = {} |
| 404 | js['literature_path'] = 'searchdoc_'+topic.replace(' ', '_')[:50]+'.pkl' |
| 405 | js['literature_number'] = len(newdocs) |
| 406 | return js |
| 407 | |
| 408 | @tool.get("/split_criteria") |
| 409 | def split_criteria( criteria:str ): |
| 410 | """split the screening requirements in the criteria of the literatures into a series of simple yes/no problems, and return the path of the splitted questions. """ |
| 411 | ques = split_question(criteria) |
| 412 | np.save('split_ques_'+criteria.replace(' ', '_')[:50]+'.npy', ques) |
| 413 | js = {'question number': len(ques), 'question_path': 'split_ques_'+criteria.replace(' ', '_')[:50]+'.npy'} |
| 414 | return js |
| 415 | |
| 416 | @tool.get("/literature_filter") |
| 417 | def literature_filter( concat_path:str ): |
| 418 | """ Check each literatures saved in the literature path according to the questions saved in the question path, and return the literatures that match the requirements. Concat path is the concatenated string of literature path and question path connected with '&&&'. """ |
| 419 | if len(concat_path.split('&&&'))!=2: |
| 420 | js = {'error': "input path cannot recognize the literature path and the question path. please input 'LITERATURE_PATH&&&QUESTION_PATH'. "} |
| 421 | return js |
| 422 | liter_pth = concat_path.split('&&&')[0].strip(' ') |
| 423 | try: |