Class: Vectorsearch::Milvus

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

Constant Summary

Constants inherited from Base

Base::DEFAULT_COHERE_DIMENSION, Base::DEFAULT_METRIC, Base::DEFAULT_OPENAI_DIMENSION, Base::LLMS

Instance Attribute Summary

Attributes inherited from Base

#client, #index_name, #llm, #llm_api_key

Instance Method Summary collapse

Methods inherited from Base

#generate_completion, #generate_embedding, #generate_prompt

Constructor Details

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

Returns a new instance of Milvus.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/vectorsearch/milvus.rb', line 7

def initialize(
  url:,
  api_key: nil,
  index_name:,
  llm:,
  llm_api_key:
)
  @client = ::Milvus::Client.new(
    url: url
  )
  @index_name = index_name

  super(llm: llm, llm_api_key: llm_api_key)
end

Instance Method Details

#add_texts(texts:) ⇒ Object



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

def add_texts(
  texts:
)
  client.entities.insert(
    collection_name: index_name,
    num_rows: texts.count,
    fields_data: [
      {
        field_name: "content",
        type: ::Milvus::DATA_TYPES["varchar"],
        field: texts
      }, {
        field_name: "vectors",
        type: ::Milvus::DATA_TYPES["binary_vector"],
        field: texts.map { |text| generate_embedding(text: text) }
      }
    ]
  )
end

#ask(question:) ⇒ Object

Raises:

  • (NotImplementedError)


107
108
109
# File 'lib/vectorsearch/milvus.rb', line 107

def ask(question:)
  raise NotImplementedError
end

#create_default_schemaHash

Create default schema

Returns:

  • (Hash)

    The response from the server



44
45
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
# File 'lib/vectorsearch/milvus.rb', line 44

def create_default_schema
  client.collections.create(
    auto_id: true,
    collection_name: index_name,
    description: "Default schema created by Vectorsearch",
    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["binary_vector"],
        is_primary_key: false,
        type_params: [
          {
            key: "dim",
            value: default_dimension.to_s
          }
        ]
      }
    ]
  )
end

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



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/vectorsearch/milvus.rb', line 80

def similarity_search(
  query:,
  k: 4
)
  embedding = generate_embedding(text: query)

  similarity_search_by_vector(
    embedding: embedding,
    k: k
  )
end

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/vectorsearch/milvus.rb', line 92

def similarity_search_by_vector(
  embedding:,
  k: 4
)
  client.search(
    collection_name: index_name,
    top_k: k.to_s,
    vectors: [ embedding ],
    dsl_type: 1,
    params: "{\"nprobe\": 10}",
    anns_field: "content",
    metric_type: "L2"
  )
end