Module: Leann::Rails

Defined in:
lib/leann/rails.rb,
lib/leann/rails/builder.rb,
lib/leann/rails/railtie.rb,
lib/leann/rails/searcher.rb,
lib/leann/rails/active_record/index.rb,
lib/leann/rails/active_record/passage.rb,
lib/leann/rails/storage/active_record_backend.rb

Defined Under Namespace

Classes: ActiveRecordBackend, Builder, Index, Passage, Railtie, Searcher

Class Method Summary collapse

Class Method Details

.build(name, **options, &block) ⇒ Leann::Rails::Index

Build a new index stored in the database

Examples:

Leann::Rails.build("products") do
  add "Red running shoes for athletes", category: "shoes"
  add "Blue denim jeans, slim fit", category: "pants"
end

Parameters:

  • name (String)

    Index name (unique identifier)

  • options (Hash)

    Options for building

Options Hash (**options):

  • :embedding (Symbol) — default: :openai

    Embedding provider

  • :model (String)

    Embedding model name

Returns:



28
29
30
31
32
# File 'lib/leann/rails.rb', line 28

def build(name, **options, &block)
  builder = Builder.new(name, **options)
  builder.instance_eval(&block) if block_given?
  builder.save
end

.delete(name) ⇒ Boolean

Delete an index and all its passages

Parameters:

  • name (String)

    Index name

Returns:

  • (Boolean)


72
73
74
75
76
77
78
# File 'lib/leann/rails.rb', line 72

def delete(name)
  index = Index.find_by(name: name)
  return false unless index

  index.destroy
  true
end

.exists?(name) ⇒ Boolean

Check if an index exists

Parameters:

  • name (String)

    Index name

Returns:

  • (Boolean)


64
65
66
# File 'lib/leann/rails.rb', line 64

def exists?(name)
  Index.exists?(name: name)
end

.listArray<String>

List all indexes

Returns:

  • (Array<String>)


83
84
85
# File 'lib/leann/rails.rb', line 83

def list
  Index.pluck(:name).sort
end

.open(name) ⇒ Leann::Rails::Index

Open an existing index

Parameters:

  • name (String)

    Index name

Returns:



56
57
58
# File 'lib/leann/rails.rb', line 56

def open(name)
  Index.find_by!(name: name)
end

.search(name, query, limit: 5, threshold: nil, filters: nil) ⇒ Leann::SearchResults

Search an existing database index

Examples:

results = Leann::Rails.search("products", "comfortable shoes")

Parameters:

  • name (String)

    Index name

  • query (String)

    Search query

  • limit (Integer) (defaults to: 5)

    Maximum results

  • threshold (Float) (defaults to: nil)

    Minimum similarity score

  • filters (Hash) (defaults to: nil)

    Metadata filters

Returns:



46
47
48
49
50
# File 'lib/leann/rails.rb', line 46

def search(name, query, limit: 5, threshold: nil, filters: nil)
  index = Index.find_by!(name: name)
  searcher = Searcher.new(index)
  searcher.search(query, limit: limit, threshold: threshold, filters: filters)
end