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ů.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. self.datatype = "json" # New parameter to determine data posting type
  36. def check(self):
  37. self.check_valid_value(self.method.lower(), "Type of content from the crawler", ['get', 'post', 'put'])
  38. self.check_empty(self.url, "End point URL")
  39. self.check_positive_integer(self.timeout, "Timeout time in second")
  40. self.check_boolean(self.clean_html, "Clean HTML")
  41. self.check_valid_value(self.datatype.lower(), "Data post type", ['json', 'formdata']) # Check for valid datapost value
  42. class Invoke(ComponentBase, ABC):
  43. component_name = "Invoke"
  44. def _run(self, history, **kwargs):
  45. args = {}
  46. for para in self._param.variables:
  47. if para.get("component_id"):
  48. if '@' in para["component_id"]:
  49. component = para["component_id"].split('@')[0]
  50. field = para["component_id"].split('@')[1]
  51. cpn = self._canvas.get_component(component)["obj"]
  52. for param in cpn._param.query:
  53. if param["key"] == field:
  54. if "value" in param:
  55. args[para["key"]] = param["value"]
  56. else:
  57. cpn = self._canvas.get_component(para["component_id"])["obj"]
  58. if cpn.component_name.lower() == "answer":
  59. args[para["key"]] = self._canvas.get_history(1)[0]["content"]
  60. continue
  61. _, out = cpn.output(allow_partial=False)
  62. if not out.empty:
  63. args[para["key"]] = "\n".join(out["content"])
  64. else:
  65. args[para["key"]] = para["value"]
  66. url = self._param.url.strip()
  67. if url.find("http") != 0:
  68. url = "http://" + url
  69. method = self._param.method.lower()
  70. headers = {}
  71. if self._param.headers:
  72. headers = json.loads(self._param.headers)
  73. proxies = None
  74. if re.sub(r"https?:?/?/?", "", self._param.proxy):
  75. proxies = {"http": self._param.proxy, "https": self._param.proxy}
  76. if method == 'get':
  77. response = requests.get(url=url,
  78. params=args,
  79. headers=headers,
  80. proxies=proxies,
  81. timeout=self._param.timeout)
  82. if self._param.clean_html:
  83. sections = HtmlParser()(None, response.content)
  84. return Invoke.be_output("\n".join(sections))
  85. return Invoke.be_output(response.text)
  86. if method == 'put':
  87. if self._param.datatype.lower() == 'json':
  88. response = requests.put(url=url,
  89. json=args,
  90. headers=headers,
  91. proxies=proxies,
  92. timeout=self._param.timeout)
  93. else:
  94. response = requests.put(url=url,
  95. data=args,
  96. headers=headers,
  97. proxies=proxies,
  98. timeout=self._param.timeout)
  99. if self._param.clean_html:
  100. sections = HtmlParser()(None, response.content)
  101. return Invoke.be_output("\n".join(sections))
  102. return Invoke.be_output(response.text)
  103. if method == 'post':
  104. if self._param.datatype.lower() == 'json':
  105. response = requests.post(url=url,
  106. json=args,
  107. headers=headers,
  108. proxies=proxies,
  109. timeout=self._param.timeout)
  110. else:
  111. response = requests.post(url=url,
  112. data=args,
  113. headers=headers,
  114. proxies=proxies,
  115. timeout=self._param.timeout)
  116. if self._param.clean_html:
  117. sections = HtmlParser()(None, response.content)
  118. return Invoke.be_output("\n".join(sections))
  119. return Invoke.be_output(response.text)