|
| 1 | +from openagi.actions.base import BaseAction |
| 2 | +from pydantic import Field |
| 3 | +from openagi.exception import OpenAGIException |
| 4 | +from typing import ClassVar, Dict, Any |
| 5 | + |
| 6 | +try: |
| 7 | + import arxiv |
| 8 | +except ImportError: |
| 9 | + raise OpenAGIException("Install arxiv with cmd `pip install arxiv`") |
| 10 | + |
| 11 | +class ConfigurableAction(BaseAction): |
| 12 | + config: ClassVar[Dict[str, Any]] = {} |
| 13 | + |
| 14 | + @classmethod |
| 15 | + def set_config(cls, *args, **kwargs): |
| 16 | + if args: |
| 17 | + if len(args) == 1 and isinstance(args[0], dict): |
| 18 | + cls.config.update(args[0]) |
| 19 | + else: |
| 20 | + raise ValueError("If using positional arguments, a single dictionary must be provided.") |
| 21 | + cls.config.update(kwargs) |
| 22 | + |
| 23 | + @classmethod |
| 24 | + def get_config(cls, key: str, default: Any = None) -> Any: |
| 25 | + return cls.config.get(key, default) |
| 26 | + |
| 27 | +class ArxivSearch(ConfigurableAction): |
| 28 | + """ |
| 29 | + Arxiv Search is a tool used to search articles in Physics, Mathematics, Computer Science, Quantitative Biology, Quantitative Finance, and Statistics |
| 30 | + """ |
| 31 | + query: str = Field(..., description="User query or question") |
| 32 | + max_results: int = Field(10, description="Total results, in int, to be executed from the search. Defaults to 10.") |
| 33 | + |
| 34 | + def execute(self): |
| 35 | + search = arxiv.Search( |
| 36 | + query = self.query, |
| 37 | + max_results = self.max_results, |
| 38 | + ) |
| 39 | + client = arxiv.Client() |
| 40 | + results = client.results(search) |
| 41 | + meta_data = "" |
| 42 | + for result in results: |
| 43 | + meta_data += f"title : {result.title}\n " |
| 44 | + meta_data += f"summary : {result.summary}\n " |
| 45 | + meta_data += f"published : {result.published}\n " |
| 46 | + meta_data += f"authors : {result.authors}\n " |
| 47 | + meta_data += f"pdf_url : {result.pdf_url}\n " |
| 48 | + meta_data += f"entry_id : {result.entry_id}\n\n " |
| 49 | + return meta_data.strip() |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
0 commit comments