Class: Durable::Llm::Providers::Cohere

Inherits:
Base
  • Object
show all
Defined in:
lib/durable/llm/providers/cohere.rb

Defined Under Namespace

Classes: CohereChoice, CohereResponse

Constant Summary collapse

BASE_URL =
'https://api.cohere.ai/v2'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#embedding, models, #stream, #stream?

Constructor Details

#initialize(api_key: nil) ⇒ Cohere

Returns a new instance of Cohere.



18
19
20
21
22
23
24
25
# File 'lib/durable/llm/providers/cohere.rb', line 18

def initialize(api_key: nil)
  @api_key = api_key || default_api_key
  @conn = Faraday.new(url: BASE_URL) do |faraday|
    faraday.request :json
    faraday.response :json
    faraday.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



16
17
18
# File 'lib/durable/llm/providers/cohere.rb', line 16

def api_key
  @api_key
end

Class Method Details

.stream?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/durable/llm/providers/cohere.rb', line 47

def self.stream?
  false
end

Instance Method Details

#completion(options) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/durable/llm/providers/cohere.rb', line 27

def completion(options)
  response = @conn.post('chat') do |req|
    req.headers['Authorization'] = "Bearer #{@api_key}"
    req.headers['Content-Type'] = 'application/json'
    req.body = options
  end

  handle_response(response)
end

#default_api_keyObject



12
13
14
# File 'lib/durable/llm/providers/cohere.rb', line 12

def default_api_key
  Durable::Llm.configuration.cohere&.api_key || ENV['COHERE_API_KEY']
end

#modelsObject



37
38
39
40
41
42
43
44
45
# File 'lib/durable/llm/providers/cohere.rb', line 37

def models
  response = @conn.get('models') do |req|
    req.headers['Authorization'] = "Bearer #{@api_key}"
    req.headers['OpenAI-Organization'] = @organization if @organization
  end

  data = handle_response(response).raw_response
  data['models']&.map { |model| model['name'] }
end