|
| 1 | +# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. =========== |
| 2 | +# Licensed under the Apache License, Version 2.0 (the “License”); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an “AS IS” BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. =========== |
| 14 | + |
| 15 | +from typing import Dict, Generator, List, Optional |
| 16 | + |
| 17 | +from camel.toolkits.base import BaseToolkit |
| 18 | +from camel.toolkits.function_tool import FunctionTool |
| 19 | +from camel.utils import dependencies_required |
| 20 | + |
| 21 | + |
| 22 | +class ArxivToolkit(BaseToolkit): |
| 23 | + r"""A toolkit for interacting with the arXiv API to search and download |
| 24 | + academic papers. |
| 25 | + """ |
| 26 | + |
| 27 | + @dependencies_required('arxiv') |
| 28 | + def __init__(self) -> None: |
| 29 | + r"""Initializes the ArxivToolkit and sets up the arXiv client.""" |
| 30 | + import arxiv |
| 31 | + |
| 32 | + self.client = arxiv.Client() |
| 33 | + |
| 34 | + def _get_search_results( |
| 35 | + self, |
| 36 | + query: str, |
| 37 | + paper_ids: Optional[List[str]] = None, |
| 38 | + max_results: Optional[int] = 5, |
| 39 | + ) -> Generator: |
| 40 | + r"""Retrieves search results from the arXiv API based on the provided |
| 41 | + query and optional paper IDs. |
| 42 | +
|
| 43 | + Args: |
| 44 | + query (str): The search query string used to search for papers on |
| 45 | + arXiv. |
| 46 | + paper_ids (List[str], optional): A list of specific arXiv paper |
| 47 | + IDs to search for. (default::obj: `None`) |
| 48 | + max_results (int, optional): The maximum number of search results |
| 49 | + to retrieve. (default::obj: `5`) |
| 50 | +
|
| 51 | + Returns: |
| 52 | + Generator: A generator that yields results from the arXiv search |
| 53 | + query, which includes metadata about each paper matching the |
| 54 | + query. |
| 55 | + """ |
| 56 | + import arxiv |
| 57 | + |
| 58 | + paper_ids = paper_ids or [] |
| 59 | + search_query = arxiv.Search( |
| 60 | + query=query, |
| 61 | + id_list=paper_ids, |
| 62 | + max_results=max_results, |
| 63 | + ) |
| 64 | + return self.client.results(search_query) |
| 65 | + |
| 66 | + def search_papers( |
| 67 | + self, |
| 68 | + query: str, |
| 69 | + paper_ids: Optional[List[str]] = None, |
| 70 | + max_results: Optional[int] = 5, |
| 71 | + ) -> List[Dict[str, str]]: |
| 72 | + r"""Searches for academic papers on arXiv using a query string and |
| 73 | + optional paper IDs. |
| 74 | +
|
| 75 | + Args: |
| 76 | + query (str): The search query string. |
| 77 | + paper_ids (List[str], optional): A list of specific arXiv paper |
| 78 | + IDs to search for. (default::obj: `None`) |
| 79 | + max_results (int, optional): The maximum number of search results |
| 80 | + to return. (default::obj: `5`) |
| 81 | +
|
| 82 | + Returns: |
| 83 | + List[Dict[str, str]]: A list of dictionaries, each containing |
| 84 | + information about a paper, including title, published date, |
| 85 | + authors, entry ID, summary, and extracted text from the paper. |
| 86 | + """ |
| 87 | + from arxiv2text import arxiv_to_text |
| 88 | + |
| 89 | + search_results = self._get_search_results( |
| 90 | + query, paper_ids, max_results |
| 91 | + ) |
| 92 | + papers_data = [] |
| 93 | + |
| 94 | + for paper in search_results: |
| 95 | + paper_info = { |
| 96 | + "title": paper.title, |
| 97 | + "published_date": paper.updated.date().isoformat(), |
| 98 | + "authors": [author.name for author in paper.authors], |
| 99 | + "entry_id": paper.entry_id, |
| 100 | + "summary": paper.summary, |
| 101 | + # TODO: Use chunkr instead of atxiv_to_text for better |
| 102 | + # performance |
| 103 | + "paper_text": arxiv_to_text(paper.pdf_url), |
| 104 | + } |
| 105 | + papers_data.append(paper_info) |
| 106 | + |
| 107 | + return papers_data |
| 108 | + |
| 109 | + def download_papers( |
| 110 | + self, |
| 111 | + query: str, |
| 112 | + paper_ids: Optional[List[str]] = None, |
| 113 | + max_results: Optional[int] = 5, |
| 114 | + output_dir: Optional[str] = "./", |
| 115 | + ) -> str: |
| 116 | + r"""Downloads PDFs of academic papers from arXiv based on the provided |
| 117 | + query. |
| 118 | +
|
| 119 | + Args: |
| 120 | + query (str): The search query string. |
| 121 | + paper_ids (List[str], optional): A list of specific arXiv paper |
| 122 | + IDs to download. (default::obj: `None`) |
| 123 | + max_results (int, optional): The maximum number of search results |
| 124 | + to download. (default::obj: `5`) |
| 125 | + output_dir (str, optional): The directory to save the downloaded |
| 126 | + PDFs. Defaults to the current directory. |
| 127 | +
|
| 128 | + Returns: |
| 129 | + str: Status message indicating success or failure. |
| 130 | + """ |
| 131 | + try: |
| 132 | + search_results = self._get_search_results( |
| 133 | + query, paper_ids, max_results |
| 134 | + ) |
| 135 | + |
| 136 | + for paper in search_results: |
| 137 | + paper.download_pdf( |
| 138 | + dirpath=output_dir, filename=f"{paper.title}" + ".pdf" |
| 139 | + ) |
| 140 | + return "papers downloaded successfully" |
| 141 | + except Exception as e: |
| 142 | + return f"An error occurred: {e}" |
| 143 | + |
| 144 | + def get_tools(self) -> List[FunctionTool]: |
| 145 | + return [ |
| 146 | + FunctionTool(self.search_papers), |
| 147 | + FunctionTool(self.download_papers), |
| 148 | + ] |
0 commit comments