Skip to content
4 changes: 2 additions & 2 deletions src/paperqa/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ async def _map_fxn_summary( # noqa: PLR0912

# Strip newlines in case chunking led to blank lines,
# but not spaces, to preserve text alignment
cleaned_text = text.text.strip("\n")
cleaned_text = text.text.strip("\n") or "(no text)"
if summary_llm_model and prompt_templates:
unique_media = list(dict.fromkeys(text.media)) # Preserve order
media_text: list[str] = [m.text for m in unique_media if m.text]
Expand All @@ -240,7 +240,7 @@ async def _map_fxn_summary( # noqa: PLR0912
text_with_tables_prompt_template.format(
text=cleaned_text,
citation=citation,
tables="\n\n----\n\n".join(media_text),
tables="\n\n".join(media_text),
)
if media_text
else cleaned_text
Expand Down
52 changes: 25 additions & 27 deletions src/paperqa/prompts.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
from datetime import datetime

summary_json_prompt = (
"Excerpt from {citation}\n\n---\n\n{text}\n\n---\n\nQuestion: {question}"
)
summary_prompt = (
"Summarize the excerpt below to help answer a question.\n\nExcerpt from"
" {citation}\n\n------------\n\n{text}\n\n------------"
"\n\nQuestion: {question}\n\nDo not directly"
" answer the question, instead summarize to give evidence to help answer the"
" question. Stay detailed; report specific numbers, equations, or direct quotes"
"Summarize the excerpt below to help answer a question."
f"\n\n{summary_json_prompt}\n\n"
"Do not directly answer the question,"
" instead summarize to give evidence to help answer the question."
" Stay detailed; report specific numbers, equations, or direct quotes"
' (marked with quotation marks). Reply "Not applicable" if the excerpt is'
" irrelevant. At the end of your response,"
"provide an integer score from 1-10 on a newline indicating relevance to question." # Don't use 0-10 since we mention "not applicable" instead # noqa: E501
" provide an integer score from 1-10 on a newline indicating relevance to question." # Don't use 0-10 since we mention "not applicable" instead # noqa: E501
" Do not explain your score."
"\n\nRelevant Information Summary ({summary_length}):"
)
# This prompt template integrates with `text` variable of the above `summary_prompt`
text_with_tables_prompt_template = (
"{text}\n\n------------\n\nMarkdown tables from {citation}."
" If the markdown is poorly formatted, defer to the images"
"\n\n------------\n\n{tables}"
)

summary_json_prompt = (
"Excerpt from {citation}\n\n------------\n\n{text}\n\n------------"
"\n\nQuestion: {question}\n\n"
"{text}\n\n---\n\nMarkdown tables from {citation}."
" If the markdown is poorly formatted, defer to the images."
"\n\n{tables}"
)

# The below "cannot answer" sentinel phrase should:
Expand All @@ -38,29 +36,29 @@
)

CITATION_KEY_CONSTRAINTS = (
"## Valid citation examples, only use comma/space delimited parentheticals: \n"
"- (pqac-d79ef6fa, pqac-0f650d59) \n"
"- (pqac-d79ef6fa) \n"
"## Invalid citation examples: \n"
"- (pqac-d79ef6fa and pqac-0f650d59) \n"
"- (pqac-d79ef6fa;pqac-0f650d59) \n"
"- (pqac-d79ef6fa-pqac-0f650d59) \n"
"- pqac-d79ef6fa and pqac-0f650d59 \n"
"- Example's work (pqac-d79ef6fa) \n"
"- (pages pqac-d79ef6fa) \n"
"## Valid citation examples, only use comma/space delimited parentheticals:\n"
"- (pqac-d79ef6fa, pqac-0f650d59)\n"
"- (pqac-d79ef6fa)\n"
"## Invalid citation examples:\n"
"- (pqac-d79ef6fa and pqac-0f650d59)\n"
"- (pqac-d79ef6fa;pqac-0f650d59)\n"
"- (pqac-d79ef6fa-pqac-0f650d59)\n"
"- pqac-d79ef6fa and pqac-0f650d59\n"
"- Example's work (pqac-d79ef6fa)\n"
"- (pages pqac-d79ef6fa)"
)

qa_prompt = (
"Answer the question below with the context.\n\n"
"Context:\n\n{context}\n\n------------\n\n"
"Context:\n\n{context}\n\n---\n\n"
"Question: {question}\n\n"
"Write an answer based on the context. "
"If the context provides insufficient information reply "
f'"{CANNOT_ANSWER_PHRASE}." '
"For each part of your answer, indicate which sources most support "
"it via citation keys at the end of sentences, like {example_citation}. "
"Only cite from the context above and only use the citation keys from the context. "
f"{CITATION_KEY_CONSTRAINTS}"
"Only cite from the context above and only use the citation keys from the context."
f"\n\n{CITATION_KEY_CONSTRAINTS}\n\n"
"Do not concatenate citation keys, just use them as is. "
"Write in the style of a scientific article, with concise sentences and "
"coherent paragraphs. This answer will be used directly, "
Expand Down
2 changes: 1 addition & 1 deletion src/paperqa/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ async def context_serializer(
)
section = f"{section_header}\n\n" + "\n\n".join(inner_strs)
context_sections.append(section)
context_str_body = "\n\n----\n\n".join(context_sections)
context_str_body = "\n\n---\n\n".join(context_sections)
else:
inner_context_strs = [
context_inner_prompt.format(
Expand Down
Loading