Repository navigation
feat: paper fetch #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andrea-gariboldi
wants to merge
7
commits into
main
Choose a base branch
from
paper-fetch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: paper fetch #163
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d9d7d9b
Create paper_fetch tool
andrea-gariboldi 2cd4183
Streamline paper_fetch validator
andrea-gariboldi 9eb5c6a
Revamp paper_fetch prompt
andrea-gariboldi 608b768
Update paper fetch tool
andrea-gariboldi f592f93
Update docs
andrea-gariboldi 5e3cb85
Add tests
andrea-gariboldi e742be4
Use already installed library in tests
andrea-gariboldi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| from pydantic import BaseModel, Field | ||
| from pydantic_ai import Agent, ModelRetry | ||
| from pydantic_ai.models import Model | ||
| from pydantic_ai.settings import ModelSettings | ||
| from pydantic_ai.common_tools.web_fetch import web_fetch_tool | ||
|
|
||
| from agentomics.tools.save_paper_pdf_tool import create_save_paper_pdf_tool | ||
| from agentomics.utils.config import Config | ||
|
|
||
|
|
||
| PAPER_FETCH_SYSTEM_PROMPT = """ | ||
| Your goal is to find scientific papers informing a machine learning modeling task. | ||
|
|
||
| Work in three phases: | ||
| 1. Check the returned search results for relevance to the query. If the same paper appears several times, choose a single host to retrieve it from. | ||
| 2. Read. Use web_fetch on the web pages the search returned to confirm what each paper actually | ||
| is and to find its PDF link. Publisher landing pages state the real PDF URL; read it rather than | ||
| assembling one from the article id. Never report a paper you have not fetched. | ||
| 3. Retrieve. Call save_paper_pdf for each paper relevant to the query. It writes the PDF to disk; you do not | ||
| need to read the PDF contents yourself. If a URL returns a download page instead of a PDF, go back | ||
| to the landing page for the direct link rather than guessing. | ||
|
|
||
| Report only papers whose PDF you successfully saved. | ||
| """ | ||
|
|
||
| class Paper(BaseModel): | ||
| title: str = Field( | ||
| description="The title of the paper." | ||
| ) | ||
| pdf_url: str | None = Field( | ||
| default=None, | ||
| description="The URL the PDF was downloaded from.") | ||
| pdf_filename: str | None = Field( | ||
| default=None, | ||
| description="Filename of the saved PDF, as returned by save_paper_pdf. " | ||
| "Empty if the PDF could not be retrieved.") | ||
| relevance: str = Field( | ||
| description="Why this paper is relevant to the query, and what it contributes to the modeling task.") | ||
|
|
||
| class PaperFetchOutput(BaseModel): | ||
| papers: list[Paper] = Field( | ||
| description="List of relevant fetched papers. Empty if no relevant papers were retrieved or saved.") | ||
|
|
||
| def create_paper_fetch_agent(config: Config, model: Model) -> Agent[None, PaperFetchOutput]: | ||
| agent = Agent( | ||
| model=model, | ||
| system_prompt=PAPER_FETCH_SYSTEM_PROMPT, | ||
| tools=[ | ||
| web_fetch_tool(timeout=config.web_fetch_timeout), | ||
| create_save_paper_pdf_tool(config), | ||
| ], | ||
| model_settings=ModelSettings(temperature=config.temperature), | ||
| output_type=PaperFetchOutput, | ||
| retries=config.max_validation_retries | ||
| ) | ||
| attach_output_validator(agent, config) | ||
| return agent | ||
|
|
||
| def attach_output_validator(agent: Agent[None, PaperFetchOutput], config: Config) -> None: | ||
| @agent.output_validator | ||
| async def _papers_stored_in_shared_dir(result: PaperFetchOutput) -> PaperFetchOutput: | ||
| papers_dir = config.fetched_papers_dir.resolve() | ||
|
|
||
| for paper in result.papers: | ||
| if paper.pdf_filename is None: | ||
| continue | ||
| pdf_path = papers_dir / Path(paper.pdf_filename).name | ||
| if not pdf_path.is_file(): | ||
| raise ModelRetry(f"'{paper.pdf_filename}' is not in {papers_dir}. Save it with save_paper_pdf or clear pdf_filename.") | ||
| return result | ||
|
|
||
| def build_prompt(config: Config, query: str, websearch_result: list[dict]) -> str: | ||
| return f"""Based on the query received as input: | ||
| {query} | ||
| and the results of web search for that query: | ||
| {websearch_result} | ||
| - Save the pdf of relevant papers at: {config.fetched_papers_dir}""" | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want the search agent to be ML-specific? IMO the query should be fine if its generic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do agree on this. I will change the prompt to make it more generic. If we are going for fetching with no LLM agent (which seems to be the direction) this prompt will be deleted completely, and only the tool docstring will guide the main agent on how to make the query itself
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, regarding the prompt itself I was thinking ideally we would retrieve papers that are implementing/summarising a ML modeling decision, but as you pointed out, the query does not have to be specific to the ML domain