Class: Langchain::Tool::Database

Inherits:
Object
  • Object
show all
Extended by:
Langchain::ToolDefinition
Includes:
DependencyHelper
Defined in:
lib/langchain/tool/database.rb

Overview

Connects to a SQL database, executes SQL queries, and outputs DB schema for Agents to use

Gem requirements:

gem "sequel", "~> 5.68.0"

Usage:

database = Langchain::Tool::Database.new(connection_string: "postgres://user:password@localhost:5432/db_name")

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Langchain::ToolDefinition

define_function, function_schemas, tool_name

Methods included from DependencyHelper

#depends_on

Constructor Details

#initialize(connection_string:, tables: [], exclude_tables: []) ⇒ Database

Establish a database connection

Parameters:

  • connection_string (String)

    Database connection info, e.g. ‘postgres://user:password@localhost:5432/db_name’

  • tables (Array<Symbol>) (defaults to: [])

    The tables to use. Will use all if empty.

  • except_tables (Array<Symbol>)

    The tables to exclude. Will exclude none if empty.

Raises:

  • (StandardError)


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

#dbObject (readonly)

Returns the value of attribute db.



31
32
33
# File 'lib/langchain/tool/database.rb', line 31

def db
  @db
end

#excluded_tablesObject (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_tablesObject (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

Parameters:

  • tables (Array<String>) (defaults to: [])

    The tables to describe.

Returns:

  • (String)

    The schema for the 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_schemaString

Database Tool: Returns the database schema

Returns:

  • (String)

    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

Parameters:

  • input (String)

    SQL query to be executed

Returns:

  • (Array)

    Results from the SQL query



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.message}")
  e.message # Return error to LLM
end

#list_tablesArray<Symbol>

Database Tool: Returns a list of tables in the database

Returns:

  • (Array<Symbol>)

    List of tables in the database



53
54
55
# File 'lib/langchain/tool/database.rb', line 53

def list_tables
  db.tables
end