Skip to content

Commit bc6a4e8

Browse files
committed
improve prompt and handling completion
1 parent dd62c29 commit bc6a4e8

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

cmd/review/main.go

+43-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"os"
89
"os/signal"
@@ -64,9 +65,9 @@ func run(ctx context.Context) error {
6465
continue
6566
}
6667

67-
if len(patch) > 4096 {
68+
if len(patch) > 3000 {
6869
fmt.Println("Patch is too long, truncating")
69-
patch = fmt.Sprintf("%s...", patch[:4093])
70+
patch = fmt.Sprintf("%s...", patch[:3000])
7071
}
7172
completion, err := openAIClient.ChatCompletion(ctx, []openai.ChatCompletionMessage{
7273
{
@@ -87,10 +88,9 @@ func run(ctx context.Context) error {
8788
fmt.Println("Completion:", completion)
8889
}
8990

90-
review := Review{}
91-
err = json.Unmarshal([]byte(completion), &review)
91+
review, err := extractJSON(completion)
9292
if err != nil {
93-
fmt.Println("Error unmarshalling completion:", err)
93+
fmt.Println("Error extracting JSON:", err)
9494
continue
9595
}
9696

@@ -124,9 +124,8 @@ func run(ctx context.Context) error {
124124
}
125125

126126
type Review struct {
127-
Quality Quality `json:"quality"`
128-
Explanation string `json:"explanation"`
129-
Issues []struct {
127+
Quality Quality `json:"quality"`
128+
Issues []struct {
130129
Type string `json:"type"`
131130
Line int `json:"line"`
132131
Description string `json:"description"`
@@ -140,3 +139,39 @@ const (
140139
Bad Quality = "bad"
141140
Neutral Quality = "neutral"
142141
)
142+
143+
func extractJSON(input string) (*Review, error) {
144+
var jsonObj *Review
145+
146+
// find the start and end positions of the JSON object
147+
start := 0
148+
end := len(input)
149+
for i, c := range input {
150+
if c == '{' {
151+
start = i
152+
break
153+
}
154+
if i == len(input)-1 {
155+
return nil, errors.New("invalid JSON object")
156+
}
157+
}
158+
for i := len(input) - 1; i >= 0; i-- {
159+
if input[i] == '}' {
160+
end = i + 1
161+
break
162+
}
163+
164+
if i == 0 {
165+
return nil, errors.New("invalid JSON object")
166+
}
167+
}
168+
169+
// extract the JSON object from the input
170+
jsonStr := input[start:end]
171+
err := json.Unmarshal([]byte(jsonStr), &jsonObj)
172+
if err != nil {
173+
return nil, errors.New("invalid JSON object")
174+
}
175+
176+
return jsonObj, nil
177+
}

openai/assets/review.txt

+2-11
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,18 @@ GOALS:
55

66
1. Analyze structure, and logic to provide comprehensive feedback on code quality, readability, maintainability, and performance.
77
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
8+
3. Possible quality values: good, bad, neutral. If quality is good, issues should be empty.
9+
4. Generate a json report in specific format to help developers improve their code. If context is not enough quality is good. You should only respond in JSON format as described below
910
Response Format:
1011
```
1112
{
1213
"quality": "good",
13-
"explanation": "Your code is well-structured and has no obvious issues.",
1414
"issues": [
1515
{
1616
"type": "bug",
1717
"line": 10,
1818
"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."
2419
}
2520
]
2621
}
2722
```
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.

0 commit comments

Comments
 (0)