generated from nhcarrigan/template
feat: log token usage
This commit is contained in:
parent
a347ff8eb6
commit
8c72375761
@ -41,6 +41,14 @@ client.on(Events.InteractionCreate, (interaction) => {
|
||||
}
|
||||
});
|
||||
|
||||
client.on(Events.EntitlementCreate, (entitlement) => {
|
||||
void logger.log("info", `User ${entitlement.userId} has subscribed!`);
|
||||
});
|
||||
|
||||
client.on(Events.EntitlementDelete, (entitlement) => {
|
||||
void logger.log("info", `User ${entitlement.userId} has unsubscribed... :c`);
|
||||
});
|
||||
|
||||
client.on(Events.ClientReady, () => {
|
||||
void logger.log("debug", "Bot is ready.");
|
||||
});
|
||||
|
@ -6,6 +6,7 @@
|
||||
import { MessageFlags, type ChatInputCommandInteraction } from "discord.js";
|
||||
import { personality } from "../config/personality.js";
|
||||
import { ai } from "../utils/ai.js";
|
||||
import { calculateCost } from "../utils/calculateCost.js";
|
||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
import type { ImageBlockParam } from "@anthropic-ai/sdk/resources/index.js";
|
||||
|
||||
@ -109,4 +110,7 @@ export const alt = async(
|
||||
response?.text
|
||||
?? "I'm sorry, I don't have an answer for that. Please try again later.",
|
||||
);
|
||||
|
||||
const { usage } = messages;
|
||||
await calculateCost(usage, interaction.user.username, "alt-text");
|
||||
};
|
||||
|
@ -6,6 +6,7 @@
|
||||
import { MessageFlags, type ChatInputCommandInteraction } from "discord.js";
|
||||
import { personality } from "../config/personality.js";
|
||||
import { ai } from "../utils/ai.js";
|
||||
import { calculateCost } from "../utils/calculateCost.js";
|
||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
|
||||
/**
|
||||
@ -40,4 +41,7 @@ export const evaluate = async(
|
||||
response?.text
|
||||
?? "I'm sorry, I don't have an answer for that. Please try again later.",
|
||||
);
|
||||
|
||||
const { usage } = messages;
|
||||
await calculateCost(usage, interaction.user.username, "evaluate");
|
||||
};
|
||||
|
@ -6,6 +6,7 @@
|
||||
import { MessageFlags, type ChatInputCommandInteraction } from "discord.js";
|
||||
import { personality } from "../config/personality.js";
|
||||
import { ai } from "../utils/ai.js";
|
||||
import { calculateCost } from "../utils/calculateCost.js";
|
||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
|
||||
/**
|
||||
@ -41,4 +42,7 @@ export const mood = async(
|
||||
response?.text
|
||||
?? "I'm sorry, I don't have an answer for that. Please try again later.",
|
||||
);
|
||||
|
||||
const { usage } = messages;
|
||||
await calculateCost(usage, interaction.user.username, "mood");
|
||||
};
|
||||
|
@ -6,6 +6,7 @@
|
||||
import { MessageFlags, type ChatInputCommandInteraction } from "discord.js";
|
||||
import { personality } from "../config/personality.js";
|
||||
import { ai } from "../utils/ai.js";
|
||||
import { calculateCost } from "../utils/calculateCost.js";
|
||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
|
||||
/**
|
||||
@ -41,4 +42,7 @@ export const proofread = async(
|
||||
response?.text
|
||||
?? "I'm sorry, I don't have an answer for that. Please try again later.",
|
||||
);
|
||||
|
||||
const { usage } = messages;
|
||||
await calculateCost(usage, interaction.user.username, "proofread");
|
||||
};
|
||||
|
@ -6,6 +6,7 @@
|
||||
import { MessageFlags, type ChatInputCommandInteraction } from "discord.js";
|
||||
import { personality } from "../config/personality.js";
|
||||
import { ai } from "../utils/ai.js";
|
||||
import { calculateCost } from "../utils/calculateCost.js";
|
||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
|
||||
/**
|
||||
@ -41,4 +42,7 @@ export const query = async(
|
||||
response?.text
|
||||
?? "I'm sorry, I don't have an answer for that. Please try again later.",
|
||||
);
|
||||
|
||||
const { usage } = messages;
|
||||
await calculateCost(usage, interaction.user.username, "query");
|
||||
};
|
||||
|
@ -6,6 +6,7 @@
|
||||
import { MessageFlags, type ChatInputCommandInteraction } from "discord.js";
|
||||
import { personality } from "../config/personality.js";
|
||||
import { ai } from "../utils/ai.js";
|
||||
import { calculateCost } from "../utils/calculateCost.js";
|
||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
|
||||
/**
|
||||
@ -41,4 +42,7 @@ export const summarise = async(
|
||||
response?.text
|
||||
?? "I'm sorry, I don't have an answer for that. Please try again later.",
|
||||
);
|
||||
|
||||
const { usage } = messages;
|
||||
await calculateCost(usage, interaction.user.username, "summarise");
|
||||
};
|
||||
|
30
src/utils/calculateCost.ts
Normal file
30
src/utils/calculateCost.ts
Normal file
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
import { logger } from "./logger.js";
|
||||
import type { Usage } from "@anthropic-ai/sdk/resources/index.js";
|
||||
|
||||
/**
|
||||
* Calculates the cost of a command run by a user, and sends to
|
||||
* our logging service.
|
||||
* @param usage -- The usage payload from Anthropic.
|
||||
* @param uuid -- The Discord ID of the user who ran the command.
|
||||
* @param command -- The command that was run.
|
||||
*/
|
||||
export const calculateCost = async(
|
||||
usage: Usage,
|
||||
uuid: string,
|
||||
command: string,
|
||||
): Promise<void> => {
|
||||
const inputCost = usage.input_tokens * (3 / 1_000_000);
|
||||
const outputCost = usage.output_tokens * (15 / 1_000_000);
|
||||
const totalCost = inputCost + outputCost;
|
||||
await logger.log(
|
||||
"info",
|
||||
`User ${uuid} ran \`${command}\` which accepted ${usage.input_tokens.toString()} and generated ${usage.output_tokens.toString()}.
|
||||
|
||||
Total cost: ${totalCost.toLocaleString("en-GB", { currency: "USD", style: "currency" })}`,
|
||||
);
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user