Class: Langchain::Vectorsearch::Milvus

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

Constant Summary

Constants inherited from Base

Base::DEFAULT_METRIC

Instance Attribute Summary

Attributes inherited from Base

#client, #index_name, #llm

Instance Method Summary collapse

Methods inherited from Base

#add_data, #generate_hyde_prompt, #generate_rag_prompt, logger_options, #remove_texts, #similarity_search_with_hyde, #update_texts

Methods included from DependencyHelper

#depends_on

Constructor Details

#initialize(url:, index_name:, llm:, api_key: nil) ⇒ Milvus

Wrapper around Milvus REST APIs.

Gem requirements:

gem "milvus", "~> 0.9.2"

Usage: milvus = Langchain::Vectorsearch::Milvus.new(url:, index_name:, llm:, api_key:)



15
16
17
18
19
20
21
22
# File 'lib/langchain/vectorsearch/milvus.rb', line 15

def initialize(url:, index_name:, llm:, api_key: nil)
  depends_on "milvus"

  @client = ::Milvus::Client.new(url: url)
  @index_name = index_name

  super(llm: llm)
end

Instance Method Details

#add_texts(texts:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/langchain/vectorsearch/milvus.rb', line 24

def add_texts(texts:)
  client.entities.insert(
    collection_name: index_name,
    num_rows: Array(texts).size,
    fields_data: [
      {
        field_name: "content",
        type: ::Milvus::DATA_TYPES["varchar"],
        field: Array(texts)
      }, {
        field_name: "vectors",
        type: ::Milvus::DATA_TYPES["float_vector"],
        field: Array(texts).map { |text| llm.embed(text: text).embedding }
      }
    ]
  )
end

#ask(question:, k: 4) {|String| ... } ⇒ String

Ask a question and return the answer

Parameters:

  • question (String)

    The question to ask

  • k (Integer) (defaults to: 4)

    The number of results to have in context

Yields:

  • (String)

    Stream responses back one String at a time

Returns:

  • (String)

    The answer to the question



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/langchain/vectorsearch/milvus.rb', line 144

def ask(question:, k: 4, &block)
  search_results = similarity_search(query: question, k: k)

  content_field = search_results.dig("results", "fields_data").select { |field| field.dig("field_name") == "content" }
  content_data = content_field.first.dig("Field", "Scalars", "Data", "StringData", "data")

  context = content_data.join("\n---\n")

  prompt = generate_rag_prompt(question: question, context: context)

  messages = [{role: "user", content: prompt}]
  response = llm.chat(messages: messages, &block)

  response.context = context
  response
end

#create_default_indexBoolean

Create the default index

Returns:

  • (Boolean)

    The response from the server



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/langchain/vectorsearch/milvus.rb', line 84

def create_default_index
  client.indices.create(
    collection_name: "Documents",
    field_name: "vectors",
    extra_params: [
      {key: "metric_type", value: "L2"},
      {key: "index_type", value: "IVF_FLAT"},
      {key: "params", value: "{\"nlist\":1024}"}
    ]
  )
end

#create_default_schemaHash

Create default schema

Returns:

  • (Hash)

    The response from the server



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/langchain/vectorsearch/milvus.rb', line 46

def create_default_schema
  client.collections.create(
    auto_id: true,
    collection_name: index_name,
    description: "Default schema created by langchain.rb",
    fields: [
      {
        name: "id",
        is_primary_key: true,
        autoID: true,
        data_type: ::Milvus::DATA_TYPES["int64"]
      }, {
        name: "content",
        is_primary_key: false,
        data_type: ::Milvus::DATA_TYPES["varchar"],
        type_params: [
          {
            key: "max_length",
            value: "32768" # Largest allowed value
          }
        ]
      }, {
        name: "vectors",
        data_type: ::Milvus::DATA_TYPES["float_vector"],
        is_primary_key: false,
        type_params: [
          {
            key: "dim",
            value: llm.default_dimensions.to_s
          }
        ]
      }
    ]
  )
end

#destroy_default_schemaHash

Delete default schema

Returns:

  • (Hash)

    The response from the server



104
105
106
# File 'lib/langchain/vectorsearch/milvus.rb', line 104

def destroy_default_schema
  client.collections.delete(collection_name: index_name)
end

#get_default_schemaHash

Get the default schema

Returns:

  • (Hash)

    The response from the server



98
99
100
# File 'lib/langchain/vectorsearch/milvus.rb', line 98

def get_default_schema
  client.collections.get(collection_name: index_name)
end

#load_default_schemaBoolean

Load default schema into memory

Returns:

  • (Boolean)

    The response from the server



110
111
112
# File 'lib/langchain/vectorsearch/milvus.rb', line 110

def load_default_schema
  client.collections.load(collection_name: index_name)
end

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



114
115
116
117
118
119
120
121
# File 'lib/langchain/vectorsearch/milvus.rb', line 114

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) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/langchain/vectorsearch/milvus.rb', line 123

def similarity_search_by_vector(embedding:, k: 4)
  load_default_schema

  client.search(
    collection_name: index_name,
    output_fields: ["id", "content", "vectors"],
    top_k: k.to_s,
    vectors: [embedding],
    dsl_type: 1,
    params: "{\"nprobe\": 10}",
    anns_field: "vectors",
    metric_type: "L2",
    vector_type: ::Milvus::DATA_TYPES["float_vector"]
  )
end