Class: OpenAI::ChatThread

Inherits:
Object
  • Object
show all
Defined in:
lib/open_ai/chat_thread.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(defaults = [], model) ⇒ ChatThread



5
6
7
8
9
# File 'lib/open_ai/chat_thread.rb', line 5

def initialize(defaults = [], model)
  @history ||= defaults
  @model = model.is_a?(Model) ? model : Model.new(model)
  puts @history
end

Instance Attribute Details

#historyObject (readonly) Also known as: messages

Returns the value of attribute history.



11
12
13
# File 'lib/open_ai/chat_thread.rb', line 11

def history
  @history
end

#modelObject

Returns the value of attribute model.



12
13
14
# File 'lib/open_ai/chat_thread.rb', line 12

def model
  @model
end

Instance Method Details

#add(message) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/open_ai/chat_thread.rb', line 27

def add(message)
  return false unless message&.valid?
  return false if @history.any? { message.id == _1.id}

  @history << message
  puts message

  true
end

#as_jsonObject



37
38
39
# File 'lib/open_ai/chat_thread.rb', line 37

def as_json
  @history.map(&:as_json)
end

#delete(id) ⇒ Object



16
17
18
19
20
21
# File 'lib/open_ai/chat_thread.rb', line 16

def delete(id)
  return false unless id

  @history.delete_if { _1.id == id }
  true
end

#for_logsObject



41
42
43
# File 'lib/open_ai/chat_thread.rb', line 41

def for_logs
  @history.map(&:for_logs)
end

#total_costObject



23
24
25
# File 'lib/open_ai/chat_thread.rb', line 23

def total_cost
  @history.map(&:cost).compact.sum
end