Class: LlmMetadataFetcherJob

Inherits:
ApplicationJob show all
Defined in:
app/jobs/llm_metadata_fetcher_job.rb

Constant Summary collapse

MAX_ATTEMPTS =

Number of retry attempts

5
INITIAL_WAIT_TIME =

Initial wait time in seconds

1

Instance Method Summary collapse

Instance Method Details

#perform(chat_message_id:, completion_id:, attempt: 1) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/jobs/llm_metadata_fetcher_job.rb', line 9

def perform(chat_message_id:, completion_id:, attempt: 1)
  chat_message = ChatMessage.find(chat_message_id)

  begin
    response = PythonWebClient.get(
      path: '/tools/llm/metadata',
      params: { completion_id: completion_id }
    )

    if response.present? && response['data'].present?
      data = response['data']

      # Update the message with metadata
      chat_message.update!(
        status: 'updated',
        completion_cost: data['total_cost'],
        completion_metadata: JSON.dump(response),
        completion_time: data['generation_time'],
        completion_latency: data['latency'],
        speed: calculate_speed(data['tokens_completion'], data['generation_time'], data['latency']),
        tokens_completion: data['tokens_completion'].to_i,
        tokens_prompt: data['tokens_prompt'].to_i
      )

      # Broadcast that metadata has been updated
      chat_message.broadcast_message('metadata_updated')

      Rails.logger.info "Successfully fetched metadata for completion_id: #{completion_id} on attempt #{attempt}"
    else
      handle_retry(chat_message_id, completion_id, attempt)
    end
  rescue StandardError => e
    Rails.logger.error "Error fetching metadata from endpoint (attempt #{attempt}): #{e.message}"
    Sentry.capture_exception(e, extra: { completion_id: completion_id, attempt: attempt })

    handle_retry(chat_message_id, completion_id, attempt)
  end
end