2024-01-04 08:44:07 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module AiBot
|
|
|
|
module Tools
|
|
|
|
class DbSchema < Tool
|
|
|
|
def self.signature
|
|
|
|
{
|
|
|
|
name: name,
|
|
|
|
description: "Will load schema information for specific tables in the database",
|
|
|
|
parameters: [
|
|
|
|
{
|
|
|
|
name: "tables",
|
|
|
|
description:
|
2024-01-19 06:51:26 -05:00
|
|
|
"list of tables to load schema information for, comma separated list eg: (users,posts))",
|
2024-01-04 08:44:07 -05:00
|
|
|
type: "string",
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.name
|
|
|
|
"schema"
|
|
|
|
end
|
|
|
|
|
|
|
|
def tables
|
|
|
|
parameters[:tables]
|
|
|
|
end
|
|
|
|
|
|
|
|
def invoke(_bot_user, _llm)
|
|
|
|
tables_arr = tables.split(",").map(&:strip)
|
|
|
|
|
|
|
|
table_info = {}
|
|
|
|
DB
|
|
|
|
.query(<<~SQL, tables_arr)
|
|
|
|
select table_name, column_name, data_type from information_schema.columns
|
|
|
|
where table_schema = 'public'
|
|
|
|
and table_name in (?)
|
|
|
|
order by table_name
|
|
|
|
SQL
|
|
|
|
.each do |row|
|
|
|
|
(table_info[row.table_name] ||= []) << "#{row.column_name} #{row.data_type}"
|
|
|
|
end
|
|
|
|
|
|
|
|
schema_info =
|
|
|
|
table_info
|
|
|
|
.map { |table_name, columns| "#{table_name}(#{columns.join(",")})" }
|
|
|
|
.join("\n")
|
|
|
|
|
|
|
|
{ schema_info: schema_info, tables: tables }
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def description_args
|
2024-01-04 09:50:26 -05:00
|
|
|
{ tables: tables }
|
2024-01-04 08:44:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|