-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
review.go
119 lines (105 loc) · 3.83 KB
/
review.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
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
110
111
112
113
114
115
116
117
118
119
package cmd
import (
"strconv"
"strings"
"github.com/appleboy/CodeGPT/git"
"github.com/appleboy/CodeGPT/openai"
"github.com/appleboy/CodeGPT/prompt"
"github.com/appleboy/CodeGPT/util"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
// The maximum number of tokens to generate in the chat completion.
// The total length of input tokens and generated tokens is limited by the model's context length.
maxTokens int
)
func init() {
reviewCmd.Flags().IntVar(&diffUnified, "diff_unified", 3, "generate diffs with <n> lines of context, default is 3")
reviewCmd.Flags().IntVar(&maxTokens, "max_tokens", 300, "the maximum number of tokens to generate in the chat completion.")
reviewCmd.Flags().StringVar(&commitModel, "model", "gpt-3.5-turbo", "select openai model")
reviewCmd.Flags().StringVar(&commitLang, "lang", "en", "summarizing language uses English by default")
reviewCmd.Flags().StringSliceVar(&excludeList, "exclude_list", []string{}, "exclude file from git diff command")
reviewCmd.Flags().BoolVar(&commitAmend, "amend", false, "replace the tip of the current branch by creating a new commit.")
}
var reviewCmd = &cobra.Command{
Use: "review",
Short: "Auto review code changes",
RunE: func(cmd *cobra.Command, args []string) error {
if err := check(); err != nil {
return err
}
g := git.New(
git.WithDiffUnified(viper.GetInt("git.diff_unified")),
git.WithExcludeList(viper.GetStringSlice("git.exclude_list")),
git.WithEnableAmend(commitAmend),
)
diff, err := g.DiffFiles()
if err != nil {
return err
}
color.Green("Code review your changes using " + viper.GetString("openai.model") + " model")
client, err := openai.New(
openai.WithToken(viper.GetString("openai.api_key")),
openai.WithModel(viper.GetString("openai.model")),
openai.WithOrgID(viper.GetString("openai.org_id")),
openai.WithProxyURL(viper.GetString("openai.proxy")),
openai.WithSocksURL(viper.GetString("openai.socks")),
openai.WithBaseURL(viper.GetString("openai.base_url")),
openai.WithTimeout(viper.GetDuration("openai.timeout")),
openai.WithMaxTokens(viper.GetInt("openai.max_tokens")),
)
if err != nil {
return err
}
out, err := util.GetTemplateByString(
prompt.CodeReviewTemplate,
util.Data{
"file_diffs": diff,
},
)
if err != nil {
return err
}
// Get summarize comment from diff datas
color.Cyan("We are trying to review code changes")
resp, err := client.Completion(cmd.Context(), out)
if err != nil {
return err
}
summarizeMessage := resp.Content
color.Magenta("PromptTokens: " + strconv.Itoa(resp.Usage.PromptTokens) +
", CompletionTokens: " + strconv.Itoa(resp.Usage.CompletionTokens) +
", TotalTokens: " + strconv.Itoa(resp.Usage.TotalTokens),
)
if prompt.GetLanguage(viper.GetString("output.lang")) != prompt.DefaultLanguage {
out, err = util.GetTemplateByString(
prompt.TranslationTemplate,
util.Data{
"output_language": prompt.GetLanguage(viper.GetString("output.lang")),
"output_message": summarizeMessage,
},
)
if err != nil {
return err
}
// translate a git commit message
color.Cyan("We are trying to translate code review to " + prompt.GetLanguage(viper.GetString("output.lang")) + " language")
resp, err := client.Completion(cmd.Context(), out)
if err != nil {
return err
}
color.Magenta("PromptTokens: " + strconv.Itoa(resp.Usage.PromptTokens) +
", CompletionTokens: " + strconv.Itoa(resp.Usage.CompletionTokens) +
", TotalTokens: " + strconv.Itoa(resp.Usage.TotalTokens),
)
summarizeMessage = resp.Content
}
// Output core review summary
color.Yellow("================Review Summary====================")
color.Yellow("\n" + strings.TrimSpace(summarizeMessage) + "\n\n")
color.Yellow("==================================================")
return nil
},
}