{"version":3,"file":"sql.cjs","names":["Tool","PromptTemplate","LLMChain"],"sources":["../../src/tools/sql.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport { Tool } from \"@langchain/core/tools\";\nimport { PromptTemplate } from \"@langchain/core/prompts\";\nimport { LLMChain } from \"../chains/llm_chain.js\";\nimport type { SqlDatabase } from \"../sql_db.js\";\nimport { SqlTable } from \"../util/sql_utils.js\";\n\n/**\n * Interface for SQL tools. It has a `db` property which is a SQL\n * database.\n */\ninterface SqlTool {\n  db: SqlDatabase;\n}\n\n/**\n * A tool for executing SQL queries. It takes a SQL database as a\n * parameter and assigns it to the `db` property. The `_call` method is\n * used to run the SQL query and return the result. If the query is\n * incorrect, an error message is returned.\n */\nexport class QuerySqlTool extends Tool implements SqlTool {\n  static lc_name() {\n    return \"QuerySqlTool\";\n  }\n\n  name = \"query-sql\";\n\n  db: SqlDatabase;\n\n  constructor(db: SqlDatabase) {\n    super(...arguments);\n    this.db = db;\n  }\n\n  /** @ignore */\n  async _call(input: string) {\n    try {\n      return await this.db.run(input);\n    } catch (error) {\n      return `${error}`;\n    }\n  }\n\n  description = `Input to this tool is a detailed and correct SQL query, output is a result from the database.\n  If the query is not correct, an error message will be returned.\n  If an error is returned, rewrite the query, check the query, and try again.`;\n}\n\n/**\n * A tool for retrieving information about SQL tables. It takes a SQL\n * database as a parameter and assigns it to the `db` property. The\n * `_call` method is used to retrieve the schema and sample rows for the\n * specified tables. If the tables do not exist, an error message is\n * returned.\n */\nexport class InfoSqlTool extends Tool implements SqlTool {\n  static lc_name() {\n    return \"InfoSqlTool\";\n  }\n\n  name = \"info-sql\";\n\n  db: SqlDatabase;\n\n  constructor(db: SqlDatabase) {\n    super();\n    this.db = db;\n  }\n\n  /** @ignore */\n  async _call(input: string) {\n    try {\n      const tables = input.split(\",\").map((table) => table.trim());\n      return await this.db.getTableInfo(tables);\n    } catch (error) {\n      return `${error}`;\n    }\n  }\n\n  description = `Input to this tool is a comma-separated list of tables, output is the schema and sample rows for those tables.\n    Be sure that the tables actually exist by calling list-tables-sql first!\n\n    Example Input: \"table1, table2, table3.`;\n}\n\n/**\n * A tool for listing all tables in a SQL database. It takes a SQL\n * database as a parameter and assigns it to the `db` property. The\n * `_call` method is used to return a comma-separated list of all tables\n * in the database.\n */\nexport class ListTablesSqlTool extends Tool implements SqlTool {\n  static lc_name() {\n    return \"ListTablesSqlTool\";\n  }\n\n  name = \"list-tables-sql\";\n\n  db: SqlDatabase;\n\n  constructor(db: SqlDatabase) {\n    super();\n    this.db = db;\n  }\n\n  async _call(_: string) {\n    try {\n      let selectedTables: SqlTable[] = this.db.allTables;\n\n      if (this.db.includesTables.length > 0) {\n        selectedTables = selectedTables.filter((currentTable) =>\n          this.db.includesTables.includes(currentTable.tableName)\n        );\n      }\n\n      if (this.db.ignoreTables.length > 0) {\n        selectedTables = selectedTables.filter(\n          (currentTable) =>\n            !this.db.ignoreTables.includes(currentTable.tableName)\n        );\n      }\n\n      const tables = selectedTables.map((table: SqlTable) => table.tableName);\n      return tables.join(\", \");\n    } catch (error) {\n      return `${error}`;\n    }\n  }\n\n  description = `Input is an empty string, output is a comma-separated list of tables in the database.`;\n}\n\n/**\n * Arguments for the QueryCheckerTool class.\n */\ntype QueryCheckerToolArgs = {\n  llmChain?: LLMChain;\n  llm: BaseLanguageModelInterface;\n  _chainType?: never;\n};\n\n/**\n * A tool for checking SQL queries for common mistakes. It takes a\n * LLMChain or QueryCheckerToolArgs as a parameter. The `_call` method is\n * used to check the input query for common mistakes and returns a\n * prediction.\n */\nexport class QueryCheckerTool extends Tool {\n  static lc_name() {\n    return \"QueryCheckerTool\";\n  }\n\n  name = \"query-checker\";\n\n  template = `\n    {query}\nDouble check the SQL query above for common mistakes, including:\n- Using NOT IN with NULL values\n- Using UNION when UNION ALL should have been used\n- Using BETWEEN for exclusive ranges\n- Data type mismatch in predicates\n- Properly quoting identifiers\n- Using the correct number of arguments for functions\n- Casting to the correct data type\n- Using the proper columns for joins\n\nIf there are any of the above mistakes, rewrite the query. If there are no mistakes, just reproduce the original query.`;\n\n  llmChain: LLMChain;\n\n  constructor(llmChainOrOptions?: LLMChain | QueryCheckerToolArgs) {\n    super();\n    if (typeof llmChainOrOptions?._chainType === \"function\") {\n      this.llmChain = llmChainOrOptions as LLMChain;\n    } else {\n      const options = llmChainOrOptions as QueryCheckerToolArgs;\n      if (options?.llmChain !== undefined) {\n        this.llmChain = options.llmChain;\n      } else {\n        const prompt = new PromptTemplate({\n          template: this.template,\n          inputVariables: [\"query\"],\n        });\n        const llm = options.llm;\n        this.llmChain = new LLMChain({ llm, prompt });\n      }\n    }\n  }\n\n  /** @ignore */\n  async _call(input: string) {\n    return this.llmChain.predict({ query: input });\n  }\n\n  description = `Use this tool to double check if your query is correct before executing it.\n    Always use this tool before executing a query with query-sql!`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAqBA,IAAa,eAAb,cAAkCA,sBAAAA,KAAwB;CACxD,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP;CAEA,YAAY,IAAiB;AAC3B,QAAM,GAAG,UAAU;AACnB,OAAK,KAAK;;;CAIZ,MAAM,MAAM,OAAe;AACzB,MAAI;AACF,UAAO,MAAM,KAAK,GAAG,IAAI,MAAM;WACxB,OAAO;AACd,UAAO,GAAG;;;CAId,cAAc;;;;;;;;;;;AAYhB,IAAa,cAAb,cAAiCA,sBAAAA,KAAwB;CACvD,OAAO,UAAU;AACf,SAAO;;CAGT,OAAO;CAEP;CAEA,YAAY,IAAiB;AAC3B,SAAO;AACP,OAAK,KAAK;;;CAIZ,MAAM,MAAM,OAAe;AACzB,MAAI;GACF,MAAM,SAAS,MAAM,MAAM,IAAI,CAAC,KAAK,UAAU,MAAM,MAAM,CAAC;AAC5D,UAAO,MAAM,KAAK,GAAG,aAAa,OAAO;WAClC,OAAO;AACd,UAAO,GAAG;;;CAId,cAAc;;;;;;;;;;;AAYhB,IAAa,oBAAb,cAAuCA,sBAAAA,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