Module: GPT::ResponseExtender
- Defined in:
- lib/gpt/response_extender.rb
Instance Method Summary collapse
- #completion_tokens ⇒ Object
- #content ⇒ Object
- #content? ⇒ Boolean
- #created_at ⇒ Object
- #message ⇒ Object
- #model ⇒ Object
- #prompt_tokens ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
- #total_tokens ⇒ Object
- #usage ⇒ Object
Instance Method Details
#completion_tokens ⇒ Object
38 39 40 |
# File 'lib/gpt/response_extender.rb', line 38 def completion_tokens usage['completion_tokens'] || 0 end |
#content ⇒ Object
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 = 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
26 27 28 |
# File 'lib/gpt/response_extender.rb', line 26 def content? !content.nil? && !content.empty? end |
#created_at ⇒ Object
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 |
#message ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/gpt/response_extender.rb', line 3 def if self['choices'] dig('choices', 0, 'message') || {} else = if self['output'].is_a?(Array) self['output'].find { |i| i['type'] == 'message' } || self['output'].first end || {} end end |
#model ⇒ Object
46 47 48 |
# File 'lib/gpt/response_extender.rb', line 46 def model self['model'] end |
#prompt_tokens ⇒ Object
34 35 36 |
# File 'lib/gpt/response_extender.rb', line 34 def prompt_tokens usage['prompt_tokens'] || 0 end |
#to_h ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/gpt/response_extender.rb', line 58 def to_h { content: content, role: ['role'], model: model, usage: usage, created_at: created_at }.compact end |
#to_s ⇒ Object
68 69 70 |
# File 'lib/gpt/response_extender.rb', line 68 def to_s content || '[No content]' end |
#total_tokens ⇒ Object
42 43 44 |
# File 'lib/gpt/response_extender.rb', line 42 def total_tokens usage['total_tokens'] || 0 end |
#usage ⇒ Object
30 31 32 |
# File 'lib/gpt/response_extender.rb', line 30 def usage self['usage'] || {} end |