Skip to content

Commit 81fa2f5

Browse files
fix: consume less tokens and update readme for openai and gemini example
1 parent 76287d6 commit 81fa2f5

5 files changed

Lines changed: 81 additions & 61 deletions

File tree

README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ Go to your repo → Settings → Secrets → Actions → New repository secret
3434

3535
**2. Create `.github/workflows/ai-review.yml`**
3636

37+
Pick your provider and paste the matching snippet:
38+
39+
**Anthropic (Claude)**
3740
```yaml
3841
name: AI Code Review
3942

@@ -54,6 +57,48 @@ jobs:
5457
api_key: ${{ secrets.ANTHROPIC_API_KEY }}
5558
```
5659
60+
**OpenAI (GPT)**
61+
```yaml
62+
name: AI Code Review
63+
64+
on:
65+
pull_request:
66+
types: [opened, synchronize, reopened]
67+
68+
jobs:
69+
review:
70+
runs-on: ubuntu-latest
71+
permissions:
72+
pull-requests: write
73+
74+
steps:
75+
- uses: Mohit-Raj-Singh/MergeMind_AI@v1
76+
with:
77+
provider: openai
78+
api_key: ${{ secrets.OPENAI_API_KEY }}
79+
```
80+
81+
**Google Gemini**
82+
```yaml
83+
name: AI Code Review
84+
85+
on:
86+
pull_request:
87+
types: [opened, synchronize, reopened]
88+
89+
jobs:
90+
review:
91+
runs-on: ubuntu-latest
92+
permissions:
93+
pull-requests: write
94+
95+
steps:
96+
- uses: Mohit-Raj-Singh/MergeMind_AI@v1
97+
with:
98+
provider: gemini
99+
api_key: ${{ secrets.GEMINI_API_KEY }}
100+
```
101+
57102
That's it. Open a PR and the review will appear automatically.
58103
59104
---
@@ -185,8 +230,9 @@ pytest
185230
### Running locally against a real PR
186231

187232
```bash
233+
# Set your chosen provider (anthropic | openai | gemini)
188234
export PROVIDER=anthropic
189-
export API_KEY=your-key
235+
export API_KEY=your-key # ANTHROPIC_API_KEY / OPENAI_API_KEY / GEMINI_API_KEY
190236
export GITHUB_TOKEN=your-github-token
191237
export GITHUB_REPOSITORY=owner/repo
192238
export GITHUB_EVENT_PATH=/path/to/event.json

src/ai_reviewer/providers/anthropic.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ def __init__(self, api_key: str, model: str):
1616
async def review(self, diff: str, review_level: str, security_only: bool) -> ReviewResult:
1717
message = await self.client.messages.create(
1818
model=self.model,
19-
max_tokens=4096,
20-
system=SYSTEM_PROMPT,
19+
max_tokens=1024,
20+
system=[
21+
{
22+
"type": "text",
23+
"text": SYSTEM_PROMPT,
24+
"cache_control": {"type": "ephemeral"},
25+
}
26+
],
2127
messages=[
2228
{"role": "user", "content": build_user_prompt(diff, review_level, security_only)}
2329
],

src/ai_reviewer/providers/gemini.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def __init__(self, api_key: str, model: str):
1515
self.config = genai_types.GenerateContentConfig(
1616
system_instruction=SYSTEM_PROMPT,
1717
response_mime_type="application/json",
18-
max_output_tokens=4096,
18+
max_output_tokens=1024,
1919
)
2020

2121
async def review(self, diff: str, review_level: str, security_only: bool) -> ReviewResult:

src/ai_reviewer/providers/openai.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async def review(self, diff: str, review_level: str, security_only: bool) -> Rev
2020
{"role": "system", "content": SYSTEM_PROMPT},
2121
{"role": "user", "content": build_user_prompt(diff, review_level, security_only)},
2222
],
23-
max_tokens=4096,
23+
max_tokens=1024,
2424
)
2525
raw = response.choices[0].message.content
2626
if raw is None:
Lines changed: 24 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,29 @@
11
SYSTEM_PROMPT = """\
2-
You are an expert code reviewer. Your job is to review a pull request diff and provide:
3-
1. Inline comments on specific lines that have issues
4-
2. An overall summary of the PR
5-
6-
Focus on:
7-
- Bugs and logic errors
8-
- Security vulnerabilities (SQL injection, XSS, secrets in code, insecure dependencies)
9-
- Performance issues
10-
- Code style and maintainability
11-
- Missing error handling
12-
13-
Be constructive and specific. Always suggest a fix, not just a problem.
14-
Severity levels:
15-
- error: must fix before merging (bugs, security issues)
16-
- warning: should fix (poor practices, potential issues)
17-
- info: optional improvement (style, minor refactors)
18-
- security: security-specific finding (always treat as high priority)
19-
20-
You MUST respond with valid JSON matching this exact schema:
21-
{
22-
"summary": {
23-
"overall": "<one paragraph assessment>",
24-
"highlights": ["<positive thing 1>", ...],
25-
"issues": ["<top issue 1>", ...],
26-
"security_flags": ["<security concern 1>", ...]
27-
},
28-
"inline_comments": [
29-
{
30-
"path": "<file path>",
31-
"line": <line number in new file>,
32-
"severity": "error|warning|info|security",
33-
"title": "<short title>",
34-
"body": "<detailed explanation and fix suggestion>"
35-
}
36-
]
2+
You are a code reviewer. Review a PR diff and respond with valid JSON only \
3+
— no markdown fences, no extra text.
4+
5+
Severity: error (bugs/security, must fix), warning (poor practice), \
6+
info (optional), security (high priority).
7+
8+
JSON schema:
9+
{"summary":{"overall":"<2-3 sentence assessment>","highlights":["<positive>"],\
10+
"issues":["<issue>"],"security_flags":["<concern>"]},"inline_comments":[{"path":"<file>",\
11+
"line":<int>,"severity":"error|warning|info|security","title":"<short>",\
12+
"body":"<1-2 sentence explanation and fix>"}]}"""
13+
14+
15+
_DEPTH = {
16+
"quick": (
17+
"Quick pass: flag only errors and security issues. Max 3 inline comments. No info/warning."
18+
),
19+
"standard": "Standard review: bugs, security, important style. Max 5 inline comments.",
20+
"thorough": (
21+
"Exhaustive review: correctness, security, performance, style. Max 10 inline comments."
22+
),
3723
}
3824

39-
Do not include markdown fences or any text outside the JSON object."""
40-
4125

4226
def build_user_prompt(diff: str, review_level: str, security_only: bool) -> str:
43-
focus = "Focus ONLY on security vulnerabilities." if security_only else ""
44-
depth = {
45-
"quick": "Do a quick pass — flag only errors and security issues.",
46-
"standard": "Do a thorough review covering bugs, security, and important style issues.",
47-
"thorough": (
48-
"Do an exhaustive review of every aspect: correctness, security, performance,"
49-
" style, and maintainability."
50-
),
51-
}[review_level]
52-
53-
return f"""{depth} {focus}
54-
55-
Here is the pull request diff to review:
56-
57-
```diff
58-
{diff}
59-
```
60-
61-
Respond with JSON only."""
27+
depth = _DEPTH[review_level]
28+
focus = " Focus ONLY on security vulnerabilities." if security_only else ""
29+
return f"{depth}{focus}\n\nDiff:\n```diff\n{diff}\n```\n\nJSON only."

0 commit comments

Comments
 (0)