Class: Langchain::LLM::OpenAIResponse

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

Instance Attribute Summary

Attributes inherited from BaseResponse

#context, #raw_response

Instance Method Summary collapse

Methods inherited from BaseResponse

#initialize

Constructor Details

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

Instance Method Details

#chat_completionObject



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

def chat_completion
  completion
end

#chat_completionsObject



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

def chat_completions
  raw_response.dig("choices")
end

#completionObject



15
16
17
# File 'lib/langchain/llm/response/openai_response.rb', line 15

def completion
  completions&.dig(0, "message", "content")
end

#completion_tokensObject



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

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

#completionsObject



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

def completions
  raw_response.dig("choices")
end

#created_atObject



9
10
11
12
13
# File 'lib/langchain/llm/response/openai_response.rb', line 9

def created_at
  if raw_response.dig("created")
    Time.at(raw_response.dig("created"))
  end
end

#embeddingObject



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

def embedding
  embeddings&.first
end

#embeddingsObject



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

def embeddings
  raw_response.dig("data")&.map { |datum| datum.dig("embedding") }
end

#modelObject



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

def model
  raw_response["model"]
end

#prompt_tokensObject



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

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

#roleObject



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

def role
  completions&.dig(0, "message", "role")
end

#tool_callsObject



27
28
29
30
31
32
33
# File 'lib/langchain/llm/response/openai_response.rb', line 27

def tool_calls
  if chat_completions.dig(0, "message").has_key?("tool_calls")
    chat_completions.dig(0, "message", "tool_calls")
  else
    []
  end
end

#total_tokensObject



59
60
61
# File 'lib/langchain/llm/response/openai_response.rb', line 59

def total_tokens
  raw_response.dig("usage", "total_tokens")
end