boxcars 0.2.11 → 0.2.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/.env_sample +1 -0
  3. data/.rubocop.yml +16 -0
  4. data/CHANGELOG.md +12 -0
  5. data/Gemfile +15 -11
  6. data/Gemfile.lock +40 -32
  7. data/README.md +4 -1
  8. data/boxcars.gemspec +4 -7
  9. data/lib/boxcars/boxcar/active_record.rb +2 -2
  10. data/lib/boxcars/boxcar/engine_boxcar.rb +2 -2
  11. data/lib/boxcars/boxcar/sql.rb +1 -1
  12. data/lib/boxcars/boxcar/swagger.rb +1 -1
  13. data/lib/boxcars/boxcar/vector_answer.rb +71 -0
  14. data/lib/boxcars/boxcar.rb +2 -0
  15. data/lib/boxcars/engine/openai.rb +8 -1
  16. data/lib/boxcars/train/zero_shot.rb +1 -1
  17. data/lib/boxcars/train.rb +1 -1
  18. data/lib/boxcars/vector_search.rb +66 -2
  19. data/lib/boxcars/vector_store/document.rb +3 -2
  20. data/lib/boxcars/vector_store/embed_via_open_ai.rb +2 -2
  21. data/lib/boxcars/vector_store/hnswlib/build_from_files.rb +104 -0
  22. data/lib/boxcars/vector_store/hnswlib/load_from_disk.rb +57 -0
  23. data/lib/boxcars/vector_store/hnswlib/save_to_hnswlib.rb +48 -38
  24. data/lib/boxcars/vector_store/hnswlib/search.rb +70 -0
  25. data/lib/boxcars/vector_store/in_memory/build_from_document_array.rb +51 -0
  26. data/lib/boxcars/vector_store/in_memory/build_from_files.rb +61 -0
  27. data/lib/boxcars/vector_store/in_memory/search.rb +29 -49
  28. data/lib/boxcars/vector_store/pgvector/build_from_array.rb +95 -0
  29. data/lib/boxcars/vector_store/pgvector/build_from_files.rb +97 -0
  30. data/lib/boxcars/vector_store/pgvector/save_to_database.rb +152 -0
  31. data/lib/boxcars/vector_store/pgvector/search.rb +144 -0
  32. data/lib/boxcars/vector_store/split_text.rb +2 -3
  33. data/lib/boxcars/vector_store.rb +73 -7
  34. data/lib/boxcars/version.rb +1 -1
  35. data/lib/boxcars.rb +1 -1
  36. metadata +31 -40
  37. data/lib/boxcars/vector_store/hnswlib/build_vector_store.rb +0 -157
  38. data/lib/boxcars/vector_store/hnswlib/hnswlib_config.rb +0 -56
  39. data/lib/boxcars/vector_store/hnswlib/hnswlib_search.rb +0 -54
  40. data/lib/boxcars/vector_store/in_memory/add_documents.rb +0 -67
  41. data/lib/boxcars/vector_store/similarity_search.rb +0 -55
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pgvector'
4
+ require 'fileutils'
5
+ require 'json'
6
+
7
+ module Boxcars
8
+ module VectorStore
9
+ module Pgvector
10
+ class BuildFromFiles
11
+ include VectorStore
12
+
13
+ # params = {
14
+ # training_data_path: training_data_path,
15
+ # split_chunk_size: 200,
16
+ # embedding_tool: embedding_tool,
17
+ # database_url: db_url,
18
+ # table_name: table_name,
19
+ # embedding_column_name: embedding_column_name,
20
+ # content_column_name: content_column_name
21
+ # }
22
+ def initialize(params)
23
+ @split_chunk_size = params[:split_chunk_size] || 2000
24
+ @training_data_path = File.absolute_path(params[:training_data_path])
25
+ @embedding_tool = params[:embedding_tool] || :openai
26
+
27
+ validate_params(embedding_tool, training_data_path)
28
+
29
+ @database_url = params[:database_url]
30
+ @table_name = params[:table_name]
31
+ @embedding_column_name = params[:embedding_column_name]
32
+ @content_column_name = params[:content_column_name]
33
+ @metadata_column_name = params[:metadata_column_name]
34
+
35
+ @pg_vectors = []
36
+ end
37
+
38
+ def call
39
+ data = load_data_files(training_data_path)
40
+ texts = split_text_into_chunks(data)
41
+ embeddings = generate_vectors(texts)
42
+ add_vectors(embeddings, texts)
43
+ documents = save_vector_store
44
+
45
+ {
46
+ type: :pgvector,
47
+ vector_store: documents
48
+ }
49
+ end
50
+
51
+ private
52
+
53
+ attr_reader :split_chunk_size, :training_data_path, :embedding_tool, :database_url,
54
+ :table_name, :embedding_column_name, :content_column_name,
55
+ :metadata_column_name, :pg_vectors
56
+
57
+ def validate_params(embedding_tool, training_data_path)
58
+ training_data_dir = File.dirname(training_data_path.gsub(/\*{1,2}/, ''))
59
+
60
+ raise_argument_error('training_data_path parent directory must exist') unless File.directory?(training_data_dir)
61
+ raise_argument_error('No files found at the training_data_path pattern') if Dir.glob(training_data_path).empty?
62
+ return if %i[openai tensorflow].include?(embedding_tool)
63
+
64
+ raise_argument_error('embedding_tool is invalid')
65
+ end
66
+
67
+ def add_vectors(vectors, texts)
68
+ vectors.map.with_index do |vector, index|
69
+ pg_vector = Document.new(
70
+ content: texts[index],
71
+ embedding: vector[:embedding],
72
+ metadata: {
73
+ doc_id: index,
74
+ training_data_path: training_data_path
75
+ }
76
+ )
77
+ pg_vectors << pg_vector
78
+ end
79
+ end
80
+
81
+ def save_vector_store
82
+ result = Boxcars::VectorStore::Pgvector::SaveToDatabase.call(
83
+ pg_vectors: pg_vectors,
84
+ database_url: database_url,
85
+ table_name: table_name,
86
+ embedding_column_name: embedding_column_name,
87
+ content_column_name: content_column_name,
88
+ metadata_column_name: metadata_column_name
89
+ )
90
+ raise_argument_error('Error saving vector store to database.') unless result
91
+
92
+ result
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pg'
4
+ require 'pgvector'
5
+
6
+ module Boxcars
7
+ module VectorStore
8
+ module Pgvector
9
+ class SaveToDatabase
10
+ include VectorStore
11
+
12
+ # params = {
13
+ # pg_vectors: pg_vectors,
14
+ # database_url: db_url,
15
+ # table_name: table_name,
16
+ # embedding_column_name: embedding_column_name,
17
+ # content_column_name: content_column_name
18
+ # }
19
+ def initialize(params)
20
+ @errors = []
21
+ validate_param_types(params)
22
+ @db_connection = test_db_params(params)
23
+
24
+ @table_name = params[:table_name]
25
+ @content_column_name = params[:content_column_name]
26
+ @embedding_column_name = params[:embedding_column_name]
27
+ @metadata_column_name = params[:metadata_column_name]
28
+
29
+ @pg_vectors = params[:pg_vectors]
30
+ end
31
+
32
+ def call
33
+ return { success: false, error: errors } unless errors.empty?
34
+
35
+ add_vectors_to_database
36
+ end
37
+
38
+ private
39
+
40
+ attr_reader :database_url, :pg_vectors, :db_connection, :table_name,
41
+ :embedding_column_name, :content_column_name,
42
+ :metadata_column_name, :errors
43
+
44
+ def validate_param_types(params)
45
+ pg_vectors = params[:pg_vectors]
46
+
47
+ raise_argument_error('pg_vectors must be an array') unless pg_vectors.is_a?(Array)
48
+ raise_argument_error('missing data') if pg_vectors.empty?
49
+ raise_argument_error('invalid vector_store') unless valid_vector_store?(pg_vectors)
50
+ @database_url = params[:database_url]
51
+ raise_argument_error('missing database_url argument') if @database_url.to_s.empty?
52
+ end
53
+
54
+ def valid_vector_store?(pg_vectors)
55
+ pg_vectors.all? do |doc|
56
+ doc.is_a?(Boxcars::VectorStore::Document)
57
+ end
58
+ rescue TypeError => e
59
+ raise_argument_error(e.message)
60
+ end
61
+
62
+ def test_db_params(params)
63
+ conn = ::PG::Connection.new(@database_url)
64
+
65
+ check_db_connection(conn)
66
+ check_vector_extension(conn)
67
+ check_table_exists(conn, params[:table_name])
68
+ check_column_exists(conn, params)
69
+
70
+ registry = PG::BasicTypeRegistry.new.define_default_types
71
+ ::Pgvector::PG.register_vector(registry)
72
+ conn.type_map_for_queries = PG::BasicTypeMapForQueries.new(conn, registry: registry)
73
+ conn.type_map_for_results = PG::BasicTypeMapForResults.new(conn, registry: registry)
74
+ conn
75
+ rescue PG::Error, NameError => e
76
+ raise_argument_error(e.message)
77
+ end
78
+
79
+ def check_db_connection(conn)
80
+ return if conn.status == PG::CONNECTION_OK
81
+
82
+ raise_argument_error("PostgreSQL connection is not ok")
83
+ end
84
+
85
+ def check_vector_extension(conn)
86
+ return if conn.exec("SELECT 1 FROM pg_extension WHERE extname = 'vector'").any?
87
+
88
+ raise_argument_error("PostgreSQL 'vector' extension is not installed")
89
+ end
90
+
91
+ def check_table_exists(conn, table_name)
92
+ table_exists = conn.exec_params(
93
+ "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = $1)", [table_name]
94
+ ).getvalue(0, 0) == "t"
95
+ return if table_exists
96
+
97
+ raise_argument_error("Table '#{table_name}' does not exist")
98
+ end
99
+
100
+ def check_column_exists(conn, params)
101
+ column_names = %i[embedding_column_name content_column_name metadata_column_name]
102
+ table_name = params[:table_name]
103
+
104
+ column_names.each do |target|
105
+ column_name = params[target]
106
+ column_exists = conn.exec_params(
107
+ "SELECT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = $1 AND column_name = $2)",
108
+ [table_name, column_name]
109
+ ).getvalue(0, 0) == "t"
110
+ next if column_exists
111
+
112
+ raise_argument_error("Column '#{column_name}' does not exist in table '#{table_name}'")
113
+ end
114
+ end
115
+
116
+ def add_vectors_to_database
117
+ pg_vectors.each do |document|
118
+ embedding = document.embedding.map(&:to_f)
119
+ content = document.content
120
+ metadata = document.metadata.to_json
121
+
122
+ if document.metadata[:id]
123
+ id = document.metadata[:id]
124
+ # directly inserting table_name, embedding_column_name, and content_column_name
125
+ # into the SQL command. If these values are coming from an untrusted source,
126
+ # there is a risk of SQL injection
127
+ sql = <<-SQL
128
+ INSERT INTO #{table_name} (id, #{embedding_column_name}, #{content_column_name}, #{metadata_column_name})
129
+ VALUES ($1, $2, $3, $4)
130
+ ON CONFLICT (id) DO UPDATE
131
+ SET #{embedding_column_name} = EXCLUDED.#{embedding_column_name},
132
+ #{content_column_name} = EXCLUDED.#{content_column_name},
133
+ #{metadata_column_name} = EXCLUDED.#{metadata_column_name}
134
+ SQL
135
+ # parameters are given separately from the SQL command,
136
+ # there's no risk of them being interpreted as part of the command.
137
+ db_connection.exec_params(sql, [id, embedding, content, metadata])
138
+ else
139
+ sql = <<-SQL
140
+ INSERT INTO #{table_name} (#{embedding_column_name}, #{content_column_name}, #{metadata_column_name})
141
+ VALUES ($1, $2, $3)
142
+ SQL
143
+ db_connection.exec_params(sql, [embedding, content, metadata])
144
+ end
145
+ end
146
+ rescue PG::Error => e
147
+ raise_argument_error(e.message)
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,144 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pg'
4
+ require 'json'
5
+
6
+ module Boxcars
7
+ module VectorStore
8
+ module Pgvector
9
+ class Search
10
+ include VectorStore
11
+
12
+ # required params:
13
+ # {
14
+ # type: :pgvector,
15
+ # vector_store: {
16
+ # database_url: database_url,
17
+ # table_name: table_name,
18
+ # embedding_column_name: embedding_column_name,
19
+ # content_column_name: content_column_name,
20
+ # metadata_column_name: metadata_column_name
21
+ # }
22
+ # }
23
+ def initialize(params)
24
+ vector_store = validate_params(params)
25
+ db_url = validate_vector_store(vector_store)
26
+ @db_connection = test_db(db_url)
27
+
28
+ @vector_documents = params[:vector_documents]
29
+ end
30
+
31
+ def call(query_vector:, count: 1)
32
+ raise ::Boxcars::ArgumentError, 'query_vector is empty' if query_vector.empty?
33
+
34
+ search(query_vector, count)
35
+ end
36
+
37
+ private
38
+
39
+ attr_reader :vector_documents, :vector_store, :db_connection,
40
+ :table_name, :embedding_column_name, :content_column_name
41
+
42
+ def validate_params(params)
43
+ @vector_documents = params[:vector_documents]
44
+
45
+ raise_argument_error('vector_documents is nil') unless vector_documents
46
+ raise_arugment_error('vector_documents must be a hash') unless vector_documents.is_a?(Hash)
47
+ raise_arugment_error('type must be pgvector') unless vector_documents[:type] == :pgvector
48
+
49
+ @vector_store = vector_documents[:vector_store]
50
+ @vector_store
51
+ end
52
+
53
+ def validate_vector_store(vector_store)
54
+ raise_arugment_error('vector_store is nil') unless vector_store
55
+ raise_arugment_error('vector_store must be a hash') unless vector_store.is_a?(Hash)
56
+ raise_arugment_error('vector_store must have a table_name') unless vector_store[:table_name]
57
+ raise_arugment_error('vector_store must have a embedding_column_name') unless vector_store[:embedding_column_name]
58
+ raise_arugment_error('vector_store must have a content_column_name') unless vector_store[:content_column_name]
59
+ raise_argument_error('missing DATABASE_URL') unless vector_store[:database_url]
60
+
61
+ vector_store[:database_url]
62
+ end
63
+
64
+ def test_db(db_url)
65
+ conn = ::PG::Connection.new(db_url)
66
+
67
+ check_db_connection(conn)
68
+ check_vector_extension(conn)
69
+ check_table_exists(conn, vector_store[:table_name])
70
+ check_column_exists(conn)
71
+
72
+ @table_name = vector_store[:table_name]
73
+ @embedding_column_name = vector_store[:embedding_column_name]
74
+ @content_column_name = vector_store[:content_column_name]
75
+
76
+ conn
77
+ rescue PG::Error, PG::UndefinedTable, NameError => e
78
+ raise_argument_error(e.message)
79
+ end
80
+
81
+ def check_db_connection(conn)
82
+ return if conn.status == PG::CONNECTION_OK
83
+
84
+ raise_argument_error("PostgreSQL connection is not ok")
85
+ end
86
+
87
+ def check_vector_extension(conn)
88
+ return if conn.exec("SELECT 1 FROM pg_extension WHERE extname = 'vector'").any?
89
+
90
+ raise_argument_error("PostgreSQL 'vector' extension is not installed")
91
+ end
92
+
93
+ def check_table_exists(conn, table_name)
94
+ table_exists = conn.exec_params(
95
+ "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = $1)", [table_name]
96
+ ).getvalue(0, 0) == "t"
97
+ return if table_exists
98
+
99
+ raise_argument_error("Table '#{table_name}' does not exist")
100
+ end
101
+
102
+ def check_column_exists(conn)
103
+ column_names = %i[embedding_column_name content_column_name]
104
+ table_name = vector_store[:table_name]
105
+
106
+ column_names.each do |target|
107
+ column_name = vector_store[target]
108
+ column_exists = conn.exec_params(
109
+ "SELECT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = $1 AND column_name = $2)",
110
+ [table_name, column_name]
111
+ ).getvalue(0, 0) == "t"
112
+ next if column_exists
113
+
114
+ raise_argument_error("Column '#{column_name}' does not exist in table '#{table_name}'")
115
+ end
116
+ end
117
+
118
+ def search(query_vector, num_neighbors)
119
+ sql = <<-SQL
120
+ SELECT *, #{embedding_column_name} <-> $1 AS distance FROM #{table_name}
121
+ ORDER BY #{embedding_column_name} <-> $1
122
+ LIMIT #{num_neighbors}
123
+ SQL
124
+ result = db_connection.exec_params(sql, [query_vector.to_s]).to_a
125
+ return [] if result.empty?
126
+
127
+ result.map { |hash| hash.transform_keys(&:to_sym) }
128
+ .map do |item|
129
+ {
130
+ document: Boxcars::VectorStore::Document.new(
131
+ content: item[:content],
132
+ embedding: JSON.parse(item[:embedding]),
133
+ metadata: JSON.parse(item[:metadata], symbolize_names: true)
134
+ ),
135
+ distance: item[:distance].to_f
136
+ }
137
+ end
138
+ rescue StandardError => e
139
+ raise_argument_error("Error searching for #{query_vector}: #{e.message}")
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
@@ -6,14 +6,11 @@ module Boxcars
6
6
  class SplitText
