Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. import os
  18. import time
  19. from abc import ABC
  20. import requests
  21. from agent.tools.base import ToolParamBase, ToolMeta, ToolBase
  22. from api.utils.api_utils import timeout
  23. class GitHubParam(ToolParamBase):
  24. """
  25. Define the GitHub component parameters.
  26. """
  27. def __init__(self):
  28. self.meta:ToolMeta = {
  29. "name": "github_search",
  30. "description": """GitHub repository search is a feature that enables users to find specific repositories on the GitHub platform. This search functionality allows users to locate projects, codebases, and other content hosted on GitHub based on various criteria.""",
  31. "parameters": {
  32. "query": {
  33. "type": "string",
  34. "description": "The search keywords to execute with GitHub. The keywords should be the most important words/terms(includes synonyms) from the original request.",
  35. "default": "{sys.query}",
  36. "required": True
  37. }
  38. }
  39. }
  40. super().__init__()
  41. self.top_n = 10
  42. def check(self):
  43. self.check_positive_integer(self.top_n, "Top N")
  44. def get_input_form(self) -> dict[str, dict]:
  45. return {
  46. "query": {
  47. "name": "Query",
  48. "type": "line"
  49. }
  50. }
  51. class GitHub(ToolBase, ABC):
  52. component_name = "GitHub"
  53. @timeout(os.environ.get("COMPONENT_EXEC_TIMEOUT", 12))
  54. def _invoke(self, **kwargs):
  55. if not kwargs.get("query"):
  56. self.set_output("formalized_content", "")
  57. return ""
  58. last_e = ""
  59. for _ in range(self._param.max_retries+1):
  60. try:
  61. url = 'https://api.github.com/search/repositories?q=' + kwargs["query"] + '&sort=stars&order=desc&per_page=' + str(
  62. self._param.top_n)
  63. headers = {"Content-Type": "application/vnd.github+json", "X-GitHub-Api-Version": '2022-11-28'}
  64. response = requests.get(url=url, headers=headers).json()
  65. self._retrieve_chunks(response['items'],
  66. get_title=lambda r: r["name"],
  67. get_url=lambda r: r["html_url"],
  68. get_content=lambda r: str(r["description"]) + '\n stars:' + str(r['watchers']))
  69. self.set_output("json", response['items'])
  70. return self.output("formalized_content")
  71. except Exception as e:
  72. last_e = e
  73. logging.exception(f"GitHub error: {e}")
  74. time.sleep(self._param.delay_after_error)
  75. if last_e:
  76. self.set_output("_ERROR", str(last_e))
  77. return f"GitHub error: {last_e}"
  78. assert False, self.output()
  79. def thoughts(self) -> str:
  80. return "Scanning GitHub repos related to `{}`.".format(self.get_input().get("query", "-_-!"))