Module: LLM::Anthropic::ResponseParser

Defined in:
lib/llm/providers/anthropic/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)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/llm/providers/anthropic/response_parser.rb', line 17

def parse_completion(body)
  {
    model: body["model"],
    choices: body["content"].map do
      # TODO: don't hardcode role
      LLM::Message.new("assistant", _1["text"], {completion: self})
    end,
    prompt_tokens: body.dig("usage", "input_tokens"),
    completion_tokens: body.dig("usage", "output_tokens")
  }
end

#parse_embedding(body) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/llm/providers/anthropic/response_parser.rb', line 5

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