Class: Langchain::LLM::AnthropicResponse

Inherits:
BaseResponse show all
Defined in:
lib/langchain/llm/response/anthropic_response.rb

Instance Attribute Summary

Attributes inherited from BaseResponse

#context, #raw_response

Instance Method Summary collapse

Methods inherited from BaseResponse

#created_at, #embedding, #embeddings, #initialize

Constructor Details

This class inherits a constructor from Langchain::LLM::BaseResponse

Instance Method Details

#chat_completionObject



13
14
15
16
# File 'lib/langchain/llm/response/anthropic_response.rb', line 13

def chat_completion
  chat_completion = chat_completions.find { |h| h["type"] == "text" }
  chat_completion&.dig("text")
end

#chat_completionsObject



23
24
25
# File 'lib/langchain/llm/response/anthropic_response.rb', line 23

def chat_completions
  raw_response.dig("content")
end

#completionObject



9
10
11
# File 'lib/langchain/llm/response/anthropic_response.rb', line 9

def completion
  completions.first
end

#completion_tokensObject



47
48
49
# File 'lib/langchain/llm/response/anthropic_response.rb', line 47

def completion_tokens
  raw_response.dig("usage", "output_tokens").to_i
end

#completionsObject



27
28
29
# File 'lib/langchain/llm/response/anthropic_response.rb', line 27

def completions
  [raw_response.dig("completion")]
end

#log_idObject



39
40
41
# File 'lib/langchain/llm/response/anthropic_response.rb', line 39

def log_id
  raw_response.dig("log_id")
end

#modelObject



5
6
7
# File 'lib/langchain/llm/response/anthropic_response.rb', line 5

def model
  raw_response.dig("model")
end

#prompt_tokensObject



43
44
45
# File 'lib/langchain/llm/response/anthropic_response.rb', line 43

def prompt_tokens
  raw_response.dig("usage", "input_tokens").to_i
end

#roleObject



55
56
57
# File 'lib/langchain/llm/response/anthropic_response.rb', line 55

def role
  raw_response.dig("role")
end

#stopObject



35
36
37
# File 'lib/langchain/llm/response/anthropic_response.rb', line 35

def stop
  raw_response.dig("stop")
end

#stop_reasonObject



31
32
33
# File 'lib/langchain/llm/response/anthropic_response.rb', line 31

def stop_reason
  raw_response.dig("stop_reason")
end

#tool_callsObject



18
19
20
21
# File 'lib/langchain/llm/response/anthropic_response.rb', line 18

def tool_calls
  tool_call = chat_completions.find { |h| h["type"] == "tool_use" }
  tool_call ? [tool_call] : []
end

#total_tokensObject



51
52
53
# File 'lib/langchain/llm/response/anthropic_response.rb', line 51

def total_tokens
  prompt_tokens + completion_tokens
end