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 = nil, host: nil) ⇒ Vector

Returns a new instance of Vector.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pinecone/vector.rb', line 15

def initialize(index_name = nil, host: nil)
  if host
    # Direct host targeting (preferred)
    @base_uri = if host.start_with?("http://", "https://")
      host
    elsif host.start_with?("localhost")
      "http://#{host}" # Use HTTP for localhost
    else
      "https://#{host}" # Use HTTPS for production hosts
    end
  elsif index_name
    # Legacy path: call describe_index
    @base_uri = set_fallback_base_uri(index_name)
  else
    raise ArgumentError, "Must provide either index_name or host: parameter"
  end

  @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.



13
14
15
# File 'lib/pinecone/vector.rb', line 13

def base_uri
  @base_uri
end

Instance Method Details

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



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pinecone/vector.rb', line 39

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



132
133
134
135
136
137
138
139
# File 'lib/pinecone/vector.rb', line 132

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

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



51
52
53
54
# File 'lib/pinecone/vector.rb', line 51

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



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pinecone/vector.rb', line 56

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



97
98
99
100
101
102
103
104
105
106
# File 'lib/pinecone/vector.rb', line 97

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



141
142
143
144
145
# File 'lib/pinecone/vector.rb', line 141

def options
  {
    headers: @headers
  }
end

#query(query) ⇒ Object



113
114
115
116
117
# File 'lib/pinecone/vector.rb', line 113

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



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/pinecone/vector.rb', line 119

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



108
109
110
111
# File 'lib/pinecone/vector.rb', line 108

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