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

(feat) set limit of requests to openai so that i dont go broke #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 18 additions & 1 deletion src/gpt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import { openAI } from "./init.ts";

const REQUEST_LIMIT = 100;
const TIME_FRAME = 60 * 60 * 1000; // Time frame in milliseconds (1 hour)
let requestCount = 0;
let firstRequestTimestamp = Date.now();

export async function generate_gpt_cron(text: string) {
if (requestCount >= REQUEST_LIMIT && (Date.now() - firstRequestTimestamp) < TIME_FRAME) {
console.log("Request limit reached. Please wait before making more requests.");
return;
}

if ((Date.now() - firstRequestTimestamp) >= TIME_FRAME) {
requestCount = 0;
firstRequestTimestamp = Date.now();
}

const content = `send me a text cron that means ${text}. wrap the cron itself in ""`;

const reply = await openAI.createChatCompletion({
Expand All @@ -22,10 +37,12 @@ export async function generate_gpt_cron(text: string) {
return;
}

requestCount++;

return get_cron(reply_text)?.[1];
}

function get_cron(gpt_reply: string) {
const re = /"([^"]+)"/g;
return re.exec(gpt_reply);
}
}