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)


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

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) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/llm/providers/openai/response_parser.rb', line 5

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