Class: Langchain::Vectorsearch::Pgvector
- Defined in:
- lib/langchain/vectorsearch/pgvector.rb
Constant Summary collapse
- OPERATORS =
The operators supported by the PostgreSQL vector search adapter
{ "cosine_distance" => "cosine", "euclidean_distance" => "euclidean", "inner_product_distance" => "inner_product" }
- 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 ⇒ Object
readonly
Returns the value of attribute namespace.
-
#namespace_column ⇒ Object
readonly
Returns the value of attribute namespace_column.
-
#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_texts(texts:, ids: nil) ⇒ Array<Integer>
Add a list of texts to the index.
-
#ask(question:, k: 4) {|String| ... } ⇒ String
Ask a question and return the answer.
-
#create_default_schema ⇒ Object
Create default schema.
-
#destroy_default_schema ⇒ Object
Destroy default schema.
- #documents_model ⇒ Object
-
#initialize(url:, index_name:, llm:, namespace: nil) ⇒ Pgvector
constructor
A new instance of Pgvector.
-
#remove_texts(ids:) ⇒ Integer
Remove a list of texts from the index.
-
#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
#add_data, #generate_hyde_prompt, #generate_rag_prompt, #get_default_schema, #similarity_search_with_hyde
Methods included from DependencyHelper
Constructor Details
#initialize(url:, index_name:, llm:, namespace: nil) ⇒ Pgvector
Returns a new instance of Pgvector.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/langchain/vectorsearch/pgvector.rb', line 30 def initialize(url:, index_name:, llm:, namespace: nil) depends_on "sequel" depends_on "pgvector" @db = Sequel.connect(url) @table_name = index_name @namespace_column = "namespace" @namespace = namespace @operator = OPERATORS[DEFAULT_OPERATOR] super(llm: llm) end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
24 25 26 |
# File 'lib/langchain/vectorsearch/pgvector.rb', line 24 def db @db end |
#documents_table ⇒ Object (readonly)
Returns the value of attribute documents_table.
24 25 26 |
# File 'lib/langchain/vectorsearch/pgvector.rb', line 24 def documents_table @documents_table end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
24 25 26 |
# File 'lib/langchain/vectorsearch/pgvector.rb', line 24 def namespace @namespace end |
#namespace_column ⇒ Object (readonly)
Returns the value of attribute namespace_column.
24 25 26 |
# File 'lib/langchain/vectorsearch/pgvector.rb', line 24 def namespace_column @namespace_column end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
24 25 26 |
# File 'lib/langchain/vectorsearch/pgvector.rb', line 24 def operator @operator end |
#table_name ⇒ Object (readonly)
Returns the value of attribute table_name.
24 25 26 |
# File 'lib/langchain/vectorsearch/pgvector.rb', line 24 def table_name @table_name end |
Instance Method Details
#add_texts(texts:, ids: nil) ⇒ Array<Integer>
Add a list of texts to the index
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/langchain/vectorsearch/pgvector.rb', line 73 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: namespace} end @db[table_name.to_sym].multi_insert(data, return: :primary_key) else upsert_texts(texts: texts, ids: ids) end end |
#ask(question:, k: 4) {|String| ... } ⇒ String
Ask a question and return the answer
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/langchain/vectorsearch/pgvector.rb', line 149 def ask(question:, k: 4, &block) search_results = similarity_search(query: question, k: k) context = search_results.map do |result| result.content.to_s end context = context.join("\n---\n") prompt = generate_rag_prompt(question: question, context: context) = [{role: "user", content: prompt}] response = llm.chat(messages: , &block) response.context = context response end |
#create_default_schema ⇒ Object
Create default schema
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/langchain/vectorsearch/pgvector.rb', line 101 def create_default_schema db.run "CREATE EXTENSION IF NOT EXISTS vector" namespace_column = @namespace_column vector_dimensions = llm.default_dimensions db.create_table? table_name.to_sym do primary_key :id text :content column :vectors, "vector(#{vector_dimensions})" text namespace_column.to_sym, default: nil end end |
#destroy_default_schema ⇒ Object
Destroy default schema
114 115 116 |
# File 'lib/langchain/vectorsearch/pgvector.rb', line 114 def destroy_default_schema db.drop_table? table_name.to_sym end |
#documents_model ⇒ Object
45 46 47 48 49 |
# File 'lib/langchain/vectorsearch/pgvector.rb', line 45 def documents_model Class.new(Sequel::Model(table_name.to_sym)) do plugin :pgvector, :vectors end end |
#remove_texts(ids:) ⇒ Integer
Remove a list of texts from the index
96 97 98 |
# File 'lib/langchain/vectorsearch/pgvector.rb', line 96 def remove_texts(ids:) @db[table_name.to_sym].where(id: ids).delete end |
#similarity_search(query:, k: 4) ⇒ Array<Hash>
Search for similar texts in the index
122 123 124 125 126 127 128 129 |
# File 'lib/langchain/vectorsearch/pgvector.rb', line 122 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.
136 137 138 139 140 141 142 |
# File 'lib/langchain/vectorsearch/pgvector.rb', line 136 def similarity_search_by_vector(embedding:, k: 4) db.transaction do # BEGIN documents_model .nearest_neighbors(:vectors, , distance: operator).limit(k) .where(namespace_column.to_sym => namespace) end end |
#update_texts(texts:, ids:) ⇒ Array<Integer>
Update a list of ids and corresponding texts to the index
89 90 91 |
# File 'lib/langchain/vectorsearch/pgvector.rb', line 89 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.
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/langchain/vectorsearch/pgvector.rb', line 56 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: namespace} 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 |