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

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

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ CompletionFormat



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

def initialize(message)
  @message = message
end

Instance Method Details

#contentObject



67
# File 'lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/format/completion_format.rb', line 67

def content = message.content

#formatHash

Formats the message for the Gemini chat completions API



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/llm/shell/internal/llm.rb/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
50
# File 'lib/llm/shell/internal/llm.rb/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
    format_response(content)
  when File
    content.close unless content.closed?
    format_content(LLM.File(content.path))
  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
    [{functionResponse: {name: content.name, response: content.value}}]
  else
    prompt_error!(content)
  end
end

#format_response(response) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/format/completion_format.rb', line 52

def format_response(response)
  if response.file?
    file = response
    [{file_data: {mime_type: file.mime_type, file_uri: file.uri}}]
  else
    prompt_error!(content)
  end
end

#messageObject



66
# File 'lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/format/completion_format.rb', line 66

def message = @message

#prompt_error!(object) ⇒ Object

Raises:



61
62
63
64
# File 'lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/format/completion_format.rb', line 61

def prompt_error!(object)
  raise LLM::PromptError, "The given object (an instance of #{object.class}) " \
                          "is not supported by the Gemini API"
end