-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai.go
79 lines (65 loc) · 1.87 KB
/
openai.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
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
package torvalds
import (
"context"
"errors"
"fmt"
"os"
openai "github.com/sashabaranov/go-openai"
)
var ErrDiffTooLarge = errors.New("diff too large")
var ErrNoOpenAIToken = errors.New("no openai token")
var systemPrompt = `You are Linus Torvalds.
You will receive a git patch via mail.
Analyze the patch and be very crude while reviewing and very occasionally resort to swearing.
Make the review personal.
Go into detail why the code is bad and adjust your input based on the programming language that the file extension specifies.
Do not give any compliments.
The review should be succinct, to the point and should not exceed 5 paragraphs of each 2 sentences.
Prefix the response with an email subject header.
`
func AskTorvalds(diff string) (string, error) {
token, ok := os.LookupEnv("OPENAI_TOKEN")
if !ok {
return "", ErrNoOpenAIToken
}
if len(diff) > 12_000 {
return "", errors.Join(ErrDiffTooLarge, fmt.Errorf("length is %d", len(diff)))
}
client := openai.NewClient(token)
model := figureOutWhichModelToUse(client)
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: model,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: systemPrompt,
},
{
Role: openai.ChatMessageRoleUser,
Content: diff,
},
},
Temperature: 0.5,
},
)
if err != nil {
return "", err
}
return resp.Choices[0].Message.Content, nil
}
func figureOutWhichModelToUse(client *openai.Client) string {
customModel, hasCustomModel := os.LookupEnv("OPENAI_MODEL")
availableModels, err := client.ListModels(context.Background())
if err != nil && hasCustomModel {
// trust that the model is good
return customModel
}
for _, model := range availableModels.Models {
if model.ID == customModel {
return customModel
}
}
return openai.GPT3Dot5Turbo
}