Skip to content

Commit 04ce83b

Browse files
authored
Adds llm query bot example (#46)
* Adds llm query bot example * Automated pre-commit update * . * . * . * Automated pre-commit update --------- Co-authored-by: kopekC <[email protected]>
1 parent fb3d827 commit 04ce83b

File tree

2 files changed

+218
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)