Class: Cohere::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/cohere/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(access_token: nil, version: nil) ⇒ Client

Returns a new instance of Client.



6
7
8
9
# File 'lib/cohere/client.rb', line 6

def initialize(access_token: nil, version: nil)
  @access_token = access_token
  @version = version || "2021-11-08"; # currently latest, update when we version better
end

Instance Method Details

#classify(model:, options:) ⇒ Object



11
12
13
# File 'lib/cohere/client.rb', line 11

def classify(model:, options:)
  serialize post(endpoint: "classify", model: model, body: DEFAULT_CLASSIFY_PARAMS.merge(options))
end

#embed(model:, options:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cohere/client.rb', line 15

def embed(model:, options:)
  threads = []
  default_options = DEFAULT_EMBED_PARAMS.merge(options)
  return OpenStruct.new(code: 400, body: { embeddings: [], ids: [] }) if default_options[:texts].empty?

  default_options[:texts].each_slice(1) do |batch|
    threads << threded_post(model: model, endpoint: "embed", body: default_options.merge(texts: batch))
  end

  results = threads.each(&:join).map(&:value)
  serialize_embed_results(results)
end

#generate(model:, options:) ⇒ Object



28
29
30
# File 'lib/cohere/client.rb', line 28

def generate(model:, options:)
  serialize post(endpoint: "generate", model: model, body: DEFAULT_GENERATE_PARAMS.merge(options))
end