forked from insulineru/ai-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgemini.js
More file actions
109 lines (96 loc) · 3.07 KB
/
gemini.js
File metadata and controls
109 lines (96 loc) · 3.07 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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import inquirer from "inquirer";
import { AI_PROVIDER } from "./config.js";
const FEE_PER_1K_TOKENS = 0.0;
const MAX_TOKENS = 1_000_000;
const FEE_COMPLETION = 0.001;
const gemini = {
sendMessage: async (
input,
{ apiKey, model = "google/gemini-2.0-flash-lite-preview-02-05:free" }
) => {
console.log("prompting Gemini API...");
console.log("prompt: ", input);
const body = {
contents: [{
parts: [{
text: input
}]
}]
};
console.log("Request body:", JSON.stringify(body, null, 2));
const response = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent?key=${apiKey}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
}
);
const data = await response.json();
if (data.error) {
console.error("Error generating commit message: ", data.error);
return "";
}
return data.candidates?.[0]?.content?.parts?.[0]?.text || "";
},
getPromptForSingleCommit: (
diff,
{ commitType, customMessageConvention, language }
) => {
return (
`Write a professional git commit message based on the diff below in ${language} language` +
(commitType ? ` with commit type '${commitType}'. ` : ". ") +
`${
customMessageConvention
? `Apply these JSON formatted rules: ${customMessageConvention}.`
: ""
}` +
"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, customMessageConvention, numOptions, language }
) => {
return (
`Write a professional git commit message based on the diff below in ${language} language` +
(commitType ? ` with commit type '${commitType}'. ` : ". ") +
`Generate ${numOptions} options separated by ";".` +
"For each option, use the present tense, return the full sentence and also commit type." +
`${
customMessageConvention
? ` Apply these JSON formatted rules: ${customMessageConvention}.`
: ""
}` +
`\n\n${diff}`
);
},
filterApi: async ({ prompt, numCompletion = 1, filterFee }) => {
const numTokens = prompt.split(" ").length; // Approximate token count
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 Gemini API. Max 128k tokens."
);
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 gemini;