Skip to content

Commit

Permalink
message cost calculations and display (#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlvallelonga committed Sep 19, 2024
1 parent b59d1b6 commit 080245f
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 19 deletions.
4 changes: 4 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,8 @@ def viewport_tag(content)
def n_a_if_blank(value, n_a = "Not Available")
value.blank? ? n_a : value.to_s
end

def to_dollars(cents, precision: 2)
number_to_currency(cents / 100.0, precision: precision)
end
end
4 changes: 4 additions & 0 deletions app/models/conversation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def self.grouped_by_increasing_time_interval_for_user(user)
.delete_if { |_, v| v.empty? }
end

def total_cost
input_token_total_cost + output_token_total_cost
end

private

def set_title_async
Expand Down
10 changes: 10 additions & 0 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Message < ApplicationRecord
after_create :start_assistant_reply, if: :user?
after_create :set_last_assistant_message, if: :assistant?
after_save :update_assistant_on_conversation, if: -> { assistant.present? && conversation.present? }
before_save :update_input_token_cost, if: :input_token_count_changed?
before_save :update_output_token_cost, if: :output_token_count_changed?

scope :ordered, -> { latest_version_for_conversation }

Expand Down Expand Up @@ -77,4 +79,12 @@ def update_assistant_on_conversation
return if conversation.assistant == assistant
conversation.update!(assistant: assistant)
end

def update_input_token_cost
self.input_token_cost = assistant.language_model.input_token_cost_cents * input_token_count
end

def update_output_token_cost
self.output_token_cost = assistant.language_model.output_token_cost_cents * output_token_count
end
end
26 changes: 20 additions & 6 deletions app/views/conversations/_conversation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,30 @@
<footer class="py-3 px-4 mt-1 border-t-2 border-gray-100 dark:border-gray-600 gap-2 flex flex-col">
<table class="w-full text-left">
<tr>
<td class="pb-2 whitespace-nowrap">Conversation</td>
<td class="pb-2">$</td>
<td class="pb-2 whitespace-nowrap">Conversation ~</td>
<td class="pb-2"><%= to_dollars(conversation.total_cost)%></td>
</tr>
<tr>
<td class="pb-2">Input Tokens</td>
<td class="pb-2"><%= conversation.messages.sum(:input_token_count) %></td>
<td class="pb-2 font-bold" colspan="2">Input</td>
</tr>
<tr>
<td>Output Tokens</td>
<td><%= conversation.messages.sum(:output_token_count) %></td>
<td class="pb-2">Tokens</td>
<td class="pb-2"><%= conversation.input_token_total_count %></td>
</tr>
<tr>
<td class="pb-2">Cost</td>
<td class="pb-2"><%= to_dollars(conversation.input_token_total_cost, precision: 5) %></td>
</tr>
<tr>
<td class="pb-2 font-bold" colspan="2">Output</td>
</tr>
<tr>
<td>Tokens</td>
<td><%= conversation.output_token_total_count %></td>
</tr>
<tr>
<td>Cost</td>
<td><%= to_dollars(conversation.output_token_total_cost, precision: 5) %></td>
</tr>
</table>
</footer>
Expand Down
2 changes: 1 addition & 1 deletion app/views/settings/api_services/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
%>).</li>
<li>Click "Create API Key", name it something like "<%= Setting.product_name %>" and paste it below.</li>
</ol>
<%= form.text_field :token, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full dark:text-black" %>
<%= form.text_field :token, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full dark:text-black", autocomplete: "off" %>
</div>

<%= form.submit "Save",
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/filter_parameter_logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ def filter_regexp(filter_key, exemptions = [])
:certificate,
:otp,
:ssn,
filter_regexp(:token, [:token_count, :token_cost]),
filter_regexp(:token, [:token_count, :token_cost, :token_total_count, :token_total_cost]),
]
60 changes: 59 additions & 1 deletion test/fixtures/language_models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pacos:
api_name: pacos-imagine
supports_images: true
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

# Rob's Language Models
gpt_4_turbo:
Expand All @@ -26,6 +28,8 @@ gpt_4_turbo:
api_service: rob_openai_service
supports_images: true
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

gpt_4_turbo_2024_04_09:
position: 2
Expand All @@ -35,6 +39,8 @@ gpt_4_turbo_2024_04_09:
api_service: rob_openai_service
supports_images: true
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

gpt_4_turbo_preview:
position: 3
Expand All @@ -44,6 +50,8 @@ gpt_4_turbo_preview:
api_service: rob_openai_service
supports_images: false
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

