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, where_json_not_blank

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



114
115
116
117
118
# File 'app/models/raif/conversation_entry.rb', line 114

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
103
104
105
106
# File 'app/models/raif/conversation_entry.rb', line 74

def process_entry!
  self.model_response_message = ""

  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

  # Failed prompt attempts can still persist a model completion for debugging.
  # Avoid clearing the has_one association with nil, which would delete that row.
  self.raif_model_completion = model_completion if model_completion.present?

  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)


108
109
110
111
112
# File 'app/models/raif/conversation_entry.rb', line 108

def triggers_observation_to_model?
  return false unless completed?

  raif_model_tool_invocations.any?(&:triggers_observation_to_model?)
end