Class: Roseflow::Chat::Dialogue

Inherits:
Object
  • Object
show all
Defined in:
lib/roseflow/chat/dialogue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(personality: nil) ⇒ Dialogue

Returns a new instance of Dialogue.



13
14
15
16
17
# File 'lib/roseflow/chat/dialogue.rb', line 13

def initialize(personality: nil)
  @messages = []
  @exchanges = []
  initialize_with_personality(personality) if personality
end

Instance Attribute Details

#exchangesObject (readonly)

Returns the value of attribute exchanges.



10
11
12
# File 'lib/roseflow/chat/dialogue.rb', line 10

def exchanges
  @exchanges
end

#messagesObject (readonly)

Returns the value of attribute messages.



9
10
11
# File 'lib/roseflow/chat/dialogue.rb', line 9

def messages
  @messages
end

#personalityObject (readonly)

Returns the value of attribute personality.



11
12
13
# File 'lib/roseflow/chat/dialogue.rb', line 11

def personality
  @personality
end

Instance Method Details

#modelObject



19
20
21
# File 'lib/roseflow/chat/dialogue.rb', line 19

def model
  @model ||= provider.models.chattable.first
end

#providerObject



23
24
25
# File 'lib/roseflow/chat/dialogue.rb', line 23

def provider
  @provider ||= OpenAI::Provider.new(Roseflow::OpenAI::Config.new)
end

#recall(messages) ⇒ Object



27
28
29
30
# File 'lib/roseflow/chat/dialogue.rb', line 27

def recall(messages)
  @messages = messages
  parse_exchanges
end

#say(input, **options) ⇒ Object

This method takes a string input as input, which is a message to send to the LLM model. If the model is chattable, it sends the messages to the model and returns a response.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/roseflow/chat/dialogue.rb', line 34

def say(input, **options)
  if model.chattable?
    message = create_user_message(input)
    @messages.push(message)

    model_response = provider.chat(model: model, messages: @messages, **options)

    if model_response && model_response.success?
      exchange = create_exchange(message, model_response.choices.first.message)
      @exchanges.push(exchange)
      @messages.push(exchange.response)
      exchange.response
    else
      raise "Did not receive a response from the model"
    end
  else
    raise "Model is not chattable"
  end
end