Module: GPT::ResponseExtender

Defined in:
lib/gpt/response_extender.rb

Instance Method Summary collapse

Instance Method Details

#completion_tokensObject



38
39
40
# File 'lib/gpt/response_extender.rb', line 38

def completion_tokens
  usage['completion_tokens'] || 0
end

#contentObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gpt/response_extender.rb', line 14

def content
  if self['choices']
    dig('choices', 0, 'message', 'content')
  else
    msg = message
    contents = msg && msg['content']
    return nil unless contents.is_a?(Array)
    text_item = contents.find { |c| c['type'] == 'output_text' || c['type'] == 'text' }
    text_item && text_item['text']
  end
end

#content?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/gpt/response_extender.rb', line 26

def content?
  !content.nil? && !content.empty?
end

#created_atObject



50
51
52
53
54
55
56
# File 'lib/gpt/response_extender.rb', line 50

def created_at
  if self['created']
    Time.at(self['created'])
  elsif self['created_at']
    Time.at(self['created_at'])
  end
end

#messageObject



3
4
5
6
7
8
9
10
11
12
# File 'lib/gpt/response_extender.rb', line 3

def message
  if self['choices']
    dig('choices', 0, 'message') || {}
  else
    output_message = if self['output'].is_a?(Array)
      self['output'].find { |i| i['type'] == 'message' } || self['output'].first
    end
    output_message || {}
  end
end

#modelObject



46
47
48
# File 'lib/gpt/response_extender.rb', line 46

def model
  self['model']
end

#prompt_tokensObject



34
35
36
# File 'lib/gpt/response_extender.rb', line 34

def prompt_tokens
  usage['prompt_tokens'] || 0
end

#to_hObject



58
59
60
61
62
63
64
65
66
# File 'lib/gpt/response_extender.rb', line 58

def to_h
  {
    content: content,
    role: message['role'],
    model: model,
    usage: usage,
    created_at: created_at
  }.compact
end

#to_sObject



68
69
70
# File 'lib/gpt/response_extender.rb', line 68

def to_s
  content || '[No content]'
end

#total_tokensObject



42
43
44
# File 'lib/gpt/response_extender.rb', line 42

def total_tokens
  usage['total_tokens'] || 0
end

#usageObject



30
31
32
# File 'lib/gpt/response_extender.rb', line 30

def usage
  self['usage'] || {}
end