boxcars 0.2.11 → 0.2.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) 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 +12 -12
  6. data/Gemfile.lock +34 -28
  7. data/README.md +4 -1
  8. data/boxcars.gemspec +2 -2
  9. data/lib/boxcars/boxcar/active_record.rb +1 -1
  10. data/lib/boxcars/boxcar.rb +1 -0
  11. data/lib/boxcars/engine/openai.rb +8 -1
  12. data/lib/boxcars/vector_search.rb +66 -2
  13. data/lib/boxcars/vector_store/document.rb +3 -2
  14. data/lib/boxcars/vector_store/embed_via_open_ai.rb +2 -2
  15. data/lib/boxcars/vector_store/hnswlib/build_from_files.rb +100 -0
  16. data/lib/boxcars/vector_store/hnswlib/load_from_disk.rb +57 -0
  17. data/lib/boxcars/vector_store/hnswlib/save_to_hnswlib.rb +48 -38
  18. data/lib/boxcars/vector_store/hnswlib/search.rb +70 -0
  19. data/lib/boxcars/vector_store/in_memory/build_from_document_array.rb +51 -0
  20. data/lib/boxcars/vector_store/in_memory/build_from_files.rb +61 -0
  21. data/lib/boxcars/vector_store/in_memory/search.rb +29 -49
  22. data/lib/boxcars/vector_store/pgvector/build_from_array.rb +95 -0
  23. data/lib/boxcars/vector_store/pgvector/build_from_files.rb +97 -0
  24. data/lib/boxcars/vector_store/pgvector/save_to_database.rb +152 -0
  25. data/lib/boxcars/vector_store/pgvector/search.rb +144 -0
  26. data/lib/boxcars/vector_store/split_text.rb +2 -3
  27. data/lib/boxcars/vector_store.rb +73 -7
  28. data/lib/boxcars/version.rb +1 -1
  29. data/lib/boxcars.rb +1 -1
  30. metadata +14 -10
  31. data/lib/boxcars/vector_store/hnswlib/build_vector_store.rb +0 -157
  32. data/lib/boxcars/vector_store/hnswlib/hnswlib_config.rb +0 -56
  33. data/lib/boxcars/vector_store/hnswlib/hnswlib_search.rb +0 -54
  34. data/lib/boxcars/vector_store/in_memory/add_documents.rb +0 -67
  35. data/lib/boxcars/vector_store/similarity_search.rb +0 -55
