Module: Leann

Defined in:
lib/leann.rb,
lib/leann/index.rb,
lib/leann/rails.rb,
lib/leann/errors.rb,
lib/leann/builder.rb,
lib/leann/version.rb,
lib/leann/searcher.rb,
lib/leann/backend/base.rb,
lib/leann/configuration.rb,
lib/leann/rails/builder.rb,
lib/leann/rails/railtie.rb,
lib/leann/search_result.rb,
lib/leann/embedding/base.rb,
lib/leann/rails/searcher.rb,
lib/leann/ruby_llm/search.rb,
lib/leann/embedding/ollama.rb,
lib/leann/embedding/openai.rb,
lib/leann/embedding/ruby_llm.rb,
lib/leann/backend/leann_graph.rb,
lib/leann/embedding/fastembed.rb,
lib/leann/rails/active_record/index.rb,
lib/leann/rails/active_record/passage.rb,
lib/leann/rails/storage/active_record_backend.rb,
lib/generators/leann/install/install_generator.rb

Overview

LEANN - Lightweight Embedding-Aware Neural Neighbor search

A Ruby gem for building and searching vector indexes with minimal storage. Stores only the graph structure, achieving 85-96% storage savings by recomputing embeddings on-the-fly during search.

Examples:

Quick start - build an index

Leann.build("knowledge_base") do
  add "LEANN saves 85-96% storage compared to traditional vector databases."
  add "It uses graph-only storage with on-demand recomputation."
end

Search

results = Leann.search("knowledge_base", "storage savings")
results.each { |r| puts "#{r.score}: #{r.text}" }

Defined Under Namespace

Modules: Backend, Embedding, Generators, Rails, RubyLLM Classes: Builder, Configuration, ConfigurationError, CorruptedIndexError, EmbeddingError, EmptyIndexError, Error, Index, IndexExistsError, IndexNotFoundError, LLMError, SearchResult, SearchResults, Searcher

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

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

Build a new index with a DSL block

Examples:

Simple usage

Leann.build("my_index") do
  add "First document"
  add "Second document"
end

With metadata

Leann.build("docs", embedding: :ollama) do
  add "Content here", source: "file.md", chapter: 1
  add_file "README.md"
  add_directory "docs/", pattern: "**/*.md"
end

Parameters:

  • name (String)

    Index name (will be created in current directory or specified path)

  • options (Hash)

    Options for building

Options Hash (**options):

  • :embedding (Symbol) — default: :openai

    Embedding provider (:openai, :ollama, :ruby_llm)

  • :model (String)

    Embedding model name

  • :path (String)

    Custom path for index storage

Returns:

  • (Index)

    The built index



75
76
77
78
79
# File 'lib/leann.rb', line 75

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

.configurationConfiguration

Global configuration

Returns:



36
37
38
# File 'lib/leann.rb', line 36

def configuration
  @configuration ||= Configuration.new
end

.configure {|Configuration| ... } ⇒ Object

Configure Leann globally

Examples:

Leann.configure do |config|
  config.embedding_provider = :openai
  config.openai_api_key = ENV["OPENAI_API_KEY"]
end

Yields:



49
50
51
# File 'lib/leann.rb', line 49

def configure
  yield(configuration)
end

.delete(name) ⇒ Boolean

Delete an index

Parameters:

  • name (String)

    Index name or path

Returns:

  • (Boolean)

    true if deleted



129
130
131
# File 'lib/leann.rb', line 129

def delete(name)
  Index.delete(name)
end

.exists?(name) ⇒ Boolean

Check if an index exists

Parameters:

  • name (String)

    Index name or path

Returns:

  • (Boolean)


121
122
123
# File 'lib/leann.rb', line 121

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

.list(path: ".") ⇒ Array<String>

List all indexes in a directory

Parameters:

  • path (String) (defaults to: ".")

    Directory to scan (default: current directory)

Returns:

  • (Array<String>)

    Index names



113
114
115
# File 'lib/leann.rb', line 113

def list(path: ".")
  Index.list(path)
end

.open(name) ⇒ Index

Open an existing index for advanced operations

Parameters:

  • name (String)

    Index name or path

Returns:



105
106
107
# File 'lib/leann.rb', line 105

def open(name)
  Index.open(name)
end

.search(name, query, limit: 5, threshold: nil, filters: nil) ⇒ Array<SearchResult>

Search an existing index

Examples:

Basic search

results = Leann.search("my_index", "machine learning")

With filters

results = Leann.search("docs", "auth", limit: 10, filters: { chapter: 1..5 })

Parameters:

  • name (String)

    Index name or path

  • query (String)

    Search query

  • limit (Integer) (defaults to: 5)

    Maximum results (default: 5)

  • threshold (Float) (defaults to: nil)

    Minimum similarity score (0.0-1.0)

  • filters (Hash) (defaults to: nil)

    Metadata filters

Returns:



96
97
98
99
# File 'lib/leann.rb', line 96

def search(name, query, limit: 5, threshold: nil, filters: nil)
  index = Index.open(name)
  index.search(query, limit: limit, threshold: threshold, filters: filters)
end