Class: Ruby::OpenaiPinecone::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/openai_pinecone/client.rb

Class Method Summary collapse

Class Method Details

.query(text, topK: 5, includeMetadata: true, includeValues: true, namespace: "") ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruby/openai_pinecone/client.rb', line 32

def self.query(text, topK: 5, includeMetadata: true, includeValues: true, namespace: "")
  vector = Ruby::OpenaiPinecone::Embeddings.generate(text)

  uri = URI.parse(Ruby::OpenaiPinecone.configuration.pinecone_query_endpoint)

  request = Net::HTTP::Post.new(uri)
  request["Content-Type"] = "application/json"
  request["Api-Key"] = Ruby::OpenaiPinecone.configuration.pinecone_api_key
  request.body =  {
    "vector": vector,
    "topK": topK,
    "includeMetadata": ,
    "includeValues": includeValues,
    "namespace": namespace
  }.to_json

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(request)
  end

  if response.code == "200"
    JSON.parse(response.body)
  else
    nil
  end
end

.upsert(id, vector, metadata: {}, namespace: "") ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby/openai_pinecone/client.rb', line 10

def self.upsert(id, vector, metadata: {}, namespace: "")
  uri = URI.parse(Ruby::OpenaiPinecone.configuration.pinecone_upsert_endpoint)

  request = Net::HTTP::Post.new(uri)
  request["Content-Type"] = "application/json"
  request["Api-Key"] = Ruby::OpenaiPinecone.configuration.pinecone_api_key
  request.body = {
    "vectors": [{
      "id": id,
      "metadata": ,
      "values": vector
    }],
    "namespace": namespace
  }.to_json

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(request)
  end

  response.code == "200"
end