Module: RubyLLM::Providers::OpenRouter::Embeddings

Included in:
ChatCompletions
Defined in:
lib/ruby_llm/providers/openrouter/embeddings.rb

Overview

Text and multimodal embedding requests for OpenRouter.

Constant Summary collapse

EMBEDDING_MEDIA_TYPES =
{ audio: :input_audio, video: :input_video, pdf: :input_file }.freeze

Class Method Summary collapse

Class Method Details

.format_embedding_attachment(attachment) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby_llm/providers/openrouter/embeddings.rb', line 37

def format_embedding_attachment(attachment)
  raise UnsupportedAttachmentError, attachment.mime_type if attachment.provider_file?

  if (type = EMBEDDING_MEDIA_TYPES[attachment.type])
    { type: type.to_s, type => { data: attachment.for_llm, format: attachment.format } }
  else
    Protocols::ChatCompletions::Media.format_attachment(
      attachment, document_attachments: :none, image_attachments: true, audio_attachments: false
    )
  end
end

.format_embedding_content(text, attachments) ⇒ Object



31
32
33
34
35
# File 'lib/ruby_llm/providers/openrouter/embeddings.rb', line 31

def format_embedding_content(text, attachments)
  Protocols::ChatCompletions::Media.format_parts(text, attachments) do |attachment|
    format_embedding_attachment(attachment)
  end
end

.render_embedding_payload(text, model:, dimensions:, task_type: nil, title: nil, with: [], provider_options: {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ruby_llm/providers/openrouter/embeddings.rb', line 16

def render_embedding_payload(text, model:, dimensions:, task_type: nil, title: nil, with: [],
                             provider_options: {})
  input = if with.any?
            raise ArgumentError, 'embed one text at a time when embedding attachments' if text.is_a?(Array)

            [{ content: format_embedding_content(text, with) }]
          else
            text
          end

  payload = super(input, model:, dimensions:, task_type:, title:, provider_options: {})
  payload[:input_type] = task_type if task_type
  payload.merge(provider_options)
end

.supports_embedding_media?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/ruby_llm/providers/openrouter/embeddings.rb', line 12

def supports_embedding_media?
  true
end