-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
98 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pre-commit | ||
pytest | ||
pytest-mock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,69 @@ | ||
import pqapi | ||
from langchain.tools import BaseTool | ||
import os | ||
import re | ||
from langchain.base_language import BaseLanguageModel | ||
import langchain | ||
import paperqa | ||
import paperscraper | ||
from pypdf.errors import PdfReadError | ||
|
||
|
||
class Scholar2ResultLLM(BaseTool): | ||
name = "LiteratureSearch" | ||
description = """Input a specific question, | ||
returns an answer from literature search.""" | ||
def paper_scraper(search:str, pdir:str="query") -> dict: | ||
try: | ||
return paperscraper.search_papers(search, pdir=pdir) | ||
except KeyError: | ||
return {} | ||
|
||
def paper_search(llm, query): | ||
prompt = langchain.prompts.PromptTemplate( | ||
input_variables=["question"], | ||
template=""" | ||
I would like to find scholarly papers to answer | ||
this question: {question}. | ||
'A search query that would bring up papers that can answer | ||
this question would be: '""",) | ||
|
||
query_chain = langchain.chains.llm.LLMChain(llm=llm, prompt=prompt) | ||
if not os.path.isdir("./query"): #todo: move to ckpt | ||
os.mkdir("query/") | ||
|
||
pqa_key: str = "" | ||
search = query_chain.run(query) | ||
print("\nSearch:", search) | ||
papers = paper_scraper(search, pdir=f"query/{re.sub(' ', '', search)}") | ||
return papers | ||
|
||
def __init__(self, pqa_key: str): | ||
super().__init__() | ||
self.pqa_key = pqa_key | ||
|
||
def _run(self, question: str) -> str: | ||
"""Use the tool""" | ||
def scholar2result_llm(llm, query): | ||
"""Useful to answer questions that require | ||
technical knowledge. Ask a specific question.""" | ||
papers = paper_search(llm, query) | ||
if len(papers) == 0: | ||
return "Not enough papers found" | ||
docs = paperqa.Docs(llm=llm) | ||
not_loaded = 0 | ||
for path, data in papers.items(): | ||
try: | ||
response = pqapi.agent_query("default", question) | ||
return response.answer | ||
except Exception: | ||
return "Literature search failed." | ||
|
||
async def _arun(self, question: str) -> str: | ||
"""Use the tool asynchronously""" | ||
raise NotImplementedError | ||
docs.add(path, data["citation"]) | ||
except (ValueError, FileNotFoundError, PdfReadError): | ||
not_loaded += 1 | ||
|
||
print(f"\nFound {len(papers.items())} papers but couldn't load {not_loaded}") | ||
return docs.query(query).formatted_answer | ||
|
||
|
||
class Scholar2ResultLLM: | ||
name = "Literature Search" | ||
description = ( | ||
"Useful to answer questions that require technical ", | ||
"knowledge. Ask a specific question.", | ||
) | ||
llm: BaseLanguageModel | ||
|
||
def __init__(self, llm): | ||
self.llm = llm | ||
|
||
def _run(self, query) -> str: | ||
return scholar2result_llm(self.llm, query) | ||
|
||
async def _arun(self, query) -> str: | ||
"""Use the tool asynchronously.""" | ||
raise NotImplementedError("this tool does not support async") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters