Skip to content

Commit e456ea9

Browse files
committed
improve prompt
1 parent eebd10a commit e456ea9

File tree

4 files changed

+53
-34
lines changed

4 files changed

+53
-34
lines changed

cmd/review/main.go

+20-21
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"os"
88
"os/signal"
9-
"strconv"
109
"syscall"
1110

1211
"github.com/google/go-github/v51/github"
@@ -59,23 +58,27 @@ func run(ctx context.Context) error {
5958
var comments []*github.PullRequestComment
6059

6160
for i, file := range diff.Files {
61+
patch := file.GetPatch()
6262
fmt.Printf("processing file: %s %d/%d\n", file.GetFilename(), i+1, len(diff.Files))
63-
if file.Patch == nil || file.GetStatus() == "removed" || file.GetStatus() == "renamed" {
63+
if patch == "" || file.GetStatus() == "removed" || file.GetStatus() == "renamed" {
6464
continue
6565
}
6666

67-
prompt := fmt.Sprintf(oAIClient.PromptReview, oAIClient.JsonReviewPrompt, *file.Patch)
68-
69-
if len(prompt) > 4096 {
70-
prompt = fmt.Sprintf("%s...", prompt[:4093])
67+
if len(patch) > 4096 {
68+
fmt.Println("Patch is too long, truncating")
69+
patch = fmt.Sprintf("%s...", patch[:4093])
7170
}
72-
7371
completion, err := openAIClient.ChatCompletion(ctx, []openai.ChatCompletionMessage{
7472
{
7573
Role: openai.ChatMessageRoleUser,
76-
Content: prompt,
74+
Content: oAIClient.PromptReview,
75+
},
76+
{
77+
Role: openai.ChatMessageRoleUser,
78+
Content: patch,
7779
},
7880
})
81+
7982
if err != nil {
8083
return fmt.Errorf("error getting completion: %w", err)
8184
}
@@ -95,18 +98,13 @@ func run(ctx context.Context) error {
9598
fmt.Println("Review is good")
9699
continue
97100
}
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-
101+
for _, issue := range review.Issues {
102+
body := fmt.Sprintf("[%s] %s", issue.Type, issue.Description)
105103
comment := &github.PullRequestComment{
106104
CommitID: diff.Commits[len(diff.Commits)-1].SHA,
107105
Path: file.Filename,
108-
Body: &c.Comment,
109-
Position: &lineNumber,
106+
Body: &body,
107+
Position: &issue.Line,
110108
}
111109
comments = append(comments, comment)
112110
}
@@ -128,10 +126,11 @@ func run(ctx context.Context) error {
128126
type Review struct {
129127
Quality Quality `json:"quality"`
130128
Explanation string `json:"explanation"`
131-
Comments []struct {
132-
LineNumber string `json:"line_number_string"`
133-
Comment string `json:"comment"`
134-
}
129+
Issues []struct {
130+
Type string `json:"type"`
131+
Line int `json:"line"`
132+
Description string `json:"description"`
133+
} `json:"issues"`
135134
}
136135

137136
type Quality string

openai/assets/review.json

-10
This file was deleted.

openai/assets/review.txt

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
You are CodeReviewGPT, an AI agent that specializes in generating code reviews for software projects using advanced natural language processing and machine learning techniques.
2+
Your 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.
3+
4+
GOALS:
5+
6+
1. Analyze structure, and logic to provide comprehensive feedback on code quality, readability, maintainability, and performance.
7+
2. Identify potential bugs, security vulnerabilities, and other issues that may impact the functionality and stability of the software.
8+
3. 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
9+
Response Format:
10+
```
11+
{
12+
"quality": "good",
13+
"explanation": "Your code is well-structured and has no obvious issues.",
14+
"issues": [
15+
{
16+
"type": "bug",
17+
"line": 10,
18+
"description": "You are missing a semicolon at the end of the line."
19+
},
20+
{
21+
"type": "performance",
22+
"line": 20,
23+
"description": "You should use a StringBuilder instead of a String to improve performance."
24+
}
25+
]
26+
}
27+
```
28+
4. Rules:
29+
Possible quality values: good, bad, neutral
30+
Possible issue types: bug, security, performance, readability, maintainability, other
31+
If quality is good, issues should be empty.

openai/openai.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ import (
88
"github.com/sashabaranov/go-openai"
99
)
1010

11-
//go:embed assets/review.json
12-
var JsonReviewPrompt string
11+
//go:embed assets/review.txt
12+
var PromptReview string
1313

1414
const (
1515
PromptDescribeChanges = "Below is the code patch, Generate a GitHub pull request description based on the following comments without basic prefix\n%s\n"
1616
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"
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"
1817
)
1918

2019
type Client struct {

0 commit comments

Comments
 (0)