Class: LLMDB::Tools::ExecuteQuery

Inherits:
RubyLLM::Tool
  • Object
show all
Defined in:
lib/llmdb/tools/execute_query.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ ExecuteQuery

Returns a new instance of ExecuteQuery.



15
16
17
18
# File 'lib/llmdb/tools/execute_query.rb', line 15

def initialize(connection)
  super()
  @connection = connection
end

Instance Method Details

#execute(sql:) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/llmdb/tools/execute_query.rb', line 20

def execute(sql:)
  rows = @connection.execute(sql)
  return "Query returned no rows." if rows.empty?

  total  = rows.length
  suffix = total == 1 ? "row" : "rows"
  "#{total} #{suffix}:\n#{JSON.generate(rows)}"
rescue LLMDB::SafetyError => e
  { error: "Blocked: #{e.message}" }
rescue LLMDB::QueryError => e
  { error: "Query error: #{e.message}" }
end