7
7
  include VectorStore
8
8
 
9
- attr_reader :separator, :chunk_size, :chunk_overlap, :text
10
-
11
9
  # @param separator [String] The string to use to split the text.
12
10
  # @param chunk_size [Integer] The size of each chunk.
13
11
  # @param chunk_overlap [Integer] The amount of overlap between chunks.
14
12
  # @param text [String] The text to split.
15
13
  def initialize(separator: "Search", chunk_size: 7, chunk_overlap: 3, text: "")
16
- # require 'debugger'; debugger
17
14
  validate_params(separator, chunk_size, chunk_overlap, text)
18
15
 
19
16
  @separator = separator
@@ -31,6 +28,8 @@ module Boxcars
31
28
 
32
29
  private
33
30
 
31
+ attr_reader :separator, :chunk_size, :chunk_overlap, :text
32
+
34
33
  def validate_params(separator, chunk_size, chunk_overlap, text)
35
34
  raise_error("separator must be a string") unless separator.is_a?(String)
36
35
  raise_error("chunk_size must be an integer") unless chunk_size.is_a?(Integer)
@@ -13,10 +13,72 @@ module Boxcars
13
13
 
14
14
  def self.included(base)
15
15
  base.extend(ClassMethods)
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :embedding_tool
21
+
22
+ def generate_vectors(texts)
23
+ @embedding_tool = embedding_tool || :openai
24
+
25
+ embeddings_method[:klass]
26
+ .call(
27
+ texts: texts, client: embeddings_method[:client]
28
+ )
29
+ .map { |item| item.transform_keys(&:to_sym) }
30
+ end
31
+
32
+ def embeddings_method
33
+ case @embedding_tool
34
+ when :openai
35
+ { klass: Boxcars::VectorStore::EmbedViaOpenAI, client: openai_client }
36
+ when :tensorflow
37
+ { klass: Boxcars::VectorStore::EmbedViaTensorflow, client: nil }
38
+ end
39
+ end
40
+
41
+ # Get the OpenAI client
42
+ # @param openai_access_token [String] the OpenAI access token
43
+ # @return [OpenAI::Client]
44
+ def openai_client(openai_access_token: nil)
45
+ @openai_client ||= Openai.open_ai_client(openai_access_token: openai_access_token)
46
+ end
47
+
48
+ def raise_argument_error(message)
49
+ raise ::Boxcars::ArgumentError, message
50
+ end
51
+
52
+ def parse_json_file(file_path)
53
+ return [] if file_path.nil?
54
+
55
+ file_content = File.read(file_path)
56
+ JSON.parse(file_content, symbolize_names: true)
57
+ rescue JSON::ParserError => e
58
+ raise_argument_error("Error parsing #{file_path}: #{e.message}")
59
+ end
60
+
61
+ def load_data_files(training_data_path)
62
+ data = []
63
+ files = Dir.glob(training_data_path)
64
+ raise_error "No files found at #{training_data_path}" if files.empty?
65
+
66
+ files.each do |file|
67
+ data << File.read(file)
68
+ end
69
+ puts "Added #{files.length} files to data. Splitting text into chunks..."
70
+ data
71
+ end
16
72
 
