Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add context tokens from api response to cost calculation #78

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ketchupbot-discord/ApiResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ 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; }

[JsonPropertyName("response_tokens")] public string? ResponseTokens { get; init; }
Expand Down
8 changes: 6 additions & 2 deletions ketchupbot-discord/GalaxyGPT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,19 @@ 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))
answerMessage.AppendLine($"Question Tokens: {questionTokens}");
if (int.TryParse(apiResponse.ResponseTokens, out int responseTokens))
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(questionTokens * 0.00000015 + responseTokens * 0.0000006, 10)}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really see why we remove the dollar sign. Does this somehow get rid of the scientific notation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it round to like 6 places or something and i put it in terms of cents, not dollars so theres less 0s. also multiplied every constant there by 100 to keep the value the same

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh nice. can you get rid of the "cents" and use ¢

Copy link
Contributor Author

@CornHusker89 CornHusker89 Oct 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was considering that, but idk if c# likes the cent symbol in strings or if I have to do some u/ bs so I went safe lol if that breaks it thats your fault L

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nah you'll be fine. c# string interpolation works like python's rather than js's

$"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