Class: BxBuilderChain::Vectorsearch::Pgvector
- Defined in:
- lib/bx_builder_chain/vectorsearch/pgvector.rb
Constant Summary collapse
- OPERATORS =
The operators supported by the PostgreSQL vector search adapter
{ "cosine_distance" => "cosine", "euclidean_distance" => "euclidean" }
- DEFAULT_OPERATOR =
"cosine_distance"
Constants inherited from Base
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#documents_table ⇒ Object
readonly
Returns the value of attribute documents_table.
-
#namespace_column ⇒ Object
readonly
Returns the value of attribute namespace_column.
-
#namespaces ⇒ Object
readonly
Returns the value of attribute namespaces.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Attributes inherited from Base
Instance Method Summary collapse
- #add_data(paths:) ⇒ Object
-
#add_texts(texts:, ids: nil) ⇒ Array<Integer>
Add a list of texts to the index.
-
#ask(question:, context_results: 4, prompt_template: nil) ⇒ String
Ask a question and return the answer.
- #create_default_schema ⇒ Object
-
#destroy_default_schema ⇒ Object
Destroy default schema.
- #documents_model ⇒ Object
-
#initialize(llm:, namespaces: [BxBuilderChain.configuration.public_namespace] || ['public']) ⇒ Pgvector
constructor
A new instance of Pgvector.
-
#similarity_search(query:, k: 4) ⇒ Array<Hash>
Search for similar texts in the index.
-
#similarity_search_by_vector(embedding:, k: 4) ⇒ Array<Hash>
Search for similar texts in the index by the passed in vector.
-
#update_texts(texts:, ids:) ⇒ Array<Integer>
Update a list of ids and corresponding texts to the index.
-
#upsert_texts(texts:, ids:) ⇒ PG::Result
Upsert a list of texts to the index the added or updated texts.
Methods inherited from Base
#generate_prompt, #get_default_schema, logger_options
Methods included from DependencyHelper
Constructor Details
#initialize(llm:, namespaces: [BxBuilderChain.configuration.public_namespace] || ['public']) ⇒ Pgvector
Returns a new instance of Pgvector.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bx_builder_chain/vectorsearch/pgvector.rb', line 25 def initialize(llm:, namespaces: [BxBuilderChain.configuration.public_namespace] || ['public']) depends_on "sequel" require "sequel" @db = create_sequel_connection @table_name = "bx_builder_chain_embeddings" @namespace_column = "namespace" set_namespaces(namespaces) @threshold = BxBuilderChain.configuration.threshold validate_threshold(@threshold) @operator = OPERATORS[DEFAULT_OPERATOR] super(llm: llm) end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
19 20 21 |
# File 'lib/bx_builder_chain/vectorsearch/pgvector.rb', line 19 def db @db end |
#documents_table ⇒ Object (readonly)
Returns the value of attribute documents_table.
19 20 21 |
# File 'lib/bx_builder_chain/vectorsearch/pgvector.rb', line 19 def documents_table @documents_table end |
#namespace_column ⇒ Object (readonly)
Returns the value of attribute namespace_column.
19 20 21 |
# File 'lib/bx_builder_chain/vectorsearch/pgvector.rb', line 19 def namespace_column @namespace_column end |
#namespaces ⇒ Object (readonly)
Returns the value of attribute namespaces.
19 20 21 |
# File 'lib/bx_builder_chain/vectorsearch/pgvector.rb', line 19 def namespaces @namespaces end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
19 20 21 |
# File 'lib/bx_builder_chain/vectorsearch/pgvector.rb', line 19 def operator @operator end |
#table_name ⇒ Object (readonly)
Returns the value of attribute table_name.
19 20 21 |
# File 'lib/bx_builder_chain/vectorsearch/pgvector.rb', line 19 def table_name @table_name end |
Instance Method Details
#add_data(paths:) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/bx_builder_chain/vectorsearch/pgvector.rb', line 177 def add_data(paths:) raise ArgumentError, "Paths must be provided" if Array(paths).empty? all_added_chunk_ids = [] @db.transaction do # Start the transaction paths.each do |file_n_path| path, file = extract_path_and_file(file_n_path) texts = BxBuilderChain::Loader.new(path)&.load&.chunks.map { |chunk| chunk[:text] } texts.flatten! added_chunk_ids_for_current_path = add_texts(texts: texts) all_added_chunk_ids.concat(added_chunk_ids_for_current_path) document_record_id = @db[:bx_builder_chain_documents].insert( name: file, namespace: namespaces[0], created_at: Time.now.utc, updated_at: Time.now.utc ) document_chunks_data = added_chunk_ids_for_current_path.map do |chunk_id| {document_id: document_record_id, embedding_id: chunk_id} end @db[:bx_builder_chain_document_chunks].multi_insert(document_chunks_data) end end # End the transaction all_added_chunk_ids end |
#add_texts(texts:, ids: nil) ⇒ Array<Integer>
Add a list of texts to the index
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/bx_builder_chain/vectorsearch/pgvector.rb', line 70 def add_texts(texts:, ids: nil) if ids.nil? || ids.empty? data = texts.map do |text| {content: text, vectors: llm.(text: text).to_s, namespace: namespaces[0]} end @db[@table_name.to_sym].multi_insert(data, return: :primary_key) else upsert_texts(texts: texts, ids: ids) end end |
#ask(question:, context_results: 4, prompt_template: nil) ⇒ String
Ask a question and return the answer
164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/bx_builder_chain/vectorsearch/pgvector.rb', line 164 def ask(question:, context_results: 4, prompt_template: nil) search_results = similarity_search(query: question, k: context_results) context = search_results.map do |result| result.content.to_s end context = context.join("\n---\n") prompt = generate_prompt(question: question, context: context, prompt_template: nil) llm.chat(prompt: prompt) end |
#create_default_schema ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/bx_builder_chain/vectorsearch/pgvector.rb', line 89 def create_default_schema db.run "CREATE EXTENSION IF NOT EXISTS vector" namespace_column = @namespace_column vector_dimension = llm.default_dimension || 1000 # bx_builder_chain_embeddings table db.create_table? :bx_builder_chain_embeddings do primary_key :id text :content column :vectors, "vector(#{vector_dimension})" text namespace_column.to_sym, default: 'public' index namespace_column.to_sym end # bx_builder_chain_documents table db.create_table? :bx_builder_chain_documents do primary_key :id text :name text namespace_column.to_sym, default: 'public' :created_at :updated_at index [:name, namespace_column.to_sym], unique: true end # bx_builder_chain_document_chunks table db.create_table? :bx_builder_chain_document_chunks do primary_key :id foreign_key :document_id, :bx_builder_chain_documents, null: false, on_delete: :cascade foreign_key :embedding_id, :bx_builder_chain_embeddings, null: false, on_delete: :cascade unique [:document_id, :embedding_id] end end |
#destroy_default_schema ⇒ Object
Destroy default schema
128 129 130 131 132 |
# File 'lib/bx_builder_chain/vectorsearch/pgvector.rb', line 128 def destroy_default_schema db.drop_table? :bx_builder_chain_document_chunks db.drop_table? :bx_builder_chain_documents db.drop_table? :bx_builder_chain_embeddings end |
#documents_model ⇒ Object
42 43 44 45 46 |
# File 'lib/bx_builder_chain/vectorsearch/pgvector.rb', line 42 def documents_model Class.new(Sequel::Model(@table_name.to_sym)) do plugin :pgvector, :vectors end end |
#similarity_search(query:, k: 4) ⇒ Array<Hash>
Search for similar texts in the index
138 139 140 141 142 143 144 145 |
# File 'lib/bx_builder_chain/vectorsearch/pgvector.rb', line 138 def similarity_search(query:, k: 4) = llm.(text: query) similarity_search_by_vector( embedding: , k: k ) end |
#similarity_search_by_vector(embedding:, k: 4) ⇒ Array<Hash>
Search for similar texts in the index by the passed in vector. You must generate your own vector using the same LLM that generated the embeddings stored in the Vectorsearch DB.
152 153 154 155 156 157 158 159 |
# File 'lib/bx_builder_chain/vectorsearch/pgvector.rb', line 152 def similarity_search_by_vector(embedding:, k: 4) db.transaction do # BEGIN documents_model .nearest_neighbors(:vectors, , distance: operator, threshold: @threshold) .where(@namespace_column.to_sym => namespaces) .limit(k) end end |
#update_texts(texts:, ids:) ⇒ Array<Integer>
Update a list of ids and corresponding texts to the index
85 86 87 |
# File 'lib/bx_builder_chain/vectorsearch/pgvector.rb', line 85 def update_texts(texts:, ids:) upsert_texts(texts: texts, ids: ids) end |
#upsert_texts(texts:, ids:) ⇒ PG::Result
Upsert a list of texts to the index the added or updated texts.
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/bx_builder_chain/vectorsearch/pgvector.rb', line 53 def upsert_texts(texts:, ids:) data = texts.zip(ids).flat_map do |(text, id)| {id: id, content: text, vectors: llm.(text: text).to_s, namespace: namespaces[0]} end # @db[table_name.to_sym].multi_insert(data, return: :primary_key) @db[@table_name.to_sym] .insert_conflict( target: :id, update: {content: Sequel[:excluded][:content], vectors: Sequel[:excluded][:vectors]} ) .multi_insert(data, return: :primary_key) end |