Class: LangchainrbRails::Generators::PgvectorGenerator

Inherits:
BaseGenerator
  • Object
show all
Defined in:
lib/langchainrb_rails/generators/langchainrb_rails/pgvector_generator.rb

Overview

Usage:

rails generate langchainrb_rails:pgvector --model=Product --llm=openai

Constant Summary

Constants inherited from BaseGenerator

BaseGenerator::LLMS

Instance Method Summary collapse

Methods inherited from BaseGenerator

#after_generate, #post_install_message

Instance Method Details

#add_to_gemfileObject



32
33
34
35
36
# File 'lib/langchainrb_rails/generators/langchainrb_rails/pgvector_generator.rb', line 32

def add_to_gemfile
  # Dependency for Langchain PgVector
  gem "neighbor"
  gem "ruby-openai"
end

#add_to_modelObject



26
27
28
29
30
# File 'lib/langchainrb_rails/generators/langchainrb_rails/pgvector_generator.rb', line 26

def add_to_model
  inject_into_class "app/models/#{model_name.underscore}.rb", model_name do
    "  vectorsearch\n\n  after_save :upsert_to_vectorsearch\n\n"
  end
end

#copy_migrationObject



13
14
15
16
# File 'lib/langchainrb_rails/generators/langchainrb_rails/pgvector_generator.rb', line 13

def copy_migration
  migration_template "enable_vector_extension_template.rb", "db/migrate/enable_vector_extension.rb", migration_version: migration_version
  migration_template "add_vector_column_template.rb", "db/migrate/add_vector_column_to_#{table_name}.rb", migration_version: migration_version
end

#create_initializer_fileObject



18
19
20
# File 'lib/langchainrb_rails/generators/langchainrb_rails/pgvector_generator.rb', line 18

def create_initializer_file
  template "pgvector_initializer.rb", "config/initializers/langchainrb_rails.rb"
end

#migration_versionObject



22
23
24
# File 'lib/langchainrb_rails/generators/langchainrb_rails/pgvector_generator.rb', line 22

def migration_version
  "[#{::ActiveRecord::VERSION::MAJOR}.#{::ActiveRecord::VERSION::MINOR}]"
end