forked from insulineru/ai-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilterApi.js
More file actions
49 lines (42 loc) · 1.54 KB
/
filterApi.js
File metadata and controls
49 lines (42 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { encode } from 'gpt-3-encoder';
import inquirer from "inquirer";
import { AI_PROVIDER } from "./config.js"
const FEE_PER_1K_TOKENS = 0.02;
const MAX_TOKENS = 4000;
//this is the approximate cost of a completion (answer) fee from CHATGPT
const FEE_COMPLETION = 0.001;
async function filterApi({ prompt, numCompletion = 1, filterFee }) {
if(AI_PROVIDER == 'ollama'){
//ollama and groq dont have any limits and is free so we dont need to filter anything
return true
}
if(AI_PROVIDER == 'groq'){
return true;
// const numTokens = encode(prompt).length;
// if (numTokens > MAX_TOKENS) {
// console.log("The commit diff is too large for the ChatGPT API. Max 4k tokens or ~8k characters. ");
// return false;
// }
// return true
}
const numTokens = encode(prompt).length;
const fee = numTokens / 1000 * FEE_PER_1K_TOKENS + (FEE_COMPLETION * numCompletion);
if (numTokens > MAX_TOKENS) {
console.log("The commit diff is too large for the ChatGPT API. Max 4k tokens or ~8k characters. ");
return false;
}
if (filterFee) {
console.log(`This will cost you ~$${+fee.toFixed(3)} for using the API.`);
const answer = await inquirer.prompt([
{
type: "confirm",
name: "continue",
message: "Do you want to continue 💸?",
default: true,
},
]);
if (!answer.continue) return false;
}
return true;
};
export { filterApi }