Skip to content

Commit

Permalink
Propogated extra props in JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
whitead committed Feb 21, 2024
1 parent 891c3e8 commit e3aaec6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Allow lines to be as long as 120 characters.
line-length = 120
# Allow lines to be as longer.
line-length = 180
1 change: 1 addition & 0 deletions paperqa/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,7 @@ async def process(match):
context_str = "\n\n".join(
[
f"{c.text.name}: {c.context}"
+ "".join([f"\n{k}: {v}" for k, v in c.model_extra.items()])
+ (f"\n\nBased on {c.text.doc.citation}" if detailed_citations else "")
for c in answer.contexts
]
Expand Down
3 changes: 3 additions & 0 deletions paperqa/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ class Context(BaseModel):
context: str
text: Text
score: int = 5
model_config = ConfigDict(
extra="allow",
)


def __str__(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion paperqa/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "4.0.0-pre.7"
__version__ = "4.0.0-pre.8"
41 changes: 41 additions & 0 deletions tests/test_paperqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,47 @@ def test_json_evidence():
os.remove(doc_path)


def test_custom_json_props():
doc_path = "example.html"
with open(doc_path, "w", encoding="utf-8") as f:
# get wiki page about politician
r = requests.get("https://en.wikipedia.org/wiki/Frederick_Bates_(politician)")
f.write(r.text)
summary_llm = OpenAILLMModel(
config=dict(
model="gpt-3.5-turbo-0125",
response_format=dict(type="json_object"),
temperature=0.0,
)
)
my_prompts = PromptCollection(
json_summary=True,
summary_json_system="Provide a summary of the excerpt that could help answer the question based on the excerpt. "
"The excerpt may be irrelevant. Do not directly answer the question - only summarize relevant information. "
"Respond with the following JSON format:\n\n"
'{{\n"summary": "...",\n"person_name": "...",\n"relevance_score": "..."}}\n\n'
"where `summary` is relevant information from text - "
"about 100 words words, `person_name` specifies the person discussed in "
"the excerpt (may be different than query), and `relevance_score` is "
"the relevance of `summary` to answer the question (integer out of 10).",
)
docs = Docs(
prompts=my_prompts,
summary_llm_model=summary_llm,
llm_result_callback=print_callback,
)
docs.add(doc_path, "WikiMedia Foundation, 2023, Accessed now")
evidence = docs.get_evidence(
Answer(question="For which state was Bates a governor?"), k=1, max_sources=1
)
assert "person_name" in evidence.contexts[0].model_extra
assert "person_name: " in evidence.context
print(evidence.context)
answer = docs.query("What is Frederick Bates's greatest accomplishment?")
assert "person_name" in answer.context
os.remove(doc_path)


def test_query():
docs = Docs()
docs.add_url(
Expand Down

0 comments on commit e3aaec6

Please sign in to comment.