Class: Pinecone::Vector

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/pinecone/vector.rb,
lib/pinecone/vector/query.rb,
lib/pinecone/vector/filter.rb,
lib/pinecone/vector/sparse_vector.rb

Defined Under Namespace

Classes: Filter, Query, SparseVector

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index_name) ⇒ Vector

Returns a new instance of Vector.



12
13
14
15
16
17
18
19
20
# File 'lib/pinecone/vector.rb', line 12

def initialize(index_name)
  @base_uri = set_base_uri(index_name)

  @headers = {
    "Content-Type" => "application/json",
    "Accept" => "application/json",
    "Api-Key" => Pinecone.configuration.api_key
  }
end

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



10
11
12
# File 'lib/pinecone/vector.rb', line 10

def base_uri
  @base_uri
end

Instance Method Details

#delete(namespace: "", ids: [], delete_all: false, filter: {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pinecone/vector.rb', line 22

def delete(namespace: "", ids: [], delete_all: false, filter: {})
  inputs = {
    namespace: namespace,
    ids: ids,
    deleteAll: delete_all,
    filter: filter
  }
  inputs.delete(:filter) if delete_all || ids.any?
  payload = options.merge(body: inputs.to_json)
  self.class.post("#{@base_uri}/vectors/delete", payload)
end

#describe_index_stats(filter: {}) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/pinecone/vector.rb', line 115

def describe_index_stats(filter: {})
  payload = if filter.empty?
    options
  else
    options.merge(body: {filter: filter}.to_json)
  end
  self.class.post("#{@base_uri}/describe_index_stats", payload)
end

#fetch(namespace: "", ids: []) ⇒ Object



34
35
36
37
# File 'lib/pinecone/vector.rb', line 34

def fetch(namespace: "", ids: [])
  query_string = URI.encode_www_form({namespace: namespace, ids: ids})
  self.class.get("#{@base_uri}/vectors/fetch?#{query_string}", options)
end

#list(prefix: nil, limit: nil, namespace: nil, &block) ⇒ Object



39
40
41
42
43
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/pinecone/vector.rb', line 39

def list(prefix: nil, limit: nil, namespace: nil, &block)
  all_ids = []
  pagination_token = nil
  total_fetched = 0

  loop do
    current_limit = limit.nil? ? 100 : [limit - total_fetched, 100].min

    response = list_paginated(
      prefix: prefix,
      limit: current_limit,
      pagination_token: pagination_token,
      namespace: namespace
    )

    break unless response.success?

    results = response.parsed_response
    ids = results["vectors"]&.map { |v| v["id"] } || []

    if block
      yield ids
    else
      all_ids.concat(ids)
    end

    total_fetched += ids.length
    pagination_token = results.dig("pagination", "next")

    break if ids.empty?
    break if limit && total_fetched >= limit
    break if pagination_token.nil? || pagination_token.empty?
  end

  if block
    nil
  else
    limit ? all_ids.first(limit) : all_ids
  end
end

#list_paginated(prefix: nil, limit: nil, pagination_token: nil, namespace: nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/pinecone/vector.rb', line 80

def list_paginated(prefix: nil, limit: nil, pagination_token: nil, namespace: nil)
  query_params = {}
  query_params["prefix"] = prefix if prefix
  query_params["limit"] = limit if limit
  query_params["paginationToken"] = pagination_token if pagination_token
  query_params["namespace"] = namespace if namespace

  query_string = URI.encode_www_form(query_params)
  self.class.get("#{@base_uri}/vectors/list?#{query_string}", options)
end

#optionsObject



124
125
126
127
128
# File 'lib/pinecone/vector.rb', line 124

def options
  {
    headers: @headers
  }
end

#query(query) ⇒ Object



96
97
98
99
100
# File 'lib/pinecone/vector.rb', line 96

def query(query)
  object = query.is_a?(Pinecone::Vector::Query) ? query : Pinecone::Vector::Query.new(query)
  payload = options.merge(body: object.to_json)
  self.class.post("#{@base_uri}/query", payload)
end

#update(id:, values: [], sparse_values: {indices: [], values: []}, set_metadata: {}, namespace: "") ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pinecone/vector.rb', line 102

def update(id:, values: [], sparse_values: {indices: [], values: []}, set_metadata: {}, namespace: "")
  inputs = {
    id: id
  }
  inputs["values"] = values unless values.empty?
  inputs["sparseValues"] = sparse_values unless sparse_values[:indices].empty? || sparse_values[:values].empty?
  inputs["setMetadata"] =  unless .empty?
  inputs["namespace"] = namespace unless namespace.empty?

  payload = options.merge(body: inputs.to_json)
  self.class.post("#{@base_uri}/vectors/update", payload)
end

#upsert(body) ⇒ Object



91
92
93
94
# File 'lib/pinecone/vector.rb', line 91

def upsert(body)
  payload = options.merge(body: body.to_json)
  self.class.post("#{@base_uri}/vectors/upsert", payload)
end