Class: Raif::ConversationEntry

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

Overview

Schema Information

Table name: raif_conversation_entries

id                     :bigint           not null, primary key
completed_at           :datetime
creator_type           :string           not null
failed_at              :datetime
model_response_message :text
raw_response           :text
started_at             :datetime
user_message           :text
created_at             :datetime         not null
updated_at             :datetime         not null
creator_id             :bigint           not null
raif_conversation_id   :bigint           not null

Indexes

index_raif_conversation_entries_on_created_at            (created_at)
index_raif_conversation_entries_on_creator               (creator_type,creator_id)
index_raif_conversation_entries_on_raif_conversation_id  (raif_conversation_id)

Foreign Keys

fk_rails_...  (raif_conversation_id => raif_conversations.id)

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



59
60
61
62
63
64
# File 'app/models/raif/conversation_entry.rb', line 59

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



110
111
112
113
114
# File 'app/models/raif/conversation_entry.rb', line 110

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: ActionView::RecordIdentifier.dom_id(raif_conversation, :entries)
end

#generating_response?Boolean

Returns:

  • (Boolean)


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

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

#process_entry!Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/raif/conversation_entry.rb', line 74

def process_entry!
  self.model_response_message = ""

  self.raif_model_completion = raif_conversation.prompt_model_for_entry_response(entry: self) do |model_completion, _delta, _sse_event|
    self.raw_response = model_completion.raw_response
    self.model_response_message = raif_conversation.process_model_response_message(
      message: model_completion.parsed_response(force_reparse: true),
      entry: self
    )

    update_columns(
      model_response_message: model_response_message,
      raw_response: raw_response,
      updated_at: Time.current
    )

    broadcast_replace_to raif_conversation
  end

  if raif_model_completion.present? && (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



66
67
68
# File 'app/models/raif/conversation_entry.rb', line 66

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

#triggers_observation_to_model?Boolean

Returns:

  • (Boolean)


104
105
106
107
108
# File 'app/models/raif/conversation_entry.rb', line 104

def triggers_observation_to_model?
  return false unless completed?

  raif_model_tool_invocations.any?(&:triggers_observation_to_model?)
end