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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. import logging
  17. from abc import ABC
  18. import arxiv
  19. import pandas as pd
  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. logging.debug(f"df: {str(df)}")
  59. return df