Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

code_exec.py 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #
  2. # Copyright 2025 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 base64
  17. import logging
  18. import os
  19. from abc import ABC
  20. from enum import StrEnum
  21. from typing import Optional
  22. from pydantic import BaseModel, Field, field_validator
  23. from agent.tools.base import ToolParamBase, ToolBase, ToolMeta
  24. from api import settings
  25. from api.utils.api_utils import timeout
  26. class Language(StrEnum):
  27. PYTHON = "python"
  28. NODEJS = "nodejs"
  29. class CodeExecutionRequest(BaseModel):
  30. code_b64: str = Field(..., description="Base64 encoded code string")
  31. language: str = Field(default=Language.PYTHON.value, description="Programming language")
  32. arguments: Optional[dict] = Field(default={}, description="Arguments")
  33. @field_validator("code_b64")
  34. @classmethod
  35. def validate_base64(cls, v: str) -> str:
  36. try:
  37. base64.b64decode(v, validate=True)
  38. return v
  39. except Exception as e:
  40. raise ValueError(f"Invalid base64 encoding: {str(e)}")
  41. @field_validator("language", mode="before")
  42. @classmethod
  43. def normalize_language(cls, v) -> str:
  44. if isinstance(v, str):
  45. low = v.lower()
  46. if low in ("python", "python3"):
  47. return "python"
  48. elif low in ("javascript", "nodejs"):
  49. return "nodejs"
  50. raise ValueError(f"Unsupported language: {v}")
  51. class CodeExecParam(ToolParamBase):
  52. """
  53. Define the code sandbox component parameters.
  54. """
  55. def __init__(self):
  56. self.meta:ToolMeta = {
  57. "name": "execute_code",
  58. "description": """
  59. This tool has a sandbox that can execute code written in 'Python'/'Javascript'. It recieves a piece of code and return a Json string.
  60. Here's a code example for Python(`main` function MUST be included):
  61. def main(arg1: str, arg2: str) -> dict:
  62. return {
  63. "result": arg1 + arg2,
  64. }
  65. Here's a code example for Javascript(`main` function MUST be included and exported):
  66. const axios = require('axios');
  67. async function main(args) {
  68. try {
  69. const response = await axios.get('https://github.com/infiniflow/ragflow');
  70. console.log('Body:', response.data);
  71. } catch (error) {
  72. console.error('Error:', error.message);
  73. }
  74. }
  75. module.exports = { main };
  76. """,
  77. "parameters": {
  78. "lang": {
  79. "type": "string",
  80. "description": "The programming language of this piece of code.",
  81. "enum": ["python", "javascript"],
  82. "required": True,
  83. },
  84. "script": {
  85. "type": "string",
  86. "description": "A piece of code in right format. There MUST be main function.",
  87. "required": True
  88. }
  89. }
  90. }
  91. super().__init__()
  92. self.lang = Language.PYTHON.value
  93. self.script = "def main(arg1: str, arg2: str) -> dict: return {\"result\": arg1 + arg2}"
  94. self.arguments = {}
  95. self.outputs = {"result": {"value": "", "type": "string"}}
  96. def check(self):
  97. self.check_valid_value(self.lang, "Support languages", ["python", "python3", "nodejs", "javascript"])
  98. self.check_empty(self.script, "Script")
  99. def get_input_form(self) -> dict[str, dict]:
  100. res = {}
  101. for k, v in self.arguments.items():
  102. res[k] = {
  103. "type": "line",
  104. "name": k
  105. }
  106. return res
  107. class CodeExec(ToolBase, ABC):
  108. component_name = "CodeExec"
  109. @timeout(os.environ.get("COMPONENT_EXEC_TIMEOUT", 10*60))
  110. def _invoke(self, **kwargs):
  111. lang = kwargs.get("lang", self._param.lang)
  112. script = kwargs.get("script", self._param.script)
  113. arguments = {}
  114. for k, v in self._param.arguments.items():
  115. if kwargs.get(k):
  116. arguments[k] = kwargs[k]
  117. continue
  118. arguments[k] = self._canvas.get_variable_value(v) if v else None
  119. self._execute_code(
  120. language=lang,
  121. code=script,
  122. arguments=arguments
  123. )
  124. def _execute_code(self, language: str, code: str, arguments: dict):
  125. import requests
  126. try:
  127. code_b64 = self._encode_code(code)
  128. code_req = CodeExecutionRequest(code_b64=code_b64, language=language, arguments=arguments).model_dump()
  129. except Exception as e:
  130. self.set_output("_ERROR", "construct code request error: " + str(e))
  131. try:
  132. resp = requests.post(url=f"http://{settings.SANDBOX_HOST}:9385/run", json=code_req, timeout=10)
  133. logging.info(f"http://{settings.SANDBOX_HOST}:9385/run", code_req, resp.status_code)
  134. if resp.status_code != 200:
  135. resp.raise_for_status()
  136. body = resp.json()
  137. if body:
  138. stderr = body.get("stderr")
  139. if stderr:
  140. self.set_output("_ERROR", stderr)
  141. return
  142. try:
  143. rt = eval(body.get("stdout", ""))
  144. except Exception:
  145. rt = body.get("stdout", "")
  146. logging.info(f"http://{settings.SANDBOX_HOST}:9385/run -> {rt}")
  147. if isinstance(rt, tuple):
  148. for i, (k, o) in enumerate(self._param.outputs.items()):
  149. if k.find("_") == 0:
  150. continue
  151. o["value"] = rt[i]
  152. elif isinstance(rt, dict):
  153. for i, (k, o) in enumerate(self._param.outputs.items()):
  154. if k not in rt or k.find("_") == 0:
  155. continue
  156. o["value"] = rt[k]
  157. else:
  158. for i, (k, o) in enumerate(self._param.outputs.items()):
  159. if k.find("_") == 0:
  160. continue
  161. o["value"] = rt
  162. else:
  163. self.set_output("_ERROR", "There is no response from sandbox")
  164. except Exception as e:
  165. self.set_output("_ERROR", "Exception executing code: " + str(e))
  166. return self.output()
  167. def _encode_code(self, code: str) -> str:
  168. return base64.b64encode(code.encode("utf-8")).decode("utf-8")
  169. def thoughts(self) -> str:
  170. return "Running a short script to process data."