-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathopenai.js
More file actions
54 lines (47 loc) · 1.38 KB
/
openai.js
File metadata and controls
54 lines (47 loc) · 1.38 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
import OpenAI from 'openai';
import config from '../utils/config.json';
import dotenv from 'dotenv';
import {getOpenAIKey, setOpenAIKey, deleteOPenAIKey} from '../utils/api.js';
dotenv.config();
async function openAiModel(model, flags, diffContent) {
if (flags.setopenai) {
setOpenAIKey(flags.setopenai);
}
if (flags.delopenai) {
deleteOPenAIKey();
}
if (!getOpenAIKey()) {
return {
message:
'Please provide an OpenAI API key.\n' +
'You can get one from https://platform.openai.com/account/api-keys\n' +
'Run `magicc --setopenai=<api-key>` to save your API key and try again.',
};
} else {
console.log(
'You have an OpenAI API key, you can now generate a commit message.',
);
const apiKey = await getOpenAIKey();
const openai = new OpenAI({apiKey: apiKey});
const category = await openai.chat.completions.create({
messages: [
{role: 'system', content: config.commitConfig.emoji},
{role: 'user', content: diffContent},
],
model,
});
// use the prmopt from the config file message and send to openai
const message = await openai.chat.completions.create({
messages: [
{role: 'system', content: config.commitConfig.message},
{role: 'user', content: diffContent},
],
model,
});
return {
category: category.choices[0].message.content,
message: message.choices[0].message.content,
};
}
}
export default openAiModel;