Module: RubyLLM::Protocols::Mistral::Content

Included in:
Conversations, MultiCompletion
Defined in:
lib/ruby_llm/protocols/mistral/content.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.parse_conversation_citation(part, offset) ⇒ Object



34
35
36
37
# File 'lib/ruby_llm/protocols/mistral/content.rb', line 34

def parse_conversation_citation(part, offset)
  Citation.new(url: part['url'], title: part['title'], cited_text: part['description'],
               start_index: offset, end_index: offset)
end

.parse_conversation_content(output) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/ruby_llm/protocols/mistral/content.rb', line 9

def parse_conversation_content(output)
  result = { text: +'', thinking: +'', attachments: [], citations: [] }
  output.select { |entry| entry['type'] == 'message.output' }.each do |entry|
    parse_conversation_parts(entry['content'], result)
  end
  result[:thinking] = nil if result[:thinking].empty?
  result
end

.parse_conversation_file(part) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/ruby_llm/protocols/mistral/content.rb', line 39

def parse_conversation_file(part)
  format = part['file_type'].to_s
  mime_type = format.include?('/') ? format : RubyLLM::Files::MimeType.for(name: "file.#{format}")
  file = UploadedFile.new(id: part.fetch('file_id'), provider: @provider.slug,
                          filename: part['file_name'], mime_type: mime_type, downloadable: true, metadata: part)
  Attachment.new(file, config: @config)
end

.parse_conversation_parts(content, result) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby_llm/protocols/mistral/content.rb', line 18

def parse_conversation_parts(content, result)
  return result[:text] << content if content.is_a?(String)

  Array(content).compact.each do |part|
    case part['type']
    when 'text' then result[:text] << part['text'].to_s
    when 'thinking' then result[:thinking] << Array(part['thinking']).filter_map { |item| item['text'] }.join
    when 'tool_reference' then result[:citations] << parse_conversation_citation(part, result[:text].length)
    when 'tool_file' then result[:attachments] << parse_conversation_file(part)
    when 'image_url'
      url = part['image_url'].is_a?(Hash) ? part['image_url']['url'] : part['image_url']
      result[:attachments] << Attachment.new(url, config: @config)
    end
  end
end