FEATURE: track duration of AI calls (#1082)

* FEATURE: track duration of AI calls

* annotate
This commit is contained in:
Sam 2025-01-23 11:32:12 +11:00 committed by GitHub
parent faa8e6e873
commit 8bf350206e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 12 additions and 0 deletions

View File

@ -46,6 +46,7 @@ end
# language_model :string(255)
# feature_context :jsonb
# cached_tokens :integer
# duration_msecs :integer
#
# Indexes
#

View File

@ -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

View File

@ -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?

View File

@ -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(