Class: Langchain::Tool::Database
- Inherits:
-
Object
- Object
- Langchain::Tool::Database
- Extended by:
- Langchain::ToolDefinition
- Includes:
- DependencyHelper
- Defined in:
- lib/langchain/tool/database.rb
Overview
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#excluded_tables ⇒ Object
readonly
Returns the value of attribute excluded_tables.
-
#requested_tables ⇒ Object
readonly
Returns the value of attribute requested_tables.
Instance Method Summary collapse
-
#describe_tables(tables: []) ⇒ String
Database Tool: Returns the schema for a list of tables.
-
#dump_schema ⇒ String
Database Tool: Returns the database schema.
-
#execute(input:) ⇒ Array
Database Tool: Executes a SQL query and returns the results.
-
#initialize(connection_string:, tables: [], exclude_tables: []) ⇒ Database
constructor
Establish a database connection.
-
#list_tables ⇒ Array<Symbol>
Database Tool: Returns a list of tables in the database.
Methods included from Langchain::ToolDefinition
define_function, function_schemas, tool_name
Methods included from DependencyHelper
Constructor Details
#initialize(connection_string:, tables: [], exclude_tables: []) ⇒ Database
Establish a database connection
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/langchain/tool/database.rb', line 39 def initialize(connection_string:, tables: [], exclude_tables: []) depends_on "sequel" raise StandardError, "connection_string parameter cannot be blank" if connection_string.empty? @db = Sequel.connect(connection_string) # TODO: This is a bug, these 2 parameters are completely ignored. @requested_tables = tables @excluded_tables = exclude_tables end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
31 32 33 |
# File 'lib/langchain/tool/database.rb', line 31 def db @db end |
#excluded_tables ⇒ Object (readonly)
Returns the value of attribute excluded_tables.
31 32 33 |
# File 'lib/langchain/tool/database.rb', line 31 def excluded_tables @excluded_tables end |
#requested_tables ⇒ Object (readonly)
Returns the value of attribute requested_tables.
31 32 33 |
# File 'lib/langchain/tool/database.rb', line 31 def requested_tables @requested_tables end |
Instance Method Details
#describe_tables(tables: []) ⇒ String
Database Tool: Returns the schema for a list of tables
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/langchain/tool/database.rb', line 61 def describe_tables(tables: []) return "No tables specified" if tables.empty? Langchain.logger.debug("#{self.class} - Describing tables: #{tables}") tables .map do |table| describe_table(table) end .join("\n") end |
#dump_schema ⇒ String
Database Tool: Returns the database schema
76 77 78 79 80 81 82 83 |
# File 'lib/langchain/tool/database.rb', line 76 def dump_schema Langchain.logger.debug("#{self.class} - Dumping schema tables and keys") schemas = db.tables.map do |table| describe_table(table) end schemas.join("\n") end |
#execute(input:) ⇒ Array
Database Tool: Executes a SQL query and returns the results
89 90 91 92 93 94 95 96 |
# File 'lib/langchain/tool/database.rb', line 89 def execute(input:) Langchain.logger.debug("#{self.class} - Executing \"#{input}\"") db[input].to_a rescue Sequel::DatabaseError => e Langchain.logger.error("#{self.class} - #{e.}") e. # Return error to LLM end |
#list_tables ⇒ Array<Symbol>
Database Tool: Returns a list of tables in the database
53 54 55 |
# File 'lib/langchain/tool/database.rb', line 53 def list_tables db.tables end |