Module: LLM::Gemini::ResponseParser

Defined in:
lib/llm/providers/gemini/response_parser.rb

Instance Method Summary collapse

Instance Method Details

#parse_completion(body) ⇒ Hash

Parameters:

  • body (Hash)

    The response body from the LLM provider

Returns:

  • (Hash)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/llm/providers/gemini/response_parser.rb', line 22

def parse_completion(body)
  {
    model: body["modelVersion"],
    choices: body["candidates"].map do
      LLM::Message.new(
        _1.dig("content", "role"),
        _1.dig("content", "parts", 0, "text"),
        {response: self}
      )
    end,
    prompt_tokens: body.dig("usageMetadata", "promptTokenCount"),
    completion_tokens: body.dig("usageMetadata", "candidatesTokenCount")
  }
end

#parse_embedding(body) ⇒ Hash

Parameters:

  • body (Hash)

    The response body from the LLM provider

Returns:

  • (Hash)


11
12
13
14
15
16
# File 'lib/llm/providers/gemini/response_parser.rb', line 11

def parse_embedding(body)
  {
    model: "text-embedding-004",
    embeddings: body.dig("embedding", "values")
  }
end

#parse_image(body) ⇒ Hash

Parameters:

  • body (Hash)

    The response body from the LLM provider

Returns:

  • (Hash)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/llm/providers/gemini/response_parser.rb', line 41

def parse_image(body)
  {
    urls: [],
    images: body["candidates"].flat_map do |candidate|
      candidate["content"]["parts"].filter_map do
        next unless _1.dig("inlineData", "data")
        StringIO.new(_1["inlineData"]["data"].unpack1("m0"))
      end
    end
  }
end