Class: Leann::Builder
- Inherits:
-
Object
- Object
- Leann::Builder
- Defined in:
- lib/leann/builder.rb
Overview
Builds a new Leann index
Instance Attribute Summary collapse
-
#documents ⇒ Array<Hash>
readonly
Documents to be indexed.
-
#name ⇒ String
readonly
Index name.
-
#path ⇒ String
readonly
Index path.
Instance Method Summary collapse
-
#add(text, **metadata) ⇒ self
(also: #<<)
Add a text document.
-
#add_all(docs) ⇒ self
Add multiple documents at once.
-
#add_directory(directory, pattern: "**/*", extensions: nil, **metadata) ⇒ self
Add all files from a directory.
-
#add_file(file_path, **metadata) ⇒ self
Add content from a file.
-
#count ⇒ Integer
(also: #size)
Get number of documents added.
-
#empty? ⇒ Boolean
Check if any documents have been added.
-
#initialize(name, embedding: nil, model: nil, path: nil, force: false, **_options) ⇒ Builder
constructor
A new instance of Builder.
-
#save ⇒ Index
(also: #build)
Build and save the index.
Constructor Details
#initialize(name, embedding: nil, model: nil, path: nil, force: false, **_options) ⇒ Builder
Returns a new instance of Builder.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/leann/builder.rb', line 39 def initialize(name, embedding: nil, model: nil, path: nil, force: false, **) @name = name @path = resolve_path(name, path) @embedding_provider = || Leann.configuration. @embedding_model = model || Leann.configuration.(@embedding_provider) @force = force @documents = [] check_existing_index unless force end |
Instance Attribute Details
#documents ⇒ Array<Hash> (readonly)
Returns Documents to be indexed.
32 33 34 |
# File 'lib/leann/builder.rb', line 32 def documents @documents end |
#name ⇒ String (readonly)
Returns Index name.
26 27 28 |
# File 'lib/leann/builder.rb', line 26 def name @name end |
#path ⇒ String (readonly)
Returns Index path.
29 30 31 |
# File 'lib/leann/builder.rb', line 29 def path @path end |
Instance Method Details
#add(text, **metadata) ⇒ self Also known as: <<
Add a text document
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/leann/builder.rb', line 59 def add(text, **) raise ArgumentError, "Text cannot be nil" if text.nil? raise ArgumentError, "Text cannot be empty" if text.to_s.strip.empty? doc = { id: .delete(:id) || generate_id, text: text.to_s.strip, metadata: } @documents << doc self end |
#add_all(docs) ⇒ self
Add multiple documents at once
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/leann/builder.rb', line 134 def add_all(docs) docs.each do |doc| case doc when String add(doc) when Hash text = doc.delete(:text) || doc.delete("text") add(text, **doc.transform_keys(&:to_sym)) else raise ArgumentError, "Invalid document type: #{doc.class}" end end self end |
#add_directory(directory, pattern: "**/*", extensions: nil, **metadata) ⇒ self
Add all files from a directory
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/leann/builder.rb', line 109 def add_directory(directory, pattern: "**/*", extensions: nil, **) raise ArgumentError, "Directory not found: #{directory}" unless Dir.exist?(directory) full_pattern = File.join(directory, pattern) Dir.glob(full_pattern).each do |file_path| next unless File.file?(file_path) next if extensions && !extensions.include?(File.extname(file_path)) add_file(file_path, **) end self end |
#add_file(file_path, **metadata) ⇒ self
Add content from a file
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/leann/builder.rb', line 85 def add_file(file_path, **) raise ArgumentError, "File not found: #{file_path}" unless File.exist?(file_path) content = File.read(file_path) = { source: file_path, filename: File.basename(file_path), extension: File.extname(file_path) }.merge() add(content, **) end |
#count ⇒ Integer Also known as: size
Get number of documents added
152 153 154 |
# File 'lib/leann/builder.rb', line 152 def count @documents.size end |
#empty? ⇒ Boolean
Check if any documents have been added
159 160 161 |
# File 'lib/leann/builder.rb', line 159 def empty? @documents.empty? end |
#save ⇒ Index Also known as: build
Build and save the index
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/leann/builder.rb', line 165 def save raise EmptyIndexError if empty? puts "Building index '#{name}' with #{count} documents..." # Create directory if needed FileUtils.mkdir_p(File.dirname(path)) # Delete existing if force mode Index.delete(path) if @force && Index.exists?(path) # Compute embeddings = # Save passages save_passages # Build and save graph save_graph() # Save metadata () puts "Index '#{name}' created successfully!" Index.open(path) end |