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.

invoke.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. if cpn.component_name.lower() == "answer":
  48. args[para["key"]] = self._canvas.get_history(1)[0]["content"]
  49. continue
  50. _, out = cpn.output(allow_partial=False)
  51. args[para["key"]] = "\n".join(out["content"])
  52. else:
  53. args[para["key"]] = "\n".join(para["value"])
  54. url = self._param.url.strip()
  55. if url.find("http") != 0:
  56. url = "http://" + url
  57. method = self._param.method.lower()
  58. headers = {}
  59. if self._param.headers:
  60. headers = json.loads(self._param.headers)
  61. proxies = None
  62. if re.sub(r"https?:?/?/?", "", self._param.proxy):
  63. proxies = {"http": self._param.proxy, "https": self._param.proxy}
  64. if method == 'get':
  65. response = requests.get(url=url,
  66. params=args,
  67. headers=headers,
  68. proxies=proxies,
  69. timeout=self._param.timeout)
  70. if self._param.clean_html:
  71. sections = HtmlParser()(None, response.content)
  72. return Invoke.be_output("\n".join(sections))
  73. return Invoke.be_output(response.text)
  74. if method == 'put':
  75. response = requests.put(url=url,
  76. data=args,
  77. headers=headers,
  78. proxies=proxies,
  79. timeout=self._param.timeout)
  80. if self._param.clean_html:
  81. sections = HtmlParser()(None, response.content)
  82. return Invoke.be_output("\n".join(sections))
  83. return Invoke.be_output(response.text)
  84. if method == 'post':
  85. response = requests.post(url=url,
  86. json=args,
  87. headers=headers,
  88. proxies=proxies,
  89. timeout=self._param.timeout)
  90. if self._param.clean_html:
  91. sections = HtmlParser()(None, response.content)
  92. return Invoke.be_output("\n".join(sections))
  93. return Invoke.be_output(response.text)