Module: RubyLLM::Protocols::Cohere::Embeddings
- Defined in:
- lib/ruby_llm/protocols/cohere/embeddings.rb
Overview
Embeddings methods for the Cohere v2 API integration
Constant Summary collapse
- DEFAULT_INPUT_TYPE =
'search_document'
Class Method Summary collapse
-
.embedding_inputs(text, attachments) ⇒ Object
embed-v4 takes mixed text and images as
inputs; text-only requests keep using the simplertextsarray every Embed model accepts. - .embedding_url ⇒ Object
-
.image_embedding_inputs(text, attachments) ⇒ Object
:nodoc:.
- .parse_embedding_response(response, model:, text:) ⇒ Object
-
.render_embedding_payload(text, model:, dimensions:, task_type: nil, title: nil, with: [], provider_options: {}) ⇒ Object
rubocop:disable-next Lint/UnusedMethodArgument.
-
.separate_image_embeddings?(model) ⇒ Boolean
:nodoc:.
- .supports_embedding_media? ⇒ Boolean
Class Method Details
.embedding_inputs(text, attachments) ⇒ Object
embed-v4 takes mixed text and images as inputs; text-only requests
keep using the simpler texts array every Embed model accepts.
58 59 60 61 62 63 64 |
# File 'lib/ruby_llm/protocols/cohere/embeddings.rb', line 58 def (text, ) return { texts: Support::Utils.to_safe_array(text).map(&:to_s) } if .empty? raise ArgumentError, 'embed one text at a time when embedding attachments' if text.is_a?(Array) { inputs: [{ content: Media.format_content(text, ) }] } end |
.embedding_url ⇒ Object
12 13 14 |
# File 'lib/ruby_llm/protocols/cohere/embeddings.rb', line 12 def (...) 'v2/embed' end |
.image_embedding_inputs(text, attachments) ⇒ Object
:nodoc:
39 40 41 42 43 44 45 |
# File 'lib/ruby_llm/protocols/cohere/embeddings.rb', line 39 def (text, ) # :nodoc: raise ArgumentError, 'Cohere Embed v3 accepts text or an image, not both' unless text.nil? || text == '' raise ArgumentError, 'Cohere Embed v3 accepts one image per request' unless .one? raise UnsupportedAttachmentError, .first.mime_type unless .first.image? { images: ["data:#{.first.mime_type};base64,#{.first.encoded}"] } end |
.parse_embedding_response(response, model:, text:) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/ruby_llm/protocols/cohere/embeddings.rb', line 47 def (response, model:, text:) data = response.body vectors = data.dig('embeddings', 'float') vectors = vectors.first if vectors&.length == 1 && !text.is_a?(Array) billed = data.dig('meta', 'billed_units') || {} Embedding.new(vectors:, model:, input_tokens: billed['input_tokens']) end |
.render_embedding_payload(text, model:, dimensions:, task_type: nil, title: nil, with: [], provider_options: {}) ⇒ Object
rubocop:disable-next Lint/UnusedMethodArgument
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ruby_llm/protocols/cohere/embeddings.rb', line 17 def (text, model:, dimensions:, task_type: nil, title: nil, with: [], provider_options: {}) image_only = with.any? && (model) payload = { model: model, input_type: task_type || (image_only ? 'image' : DEFAULT_INPUT_TYPE), embedding_types: ['float'], output_dimension: dimensions }.compact payload.merge!(image_only ? (text, with) : (text, with)) Support::Utils.deep_merge(payload, ) end |
.separate_image_embeddings?(model) ⇒ Boolean
:nodoc:
35 36 37 |
# File 'lib/ruby_llm/protocols/cohere/embeddings.rb', line 35 def (model) # :nodoc: %w[embed-english-v3.0 embed-multilingual-v3.0].include?(model) end |
.supports_embedding_media? ⇒ Boolean
31 32 33 |
# File 'lib/ruby_llm/protocols/cohere/embeddings.rb', line 31 def true end |