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)


14
15
16
# File 'lib/llm/providers/openai/response_parser.rb', line 14

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)


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

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)


55
56
57
58
59
60
61
62
63
# File 'lib/llm/providers/openai/response_parser.rb', line 55

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_moderation_list(body) ⇒ Hash

Parameters:

  • body (Hash)

    The response body from the LLM provider

Returns:

  • (Hash)


30
31
32
33
34
35
36
# File 'lib/llm/providers/openai/response_parser.rb', line 30

def parse_moderation_list(body)
  {
    id: body["id"],
    model: body["model"],
    moderations: body["results"].map { LLM::Response::ModerationList::Moderation.new(_1) }
  }
end

#parse_respond_response(body) ⇒ Hash

Parameters:

  • body (Hash)

    The response body from the LLM provider

Returns:

  • (Hash)


22
23
24
# File 'lib/llm/providers/openai/response_parser.rb', line 22

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