-
Notifications
You must be signed in to change notification settings - Fork 44
/
openai.js
77 lines (62 loc) · 2.3 KB
/
openai.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { ChatGPTAPI } from "chatgpt";
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 = 128000;
//this is the approximate cost of a completion (answer) fee from CHATGPT
const FEE_COMPLETION = 0.001;
const openai = {
sendMessage: async (input, {apiKey, model}) => {
console.log("prompting chat gpt...");
console.log("prompt: ", input);
const api = new ChatGPTAPI({
apiKey,
completionParams: {
model: "gpt-4o-mini",
},
});
const { text } = await api.sendMessage(input);
return text;
},
getPromptForSingleCommit: (diff, {commitType, language}) => {
return (
`Write a professional git commit message based on the a diff below in ${language} language` +
(commitType ? ` with commit type '${commitType}'. ` : ". ") +
"Do not preface the commit with anything, use the present tense, return the full sentence and also commit type: " +
'\n\n'+
diff
);
},
getPromptForMultipleCommits: (diff, {commitType, numOptions, language}) => {
const prompt =
`Write a professional git commit message based on the a diff below in ${language} language` +
(commitType ? ` with commit type '${commitType}'. ` : ". ")+
`and make ${numOptions} options that are separated by ";".` +
"For each option, use the present tense, return the full sentence and also commit type:" +
diff;
return prompt;
},
filterApi: async ({ prompt, numCompletion = 1, filterFee }) => {
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 default openai;