mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-06-28 18:42:16 +00:00
FEATURE: track duration of AI calls (#1082)
* FEATURE: track duration of AI calls * annotate
This commit is contained in:
parent
faa8e6e873
commit
8bf350206e
@ -46,6 +46,7 @@ end
|
|||||||
# language_model :string(255)
|
# language_model :string(255)
|
||||||
# feature_context :jsonb
|
# feature_context :jsonb
|
||||||
# cached_tokens :integer
|
# cached_tokens :integer
|
||||||
|
# duration_msecs :integer
|
||||||
#
|
#
|
||||||
# Indexes
|
# Indexes
|
||||||
#
|
#
|
||||||
|
6
db/migrate/20250122003035_add_duration_to_ai_api_log.rb
Normal file
6
db/migrate/20250122003035_add_duration_to_ai_api_log.rb
Normal 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
|
@ -66,6 +66,7 @@ module DiscourseAi
|
|||||||
&blk
|
&blk
|
||||||
)
|
)
|
||||||
LlmQuota.check_quotas!(@llm_model, user)
|
LlmQuota.check_quotas!(@llm_model, user)
|
||||||
|
start_time = Time.now
|
||||||
|
|
||||||
@partial_tool_calls = partial_tool_calls
|
@partial_tool_calls = partial_tool_calls
|
||||||
model_params = normalize_model_params(model_params)
|
model_params = normalize_model_params(model_params)
|
||||||
@ -212,6 +213,9 @@ module DiscourseAi
|
|||||||
log.raw_response_payload = response_raw
|
log.raw_response_payload = response_raw
|
||||||
final_log_update(log)
|
final_log_update(log)
|
||||||
log.response_tokens = tokenizer.size(partials_raw) if log.response_tokens.blank?
|
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!
|
log.save!
|
||||||
LlmQuota.log_usage(@llm_model, user, log.request_tokens, log.response_tokens)
|
LlmQuota.log_usage(@llm_model, user, log.request_tokens, log.response_tokens)
|
||||||
if Rails.env.development?
|
if Rails.env.development?
|
||||||
|
@ -319,6 +319,7 @@ RSpec.describe DiscourseAi::Completions::Endpoints::OpenAi do
|
|||||||
log = AiApiAuditLog.order(:id).last
|
log = AiApiAuditLog.order(:id).last
|
||||||
expect(log.request_tokens).to eq(55)
|
expect(log.request_tokens).to eq(55)
|
||||||
expect(log.response_tokens).to eq(13)
|
expect(log.response_tokens).to eq(13)
|
||||||
|
expect(log.duration_msecs).not_to be_nil
|
||||||
|
|
||||||
expected =
|
expected =
|
||||||
DiscourseAi::Completions::ToolCall.new(
|
DiscourseAi::Completions::ToolCall.new(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user