您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

exesql.py 5.1KB

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 os
  17. from abc import ABC
  18. import pandas as pd
  19. import pymysql
  20. import psycopg2
  21. import pyodbc
  22. from agent.tools.base import ToolParamBase, ToolBase, ToolMeta
  23. from api.utils.api_utils import timeout
  24. class ExeSQLParam(ToolParamBase):
  25. """
  26. Define the ExeSQL component parameters.
  27. """
  28. def __init__(self):
  29. self.meta:ToolMeta = {
  30. "name": "execute_sql",
  31. "description": "This is a tool that can execute SQL.",
  32. "parameters": {
  33. "sql": {
  34. "type": "string",
  35. "description": "The SQL needs to be executed.",
  36. "default": "{sys.query}",
  37. "required": True
  38. }
  39. }
  40. }
  41. super().__init__()
  42. self.db_type = "mysql"
  43. self.database = ""
  44. self.username = ""
  45. self.host = ""
  46. self.port = 3306
  47. self.password = ""
  48. self.max_records = 1024
  49. def check(self):
  50. self.check_valid_value(self.db_type, "Choose DB type", ['mysql', 'postgresql', 'mariadb', 'mssql'])
  51. self.check_empty(self.database, "Database name")
  52. self.check_empty(self.username, "database username")
  53. self.check_empty(self.host, "IP Address")
  54. self.check_positive_integer(self.port, "IP Port")
  55. self.check_empty(self.password, "Database password")
  56. self.check_positive_integer(self.max_records, "Maximum number of records")
  57. if self.database == "rag_flow":
  58. if self.host == "ragflow-mysql":
  59. raise ValueError("For the security reason, it dose not support database named rag_flow.")
  60. if self.password == "infini_rag_flow":
  61. raise ValueError("For the security reason, it dose not support database named rag_flow.")
  62. def get_input_form(self) -> dict[str, dict]:
  63. return {
  64. "sql": {
  65. "name": "SQL",
  66. "type": "line"
  67. }
  68. }
  69. class ExeSQL(ToolBase, ABC):
  70. component_name = "ExeSQL"
  71. @timeout(os.environ.get("COMPONENT_EXEC_TIMEOUT", 60))
  72. def _invoke(self, **kwargs):
  73. sql = kwargs.get("sql")
  74. if not sql:
  75. raise Exception("SQL for `ExeSQL` MUST not be empty.")
  76. sqls = sql.split(";")
  77. if self._param.db_type in ["mysql", "mariadb"]:
  78. db = pymysql.connect(db=self._param.database, user=self._param.username, host=self._param.host,
  79. port=self._param.port, password=self._param.password)
  80. elif self._param.db_type == 'postgresql':
  81. db = psycopg2.connect(dbname=self._param.database, user=self._param.username, host=self._param.host,
  82. port=self._param.port, password=self._param.password)
  83. elif self._param.db_type == 'mssql':
  84. conn_str = (
  85. r'DRIVER={ODBC Driver 17 for SQL Server};'
  86. r'SERVER=' + self._param.host + ',' + str(self._param.port) + ';'
  87. r'DATABASE=' + self._param.database + ';'
  88. r'UID=' + self._param.username + ';'
  89. r'PWD=' + self._param.password
  90. )
  91. db = pyodbc.connect(conn_str)
  92. try:
  93. cursor = db.cursor()
  94. except Exception as e:
  95. raise Exception("Database Connection Failed! \n" + str(e))
  96. sql_res = []
  97. formalized_content = []
  98. for single_sql in sqls:
  99. single_sql = single_sql.replace('```','')
  100. if not single_sql:
  101. continue
  102. cursor.execute(single_sql)
  103. if cursor.rowcount == 0:
  104. sql_res.append({"content": "No record in the database!"})
  105. break
  106. if self._param.db_type == 'mssql':
  107. single_res = pd.DataFrame.from_records(cursor.fetchmany(self._param.max_records),
  108. columns=[desc[0] for desc in cursor.description])
  109. else:
  110. single_res = pd.DataFrame([i for i in cursor.fetchmany(self._param.max_records)])
  111. single_res.columns = [i[0] for i in cursor.description]
  112. sql_res.append(single_res.to_dict(orient='records'))
  113. formalized_content.append(single_res.to_markdown(index=False, floatfmt=".6f"))
  114. self.set_output("json", sql_res)
  115. self.set_output("formalized_content", "\n\n".join(formalized_content))
  116. return self.output("formalized_content")
  117. def thoughts(self) -> str:
  118. return "Query sent—waiting for the data."