Class: Milvus

Inherits:
Object
  • Object
show all
Defined in:
lib/cl/magic/common/milvus.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Milvus

Returns a new instance of Milvus.



3
4
5
6
# File 'lib/cl/magic/common/milvus.rb', line 3

def initialize(host, port)
  @host = host
  @port = port
end

Instance Method Details

#create_collection(collection_name) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cl/magic/common/milvus.rb', line 29

def create_collection(collection_name)
  final_url = "http://#{@host}:#{@port}/v1/vector/collections/create"
  data = {
    dbName: "default",
    collectionName: collection_name,
    dimension: 1536,
    metricType: "L2",
    primaryField: "id",
    vectorField: "vector"
  }

  # post
  sanitized_data = data.to_json
  cmd = """
    curl -s \
    '#{final_url}' \
    -X 'POST' \
    -H 'accept: application/json' \
    -H 'Content-Type: application/json' \
    -d '#{sanitized_data}'
  """
  return `#{cmd}`
end

#post_to_collection(collection_name, doc_key, embedding) ⇒ Object



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
# File 'lib/cl/magic/common/milvus.rb', line 53

def post_to_collection(collection_name, doc_key, embedding)
  final_url = "http://#{@host}:#{@port}/v1/vector/insert"
  data = {
    collectionName: collection_name,
    data: {
      doc_key: doc_key,
      vector: embedding
    }
  }

  # post
  sanitized_data = data.to_json
  cmd = """
    curl -s \
    '#{final_url}' \
    -X POST \
    -H 'accept: application/json' \
    -H 'Content-Type: application/json' \
    -d '#{sanitized_data}'
  """
  response = `#{cmd}`
  data = JSON.parse(response)
  raise "Error: #{data.to_json}\n\nData #{sanitized_data}" if data.has_key?("message")
  return data.to_json
end

#search(collection_name, embedding) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cl/magic/common/milvus.rb', line 8

def search(collection_name, embedding)
  final_url = "http://#{@host}:#{@port}/v1/vector/search"
  data = {
    collectionName: collection_name,
    vector: embedding,
    outputFields: ["id", "name", "doc_key", "distance"],
  }

  # post
  sanitized_data = data.to_json
  cmd = """
    curl -s \
    '#{final_url}' \
    -X 'POST' \
    -H 'accept: application/json' \
    -H 'Content-Type: application/json' \
    -d '#{sanitized_data}'
  """
  return `#{cmd}`
end