1import { type WriterOptions } from "./WriterOptions";
2import { fetch } from "https://esm.town/v/std/fetch?v=4";
3import OpenAI, { type ClientOptions } from "npm:openai";
45export class ReadmeWriter {
6model: string;
7openai: OpenAI;
8apiKey: string;
9valtownKey: string;
1011constructor(options: WriterOptions) {
12const { model, ...openaiOptions } = options;
13this.model = model ? model : "gpt-3.5-turbo";
14this.openai = new OpenAI(openaiOptions);
15this.valtownKey = Deno.env.get("valtown");
16}
47}
4849private async performOpenAICall(prompt: string) {
50try {
51const response = await this.openai.chat.completions.create({
52messages: [{ role: "system", content: prompt }],
53model: this.model,
5556if (!response.choices || response.choices.length === 0) {
57throw new Error("No response from OpenAI");
58}
596162if (!readme) {
63throw new Error("No readme returned by OpenAI. Try again.");
64}
6592const { id, code } = await this.getVal(username, valName);
93const prompt = this.createPrompt(code, userPrompt);
94const readme = await this.performOpenAICall(prompt);
95return { id, readme };
96}
gptToolssqliteWriter11 matches
1import { type WriterOptions } from "./WriterOptions";
2import { sqlite } from "https://esm.town/v/std/sqlite";
3import OpenAI from "npm:openai";
45interface QueryWriterOptons extends WriterOptions {
11model: string;
12apiKey: string;
13openai: OpenAI;
1415constructor(options: QueryWriterOptons) {
16const { table, model, ...openaiOptions } = options;
17this.table = table;
18this.model = model;
19// this.apiKey = openaiOptions.apiKey ? openaiOptions.apiKey : Deno.env.get("OPENAI_API_KEY");
20this.openai = new OpenAI(openaiOptions);
21}
225455try {
56const response = await this.openai.chat.completions.create({
57messages: [{ role: "system", content: prompt }],
58model: this.model,
6061if (!response.choices || response.choices.length === 0) {
62throw new Error("No response from OpenAI");
63}
646667if (!query) {
68throw new Error("No SQL returned from OpenAI. Try again.");
69}
708081try {
82const response = await this.openai.chat.completions.create({
83messages: [{ role: "system", content: prompt }],
84model: this.model,
8687if (!response.choices || response.choices.length === 0) {
88throw new Error("No response from OpenAI");
89}
909293if (!query) {
94throw new Error("No SQL returned from OpenAI. Try again.");
95}
96
gptToolsWriterOptions1 match
1import { type ClientOptions } from "npm:openai";
23export interface WriterOptions extends ClientOptions {
gptToolsdraftReadme10 matches
1import { fetch } from "https://esm.town/v/std/fetch?v=4";
2import OpenAI, { type ClientOptions } from "npm:openai";
34export interface WriterOptions extends ClientOptions {
40}
4142async function performOpenAICall(prompt: string, model: string, openaiOptions: ClientOptions) {
43const openai = new OpenAI(openaiOptions);
4445try {
46const response = await openai.chat.completions.create({
47messages: [{ role: "system", content: prompt }],
48model: model,
5051if (!response.choices || response.choices.length === 0) {
52throw new Error("No response from OpenAI");
53}
545657if (!readme) {
58throw new Error("No readme returned by OpenAI. Try again.");
59}
608384async function draftReadme(options: WriterOptions) {
85const { username, valName, model = "gpt-3.5-turbo", userPrompt, ...openaiOptions } = options;
86const { id, code } = await getVal(username, valName);
87const prompt = createPrompt(code, userPrompt);
88const readme = await performOpenAICall(prompt, model, openaiOptions);
89return readme;
90}
9192async function writeReadme(options: WriterOptions) {
93const { username, valName, model = "gpt-3.5-turbo", userPrompt, ...openaiOptions } = options;
94const { id, code } = await getVal(username, valName);
95const prompt = createPrompt(code, userPrompt);
96const readme = await performOpenAICall(prompt, model, openaiOptions);
97try {
98const update = await updateReadme(id, readme);
fetchAndStoreOpenAiUsage2main.tsx4 matches
1import { createDayTotal } from "https://esm.town/v/nbbaier/createDayTotal";
2import { cronEvalLogger as logger } from "https://esm.town/v/nbbaier/cronLogger";
3import { fetchOpenAiUsageData } from "https://esm.town/v/nbbaier/fetchOpenAiUsageData";
4import { updateBlobUsageDB } from "https://esm.town/v/nbbaier/updateBlobUsageDB";
5import { blob } from "https://esm.town/v/std/blob?v=11";
7import { DateTime } from "npm:luxon";
89const fetchAndStoreOpenAiUsage = async (interval: Interval) => {
10const timeZone = "America/Chicago";
11const date = DateTime.now();
1516try {
17const { data, whisper_api_data, dalle_api_data } = await fetchOpenAiUsageData(today);
1819const day_total = await createDayTotal(data, whisper_api_data, dalle_api_data);
27};
2829export default logger(fetchAndStoreOpenAiUsage);
openaiPricinggetAudioTotals2 matches
1import { openAiPricing } from "./openAiPricing";
23export const getAudioTotals = (data) => {
4const totals = {};
5const { whisperPricing } = openAiPricing;
6for (const obj of data) {
7// Extract the snapshot_id and token totals from the current object
openaiPricinggetTextTotals2 matches
1import { openAiPricing } from "./openAiPricing";
2import { email } from "https://esm.town/v/std/email?v=9";
310};
11} = {};
12const { textPricing } = openAiPricing;
13for (const obj of data) {
14const { snapshot_id, n_context_tokens_total, n_generated_tokens_total }: {
openaiPricinggetImageTotals2 matches
1import { openAiPricing } from "./openAiPricing";
23export const getImageTotals = (data) => {
4const totals = {};
5const { dallePricing } = openAiPricing;
6for (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";
78const fetchAndStoreOpenAiUsage = async () => {
9const timeZone = "America/Chicago";
10const date = DateTime.now();
1415try {
16const { data, whisper_api_data, dalle_api_data } = await fetchOpenAiUsageData("2024-04-01");
17console.log(data.length);
18console.log(whisper_api_data.length);
28};
2930await fetchAndStoreOpenAiUsage();
45} & Partial<Record<Month, Day>>;
4647export let openAiUsageTypes;