Class: LLM::Gemini::Format::CompletionFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/providers/gemini/format/completion_format.rb

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ CompletionFormat

Returns a new instance of CompletionFormat.

Parameters:



10
11
12
# File 'lib/llm/providers/gemini/format/completion_format.rb', line 10

def initialize(message)
  @message = message
end

Instance Method Details

#contentObject



52
# File 'lib/llm/providers/gemini/format/completion_format.rb', line 52

def content = message.content

#formatHash

Formats the message for the Gemini chat completions API

Returns:

  • (Hash)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/llm/providers/gemini/format/completion_format.rb', line 17

def format
  catch(:abort) do
    if Hash === message
      {role: message[:role], parts: format_content(message[:content])}
    elsif message.tool_call?
      {role: message.role, parts: message.extra[:original_tool_calls].map { {"functionCall" => _1} }}
    else
      {role: message.role, parts: format_content(message.content)}
    end
  end
end

#format_content(content) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/llm/providers/gemini/format/completion_format.rb', line 29

def format_content(content)
  case content
  when Array
    content.empty? ? throw(:abort, nil) : content.flat_map { format_content(_1) }
  when LLM::Response::File
    file = content
    [{file_data: {mime_type: file.mime_type, file_uri: file.uri}}]
  when LLM::File
    file = content
    [{inline_data: {mime_type: file.mime_type, data: file.to_b64}}]
  when String
    [{text: content}]
  when LLM::Message
    format_content(content.content)
  when LLM::Function::Return
    [{text: JSON.dump(content.value)}]
  else
    raise LLM::Error::PromptError, "The given object (an instance of #{content.class}) " \
                                   "is not supported by the Gemini API"
  end
end

#messageObject



51
# File 'lib/llm/providers/gemini/format/completion_format.rb', line 51

def message = @message