Class: Raif::ConversationEntry

Inherits:
ApplicationRecord show all
Includes:
Raif::Concerns::HasAvailableModelTools, Raif::Concerns::InvokesModelTools
Defined in:
app/models/raif/conversation_entry.rb

Instance Method Summary collapse

Methods included from Raif::Concerns::HasAvailableModelTools

#available_model_tools_map

Methods inherited from ApplicationRecord

table_name_prefix

Instance Method Details

#add_user_tool_invocation_to_user_messageObject



33
34
35
36
37
38
# File 'app/models/raif/conversation_entry.rb', line 33

def add_user_tool_invocation_to_user_message
  return unless raif_user_tool_invocation.present?

  separator = response_format == "html" ? "<br>" : "\n\n"
  self.user_message = [user_message, raif_user_tool_invocation.as_user_message].join(separator)
end

#create_entry_for_observation!Object



68
69
70
71
72
# File 'app/models/raif/conversation_entry.rb', line 68

def create_entry_for_observation!
  follow_up_entry = raif_conversation.entries.create!(creator: creator)
  Raif::ConversationEntryJob.perform_later(conversation_entry: follow_up_entry)
  follow_up_entry.broadcast_append_to raif_conversation, target: dom_id(raif_conversation, :entries)
end

#generating_response?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'app/models/raif/conversation_entry.rb', line 44

def generating_response?
  started? && !completed? && !failed?
end

#process_entry!Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/raif/conversation_entry.rb', line 48

def process_entry!
  self.raif_model_completion = raif_conversation.prompt_model_for_entry_response(entry: self)

  if raif_model_completion.parsed_response.present? || raif_model_completion.response_tool_calls.present?
    extract_message_and_invoke_tools!
    create_entry_for_observation! if triggers_observation_to_model?
  else
    logger.error "Error processing conversation entry ##{id}. No model response found."
    failed!
  end

  self
end

#response_formatObject



40
41
42
# File 'app/models/raif/conversation_entry.rb', line 40

def response_format
  raif_model_completion&.response_format.presence || raif_conversation.response_format
end

#triggers_observation_to_model?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
# File 'app/models/raif/conversation_entry.rb', line 62

def triggers_observation_to_model?
  return false unless completed?

  raif_model_tool_invocations.any?(&:triggers_observation_to_model?)
end