@@ -1,56 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'json'
4
-
5
- module Boxcars
6
- module VectorStore
7
- module Hnswlib
8
- class HnswlibConfig
9
- attr_reader :metric, :max_item, :dim, :ef_construction, :m
10
-
11
- # used for search index.
12
- #
13
- # @param max_item [Integer] The maximum number of items.
14
- #
15
- # @param metric [String] The distance metric between vectors ('l2', 'dot', or 'cosine').
16
- #
17
- # @param ef_construction [Integer] The size of the dynamic list for the nearest neighbors.
18
- # It controls the index time/accuracy trade-off.
19
- #
20
- # @param max_outgoing_connection [Integer] The maximum number of outgoing connections in the graph
21
- #
22
- # reference: https://yoshoku.github.io/hnswlib.rb/doc/
23
- def initialize(
24
- metric: "l2",
25
- max_item: 10000,
26
- dim: 2,
27
- ef_construction: 200,
28
- max_outgoing_connection: 16
29
- )
30
- @metric = metric
31
- @max_item = max_item
32
- @dim = dim
33
- @ef_construction = ef_construction
34
- @max_outgoing_connection = max_outgoing_connection
35
- end
36
-
37
- def space
38
- @metric == 'dot' ? 'ip' : 'l2'
39
- end
40
-
41
- def to_json(*args)
42
- JSON.pretty_generate(
43
- {
44
- metric: @metric,
45
- max_item: @max_item,
46
- dim: @dim,
47
- ef_construction: @ef_construction,
48
- max_outgoing_connection: @max_outgoing_connection
49
- },
50
- *args
51
- )
52
- end
53
- end
54
- end
55
- end
56
- end
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'hnswlib'
4
- require 'json'
5
-
6
- module Boxcars
7
- module VectorStore
8
- module Hnswlib
9
- class HnswlibSearch
10
- def initialize(vector_store:, options: {})
11
- validate_params(vector_store)
12
- @vector_store = vector_store
13
- @json_doc_path = options[:json_doc_path]
14
- @num_neighbors = options[:num_neighbors] || 1
15
- end
16
-
17
- def call(query)
18
- search(query)
19
- end
20
-
21
- private
22
-
23
- attr_reader :json_doc_path, :vector_store, :num_neighbors
24
-
25
- def validate_params(vector_store)
26
- raise_error 'vector_store must be an Hnswlib::HierarchicalNSW' unless vector_store.is_a?(::Hnswlib::HierarchicalNSW)
27
- end
28
-
29
- def search(query)
30
- raw_results = vector_store.search_knn(query, num_neighbors)
31
- raw_results.map { |doc_id, distance| lookup_embedding2(doc_id, distance) }.compact
32
- end
33
-
34
- def lookup_embedding2(doc_id, distance)
35
- embedding_data = parsed_data.find { |embedding| embedding[:doc_id] == doc_id }
36
- return unless embedding_data
37
-
38
- { document: embedding_data[:document], distance: distance }
39
- end
40
-
41
- def parsed_data
42
- @parsed_data ||= JSON.parse(
43
- File.read(json_doc_path),
44
- symbolize_names: true
45
- )
46
- end
47
-
48
- def raise_error(message)
49
- raise ::Boxcars::ArgumentError, message
50
- end
51
- end
52
- end
53
- end
54
- end
@@ -1,67 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Boxcars
4
- module VectorStore
5
- module InMemory
6
- MemoryVector = Struct.new(:content, :embedding, :metadatax)
7
-
8
- class AddDocuments
9
- include VectorStore
10
-
11
- def initialize(embedding_tool: :openai, documents: nil)
12
- validate_params(embedding_tool, documents)
13
- @embedding_tool = embedding_tool
14
- @documents = documents
15
- @memory_vectors = []
16
- end
17
-
18
- def call
19
- texts = @documents.map { |doc| doc[:page_content] }
20
- vectors = generate_vectors(texts)
21
- add_vectors(vectors, @documents)
22
- @memory_vectors
23
- end
24
-
25
- private
26
-
27
- def validate_params(embedding_tool, documents)
28
- raise ::Boxcars::ArgumentError, 'documents is nil' unless documents
29
- return if %i[openai tensorflow].include?(embedding_tool)
30
-
31
- raise ::Boxcars::ArgumentError, 'embedding_tool is invalid'
32
- end
33
-
34
- # returns array of documents with vectors
35
- def add_vectors(vectors, documents)
36
- vectors.zip(documents).each do |vector, doc|
37
- memory_vector = MemoryVector.new(doc[:page_content], vector, doc[:metadata])
38
- @memory_vectors << memory_vector
39
- end
40
- end
41
-
42
- def generate_vectors(texts)
43
- embeddings_method[:klass].call(
44
- texts: texts, client: embeddings_method[:client]
45
- )
46
- end
47
-
48
- def embeddings_method
49
- @embeddings_method ||=
50
- case @embedding_tool
51
- when :openai
52
- { klass: Boxcars::VectorStore::EmbedViaOpenAI, client: openai_client }
53
- when :tensorflow
54
- { klass: Boxcars::VectorStore::EmbedViaTensorflow, client: nil }
55
- end
56
- end
57
-
58
- # Get the OpenAI client
59
- # @param openai_access_token [String] the OpenAI access token
60
- # @return [OpenAI::Client]
61
- def openai_client(openai_access_token: nil)
62
- @openai_client ||= Openai.open_ai_client(openai_access_token: openai_access_token)
63
- end
64
- end
65
- end
66
- end
67
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'hnswlib'
4
-
5
- module Boxcars
6
- module VectorStore
7
- class SimilaritySearch
8
- def initialize(embeddings:, vector_store:, openai_connection: nil, openai_access_token: nil)
9
- @embeddings = embeddings
10
- @vector_store = vector_store
11
- @similarity_search_instance = create_similarity_search_instance
12
- @openai_connection = openai_connection || default_connection(openai_access_token: openai_access_token)
13
- end
14
-
15
- def call(query:)
16
- validate_query(query)
17
- query_vector = convert_query_to_vector(query)
18
- @similarity_search_instance.call(query_vector)
19
- end
20
-
21
- private
22
-
23
- attr_reader :embeddings, :vector_store, :openai_connection
24
-
25
- def default_connection(openai_access_token: nil)
26
- Openai.open_ai_client(openai_access_token: openai_access_token)
27
- end
28
-
29
- def validate_query(query)
30
- raise_error 'query must be a string' unless query.is_a?(String)
31
- raise_error 'query must not be empty' if query.empty?
32
- end
33
-
34
- def convert_query_to_vector(query)
35
- Boxcars::VectorStore::EmbedViaOpenAI.call(texts: [query], client: openai_connection).first[:embedding]
36
- end
37
-
38
- def create_similarity_search_instance
39
- case vector_store
40
- when ::Hnswlib::HierarchicalNSW
41
- Boxcars::VectorStore::Hnswlib::HnswlibSearch.new(
42
- vector_store: vector_store,
43
- options: { json_doc_path: embeddings, num_neighbors: 2 }
44
- )
45
- else
46
- raise_error 'Unsupported vector store provided'
47
- end
48
- end
49
-
50
- def raise_error(message)
51
- raise ArgumentError, message
52
- end
53
- end
54
- end
55
- end