Module: AsktiveRecord

Extended by:
Service::ClassMethods
Defined in:
lib/asktive_record.rb,
lib/asktive_record/log.rb,
lib/asktive_record/error.rb,
lib/asktive_record/model.rb,
lib/asktive_record/query.rb,
lib/asktive_record/prompt.rb,
lib/asktive_record/service.rb,
lib/asktive_record/version.rb,
lib/asktive_record/llm_service.rb,
lib/asktive_record/adapters/base.rb,
lib/asktive_record/configuration.rb,
lib/asktive_record/schema_loader.rb,
lib/asktive_record/sql_sanitizer.rb,
lib/asktive_record/adapters/openai.rb,
lib/generators/asktive_record/setup_generator.rb,
lib/generators/asktive_record/install_generator.rb

Overview

The AsktiveRecord module provides natural language querying capabilities and service/model extensions for ActiveRecord-like classes.

Defined Under Namespace

Modules: Adapters, Generators, Log, Model, SchemaLoader, Service Classes: ApiError, Configuration, ConfigurationError, Error, LlmService, Prompt, Query, QueryExecutionError, QueryGenerationError, SanitizationError, SqlSanitizer

Constant Summary collapse

VERSION =
"0.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Service::ClassMethods

ask

Methods included from SchemaLoader

#ensure_schema_is_not_empty!, #load_schema_content, #validate_llm_api_key!

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



18
19
20
# File 'lib/asktive_record.rb', line 18

def configuration
  @configuration
end

Class Method Details

.ask(natural_language_query, options = {}) ⇒ Object

Class method to allow direct querying from AsktiveRecord module



45
46
47
48
49
50
51
52
53
# File 'lib/asktive_record.rb', line 45

def self.ask(natural_language_query, options = {})
  validate_configuration!
  schema_content = load_schema_for_module
  llm_service = AsktiveRecord::LlmService.new(configuration)
  target_table = options[:table_name] || "any"
  raw_sql = llm_service.generate_sql_for_service(natural_language_query, schema_content, target_table)
  target_model = options[:model] || self
  AsktiveRecord::Query.new(natural_language_query, raw_sql, target_model)
end

.configure {|configuration| ... } ⇒ Object

Yields:



27
28
29
30
# File 'lib/asktive_record.rb', line 27

def self.configure
  self.configuration ||= Configuration.new
  yield(configuration)
end

.included(base) ⇒ Object

Main entry point for ApplicationRecord to include AsktiveRecord behavior



33
34
35
36
37
38
39
40
41
42
# File 'lib/asktive_record.rb', line 33

def self.included(base)
  # If the base class is a service class (inherits from AsktiveRecord directly),
  # extend it with Service module methods
  if base.superclass == Object || base.superclass.name == "Object"
    base.extend(Service::ClassMethods)
  else
    # Otherwise, it's likely an ActiveRecord model, so extend with Model module
    base.extend(Model::ClassMethods)
  end
end

.loggerAsktiveRecord::Log

Provides access to the logger instance

Returns:



22
23
24
# File 'lib/asktive_record.rb', line 22

def logger
  AsktiveRecord::Log
end