Module: LLM::OpenAI::ResponseParser

Defined in:
lib/llm/providers/openai/response_parser.rb,
lib/llm/providers/openai/response_parser/respond_parser.rb,
lib/llm/providers/openai/response_parser/completion_parser.rb

Defined Under Namespace

Classes: CompletionParser, RespondParser

Instance Method Summary collapse

Instance Method Details

#parse_completion(body) ⇒ Hash

Parameters:

  • body (Hash)

    The response body from the LLM provider

Returns:

  • (Hash)


11
12
13
# File 'lib/llm/providers/openai/response_parser.rb', line 11

def parse_completion(body)
  CompletionParser.new(body).format(self)
end

#parse_embedding(body) ⇒ Hash

Parameters:

  • body (Hash)

    The response body from the LLM provider

Returns:

  • (Hash)


27
28
29
30
31
32
33
34
# File 'lib/llm/providers/openai/response_parser.rb', line 27

def parse_embedding(body)
  {
    model: body["model"],
    embeddings: body["data"].map { _1["embedding"] },
    prompt_tokens: body.dig("usage", "prompt_tokens"),
    total_tokens: body.dig("usage", "total_tokens")
  }
end

#parse_image(body) ⇒ Hash

Parameters:

  • body (Hash)

    The response body from the LLM provider

Returns:

  • (Hash)


40
41
42
43
44
45
46
47
48
# File 'lib/llm/providers/openai/response_parser.rb', line 40

def parse_image(body)
  {
    urls: body["data"].filter_map { _1["url"] },
    images: body["data"].filter_map do
      next unless _1["b64_json"]
      StringIO.new(_1["b64_json"].unpack1("m0"))
    end
  }
end

#parse_respond_response(body) ⇒ Hash

Parameters:

  • body (Hash)

    The response body from the LLM provider

Returns:

  • (Hash)


19
20
21
# File 'lib/llm/providers/openai/response_parser.rb', line 19

def parse_respond_response(body)
  RespondParser.new(body).format(self)
end