17
- class << base
18
- private :new
73
+ def split_text_into_chunks(data)
74
+ docs = []
75
+ data.each do |chunk|
76
+ doc_output = Boxcars::VectorStore::SplitText.call(
77
+ separator: "\n", chunk_size: split_chunk_size, chunk_overlap: 0, text: chunk
78
+ )
79
+ docs.concat(doc_output)
19
80
  end
81
+ docs
20
82
  end
21
83
  end
22
84
  end
@@ -25,10 +87,14 @@ require_relative "vector_store/document"
25
87
  require_relative "vector_store/embed_via_open_ai"
26
88
  require_relative "vector_store/embed_via_tensorflow"
27
89
  require_relative "vector_store/split_text"
28
- require_relative "vector_store/similarity_search"
29
- require_relative "vector_store/hnswlib/hnswlib_config"
90
+ require_relative "vector_store/hnswlib/load_from_disk"
30
91
  require_relative "vector_store/hnswlib/save_to_hnswlib"
31
- require_relative "vector_store/hnswlib/build_vector_store"
32
- require_relative "vector_store/hnswlib/hnswlib_search"
33
- require_relative "vector_store/in_memory/add_documents"
92
+ require_relative "vector_store/hnswlib/build_from_files"
93
+ require_relative "vector_store/hnswlib/search"
94
+ require_relative "vector_store/in_memory/build_from_files"
95
+ require_relative "vector_store/in_memory/build_from_document_array"
34
96
  require_relative "vector_store/in_memory/search"
97
+ require_relative "vector_store/pgvector/build_from_files"
98
+ require_relative "vector_store/pgvector/build_from_array"
99
+ require_relative "vector_store/pgvector/save_to_database"
100
+ require_relative "vector_store/pgvector/search"
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Boxcars
4
4
  # The current version of the gem.
5
- VERSION = "0.2.11"
5
+ VERSION = "0.2.13"
6
6
  end
data/lib/boxcars.rb CHANGED
@@ -58,7 +58,7 @@ module Boxcars
58
58
  # override with kwargs if present
59
59
  kwargs[key]
60
60
  elsif (provided_val = instance_variable_get("@#{key}"))
61
- # use saved value if present. Set using Boxcars::configuration.the_key = "abcde"
61
+ # use saved value if present. Set using Boxcars.configuration.the_key = "abcde"
62
62
  provided_val
63
63
  else
64
64
  # otherwise, dig out of the environment