Skip to content

Commit

Permalink
Added option to skip summaries and adjusted prompts (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
whitead authored Aug 24, 2023
1 parent 6652c59 commit 76ecaf4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
49 changes: 28 additions & 21 deletions paperqa/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,19 +309,23 @@ async def adoc_match(
if len(matched_docs) == 0:
return set()
# this only works for gpt-4 (in my testing)
if cast(BaseLanguageModel, self.llm).model_name.startswith("gpt-4"):
chain = make_chain(
self.prompts.select, cast(BaseLanguageModel, self.llm), skip_system=True
)
papers = [f"{d.docname}: {d.citation}" for d in matched_docs]
result = await chain.arun( # type: ignore
question=query,
papers="\n".join(papers),
callbacks=get_callbacks("filter"),
)
return set([d.dockey for d in matched_docs if d.docname in result])
else:
return set([d.dockey for d in matched_docs])
try:
if cast(BaseLanguageModel, self.llm).model_name.startswith("gpt-4"):
chain = make_chain(
self.prompts.select,
cast(BaseLanguageModel, self.llm),
skip_system=True,
)
papers = [f"{d.docname}: {d.citation}" for d in matched_docs]
result = await chain.arun( # type: ignore
question=query,
papers="\n".join(papers),
callbacks=get_callbacks("filter"),
)
return set([d.dockey for d in matched_docs if d.docname in result])
except AttributeError:
pass
return set([d.dockey for d in matched_docs])

def __getstate__(self):
state = self.__dict__.copy()
Expand Down Expand Up @@ -470,14 +474,17 @@ async def process(match):
citation = match.metadata["doc"]["citation"]
if detailed_citations:
citation = match.metadata["name"] + ": " + citation
context = await summary_chain.arun(
question=answer.question,
# Add name so chunk is stated
citation=citation,
summary_length=answer.summary_length,
text=match.page_content,
callbacks=callbacks,
)
if self.prompts.skip_summary:
context = match.page_content
else:
context = await summary_chain.arun(
question=answer.question,
# Add name so chunk is stated
citation=citation,
summary_length=answer.summary_length,
text=match.page_content,
callbacks=callbacks,
)
except Exception as e:
if guess_is_4xx(str(e)):
return None
Expand Down
4 changes: 3 additions & 1 deletion paperqa/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
template="Summarize the text below to help answer a question. "
"Do not directly answer the question, instead summarize "
"to give evidence to help answer the question. "
"Focus on specific details, including numbers, equations, or specific quotes. "
'Reply "Not applicable" if text is irrelevant. '
"Use {summary_length}. At the end of your response, provide a score from 1-10 on a newline "
"indicating relevance to question. Do not explain your score. "
Expand Down Expand Up @@ -51,6 +52,7 @@
)

default_system_prompt = (
"Answer in an direct, concise, scholarly tone. "
"Answer in an direct and concise tone, I am in a hurry. "
"Your audience is an expert, so be highly specific. "
"If there are ambiguous terms or acronyms, first define them. "
)
1 change: 1 addition & 0 deletions paperqa/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class PromptCollection(BaseModel):
pre: Optional[PromptTemplate] = None
post: Optional[PromptTemplate] = None
system: str = default_system_prompt
skip_summary: bool = False

@validator("summary")
def check_summary(cls, v: PromptTemplate) -> PromptTemplate:
Expand Down
2 changes: 1 addition & 1 deletion paperqa/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "3.7.1"
__version__ = "3.8.0"

0 comments on commit 76ecaf4

Please sign in to comment.