1import { openAiPricing } from "./openAiPricing";
2
3export const getAudioTotals = (data) => {
4 const totals = {};
5 const { whisperPricing } = openAiPricing;
6 for (const obj of data) {
7 // Extract the snapshot_id and token totals from the current object
1import { openAiPricing } from "./openAiPricing";
2import { email } from "https://esm.town/v/std/email?v=9";
3
10 };
11 } = {};
12 const { textPricing } = openAiPricing;
13 for (const obj of data) {
14 const { snapshot_id, n_context_tokens_total, n_generated_tokens_total }: {
1import { openAiPricing } from "./openAiPricing";
2
3export const getImageTotals = (data) => {
4 const totals = {};
5 const { dallePricing } = openAiPricing;
6 for (const obj of data) {
7 // Extract the snapshot_id and token totals from the current object
1import { createDayTotal } from "./createDayTotal";
2import { fetchOpenAiUsageData } from "./fetchOpenAiUsageData";
3import { updateBlobUsageDB } from "./updateBlobUsageDB";
4import { fetch } from "https://esm.town/v/std/fetch";
6import { DateTime } from "npm:luxon";
7
8const fetchAndStoreOpenAiUsage = async () => {
9 const timeZone = "America/Chicago";
10 const date = DateTime.now();
14
15 try {
16 const { data, whisper_api_data, dalle_api_data } = await fetchOpenAiUsageData("2024-04-01");
17 console.log(data.length);
18 console.log(whisper_api_data.length);
28};
29
30await fetchAndStoreOpenAiUsage();
45} & Partial<Record<Month, Day>>;
46
47export let openAiUsageTypes;
1import { fetch } from "https://esm.town/v/std/fetch?v=4";
2
3const fetchOpenAiUsageData = async (today: string) => {
4 const response = await fetch(
5 `https://api.openai.com/v1/usage?date=${today}`,
6 {
7 method: "GET",
8 headers: {
9 Authorization: `Bearer ${Deno.env.get("OPENAI_API_KEY")}`,
10 },
11 },
13
14 if (!response.ok) {
15 throw new Error("Failed to fetch openAI usage data.");
16 }
17
19};
20
21export { fetchOpenAiUsageData };
2import { getImageTotals } from "./getImageTotals";
3import { getTextTotals } from "./getTextTotals";
4import { DayTotal, UsageDB } from "./openAiUsageTypes";
5
6const createDayTotal = async (data, whisper_api_data, dalle_api_data) => {
10});
11
12interface OpenAiPricing {
13 textPricing: {
14 [key: string]: {
29}
30
31export const openAiPricing: OpenAiPricing = {
32 textPricing: {
33 "gpt-3.5-turbo-16k-0613": createPricing(3000, 4000),
1import { createDayTotal } from "./createDayTotal";
2import { cronEvalLogger as logger } from "https://esm.town/v/nbbaier/cronLogger";
3import { fetchOpenAiUsageData } from "./fetchOpenAiUsageData";
4import { updateBlobUsageDB } from "./updateBlobUsageDB";
5import { blob } from "https://esm.town/v/std/blob?v=11";
7import { DateTime } from "npm:luxon";
8
9const fetchAndStoreOpenAiUsage = async (interval: Interval) => {
10 const timeZone = "America/Chicago";
11 const date = DateTime.now();
15
16 try {
17 const { data, whisper_api_data, dalle_api_data } = await fetchOpenAiUsageData(today);
18
19 const day_total = await createDayTotal(data, whisper_api_data, dalle_api_data);
27};
28
29export default logger(fetchAndStoreOpenAiUsage);
100Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.
101
102### OpenAI
103```ts
104import { OpenAI } from "https://esm.town/v/std/openai";
105const openai = new OpenAI();
106const completion = await openai.chat.completions.create({
107 messages: [
108 { role: "user", content: "Say hello in a creative way" },