gpt_4_0125_preview:
position: 4
Expand All @@ -53,6 +61,8 @@ gpt_4_0125_preview:
api_service: rob_openai_service
supports_images: false
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

gpt_3_5_turbo_0125:
position: 5
Expand All @@ -62,6 +72,8 @@ gpt_3_5_turbo_0125:
api_service: rob_openai_service
supports_images: false
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

gpt_3_5_turbo_1106:
position: 6
Expand All @@ -71,6 +83,8 @@ gpt_3_5_turbo_1106:
api_service: rob_openai_service
supports_images: false
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

gpt_3_5_turbo_instruct:
position: 7
Expand All @@ -80,6 +94,8 @@ gpt_3_5_turbo_instruct:
api_service: rob_openai_service
supports_images: false
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

claude_2_0:
position: 8
Expand All @@ -89,6 +105,8 @@ claude_2_0:
api_service: rob_anthropic_service
supports_images: false
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

claude_instant_1_2:
position: 9
Expand All @@ -98,6 +116,8 @@ claude_instant_1_2:
api_service: rob_anthropic_service
supports_images: false
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

rob_gpt:
position: 10
Expand All @@ -107,6 +127,8 @@ rob_gpt:
api_service: rob_openai_service
supports_images: true
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

# Keith's Language Models
gpt_best:
Expand All @@ -117,6 +139,8 @@ gpt_best:
api_service: keith_openai_service
supports_images: true
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

claude_best:
position: 2
Expand All @@ -126,6 +150,8 @@ claude_best:
api_name: claude-best
supports_images: true
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

gpt_4o:
position: 3
Expand All @@ -135,7 +161,9 @@ gpt_4o:
api_service: keith_openai_service
supports_images: true
supports_tools: true

input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

gpt_4o_2024_05_13:
position: 4
user: keith
Expand All @@ -144,6 +172,8 @@ gpt_4o_2024_05_13:
api_service: keith_openai_service
supports_images: true
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

gpt_4_1106_preview:
position: 9
Expand All @@ -153,6 +183,8 @@ gpt_4_1106_preview:
api_service: keith_openai_service
supports_images: false
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

gpt_4_vision_preview:
position: 10
Expand All @@ -162,6 +194,8 @@ gpt_4_vision_preview:
api_service: keith_openai_service
supports_images: true
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

gpt_4_1106_vision_preview:
position: 11
Expand All @@ -171,6 +205,8 @@ gpt_4_1106_vision_preview:
api_service: keith_openai_service
supports_images: true
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

gpt_4:
position: 12
Expand All @@ -180,6 +216,8 @@ gpt_4:
api_service: keith_openai_service
supports_images: false
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

gpt_4_0613:
position: 13
Expand All @@ -189,6 +227,8 @@ gpt_4_0613:
api_service: keith_openai_service
supports_images: false
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

gpt_3_5_turbo:
position: 14
Expand All @@ -198,6 +238,8 @@ gpt_3_5_turbo:
api_service: keith_openai_service
supports_images: false
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

gpt_3_5_turbo_16k_0613:
position: 15
Expand All @@ -207,6 +249,8 @@ gpt_3_5_turbo_16k_0613:
api_service: keith_openai_service
supports_images: false
supports_tools: false
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

claude_3_5_sonnet_20240620:
position: 16
Expand All @@ -216,6 +260,8 @@ claude_3_5_sonnet_20240620:
api_service: keith_anthropic_service
supports_images: true
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

claude_3_opus_20240229:
position: 17
Expand All @@ -225,6 +271,8 @@ claude_3_opus_20240229:
api_service: keith_anthropic_service
supports_images: true
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

claude_3_sonnet_20240229:
position: 18
Expand All @@ -234,6 +282,8 @@ claude_3_sonnet_20240229:
api_service: keith_anthropic_service
supports_images: true
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

claude_3_haiku_20240307:
position: 19
Expand All @@ -243,6 +293,8 @@ claude_3_haiku_20240307:
api_service: keith_anthropic_service
supports_images: true
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

claude_2_1:
position: 20
Expand All @@ -252,6 +304,8 @@ claude_2_1:
api_service: keith_anthropic_service
supports_images: false
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

camel:
position: 21
Expand All @@ -261,6 +315,8 @@ camel:
api_name: camel
supports_images: false
supports_tools: true
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001

guanaco:
position: 22
Expand All @@ -270,3 +326,5 @@ guanaco:
api_name: guanaco:large
supports_images: true
supports_tools: false
input_token_cost_cents: 0.0001
output_token_cost_cents: 0.0001
Loading

0 comments on commit 080245f

Please sign in to comment.