Class: Langchain::Vectorsearch::Hnswlib
- Defined in:
- lib/langchain/vectorsearch/hnswlib.rb
Constant Summary
Constants inherited from Base
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Wrapper around HNSW (Hierarchical Navigable Small World) library.
-
#path_to_index ⇒ Object
readonly
Wrapper around HNSW (Hierarchical Navigable Small World) library.
Attributes inherited from Base
Instance Method Summary collapse
-
#add_texts(texts:, ids:) ⇒ Boolean
Add a list of texts and corresponding IDs to the index.
-
#initialize(llm:, path_to_index:) ⇒ Langchain::Vectorsearch::Hnswlib
constructor
Initialize the HNSW vector search.
-
#similarity_search(query:, k: 4) ⇒ Array
Search for similar texts.
-
#similarity_search_by_vector(embedding:, k: 4) ⇒ Array
Search for the K nearest neighbors of a given vector.
Methods inherited from Base
#add_data, #ask, #create_default_schema, #destroy_default_schema, #generate_hyde_prompt, #generate_rag_prompt, #get_default_schema, #remove_texts, #similarity_search_with_hyde, #update_texts
Methods included from DependencyHelper
Constructor Details
#initialize(llm:, path_to_index:) ⇒ Langchain::Vectorsearch::Hnswlib
Initialize the HNSW vector search
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/langchain/vectorsearch/hnswlib.rb', line 24 def initialize(llm:, path_to_index:) depends_on "hnswlib" super(llm: llm) @client = ::Hnswlib::HierarchicalNSW.new(space: DEFAULT_METRIC, dim: llm.default_dimensions) @path_to_index = path_to_index initialize_index end |
Instance Attribute Details
#client ⇒ Object (readonly)
Wrapper around HNSW (Hierarchical Navigable Small World) library. HNSWLib is an in-memory vectorstore that can be saved to a file on disk.
Gem requirements:
gem "hnswlib", "~> 0.8.1"
Usage:
hnsw = Langchain::Vectorsearch::Hnswlib.new(llm:, path_to_index:)
15 16 17 |
# File 'lib/langchain/vectorsearch/hnswlib.rb', line 15 def client @client end |
#path_to_index ⇒ Object (readonly)
Wrapper around HNSW (Hierarchical Navigable Small World) library. HNSWLib is an in-memory vectorstore that can be saved to a file on disk.
Gem requirements:
gem "hnswlib", "~> 0.8.1"
Usage:
hnsw = Langchain::Vectorsearch::Hnswlib.new(llm:, path_to_index:)
15 16 17 |
# File 'lib/langchain/vectorsearch/hnswlib.rb', line 15 def path_to_index @path_to_index end |
Instance Method Details
#add_texts(texts:, ids:) ⇒ Boolean
Add a list of texts and corresponding IDs to the index
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/langchain/vectorsearch/hnswlib.rb', line 42 def add_texts(texts:, ids:) resize_index(texts.size) Array(texts).each_with_index do |text, i| = llm.(text: text). client.add_point(, ids[i]) end client.save_index(path_to_index) end |
#similarity_search(query:, k: 4) ⇒ Array
Search for similar texts
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/langchain/vectorsearch/hnswlib.rb', line 63 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
Search for the K nearest neighbors of a given vector
82 83 84 85 86 87 |
# File 'lib/langchain/vectorsearch/hnswlib.rb', line 82 def similarity_search_by_vector( embedding:, k: 4 ) client.search_knn(, k) end |