diff --git a/app/models/ai_api_audit_log.rb b/app/models/ai_api_audit_log.rb index 075f1444..69f72267 100644 --- a/app/models/ai_api_audit_log.rb +++ b/app/models/ai_api_audit_log.rb @@ -46,6 +46,7 @@ end # language_model :string(255) # feature_context :jsonb # cached_tokens :integer +# duration_msecs :integer # # Indexes # diff --git a/db/migrate/20250122003035_add_duration_to_ai_api_log.rb b/db/migrate/20250122003035_add_duration_to_ai_api_log.rb new file mode 100644 index 00000000..a8e99b4a --- /dev/null +++ b/db/migrate/20250122003035_add_duration_to_ai_api_log.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true +class AddDurationToAiApiLog < ActiveRecord::Migration[7.2] + def change + add_column :ai_api_audit_logs, :duration_msecs, :integer + end +end diff --git a/lib/completions/endpoints/base.rb b/lib/completions/endpoints/base.rb index faf9820a..439b54f5 100644 --- a/lib/completions/endpoints/base.rb +++ b/lib/completions/endpoints/base.rb @@ -66,6 +66,7 @@ module DiscourseAi &blk ) LlmQuota.check_quotas!(@llm_model, user) + start_time = Time.now @partial_tool_calls = partial_tool_calls model_params = normalize_model_params(model_params) @@ -212,6 +213,9 @@ module DiscourseAi log.raw_response_payload = response_raw final_log_update(log) log.response_tokens = tokenizer.size(partials_raw) if log.response_tokens.blank? + log.created_at = start_time + log.updated_at = Time.now + log.duration_msecs = (Time.now - start_time) * 1000 log.save! LlmQuota.log_usage(@llm_model, user, log.request_tokens, log.response_tokens) if Rails.env.development? diff --git a/spec/lib/completions/endpoints/open_ai_spec.rb b/spec/lib/completions/endpoints/open_ai_spec.rb index f8e5d303..a58061cd 100644 --- a/spec/lib/completions/endpoints/open_ai_spec.rb +++ b/spec/lib/completions/endpoints/open_ai_spec.rb @@ -319,6 +319,7 @@ RSpec.describe DiscourseAi::Completions::Endpoints::OpenAi do log = AiApiAuditLog.order(:id).last expect(log.request_tokens).to eq(55) expect(log.response_tokens).to eq(13) + expect(log.duration_msecs).not_to be_nil expected = DiscourseAi::Completions::ToolCall.new(