Skip to content

Commit

Permalink
added prompt tokens to cost calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
CornHusker89 committed Oct 1, 2024
1 parent a84fd6c commit 3fecd05
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ketchupbot-discord/ApiResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class ApiResponse
public required string Answer { get; init; }
public string? Context { get; init; }

[JsonPropertyName("prompt_tokens")] public string? PromptTokens { get; init; }

[JsonPropertyName("context_tokens")] public string? ContextTokens { get; init; }

[JsonPropertyName("question_tokens")] public string? QuestionTokens { get; init; }
Expand Down
6 changes: 4 additions & 2 deletions ketchupbot-discord/GalaxyGPT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ await message.ReplyAsync("""

if (verbose)
{
if (int.TryParse(apiResponse.PromptTokens, out int promptTokens))
answerMessage.AppendLine($"Prompt Tokens: {promptTokens}");
if (int.TryParse(apiResponse.ContextTokens, out int contextTokens))
answerMessage.AppendLine($"Context Tokens: {contextTokens}");
if (int.TryParse(apiResponse.QuestionTokens, out int questionTokens))
Expand All @@ -153,9 +155,9 @@ await message.ReplyAsync("""
answerMessage.AppendLine($"Response Tokens: {responseTokens}");

// NOTE: These numbers are hardcoded and not necessarily representative of the actual costs, as the model can change
if (questionTokens != 0 && responseTokens != 0)
if (promptTokens != 0 && contextTokens != 0 && questionTokens != 0 && responseTokens != 0)
answerMessage.AppendLine(
$"Cost: {Math.Round(contextTokens * 0.000015 + questionTokens * 0.000015 + responseTokens * 0.00006, 7)} Cents");
$"Cost: {Math.Round(promptTokens * 0.000015 + contextTokens * 0.000015 + questionTokens * 0.000015 + responseTokens * 0.00006, 7)} Cents");

if (apiResponse.Duration != null)
answerMessage.AppendLine(
Expand Down

0 comments on commit 3fecd05

Please sign in to comment.