You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

arxiv.py 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from abc import ABC
  17. import arxiv
  18. import pandas as pd
  19. from agent.settings import DEBUG
  20. from agent.component.base import ComponentBase, ComponentParamBase
  21. class ArXivParam(ComponentParamBase):
  22. """
  23. Define the ArXiv component parameters.
  24. """
  25. def __init__(self):
  26. super().__init__()
  27. self.top_n = 6
  28. self.sort_by = 'submittedDate'
  29. def check(self):
  30. self.check_positive_integer(self.top_n, "Top N")
  31. self.check_valid_value(self.sort_by, "ArXiv Search Sort_by",
  32. ['submittedDate', 'lastUpdatedDate', 'relevance'])
  33. class ArXiv(ComponentBase, ABC):
  34. component_name = "ArXiv"
  35. def _run(self, history, **kwargs):
  36. ans = self.get_input()
  37. ans = " - ".join(ans["content"]) if "content" in ans else ""
  38. if not ans:
  39. return ArXiv.be_output("")
  40. try:
  41. sort_choices = {"relevance": arxiv.SortCriterion.Relevance,
  42. "lastUpdatedDate": arxiv.SortCriterion.LastUpdatedDate,
  43. 'submittedDate': arxiv.SortCriterion.SubmittedDate}
  44. arxiv_client = arxiv.Client()
  45. search = arxiv.Search(
  46. query=ans,
  47. max_results=self._param.top_n,
  48. sort_by=sort_choices[self._param.sort_by]
  49. )
  50. arxiv_res = [
  51. {"content": 'Title: ' + i.title + '\nPdf_Url: <a href="' + i.pdf_url + '"></a> \nSummary: ' + i.summary} for
  52. i in list(arxiv_client.results(search))]
  53. except Exception as e:
  54. return ArXiv.be_output("**ERROR**: " + str(e))
  55. if not arxiv_res:
  56. return ArXiv.be_output("")
  57. df = pd.DataFrame(arxiv_res)
  58. if DEBUG: print(df, ":::::::::::::::::::::::::::::::::")
  59. return df