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

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

Overview

Cohere provider for accessing Cohere’s language models

This class provides completion, embedding, and streaming capabilities for Cohere’s API, including proper error handling and response normalization.

Defined Under Namespace

Classes: CohereChoice, CohereEmbeddingResponse, CohereResponse, CohereStreamChoice, CohereStreamDelta, CohereStreamResponse

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

models, options, #stream?

Constructor Details

#initialize(api_key: nil) ⇒ Cohere

Returns a new instance of Cohere.



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

def initialize(api_key: nil)
  super(api_key: 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.



25
26
27
# File 'lib/durable/llm/providers/cohere.rb', line 25

def api_key
  @api_key
end

Class Method Details

.stream?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/durable/llm/providers/cohere.rb', line 83

def self.stream?
  true
end

Instance Method Details

#completion(options) ⇒ Object



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

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



21
22
23
# File 'lib/durable/llm/providers/cohere.rb', line 21

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

#embedding(model:, input:, **options) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/durable/llm/providers/cohere.rb', line 64

def embedding(model:, input:, **options)
  response = @conn.post('embed') do |req|
    req.headers['Authorization'] = "Bearer #{@api_key}"
    req.headers['Content-Type'] = 'application/json'
    req.body = { model: model, texts: Array(input), input_type: 'search_document', **options }
  end

  handle_response(response, CohereEmbeddingResponse)
end

#modelsObject



74
75
76
77
78
79
80
81
# File 'lib/durable/llm/providers/cohere.rb', line 74

def models
  response = @conn.get('../v1/models') do |req|
    req.headers['Authorization'] = "Bearer #{@api_key}"
  end

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

#stream(options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/durable/llm/providers/cohere.rb', line 46

def stream(options)
  options[:stream] = true

  response = @conn.post('chat') do |req|
    req.headers['Authorization'] = "Bearer #{@api_key}"
    req.headers['Accept'] = 'text/event-stream'
    req.body = options

    user_proc = proc do |chunk, _size, _total|
      yield CohereStreamResponse.new(chunk)
    end

    req.options.on_data = to_json_stream(user_proc: user_proc)
  end

  handle_response(response)
end