Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

invoke.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 json
  17. import re
  18. from abc import ABC
  19. import requests
  20. from deepdoc.parser import HtmlParser
  21. from agent.component.base import ComponentBase, ComponentParamBase
  22. class InvokeParam(ComponentParamBase):
  23. """
  24. Define the Crawler component parameters.
  25. """
  26. def __init__(self):
  27. super().__init__()
  28. self.proxy = None
  29. self.headers = ""
  30. self.method = "get"
  31. self.variables = []
  32. self.url = ""
  33. self.timeout = 60
  34. self.clean_html = False
  35. def check(self):
  36. self.check_valid_value(self.method.lower(), "Type of content from the crawler", ['get', 'post', 'put'])
  37. self.check_empty(self.url, "End point URL")
  38. self.check_positive_integer(self.timeout, "Timeout time in second")
  39. self.check_boolean(self.clean_html, "Clean HTML")
  40. class Invoke(ComponentBase, ABC):
  41. component_name = "Invoke"
  42. def _run(self, history, **kwargs):
  43. args = {}
  44. for para in self._param.variables:
  45. if para.get("component_id"):
  46. cpn = self._canvas.get_component(para["component_id"])["obj"]
  47. _, out = cpn.output(allow_partial=False)
  48. args[para["key"]] = "\n".join(out["content"])
  49. else:
  50. args[para["key"]] = "\n".join(para["value"])
  51. url = self._param.url.strip()
  52. if url.find("http") != 0:
  53. url = "http://" + url
  54. method = self._param.method.lower()
  55. headers = {}
  56. if self._param.headers:
  57. headers = json.loads(self._param.headers)
  58. proxies = None
  59. if re.sub(r"https?:?/?/?", "", self._param.proxy):
  60. proxies = {"http": self._param.proxy, "https": self._param.proxy}
  61. if method == 'get':
  62. response = requests.get(url=url,
  63. params=args,
  64. headers=headers,
  65. proxies=proxies,
  66. timeout=self._param.timeout)
  67. if self._param.clean_html:
  68. sections = HtmlParser()(None, response.content)
  69. return Invoke.be_output("\n".join(sections))
  70. return Invoke.be_output(response.text)
  71. if method == 'put':
  72. response = requests.put(url=url,
  73. data=args,
  74. headers=headers,
  75. proxies=proxies,
  76. timeout=self._param.timeout)
  77. if self._param.clean_html:
  78. sections = HtmlParser()(None, response.content)
  79. return Invoke.be_output("\n".join(sections))
  80. return Invoke.be_output(response.text)
  81. if method == 'post':
  82. response = requests.post(url=url,
  83. json=args,
  84. headers=headers,
  85. proxies=proxies,
  86. timeout=self._param.timeout)
  87. if self._param.clean_html:
  88. sections = HtmlParser()(None, response.content)
  89. return Invoke.be_output("\n".join(sections))
  90. return Invoke.be_output(response.text)