Class: Langchain::Vectorsearch::Hnswlib

Inherits:
Base
  • Object
show all
Defined in:
lib/langchain/vectorsearch/hnswlib.rb

Constant Summary

Constants inherited from Base

Base::DEFAULT_METRIC

Instance Attribute Summary collapse

Attributes inherited from Base

#index_name, #llm

Instance Method Summary collapse

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

#depends_on

Constructor Details

#initialize(llm:, path_to_index:) ⇒ Langchain::Vectorsearch::Hnswlib

Initialize the HNSW vector search

Parameters:

  • llm (Object)

    The LLM client to use

  • path_to_index (String)

    The local path to the index file, e.g.: β€œ/storage/index.ann”



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

#clientObject (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_indexObject (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

Parameters:

  • texts (Array<String>)

    The list of texts to add

  • ids (Array<Integer>)

    The list of corresponding IDs (integers) to the texts

Returns:

  • (Boolean)

    The response from the HNSW library



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|
    embedding = llm.embed(text: text).embedding

    client.add_point(embedding, ids[i])
  end

  client.save_index(path_to_index)
end

#similarity_search(query:, k: 4) ⇒ Array

Search for similar texts

Parameters:

  • query (String)

    The text to search for

  • k (Integer) (defaults to: 4)

    The number of results to return

Returns:

  • (Array)

    Results in the format β€˜[[id1, id2], [distance1, distance2]]`



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
)
  embedding = llm.embed(text: query).embedding

  similarity_search_by_vector(
    embedding: embedding,
    k: k
  )
end

#similarity_search_by_vector(embedding:, k: 4) ⇒ Array

Search for the K nearest neighbors of a given vector

Parameters:

  • embedding (Array<Float>)

    The embedding to search for

  • k (Integer) (defaults to: 4)

    The number of results to return

Returns:

  • (Array)

    Results in the format β€˜[[id1, id2], [distance1, distance2]]`



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(embedding, k)
end