Module: LLM::OpenAI::ResponseParser

Defined in:
lib/llm/providers/openai/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/openai/response_parser.rb', line 22

def parse_completion(body)
  {
    model: body["model"],
    choices: body["choices"].map do
      mesg = _1["message"]
      logprobs = _1["logprobs"]
      role, content = mesg.values_at("role", "content")
      LLM::Message.new(role, content, {completion: self, logprobs:})
    end,
    prompt_tokens: body.dig("usage", "prompt_tokens"),
    completion_tokens: body.dig("usage", "completion_tokens"),
    total_tokens: body.dig("usage", "total_tokens")
  }
end

#parse_embedding(body) ⇒ Hash

Parameters:

  • body (Hash)

    The response body from the LLM provider

Returns:

  • (Hash)


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

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