Skip to content

Commit d3a367f

Browse files
committed
Adds llm query bot example
1 parent fb3d827 commit d3a367f

File tree

2 files changed

+226
-0
lines changed

2 files changed

+226
-0
lines changed

examples/pr_review_bot/README.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# AI-Powered Pull Request Review Bot
2+
3+
This example demonstrates how to use Codegen to create an intelligent PR review bot that analyzes code changes and their dependencies to provide comprehensive code reviews. The bot uses GPT-4 to generate contextual feedback based on modified code and its relationships.
4+
5+
> [!NOTE]
6+
> This codemod helps development teams by providing automated, context-aware code reviews that consider both direct and indirect code dependencies.
7+
8+
## How the PR Review Bot Works
9+
10+
The script analyzes pull requests in several key steps:
11+
12+
1. **Symbol Analysis**
13+
```python
14+
modified_symbols = codebase.get_modified_symbols_in_pr(pr_number)
15+
for symbol in modified_symbols:
16+
deps = codebase.get_symbol_dependencies(symbol, max_depth=2)
17+
rev_deps = codebase.get_symbol_dependents(symbol, max_depth=2)
18+
```
19+
- Identifies modified symbols in the PR
20+
- Analyzes dependencies up to 2 levels deep
21+
- Tracks reverse dependencies (symbols that depend on changes)
22+
23+
2. **Context Building**
24+
```python
25+
context = {
26+
"pr_title": pr.title,
27+
"pr_body": pr.body,
28+
"modified_symbols": [...],
29+
"context_symbols": [...]
30+
}
31+
```
32+
- Gathers PR metadata
33+
- Collects modified code content
34+
- Includes relevant dependency context
35+
36+
3. **AI Review Generation**
37+
```python
38+
review = codebase.ai_client.llm_query_with_retry(
39+
messages=[...],
40+
model="gpt-4",
41+
max_tokens=2000
42+
)
43+
```
44+
- Uses GPT-4 for analysis
45+
- Generates comprehensive review feedback
46+
- Considers full context of changes
47+
48+
## Why This Makes Code Review Better
49+
50+
1. **Context-Aware Analysis**
51+
- Understands code dependencies
52+
- Considers impact of changes
53+
- Reviews code in proper context
54+
55+
2. **Comprehensive Review**
56+
- Analyzes direct modifications
57+
- Evaluates dependency impact
58+
- Suggests improvements
59+
60+
3. **Consistent Feedback**
61+
- Structured review format
62+
- Thorough analysis every time
63+
- Scalable review process
64+
65+
## Review Output Format
66+
67+
The bot provides structured feedback including:
68+
69+
```
70+
1. Overall Assessment
71+
- High-level review of changes
72+
- Impact analysis
73+
74+
2. Specific Code Feedback
75+
- Detailed code comments
76+
- Style suggestions
77+
- Best practices
78+
79+
3. Potential Issues
80+
- Security concerns
81+
- Performance impacts
82+
- Edge cases
83+
84+
4. Dependency Analysis
85+
- Impact on dependent code
86+
- Breaking changes
87+
- Integration considerations
88+
89+
```
90+
91+
## Key Benefits to Note
92+
93+
1. **Better Code Quality**
94+
- Thorough code analysis
95+
- Consistent review standards
96+
- Early issue detection
97+
98+
2. **Time Savings**
99+
- Automated initial review
100+
- Quick feedback loop
101+
- Reduced review burden
102+
103+
3. **Knowledge Sharing**
104+
- Educational feedback
105+
- Best practice suggestions
106+
- Team learning
107+
108+
109+
## Configuration Options
110+
111+
You can customize the review by:
112+
- Adjusting dependency depth
113+
- Modifying the AI prompt
114+
- Changing the review focus areas
115+
- Tuning the GPT-4 parameters
116+
117+
## Learn More
118+
119+
- [Codegen Documentation](https://docs.codegen.com)
120+
- [OpenAI API Documentation](https://platform.openai.com/docs/api-reference)
121+
- [GitHub API Documentation](https://docs.github.com/en/rest)
122+
- [Codegen llm integration](https://docs.codegen.com/building-with-codegen/calling-out-to-llms)
123+
124+
## Contributing
125+
126+
Feel free to submit issues and enhancement requests! Contributions to improve the review bot's capabilities are welcome.

examples/pr_review_bot/run.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import codegen
2+
from codegen import Codebase
3+
from codegen.sdk.enums import ProgrammingLanguage
4+
from codegen.sdk.codebase.config import CodebaseConfig, GSFeatureFlags, Secrets
5+
import json
6+
github_token = "Your github token"
7+
open_ai_key = "your open ai key"
8+
pr_number= 0 # Your PR number must be an integer
9+
10+
def run(codebase: Codebase):
11+
12+
context_symbols = set()
13+
14+
modified_symbols = codebase.get_modified_symbols_in_pr(pr_number)
15+
for symbol in modified_symbols:
16+
# Get direct dependencies
17+
deps = codebase.get_symbol_dependencies(symbol, max_depth=2)
18+
context_symbols.update(deps)
19+
20+
# Get reverse dependencies (symbols that depend on this one)
21+
rev_deps = codebase.get_symbol_dependents(symbol, max_depth=2)
22+
context_symbols.update(rev_deps)
23+
24+
# Prepare context for LLM
25+
context = {
26+
"pr_title": pr.title,
27+
"pr_body": pr.body,
28+
"modified_symbols": [
29+
{
30+
"name": symbol.name,
31+
"type": symbol.symbol_type.value,
32+
"filepath": symbol.filepath,
33+
"content": symbol.content,
34+
} for symbol in modified_symbols
35+
],
36+
"context_symbols": [
37+
{
38+
"name": symbol.name,
39+
"type": symbol.symbol_type.value,
40+
"filepath": symbol.filepath,
41+
"content": symbol.content,
42+
} for symbol in context_symbols
43+
]
44+
}
45+
46+
system_prompt = """
47+
You are a helpful assistant that reviews pull requests and provides feedback on the code.
48+
"""
49+
# Generate review using AI
50+
prompt = f"""Please review this pull request based on the following context:
51+
52+
Title: {context['pr_title']}
53+
Description: {context['pr_body']}
54+
55+
Modified Symbols:
56+
{json.dumps(context['modified_symbols'], indent=2)}
57+
58+
Related Context (Dependencies):
59+
{json.dumps(context['context_symbols'], indent=2)}
60+
61+
Please provide a thorough code review that includes:
62+
1. Overall assessment
63+
2. Specific feedback on modified code
64+
3. Potential issues or improvements
65+
4. Impact on dependencies
66+
5. Suggestions for testing
67+
"""
68+
69+
review = codebase.ai_client.llm_query_with_retry(
70+
messages=[
71+
{"role": "system", "content": system_prompt},
72+
{"role": "user", "content": prompt}
73+
],
74+
model="gpt-4",
75+
max_tokens=2000,
76+
temperature=0.7
77+
)
78+
return review
79+
80+
81+
if __name__ == "__main__":
82+
print("Starting codebase analysis...")
83+
codebase = Codebase.from_repo(
84+
'getsentry/sentry',
85+
shallow=False,
86+
programming_language=ProgrammingLanguage.PYTHON,
87+
config=CodebaseConfig(
88+
secrets=Secrets(
89+
openai_key=open_ai_key,
90+
github_api_key=github_token
91+
92+
),
93+
feature_flags=GSFeatureFlags(
94+
sync_enabled=True, )
95+
96+
))
97+
review = run(codebase)
98+
print(review)
99+
100+
print("Codebase analysis complete.")

0 commit comments

Comments
 (0)