Module: RubyLLM::Protocols::Cohere::Models

Defined in:
lib/ruby_llm/protocols/cohere/models.rb

Overview

Model information for the Cohere API

Constant Summary collapse

FEATURE_CAPABILITIES =
{
  'tools' => 'function_calling',
  'tool_choice' => 'tool_choice',
  'json_schema' => 'structured_output',
  'json_mode' => 'json_mode',
  'citations' => 'citations'
}.freeze

Class Method Summary collapse

Class Method Details

.capabilities_from(endpoints, features) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ruby_llm/protocols/cohere/models.rb', line 56

def capabilities_from(endpoints, features)
  return ['ocr'] if endpoints.include?('parse')
  return ['transcription'] if transcription_endpoint?(endpoints)
  return [] if embedding_endpoint?(endpoints) || rerank_endpoint?(endpoints)

  features = Array(features)
  capabilities = []
  capabilities << 'streaming' if endpoints.intersect?(%w[chat generate])
  reported = FEATURE_CAPABILITIES.filter_map do |feature, capability|
    capability if features.include?(feature)
  end
  capabilities.concat(reported)
  capabilities
end

.embedding_endpoint?(endpoints) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/ruby_llm/protocols/cohere/models.rb', line 71

def embedding_endpoint?(endpoints)
  endpoints.any? { |endpoint| endpoint.start_with?('embed') }
end

.embedding_modalities(endpoints) ⇒ Object



75
76
77
78
79
80
# File 'lib/ruby_llm/protocols/cohere/models.rb', line 75

def embedding_modalities(endpoints)
  input = []
  input << 'text' if endpoints.include?('embed')
  input << 'image' if endpoints.any? { |endpoint| endpoint.start_with?('embed_image') }
  { input: input.empty? ? ['text'] : input, output: ['embeddings'] }
end

.modalities_from(endpoints) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/ruby_llm/protocols/cohere/models.rb', line 46

def modalities_from(endpoints)
  return { input: ['image'], output: ['text'] } if endpoints.include?('parse')
  return { input: ['audio'], output: ['text'] } if transcription_endpoint?(endpoints)

  return embedding_modalities(endpoints) if embedding_endpoint?(endpoints)
  return { input: ['text'], output: ['rerank'] } if rerank_endpoint?(endpoints)

  { input: ['text'], output: ['text'] }
end

.models_urlObject

The model catalog is the one endpoint Cohere still serves from v1.



19
20
21
# File 'lib/ruby_llm/protocols/cohere/models.rb', line 19

def models_url
  'v1/models?page_size=1000'
end

.parse_list_models_response(response, slug) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby_llm/protocols/cohere/models.rb', line 23

def parse_list_models_response(response, slug)
  Array(response.body['models']).reject { |model| model['is_deprecated'] }.map do |model_data|
    model_id = model_data['name']
    endpoints = Array(model_data['endpoints'])

    Model.new(
      id: model_id,
      name: model_id,
      provider: slug,
      context_window: model_data['context_length']&.to_i,
      modalities: modalities_from(endpoints),
      capabilities: capabilities_from(endpoints, model_data['features']),
      metadata: {
        endpoints: endpoints,
        default_endpoints: Array(model_data['default_endpoints']),
        features: Array(model_data['features']),
        finetuned: model_data['finetuned'],
        tokenizer_url: model_data['tokenizer_url']
      }.compact
    )
  end
end

.rerank_endpoint?(endpoints) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/ruby_llm/protocols/cohere/models.rb', line 82

def rerank_endpoint?(endpoints)
  endpoints.any? { |endpoint| endpoint.start_with?('rerank') }
end

.transcription_endpoint?(endpoints) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/ruby_llm/protocols/cohere/models.rb', line 86

def transcription_endpoint?(endpoints)
  endpoints.any? { |endpoint| endpoint.start_with?('transcri') }
end