Skip to content

Commit eebd10a

Browse files
committed
prompt improved
1 parent 3c3d0e5 commit eebd10a

File tree

3 files changed

+74
-40
lines changed

3 files changed

+74
-40
lines changed

cmd/review/main.go

+56-36
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package main
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"os"
78
"os/signal"
8-
"strings"
9+
"strconv"
910
"syscall"
1011

1112
"github.com/google/go-github/v51/github"
@@ -55,13 +56,15 @@ func run(ctx context.Context) error {
5556
return fmt.Errorf("error getting commits: %w", err)
5657
}
5758

58-
var OverallReviewCompletion string
59-
for _, file := range diff.Files {
59+
var comments []*github.PullRequestComment
60+
61+
for i, file := range diff.Files {
62+
fmt.Printf("processing file: %s %d/%d\n", file.GetFilename(), i+1, len(diff.Files))
6063
if file.Patch == nil || file.GetStatus() == "removed" || file.GetStatus() == "renamed" {
6164
continue
6265
}
6366

64-
prompt := fmt.Sprintf(oAIClient.PromptReview, *file.Patch)
67+
prompt := fmt.Sprintf(oAIClient.PromptReview, oAIClient.JsonReviewPrompt, *file.Patch)
6568

6669
if len(prompt) > 4096 {
6770
prompt = fmt.Sprintf("%s...", prompt[:4093])
@@ -74,50 +77,67 @@ func run(ctx context.Context) error {
7477
},
7578
})
7679
if err != nil {
77-
return fmt.Errorf("error getting review: %w", err)
80+
return fmt.Errorf("error getting completion: %w", err)
7881
}
79-
OverallReviewCompletion += fmt.Sprintf("File: %s \nReview: %s \n\n", file.GetFilename(), completion)
8082

81-
position := len(strings.Split(*file.Patch, "\n")) - 1
83+
if opts.Test {
84+
fmt.Println("Completion:", completion)
85+
}
8286

83-
comment := &github.PullRequestComment{
84-
CommitID: diff.Commits[len(diff.Commits)-1].SHA,
85-
Path: file.Filename,
86-
Body: &completion,
87-
Position: &position,
87+
review := Review{}
88+
err = json.Unmarshal([]byte(completion), &review)
89+
if err != nil {
90+
fmt.Println("Error unmarshalling completion:", err)
91+
continue
8892
}
8993

90-
if opts.Test {
94+
if review.Quality == Good {
95+
fmt.Println("Review is good")
9196
continue
9297
}
98+
for _, c := range review.Comments {
99+
lineNumber, err := strconv.Atoi(c.LineNumber)
100+
if err != nil {
101+
fmt.Println("Error parsing line number:", err)
102+
continue
103+
}
104+
105+
comment := &github.PullRequestComment{
106+
CommitID: diff.Commits[len(diff.Commits)-1].SHA,
107+
Path: file.Filename,
108+
Body: &c.Comment,
109+
Position: &lineNumber,
110+
}
111+
comments = append(comments, comment)
112+
}
93113

94-
if _, err := githubClient.CreatePullRequestComment(ctx, opts.Owner, opts.Repo, opts.PRNumber, comment); err != nil {
95-
return fmt.Errorf("error creating comment: %w", err)
114+
if opts.Test {
115+
continue
96116
}
97-
}
98117

99-
overallCompletion, err := openAIClient.ChatCompletion(ctx, []openai.ChatCompletionMessage{
100-
{
101-
Role: openai.ChatMessageRoleUser,
102-
Content: fmt.Sprintf(oAIClient.PromptOverallReview, OverallReviewCompletion),
103-
},
104-
})
105-
if err != nil {
106-
return fmt.Errorf("error getting overall review: %w", err)
118+
for i, c := range comments {
119+
fmt.Printf("creating comment: %s %d/%d\n", *c.Path, i+1, len(comments))
120+
if _, err := githubClient.CreatePullRequestComment(ctx, opts.Owner, opts.Repo, opts.PRNumber, c); err != nil {
121+
return fmt.Errorf("error creating comment: %w", err)
122+
}
123+
}
107124
}
125+
return nil
126+
}
108127

109-
if opts.Test {
110-
fmt.Println(OverallReviewCompletion)
111-
fmt.Println("=====================================")
112-
fmt.Println(overallCompletion)
113-
114-
return nil
128+
type Review struct {
129+
Quality Quality `json:"quality"`
130+
Explanation string `json:"explanation"`
131+
Comments []struct {
132+
LineNumber string `json:"line_number_string"`
133+
Comment string `json:"comment"`
115134
}
135+
}
116136

117-
comment := &github.PullRequestReviewRequest{Body: &overallCompletion}
118-
if _, err = githubClient.CreateReview(ctx, opts.Owner, opts.Repo, opts.PRNumber, comment); err != nil {
119-
return fmt.Errorf("error creating comment: %w", err)
120-
}
137+
type Quality string
121138

122-
return nil
123-
}
139+
const (
140+
Good Quality = "good"
141+
Bad Quality = "bad"
142+
Neutral Quality = "neutral"
143+
)

openai/assets/review.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"quality": "good, bad, or neutral",
3+
"explanation": "explanation of the quality",
4+
"comments": [
5+
{
6+
"comment": "reasoning for the comment",
7+
"line_number_string": "line number of the comment. 0 if not applicable. number should be unique. must be a string"
8+
}
9+
]
10+
}

openai/openai.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@ package openai
22

33
import (
44
"context"
5+
_ "embed"
56
"fmt"
67

78
"github.com/sashabaranov/go-openai"
89
)
910

11+
//go:embed assets/review.json
12+
var JsonReviewPrompt string
13+
1014
const (
1115
PromptDescribeChanges = "Below is the code patch, Generate a GitHub pull request description based on the following comments without basic prefix\n%s\n"
1216
PromptOverallDescribe = "Below comments are generated by AI, Generate a GitHub pull request description based on the following comments without basic prefix in markdown format with ### Description and ### Changes blocks:\n%s\n"
13-
PromptReview = "Below is the code patch, please help me do a brief code review, Answer me in English, if any bug risk and improvement suggestion are welcome\n%s\n"
14-
PromptOverallReview = "Below comments are generated by AI, please help me do a brief code review, Answer me in English, if any bug risk and improvement suggestion are welcome\n%s\n"
17+
PromptReview = "You are CodeReviewGPT, an AI agent that specializes in generating code reviews for software projects using advanced natural language processing and machine learning techniques.\nYour decisions must always be made independently without seeking user assistance. Play to your strengths as an LLM and pursue simple strategies with no legal complications.\n\nGOALS:\n\n1. Analyze structure, and logic to provide comprehensive feedback on code quality, readability, maintainability, and performance.\n2. Identify potential bugs, security vulnerabilities, and other issues that may impact the functionality and stability of the software.\n3. Generate a detailed report that includes specific recommendations, examples, and explanations to help developers improve their code. If context is not enough mark it as good. You should only respond in JSON format as described below \nResponse Format: \n%s \n\n%s"
1518
)
1619

1720
type Client struct {
@@ -28,8 +31,9 @@ func (o *Client) ChatCompletion(ctx context.Context, messages []openai.ChatCompl
2831
resp, err := o.client.CreateChatCompletion(
2932
ctx,
3033
openai.ChatCompletionRequest{
31-
Model: openai.GPT3Dot5Turbo,
32-
Messages: messages,
34+
Model: openai.GPT3Dot5Turbo,
35+
Messages: messages,
36+
Temperature: 0,
3337
},
3438
)
3539

0 commit comments

Comments
 (0)