-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpt.go
49 lines (39 loc) · 996 Bytes
/
gpt.go
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
package main
import (
"context"
"fmt"
gogpt "github.com/sashabaranov/go-gpt3"
)
const PROMPT = "Suggest %d good commit messages for my commit%s:" +
"```\n" + "%s" + "```\n"
func fetchAiResponse(apiKey string, prompt string, aiModel string) (string, error) {
c := gogpt.NewClient(apiKey)
ctx := context.Background()
req := gogpt.CompletionRequest{
Model: aiModel,
MaxTokens: 100,
Prompt: prompt,
}
resp, err := c.CreateCompletion(ctx, req)
if err != nil {
return "", err
}
return resp.Choices[0].Text, nil
}
func fetchCommitSuggestions(apiKey string, conventional bool, aiModel string) ([]string, error) {
diff, err := gitDiff()
if err != nil {
return []string{}, err
}
cs := ""
if conventional {
cs = " in conventional commit format (type(scope): subject)"
}
fullPrompt := fmt.Sprintf(PROMPT, 5, cs, diff)
res, err := fetchAiResponse(apiKey, fullPrompt, aiModel)
if err != nil {
return []string{}, err
}
s := cleanLines(res)
return s, nil
}