Ver3.0 ๐ค ํ์ด์ฌ์ผ๋ก ์ฝ๋ ๋ฆฌ๋ทฐ์ฉ AI ๋์ฐ๋ฏธ ๋ง๋ค๊ธฐ: ๊ฐ๋ฐ์์ ๋ ๋ ํ ๋๋ฃ ํ์์ํค๊ธฐ

๐ค ํ์ด์ฌ์ผ๋ก ์ฝ๋ ๋ฆฌ๋ทฐ์ฉ AI ๋์ฐ๋ฏธ ๋ง๋ค๊ธฐ: ๊ฐ๋ฐ์์ ๋ ๋ ํ ๋๋ฃ ํ์์ํค๊ธฐ
AI๊ฐ ๋น์ ์ ์ฝ๋๋ฅผ ๋ฆฌ๋ทฐํด์ค๋ค๋ฉด? ์์์ด ํ์ค์ด ๋๋ ์๊ฐ!
๐ก ์ฌ๋ฌ๋ถ, ์ฝ๋ ๋ฆฌ๋ทฐ ๋ฐ์ ๋ ์ด๋ค ๊ธฐ๋ถ์ด์ ๊ฐ์?
์์งํ ๋งํด์ ์ข ๋จ๋ฆฌ์ฃ ? "์ด ๋ถ๋ถ ์ ์ด๋ ๊ฒ ์งฐ์ด?"๋ผ๋ ์ง๋ฌธ์ ์์๋์ด ๋๊ธฐ๋ ํ๊ณ ์. ํ์ง๋ง ๋์์ ์ฝ๋ ๋ฆฌ๋ทฐ๋ ๊ฐ๋ฐ์๋ก ์ฑ์ฅํ๋ ๊ฐ์ฅ ๋น ๋ฅธ ์ง๋ฆ๊ธธ์ด๊ธฐ๋ ํฉ๋๋ค. ๐
๊ทธ๋ฐ๋ฐ ๋ง์ฝ 24์๊ฐ ์ธ์ ๋ ์ง, ๊ธฐ๋ถ ๋์์ง ์๊ฒ, ์น์ ํ๊ฒ ์ฝ๋๋ฅผ ๋ฆฌ๋ทฐํด์ฃผ๋ AI ๋์ฐ๋ฏธ๊ฐ ์๋ค๋ฉด ์ด๋จ๊น์? ์ค๋์ ๋ฐ๋ก ๊ทธ ๊ฟ๊ฐ์ ๋๊ตฌ๋ฅผ ํ์ด์ฌ์ผ๋ก ์ง์ ๋ง๋ค์ด๋ณด๋ ์ฌ์ ์ ํจ๊ป ๋ ๋๋ณผ ๊ฑฐ์์! ๐
๐ฏ ์ ์ฝ๋ ๋ฆฌ๋ทฐ AI ๋์ฐ๋ฏธ๊ฐ ํ์ํ ๊น?
์ฝ๋ ๋ฆฌ๋ทฐ๋ ์ํํธ์จ์ด ๊ฐ๋ฐ์์ ์ ๋ง ์ค์ํ ๊ณผ์ ์ด์์. ํ์ง๋ง ํ์ค์ ์ด๋ค๊ฐ์?
๐ค ํ์ค์ ์ธ ๋ฌธ์ ๋ค:
์๊ฐ ๋ถ์กฑ ๋ฐ์ ํ์๋ค์ ์ฝ๋ ๋ฆฌ๋ทฐํ ์๊ฐ์ด ์์ด์
์ผ๊ด์ฑ ๋ถ์กฑ ๋ฆฌ๋ทฐ์ด๋ง๋ค ๊ธฐ์ค์ด ๋ฌ๋ผ์
์ฆ๊ฐ์ฑ ๋ถ์กฑ ๋ฆฌ๋ทฐ ๋ฐ๊ธฐ๊น์ง ๋ช ์๊ฐ, ์ฌ์ง์ด ๋ฉฐ์น ์ด ๊ฑธ๋ฆฌ๊ธฐ๋ ํด์
์ฌ๋ฆฌ์ ๋ถ๋ด ์ ๋ฐฐ ๊ฐ๋ฐ์์๊ฒ ๋ฆฌ๋ทฐ ์์ฒญํ๊ธฐ๊ฐ ๋ถ๋ด์ค๋ฌ์์
ํ์ต ๊ธฐํ ์์ค ํผ์ ๊ณต๋ถํ ๋๋ ํผ๋๋ฐฑ์ ๋ฐ์ ์๊ฐ ์์ฃ
์ด๋ฐ ๋ฌธ์ ๋ค์ ํด๊ฒฐํ๊ธฐ ์ํด AI ์ฝ๋ ๋ฆฌ๋ทฐ ๋์ฐ๋ฏธ๊ฐ ๋ฑ์ฅํ์ด์! ๐
ํนํ ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ํ๋ก๊ทธ๋๋ฐ ๊ฐ์๋ ์ฝ๋ ๋ฆฌ๋ทฐ ์๋น์ค๋ฅผ ์ ๊ณตํ๋ ๋ถ๋ค์๊ฒ๋ ์ด๋ฐ ๋๊ตฌ๊ฐ ์ ๋ง ์ ์ฉํ ์ ์์ด์. ํ์๋ค์๊ฒ ์ฆ๊ฐ์ ์ธ ํผ๋๋ฐฑ์ ์ ๊ณตํ๋ฉด์๋, ๊ฐ์ฌ๋์ ์๊ฐ์ ์ ์ฝํ ์ ์๊ฑฐ๋ ์.
๐ ๏ธ ํ๋ก์ ํธ ์ค๋นํ๊ธฐ: ํ์ํ ๊ฒ๋ค
์, ์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก ์์ํด๋ณผ๊น์? ๋จผ์ ํ์ํ ๋๊ตฌ๋ค์ ์ฑ๊ฒจ๋ด ์๋ค!
๐ฆ 1๋จ๊ณ: ๊ธฐ๋ณธ ํ๊ฒฝ ์ค์
ํ์ด์ฌ 3.8 ์ด์์ด ํ์ํด์. ์์ฆ์ 3.10์ด๋ 3.11์ ์ถ์ฒ๋๋ ค์. ์ฑ๋ฅ๋ ์ข๊ณ ์๋ก์ด ๊ธฐ๋ฅ๋ค๋ ๋ง๊ฑฐ๋ ์!
# ํ์ด์ฌ ๋ฒ์ ํ์ธ
python --version
# ๊ฐ์ํ๊ฒฝ ์์ฑ (ํ๋ก์ ํธ ํด๋์์)
python -m venv code_review_env
# ๊ฐ์ํ๊ฒฝ ํ์ฑํ
# Windows
code_review_env\Scripts\activate
# Mac/Linux
source code_review_env/bin/activate
๐ 2๋จ๊ณ: ํ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น
์ฐ๋ฆฌ ํ๋ก์ ํธ์ ํ์ํ ํต์ฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ ์ค์นํด๋ด
์๋ค:
# requirements.txt ํ์ผ ๋ด์ฉ
openai==1.3.0 # OpenAI API ์ฌ์ฉ
anthropic==0.7.0 # Claude API ์ฌ์ฉ (์ ํ์ฌํญ)
python-dotenv==1.0.0 # ํ๊ฒฝ๋ณ์ ๊ด๋ฆฌ
ast-parser==0.2.0 # ํ์ด์ฌ ์ฝ๋ ํ์ฑ
pylint==3.0.0 # ์ ์ ๋ถ์
black==23.11.0 # ์ฝ๋ ํฌ๋งทํ
์ฒดํฌ
radon==6.0.1 # ๋ณต์ก๋ ๋ถ์
gitpython==3.1.40 # Git ํตํฉ
flask==3.0.0 # ์น ์ธํฐํ์ด์ค (์ ํ์ฌํญ)
# ์ค์น ๋ช
๋ น์ด
pip install -r requirements.txt
๐ก Pro Tip: OpenAI API๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด API ํค๊ฐ ํ์ํด์. openai.com์์ ๊ณ์ ์ ๋ง๋ค๊ณ API ํค๋ฅผ ๋ฐ๊ธ๋ฐ์ผ์ธ์. ๋ฌด๋ฃ ํฌ๋ ๋ง๋ ์ ๊ณต๋๋ ํ ์คํธํ๊ธฐ ์ข์์! ๐
๐๏ธ ์ํคํ ์ฒ ์ค๊ณ: ์ ์ฒด ๊ตฌ์กฐ ์ดํดํ๊ธฐ
์ฝ๋๋ฅผ ์์ฑํ๊ธฐ ์ ์ ์ ์ฒด ๊ตฌ์กฐ๋ฅผ ๋จผ์ ์ดํดํด๋ด ์๋ค. ์ฐ๋ฆฌ AI ๋์ฐ๋ฏธ๋ ํฌ๊ฒ 5๊ฐ์ ๋ชจ๋๋ก ๊ตฌ์ฑ๋ ๊ฑฐ์์:
๐จ ์์คํ ๊ตฌ์ฑ์์:
| ๋ชจ๋ | ์ญํ | ํต์ฌ ๊ธฐ๋ฅ |
|---|---|---|
| ์ฝ๋ ํ์ | ์ฝ๋ ๋ถ์ | AST ํ์ฑ, ๊ตฌ์กฐ ๋ถ์, ๋ฉํธ๋ฆญ ์ถ์ถ |
| ์ ์ ๋ถ์๊ธฐ | ๊ธฐ๋ณธ ๊ฒ์ฌ | ๋ฌธ๋ฒ ์ค๋ฅ, ์คํ์ผ ์๋ฐ, ๋ณต์ก๋ ์ธก์ |
| AI ์์ง | ์ง๋ฅํ ๋ฆฌ๋ทฐ | GPT/Claude ํ์ฉ, ์ปจํ ์คํธ ์ดํด, ์ ์ ์์ฑ |
| ๋ฆฌํฌํธ ์์ฑ๊ธฐ | ๊ฒฐ๊ณผ ์ถ๋ ฅ | ๋งํฌ๋ค์ด/HTML ๋ฆฌํฌํธ, ์ฐ์ ์์ ์ ๋ ฌ |
| ์ธํฐํ์ด์ค | ์ฌ์ฉ์ ์ํธ์์ฉ | CLI/์น UI, ์ค์ ๊ด๋ฆฌ |
์ด์ ๊ฐ ๋ชจ๋์ ํ๋์ฉ ๊ตฌํํด๋ณผ๊น์? ๐
๐ 1๋จ๊ณ: ์ฝ๋ ํ์ ๋ง๋ค๊ธฐ
์ฒซ ๋ฒ์งธ๋ก ๋ง๋ค ๊ฑด ์ฝ๋ ํ์์์. ํ์ด์ฌ์ ast (Abstract Syntax Tree) ๋ชจ๋์ ์ฌ์ฉํ๋ฉด ์ฝ๋๋ฅผ ๊ตฌ์กฐ์ ์ผ๋ก ๋ถ์ํ ์ ์์ด์.
import ast
import os
from typing import Dict, List, Any
from dataclasses import dataclass
@dataclass
class CodeMetrics:
"""์ฝ๋ ๋ฉํธ๋ฆญ์ ์ ์ฅํ๋ ๋ฐ์ดํฐ ํด๋์ค"""
total_lines: int
code_lines: int
comment_lines: int
blank_lines: int
functions: List[str]
classes: List[str]
imports: List[str]
complexity_score: float
class CodeParser:
"""ํ์ด์ฌ ์ฝ๋๋ฅผ ํ์ฑํ๊ณ ๋ถ์ํ๋ ํด๋์ค"""
def __init__(self, code: str):
self.code = code
self.tree = None
self.metrics = None
def parse(self) -> bool:
"""์ฝ๋๋ฅผ AST๋ก ํ์ฑ"""
try:
self.tree = ast.parse(self.code)
return True
except SyntaxError as e:
print(f"โ ๋ฌธ๋ฒ ์ค๋ฅ ๋ฐ๊ฒฌ: {e}")
return False
def extract_metrics(self) -> CodeMetrics:
"""์ฝ๋์์ ๋ค์ํ ๋ฉํธ๋ฆญ์ ์ถ์ถ"""
if not self.tree:
raise ValueError("๋จผ์ parse()๋ฅผ ํธ์ถํด์ฃผ์ธ์!")
lines = self.code.split('\n')
total_lines = len(lines)
blank_lines = sum(1 for line in lines if not line.strip())
comment_lines = sum(1 for line in lines if line.strip().startswith('#'))
code_lines = total_lines - blank_lines - comment_lines
# ํจ์์ ํด๋์ค ์ถ์ถ
functions = []
classes = []
imports = []
for node in ast.walk(self.tree):
if isinstance(node, ast.FunctionDef):
functions.append(node.name)
elif isinstance(node, ast.ClassDef):
classes.append(node.name)
elif isinstance(node, (ast.Import, ast.ImportFrom)):
if isinstance(node, ast.Import):
imports.extend([alias.name for alias in node.names])
else:
imports.append(node.module)
# ๊ฐ๋จํ ๋ณต์ก๋ ์ ์ ๊ณ์ฐ
complexity = self._calculate_complexity()
self.metrics = CodeMetrics(
total_lines=total_lines,
code_lines=code_lines,
comment_lines=comment_lines,
blank_lines=blank_lines,
functions=functions,
classes=classes,
imports=imports,
complexity_score=complexity
)
return self.metrics
def _calculate_complexity(self) -> float:
"""์ํ ๋ณต์ก๋ ๊ณ์ฐ (๊ฐ๋จํ ๋ฒ์ )"""
complexity = 1 # ๊ธฐ๋ณธ ๋ณต์ก๋
for node in ast.walk(self.tree):
# ๋ถ๊ธฐ๋ฌธ๋ง๋ค ๋ณต์ก๋ ์ฆ๊ฐ
if isinstance(node, (ast.If, ast.While, ast.For)):
complexity += 1
elif isinstance(node, ast.ExceptHandler):
complexity += 1
elif isinstance(node, ast.BoolOp):
complexity += len(node.values) - 1
return complexity
def get_function_info(self, func_name: str) -> Dict[str, Any]:
"""ํน์ ํจ์์ ์์ธ ์ ๋ณด ์ถ์ถ"""
for node in ast.walk(self.tree):
if isinstance(node, ast.FunctionDef) and node.name == func_name:
return {
'name': node.name,
'args': [arg.arg for arg in node.args.args],
'line_number': node.lineno,
'docstring': ast.get_docstring(node),
'decorators': [d.id for d in node.decorator_list if isinstance(d, ast.Name)]
}
return None
# ์ฌ์ฉ ์์
if __name__ == "__main__":
sample_code = """
def calculate_sum(numbers):
'''์ซ์ ๋ฆฌ์คํธ์ ํฉ์ ๊ณ์ฐํฉ๋๋ค'''
total = 0
for num in numbers:
if num > 0:
total += num
return total
class DataProcessor:
def __init__(self):
self.data = []
def process(self):
pass
"""
parser = CodeParser(sample_code)
if parser.parse():
metrics = parser.extract_metrics()
print(f"๐ ์ฝ๋ ๋ฉํธ๋ฆญ:")
print(f" ์ ์ฒด ๋ผ์ธ: {metrics.total_lines}")
print(f" ์ฝ๋ ๋ผ์ธ: {metrics.code_lines}")
print(f" ํจ์: {', '.join(metrics.functions)}")
print(f" ํด๋์ค: {', '.join(metrics.classes)}")
print(f" ๋ณต์ก๋: {metrics.complexity_score}")
๐ ํ์ต ํฌ์ธํธ: AST(Abstract Syntax Tree)๋ ์ฝ๋๋ฅผ ํธ๋ฆฌ ๊ตฌ์กฐ๋ก ํํํ ๊ฑฐ์์. ๋ง์น ๋ฌธ์ฅ์ ์ฃผ์ด, ๋์ฌ, ๋ชฉ์ ์ด๋ก ๋ถํดํ๋ ๊ฒ์ฒ๋ผ ์ฝ๋๋ฅผ ๊ตฌ์กฐ์ ์ผ๋ก ์ดํดํ ์ ์๊ฒ ํด์ค์. ์ด๊ฑธ ํ์ฉํ๋ฉด ๋จ์ํ ํ ์คํธ๋ก ์ฝ๋๋ฅผ ๋ณด๋ ๊ฒ ์๋๋ผ, ์๋ฏธ ๋จ์๋ก ๋ถ์ํ ์ ์๋ต๋๋ค! ๐ณ
๐ฌ 2๋จ๊ณ: ์ ์ ๋ถ์๊ธฐ ๊ตฌ์ถํ๊ธฐ
์ด์ ์ค์ ๋ก ์ฝ๋์ ๋ฌธ์ ์ ์ ์ฐพ์๋ด๋ ์ ์ ๋ถ์๊ธฐ๋ฅผ ๋ง๋ค์ด๋ด ์๋ค. Pylint์ ๊ฐ์ ๋๊ตฌ๋ค์ ํ์ฉํ ๊ฑฐ์์.
from pylint import lint
from pylint.reporters.text import TextReporter
from io import StringIO
import subprocess
import json
from typing import List, Dict
from dataclasses import dataclass
@dataclass
class CodeIssue:
"""์ฝ๋ ์ด์๋ฅผ ํํํ๋ ๋ฐ์ดํฐ ํด๋์ค"""
severity: str # error, warning, info
line: int
column: int
message: str
rule: str
category: str
class StaticAnalyzer:
"""์ ์ ๋ถ์์ ์ํํ๋ ํด๋์ค"""
def __init__(self, code: str, filename: str = "temp.py"):
self.code = code
self.filename = filename
self.issues: List[CodeIssue] = []
def run_pylint(self) -> List[CodeIssue]:
"""Pylint๋ฅผ ์คํํ์ฌ ์ฝ๋ ํ์ง ๊ฒ์ฌ"""
# ์์ ํ์ผ์ ์ฝ๋ ์ ์ฅ
with open(self.filename, 'w', encoding='utf-8') as f:
f.write(self.code)
# Pylint ์คํ
pylint_output = StringIO()
reporter = TextReporter(pylint_output)
try:
lint.Run([self.filename, '--output-format=json'],
reporter=reporter, exit=False)
except Exception as e:
print(f"โ ๏ธ Pylint ์คํ ์ค ์ค๋ฅ: {e}")
return []
# ๊ฒฐ๊ณผ ํ์ฑ
try:
results = json.loads(pylint_output.getvalue())
for item in results:
issue = CodeIssue(
severity=item['type'],
line=item['line'],
column=item['column'],
message=item['message'],
rule=item['symbol'],
category='style'
)
self.issues.append(issue)
except:
pass
return self.issues
def check_naming_conventions(self) -> List[CodeIssue]:
"""๋ค์ด๋ฐ ์ปจ๋ฒค์
๊ฒ์ฌ"""
import re
issues = []
lines = self.code.split('\n')
# ํจ์๋ช
๊ฒ์ฌ (snake_case)
func_pattern = re.compile(r'def\s+([A-Z][a-zA-Z0-9]*)\s*\(')
# ํด๋์ค๋ช
๊ฒ์ฌ (PascalCase)
class_pattern = re.compile(r'class\s+([a-z][a-zA-Z0-9]*)\s*[:\(]')
for i, line in enumerate(lines, 1):
# ํจ์๋ช
์ด ๋๋ฌธ์๋ก ์์ํ๋ ๊ฒฝ์ฐ
func_match = func_pattern.search(line)
if func_match:
issues.append(CodeIssue(
severity='warning',
line=i,
column=0,
message=f"ํจ์๋ช
'{func_match.group(1)}'์ snake_case๋ฅผ ์ฌ์ฉํ์ธ์",
rule='naming-convention',
category='style'
))
# ํด๋์ค๋ช
์ด ์๋ฌธ์๋ก ์์ํ๋ ๊ฒฝ์ฐ
class_match = class_pattern.search(line)
if class_match:
issues.append(CodeIssue(
severity='warning',
line=i,
column=0,
message=f"ํด๋์ค๋ช
'{class_match.group(1)}'์ PascalCase๋ฅผ ์ฌ์ฉํ์ธ์",
rule='naming-convention',
category='style'
))
self.issues.extend(issues)
return issues
def check_complexity(self, parser) -> List[CodeIssue]:
"""๋ณต์ก๋ ๊ฒ์ฌ"""
issues = []
if parser.metrics and parser.metrics.complexity_score > 10:
issues.append(CodeIssue(
severity='warning',
line=0,
column=0,
message=f"์ฝ๋ ๋ณต์ก๋๊ฐ ๋์ต๋๋ค (์ ์: {parser.metrics.complexity_score}). ๋ฆฌํฉํ ๋ง์ ๊ณ ๋ คํ์ธ์.",
rule='high-complexity',
category='maintainability'
))
self.issues.extend(issues)
return issues
def check_code_smells(self) -> List[CodeIssue]:
"""์ผ๋ฐ์ ์ธ ์ฝ๋ ์ค๋ฉ ๊ฒ์ฌ"""
issues = []
lines = self.code.split('\n')
for i, line in enumerate(lines, 1):
# ๋๋ฌด ๊ธด ๋ผ์ธ
if len(line) > 120:
issues.append(CodeIssue(
severity='info',
line=i,
column=0,
message=f"๋ผ์ธ์ด ๋๋ฌด ๊น๋๋ค ({len(line)}์). 120์ ์ด๋ด๋ก ์ค์ด์ธ์.",
rule='line-too-long',
category='style'
))
# TODO/FIXME ์ฃผ์
if 'TODO' in line or 'FIXME' in line:
issues.append(CodeIssue(
severity='info',
line=i,
column=0,
message="๋ฏธ์์ฑ ์์
์ด ์์ต๋๋ค.",
rule='todo-found',
category='maintenance'
))
# ํ๋์ฝ๋ฉ๋ ๋น๋ฐ๋ฒํธ๋ ํค
if any(keyword in line.lower() for keyword in ['password', 'api_key', 'secret']):
if '=' in line and ('"' in line or "'" in line):
issues.append(CodeIssue(
severity='error',
line=i,
column=0,
message="ํ๋์ฝ๋ฉ๋ ๋น๋ฐ์ ๋ณด๊ฐ ์์ฌ๋ฉ๋๋ค. ํ๊ฒฝ๋ณ์๋ฅผ ์ฌ์ฉํ์ธ์.",
rule='hardcoded-secret',
category='security'
))
self.issues.extend(issues)
return issues
def run_all_checks(self, parser=None) -> List[CodeIssue]:
"""๋ชจ๋ ๊ฒ์ฌ ์คํ"""
print("๐ ์ ์ ๋ถ์ ์์...")
self.check_naming_conventions()
self.check_code_smells()
if parser:
self.check_complexity(parser)
# ์ฌ๊ฐ๋๋ณ๋ก ์ ๋ ฌ
self.issues.sort(key=lambda x: {'error': 0, 'warning': 1, 'info': 2}[x.severity])
print(f"โ
๋ถ์ ์๋ฃ: {len(self.issues)}๊ฐ์ ์ด์ ๋ฐ๊ฒฌ")
return self.issues
# ์ฌ์ฉ ์์
if __name__ == "__main__":
sample_code = """
def CalculateTotal(items): # ์๋ชป๋ ๋ค์ด๋ฐ
password = "mypassword123" # ๋ณด์ ์ด์
total = 0
for item in items:
if item > 0:
if item < 100:
if item % 2 == 0: # ๋ณต์ก๋ ์ฆ๊ฐ
total += item
return total
"""
analyzer = StaticAnalyzer(sample_code)
issues = analyzer.run_all_checks()
for issue in issues:
print(f"\n{issue.severity.upper()} (Line {issue.line}): {issue.message}")
โ ๏ธ ๋ณด์ ์ฃผ์์ฌํญ: ์ฝ๋์ ๋น๋ฐ๋ฒํธ๋ API ํค๋ฅผ ์ง์ ์์ฑํ๋ ๊ฑด ์ ๋ง ์ํํด์! ํญ์ ํ๊ฒฝ๋ณ์๋ ๋ณ๋์ ์ค์ ํ์ผ์ ์ฌ์ฉํ์ธ์. ํนํ Git์ ์ปค๋ฐํ๊ธฐ ์ ์๋ ๋ฐ๋์ ํ์ธํด์ผ ํฉ๋๋ค. ๐
๐ค 3๋จ๊ณ: AI ์์ง ํตํฉํ๊ธฐ
์ด์ ๊ฐ์ฅ ํต์ฌ์ ์ธ ๋ถ๋ถ์ด์์! OpenAI์ GPT๋ Anthropic์ Claude๋ฅผ ํ์ฉํด์ ์ง๋ฅํ ์ฝ๋ ๋ฆฌ๋ทฐ๋ฅผ ๊ตฌํํด๋ด ์๋ค. ๐
import openai
from typing import List, Dict, Optional
import os
from dotenv import load_dotenv
import json
# ํ๊ฒฝ๋ณ์ ๋ก๋
load_dotenv()
class AIReviewEngine:
"""AI๋ฅผ ํ์ฉํ ์ฝ๋ ๋ฆฌ๋ทฐ ์์ง"""
def __init__(self, api_key: Optional[str] = None, model: str = "gpt-4"):
self.api_key = api_key or os.getenv("OPENAI_API_KEY")
self.model = model
openai.api_key = self.api_key
if not self.api_key:
raise ValueError("OpenAI API ํค๊ฐ ํ์ํฉ๋๋ค!")
def create_review_prompt(self, code: str, metrics: Dict, issues: List) -> str:
"""์ฝ๋ ๋ฆฌ๋ทฐ๋ฅผ ์ํ ํ๋กฌํํธ ์์ฑ"""
# ์ด์ ์์ฝ
issue_summary = "\n".join([
f"- [{issue.severity}] Line {issue.line}: {issue.message}"
for issue in issues[:10] # ์์ 10๊ฐ๋ง
])
prompt = f"""
๋น์ ์ ๊ฒฝํ ๋ง์ ์๋์ด ๊ฐ๋ฐ์์
๋๋ค. ๋ค์ ํ์ด์ฌ ์ฝ๋๋ฅผ ๋ฆฌ๋ทฐํด์ฃผ์ธ์.
## ์ฝ๋:
```python
{code}
```
## ์ฝ๋ ๋ฉํธ๋ฆญ:
- ์ ์ฒด ๋ผ์ธ: {metrics.get('total_lines', 'N/A')}
- ์ฝ๋ ๋ผ์ธ: {metrics.get('code_lines', 'N/A')}
- ํจ์ ๊ฐ์: {len(metrics.get('functions', []))}
- ํด๋์ค ๊ฐ์: {len(metrics.get('classes', []))}
- ๋ณต์ก๋ ์ ์: {metrics.get('complexity_score', 'N/A')}
## ๋ฐ๊ฒฌ๋ ์ด์:
{issue_summary if issue_summary else '์ด์ ์์'}
## ๋ฆฌ๋ทฐ ์์ฒญ์ฌํญ:
๋ค์ ๊ด์ ์์ ์ฝ๋๋ฅผ ๋ฆฌ๋ทฐํด์ฃผ์ธ์:
1. **์ฝ๋ ํ์ง**: ๊ฐ๋
์ฑ, ์ ์ง๋ณด์์ฑ, ์ผ๊ด์ฑ
2. **์ค๊ณ**: ๊ตฌ์กฐ, ํจํด ์ฌ์ฉ, ๋ชจ๋ํ
3. **์ฑ๋ฅ**: ๋นํจ์จ์ ์ธ ๋ถ๋ถ, ์ต์ ํ ๊ฐ๋ฅ ์์ญ
4. **๋ณด์**: ์ ์ฌ์ ๋ณด์ ์ทจ์ฝ์
5. **๋ฒ ์คํธ ํ๋ํฐ์ค**: ํ์ด์ฌ ๊ด๋ก ์ค์ ์ฌ๋ถ
๊ฐ ํญ๋ชฉ์ ๋ํด:
- ๐ ์ํ ์
- ๐ง ๊ฐ์ ์ด ํ์ํ ์
- ๐ก ๊ตฌ์ฒด์ ์ธ ๊ฐ์ ์ ์ (์ฝ๋ ์์ ํฌํจ)
์น์ ํ๊ณ ๊ฑด์ค์ ์ธ ํค์ผ๋ก ์์ฑํด์ฃผ์ธ์.
"""
return prompt
def get_review(self, code: str, metrics: Dict, issues: List) -> Dict[str, any]:
"""AI ์ฝ๋ ๋ฆฌ๋ทฐ ์คํ"""
print("๐ค AI ๋ฆฌ๋ทฐ ์์ฑ ์ค...")
prompt = self.create_review_prompt(code, metrics, issues)
try:
response = openai.ChatCompletion.create(
model=self.model,
messages=[
{
"role": "system",
"content": "๋น์ ์ ์น์ ํ๊ณ ๊ฒฝํ ๋ง์ ์๋์ด ๊ฐ๋ฐ์์
๋๋ค. ์ฝ๋ ๋ฆฌ๋ทฐ๋ฅผ ํตํด ์ฃผ๋์ด ๊ฐ๋ฐ์๋ค์ ์ฑ์ฅ์ ๋์ต๋๋ค."
},
{
"role": "user",
"content": prompt
}
],
temperature=0.7,
max_tokens=2000
)
review_text = response.choices[0].message.content
return {
'success': True,
'review': review_text,
'model': self.model,
'tokens_used': response.usage.total_tokens
}
except Exception as e:
print(f"โ AI ๋ฆฌ๋ทฐ ์์ฑ ์คํจ: {e}")
return {
'success': False,
'error': str(e)
}
def get_specific_suggestion(self, code_snippet: str, issue: str) -> str:
"""ํน์ ์ฝ๋ ๋ถ๋ถ์ ๋ํ ๊ฐ์ ์ ์"""
prompt = f"""
๋ค์ ์ฝ๋์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค:
```python
{code_snippet}
```
๋ฌธ์ : {issue}
์ด ์ฝ๋๋ฅผ ๊ฐ์ ํ ๋ฒ์ ์ ์์ฑํ๊ณ , ์ ๋ ๋์์ง ์ค๋ช
ํด์ฃผ์ธ์.
"""
try:
response = openai.ChatCompletion.create(
model=self.model,
messages=[
{"role": "user", "content": prompt}
],
temperature=0.5,
max_tokens=500
)
return response.choices[0].message.content
except Exception as e:
return f"์ ์ ์์ฑ ์คํจ: {e}"
def explain_concept(self, concept: str) -> str:
"""์ฝ๋ ๊ฐ๋
์ด๋ ํจํด ์ค๋ช
"""
prompt = f"""
'{concept}'์ ๋ํด ํ์ด์ฌ ์ด๋ณด์๋ ์ดํดํ ์ ์๊ฒ ์ฝ๊ฒ ์ค๋ช
ํด์ฃผ์ธ์.
๊ฐ๋จํ ์ฝ๋ ์์๋ ํฌํจํด์ฃผ์ธ์.
"""
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", # ์ค๋ช
์ ๋ ์ ๋ ดํ ๋ชจ๋ธ ์ฌ์ฉ
messages=[
{"role": "user", "content": prompt}
],
temperature=0.7,
max_tokens=400
)
return response.choices[0].message.content
except Exception as e:
return f"์ค๋ช
์์ฑ ์คํจ: {e}"
class SmartReviewAssistant:
"""ํตํฉ ๋ฆฌ๋ทฐ ์ด์์คํดํธ"""
def __init__(self, api_key: Optional[str] = None):
self.parser = None
self.analyzer = None
self.ai_engine = AIReviewEngine(api_key)
def review_code(self, code: str) -> Dict:
"""์ ์ฒด ์ฝ๋ ๋ฆฌ๋ทฐ ํ๋ก์ธ์ค ์คํ"""
print("=" * 60)
print("๐ฏ ์ฝ๋ ๋ฆฌ๋ทฐ ์์!")
print("=" * 60)
# 1. ์ฝ๋ ํ์ฑ
print("\n๐ 1๋จ๊ณ: ์ฝ๋ ํ์ฑ...")
self.parser = CodeParser(code)
if not self.parser.parse():
return {'success': False, 'error': '์ฝ๋ ํ์ฑ ์คํจ'}
metrics = self.parser.extract_metrics()
print(f"โ
ํ์ฑ ์๋ฃ: {metrics.code_lines}์ค์ ์ฝ๋ ๋ถ์")
# 2. ์ ์ ๋ถ์
print("\n๐ 2๋จ๊ณ: ์ ์ ๋ถ์...")
self.analyzer = StaticAnalyzer(code)
issues = self.analyzer.run_all_checks(self.parser)
print(f"โ
๋ถ์ ์๋ฃ: {len(issues)}๊ฐ์ ์ด์ ๋ฐ๊ฒฌ")
# 3. AI ๋ฆฌ๋ทฐ
print("\n๐ค 3๋จ๊ณ: AI ๋ฆฌ๋ทฐ ์์ฑ...")
metrics_dict = {
'total_lines': metrics.total_lines,
'code_lines': metrics.code_lines,
'functions': metrics.functions,
'classes': metrics.classes,
'complexity_score': metrics.complexity_score
}
ai_review = self.ai_engine.get_review(code, metrics_dict, issues)
if ai_review['success']:
print(f"โ
AI ๋ฆฌ๋ทฐ ์๋ฃ (ํ ํฐ ์ฌ์ฉ: {ai_review['tokens_used']})")
else:
print(f"โ AI ๋ฆฌ๋ทฐ ์คํจ: {ai_review['error']}")
# 4. ๊ฒฐ๊ณผ ํตํฉ
return {
'success': True,
'metrics': metrics_dict,
'issues': issues,
'ai_review': ai_review.get('review', ''),
'tokens_used': ai_review.get('tokens_used', 0)
}
# ์ฌ์ฉ ์์
if __name__ == "__main__":
sample_code = """
def process_data(data):
result = []
for item in data:
if item > 0:
result.append(item * 2)
return result
class UserManager:
def __init__(self):
self.users = []
def add_user(self, user):
self.users.append(user)
"""
# API ํค๋ ํ๊ฒฝ๋ณ์์์ ์๋์ผ๋ก ๋ก๋๋จ
assistant = SmartReviewAssistant()
review_result = assistant.review_code(sample_code)
if review_result['success']:
print("\n" + "=" * 60)
print("๐ ๋ฆฌ๋ทฐ ๊ฒฐ๊ณผ")
print("=" * 60)
print(review_result['ai_review'])
๐ฐ ๋น์ฉ ๊ด๋ฆฌ ํ: OpenAI API๋ ์ฌ์ฉํ ํ ํฐ๋งํผ ๋น์ฉ์ด ๋ฐ์ํด์. GPT-4๋ ๋น์ธ๋๊น ๊ฐ๋ฐ ์ค์๋ GPT-3.5-turbo๋ฅผ ์ฌ์ฉํ๊ณ , ์ต์ข ๋ฆฌ๋ทฐ์๋ง GPT-4๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๋ ์ข์ ์ ๋ต์ด์์. ๋ํ ํ๋กฌํํธ๋ฅผ ๊ฐ๊ฒฐํ๊ฒ ์ ์งํ๋ฉด ํ ํฐ์ ์ ์ฝํ ์ ์๋ต๋๋ค! ๐ธ
๐ 4๋จ๊ณ: ๋ฆฌํฌํธ ์์ฑ๊ธฐ ๋ง๋ค๊ธฐ
์ด์ ๋ฆฌ๋ทฐ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ธฐ ์ข๊ฒ ์ ๋ฆฌํด์ ๋ณด์ฌ์ฃผ๋ ๋ฆฌํฌํธ ์์ฑ๊ธฐ๋ฅผ ๋ง๋ค์ด๋ด ์๋ค!
from datetime import datetime
from typing import Dict, List
import markdown
import os
class ReportGenerator:
"""์ฝ๋ ๋ฆฌ๋ทฐ ๋ฆฌํฌํธ ์์ฑ๊ธฐ"""
def __init__(self):
self.template = None
def generate_markdown_report(self, review_result: Dict, filename: str = "code_review_report.md") -> str:
"""๋งํฌ๋ค์ด ํ์์ ๋ฆฌํฌํธ ์์ฑ"""
metrics = review_result['metrics']
issues = review_result['issues']
ai_review = review_result['ai_review']
# ์ด์ ํต๊ณ
error_count = sum(1 for i in issues if i.severity == 'error')
warning_count = sum(1 for i in issues if i.severity == 'warning')
info_count = sum(1 for i in issues if i.severity == 'info')
report = f"""# ๐ฏ ์ฝ๋ ๋ฆฌ๋ทฐ ๋ฆฌํฌํธ
**์์ฑ ์๊ฐ**: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
---
## ๐ ์ฝ๋ ๋ฉํธ๋ฆญ
| ํญ๋ชฉ | ๊ฐ |
|------|-----|
| ์ ์ฒด ๋ผ์ธ ์ | {metrics['total_lines']} |
| ์ฝ๋ ๋ผ์ธ ์ | {metrics['code_lines']} |
| ํจ์ ๊ฐ์ | {len(metrics['functions'])} |
| ํด๋์ค ๊ฐ์ | {len(metrics['classes'])} |
| ๋ณต์ก๋ ์ ์ | {metrics['complexity_score']:.1f} |
### ๋ฐ๊ฒฌ๋ ํจ์๋ค
{', '.join([f'`{f}`' for f in metrics['functions']]) if metrics['functions'] else '์์'}
### ๋ฐ๊ฒฌ๋ ํด๋์ค๋ค
{', '.join([f'`{c}`' for c in metrics['classes']]) if metrics['classes'] else '์์'}
---
## ๐ ์ ์ ๋ถ์ ๊ฒฐ๊ณผ
**์ด {len(issues)}๊ฐ์ ์ด์ ๋ฐ๊ฒฌ**
- ๐ด ์๋ฌ: {error_count}๊ฐ
- ๐ก ๊ฒฝ๊ณ : {warning_count}๊ฐ
- ๐ต ์ ๋ณด: {info_count}๊ฐ
### ์์ธ ์ด์ ๋ชฉ๋ก
"""
# ์ด์ ์์ธ ๋ด์ญ
for issue in issues:
emoji = {'error': '๐ด', 'warning': '๐ก', 'info': '๐ต'}[issue.severity]
report += f"""
#### {emoji} {issue.severity.upper()} - Line {issue.line}
- **๋ฉ์์ง**: {issue.message}
- **๊ท์น**: `{issue.rule}`
- **์นดํ
๊ณ ๋ฆฌ**: {issue.category}
"""
# AI ๋ฆฌ๋ทฐ
report += f"""
---
## ๐ค AI ์ฝ๋ ๋ฆฌ๋ทฐ
{ai_review}
---
## ๐ ๋ค์ ๋จ๊ณ
1. ๐ด ์๋ฌ ์์ค์ ์ด์๋ถํฐ ํด๊ฒฐํ์ธ์
2. ๐ก ๊ฒฝ๊ณ ์์ค์ ์ด์๋ฅผ ๊ฒํ ํ์ธ์
3. ๐ก AI ์ ์์ฌํญ์ ์ฐธ๊ณ ํ์ฌ ์ฝ๋๋ฅผ ๊ฐ์ ํ์ธ์
4. โ
๊ฐ์ ํ ๋ค์ ๋ฆฌ๋ทฐ๋ฅผ ์คํํ์ธ์
---
*์ด ๋ฆฌํฌํธ๋ AI ์ฝ๋ ๋ฆฌ๋ทฐ ๋์ฐ๋ฏธ์ ์ํด ์๋ ์์ฑ๋์์ต๋๋ค.*
"""
# ํ์ผ๋ก ์ ์ฅ
with open(filename, 'w', encoding='utf-8') as f:
f.write(report)
print(f"๐ ๋ฆฌํฌํธ๊ฐ '{filename}'์ ์ ์ฅ๋์์ต๋๋ค!")
return report
def generate_html_report(self, review_result: Dict, filename: str = "code_review_report.html") -> str:
"""HTML ํ์์ ๋ฆฌํฌํธ ์์ฑ"""
# ๋จผ์ ๋งํฌ๋ค์ด ์์ฑ
md_report = self.generate_markdown_report(review_result, "temp.md")
# ๋งํฌ๋ค์ด์ HTML๋ก ๋ณํ
html_content = markdown.markdown(md_report, extensions=['tables', 'fenced_code'])
# HTML ํ
ํ๋ฆฟ
html_template = f"""
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>์ฝ๋ ๋ฆฌ๋ทฐ ๋ฆฌํฌํธ</title>
<style>.tx-header{background:linear-gradient(135deg,#667eea 0%,#764ba2 100%);
color:#fff;
padding:40px 30px;
border-radius:16px;
text-align:center;
margin-bottom:40px;
box-shadow:0 8px 24px rgba(102,126,234,0.25);}
.tx-intro{background:#f8f9fa;
padding:25px;
border-radius:12px;
margin:30px 0;
border-left:5px solid #667eea;}
.tx-section{margin:40px 0;}
.tx-highlight{background:linear-gradient(120deg,#fef3c7 0%,#fde68a 100%);
padding:3px 8px;
border-radius:4px;
font-weight:600;}
.tx-box{background:#ffffff;
border:2px solid #e5e7eb;
padding:20px;
border-radius:12px;
margin:25px 0;
box-shadow:0 2px 8px rgba(0,0,0,0.05);}
.tx-tip{background:#ecfdf5;
border-left:4px solid #10b981;
padding:18px;
margin:25px 0;
border-radius:8px;}
.tx-warning{background:#fef2f2;
border-left:4px solid #ef4444;
padding:18px;
margin:25px 0;
border-radius:8px;}
.tx-step{background:#f0f9ff;
border-left:4px solid #3b82f6;
padding:18px;
margin:25px 0;
border-radius:8px;}
.tx-table{width:100%;
border-collapse:collapse;
margin:25px 0;}
.tx-table th{background:#667eea;
color:#fff;
padding:12px;
text-align:left;
font-weight:600;}
.tx-table td{padding:12px;
border-bottom:1px solid #e5e7eb;}
.tx-table tr:hover{background:#f9fafb;}
.tx-badge{display:inline-block;
background:#8b5cf6;
color:#fff;
padding:4px 12px;
border-radius:20px;
font-size:0.85em;
margin:5px;}
.tx-svg-container{text-align:center;
margin:40px 0;}</style>
</head>
<body>
<div class="container">
{html_content}
</div>
</body>
</html>
"""
# HTML ํ์ผ๋ก ์ ์ฅ
with open(filename, 'w', encoding='utf-8') as f:
f.write(html_template)
# ์์ ๋งํฌ๋ค์ด ํ์ผ ์ญ์
if os.path.exists("temp.md"):
os.remove("temp.md")
print(f"๐ HTML ๋ฆฌํฌํธ๊ฐ '{filename}'์ ์ ์ฅ๋์์ต๋๋ค!")
return html_template
def generate_summary(self, review_result: Dict) -> str:
"""๊ฐ๋จํ ์์ฝ ์์ฑ"""
metrics = review_result['metrics']
issues = review_result['issues']
error_count = sum(1 for i in issues if i.severity == 'error')
warning_count = sum(1 for i in issues if i.severity == 'warning')
summary = f"""
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ ์ฝ๋ ๋ฆฌ๋ทฐ ์์ฝ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ ์ฝ๋ ๋ผ์ธ: {metrics['code_lines']}์ค
๐ง ํจ์: {len(metrics['functions'])}๊ฐ
๐ฆ ํด๋์ค: {len(metrics['classes'])}๊ฐ
๐ ๋ณต์ก๋: {metrics['complexity_score']:.1f}
๐ ๋ฐ๊ฒฌ๋ ์ด์:
๐ด ์๋ฌ: {error_count}๊ฐ
๐ก ๊ฒฝ๊ณ : {warning_count}๊ฐ
๐ต ์ ๋ณด: {len(issues) - error_count - warning_count}๊ฐ
{'โ
์ฝ๋ ํ์ง์ด ์ฐ์ํฉ๋๋ค!' if error_count == 0 and warning_count == 0 else '๐ง ๊ฐ์ ์ด ํ์ํฉ๋๋ค.'}
"""
return summary
# ์ฌ์ฉ ์์
if __name__ == "__main__":
# ๊ฐ์์ ๋ฆฌ๋ทฐ ๊ฒฐ๊ณผ
sample_result = {
'metrics': {
'total_lines': 50,
'code_lines': 35,
'functions': ['calculate_sum', 'process_data'],
'classes': ['DataProcessor'],
'complexity_score': 8.5
},
'issues': [
CodeIssue('error', 10, 0, '๋ณ์๊ฐ ์ ์๋์ง ์์์ต๋๋ค', 'undefined-variable', 'error'),
CodeIssue('warning', 15, 0, 'ํจ์๊ฐ ๋๋ฌด ๊น๋๋ค', 'function-too-long', 'style')
],
'ai_review': '์ ๋ฐ์ ์ผ๋ก ์ฝ๋ ๊ตฌ์กฐ๊ฐ ์ข์ต๋๋ค. ๋ค๋ง ์๋ฌ ์ฒ๋ฆฌ๋ฅผ ์ถ๊ฐํ๋ฉด ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค.'
}
generator = ReportGenerator()
# ์์ฝ ์ถ๋ ฅ
print(generator.generate_summary(sample_result))
# ๋งํฌ๋ค์ด ๋ฆฌํฌํธ ์์ฑ
generator.generate_markdown_report(sample_result)
# HTML ๋ฆฌํฌํธ ์์ฑ
generator.generate_html_report(sample_result)
๐ฅ๏ธ 5๋จ๊ณ: ์ฌ์ฉ์ ์ธํฐํ์ด์ค ๊ตฌ์ถํ๊ธฐ
์ด์ ๋ชจ๋ ๊ธฐ๋ฅ์ ํตํฉํด์ ์ค์ ๋ก ์ฌ์ฉํ ์ ์๋ ์ธํฐํ์ด์ค๋ฅผ ๋ง๋ค์ด๋ด ์๋ค. CLI์ ๊ฐ๋จํ ์น ์ธํฐํ์ด์ค ๋ ๊ฐ์ง๋ฅผ ๋ง๋ค์ด๋ณผ๊ฒ์!
CLI (Command Line Interface) ๋ฒ์
import argparse
import sys
from pathlib import Path
class CodeReviewCLI:
"""๋ช
๋ น์ค ์ธํฐํ์ด์ค"""
def __init__(self):
self.assistant = None
self.report_generator = ReportGenerator()
def setup_argparse(self):
"""๋ช
๋ น์ค ์ธ์ ํ์ ์ค์ """
parser = argparse.ArgumentParser(
description='๐ค AI ์ฝ๋ ๋ฆฌ๋ทฐ ๋์ฐ๋ฏธ',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
์ฌ์ฉ ์์:
python cli.py review mycode.py
python cli.py review mycode.py --output html
python cli.py review mycode.py --model gpt-4
"""
)
subparsers = parser.add_subparsers(dest='command', help='๋ช
๋ น์ด')
# review ๋ช
๋ น์ด
review_parser = subparsers.add_parser('review', help='์ฝ๋ ๋ฆฌ๋ทฐ ์คํ')
review_parser.add_argument('file', help='๋ฆฌ๋ทฐํ ํ์ด์ฌ ํ์ผ')
review_parser.add_argument('--output', choices=['markdown', 'html', 'both'],
default='markdown', help='์ถ๋ ฅ ํ์')
review_parser.add_argument('--model', default='gpt-3.5-turbo',
help='์ฌ์ฉํ AI ๋ชจ๋ธ')
review_parser.add_argument('--no-ai', action='store_true',
help='AI ๋ฆฌ๋ทฐ ์์ด ์ ์ ๋ถ์๋ง ์คํ')
# explain ๋ช
๋ น์ด
explain_parser = subparsers.add_parser('explain', help='๊ฐ๋
์ค๋ช
')
explain_parser.add_argument('concept', help='์ค๋ช
ํ ๊ฐ๋
')
return parser
def run_review(self, args):
"""๋ฆฌ๋ทฐ ์คํ"""
# ํ์ผ ์ฝ๊ธฐ
file_path = Path(args.file)
if not file_path.exists():
print(f"โ ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค: {args.file}")
return
with open(file_path, 'r', encoding='utf-8') as f:
code = f.read()
print(f"\n๐ฏ '{args.file}' ๋ฆฌ๋ทฐ ์์...\n")
# ์ด์์คํดํธ ์ด๊ธฐํ
if args.no_ai:
# AI ์์ด ์ ์ ๋ถ์๋ง
parser = CodeParser(code)
parser.parse()
metrics = parser.extract_metrics()
analyzer = StaticAnalyzer(code)
issues = analyzer.run_all_checks(parser)
review_result = {
'metrics': {
'total_lines': metrics.total_lines,
'code_lines': metrics.code_lines,
'functions': metrics.functions,
'classes': metrics.classes,
'complexity_score': metrics.complexity_score
},
'issues': issues,
'ai_review': 'AI ๋ฆฌ๋ทฐ๊ฐ ๋นํ์ฑํ๋์์ต๋๋ค.'
}
else:
# ์ ์ฒด ๋ฆฌ๋ทฐ
self.assistant = SmartReviewAssistant()
review_result = self.assistant.review_code(code)
# ์์ฝ ์ถ๋ ฅ
print("\n" + self.report_generator.generate_summary(review_result))
# ๋ฆฌํฌํธ ์์ฑ
base_name = file_path.stem
if args.output in ['markdown', 'both']:
self.report_generator.generate_markdown_report(
review_result,
f"{base_name}_review.md"
)
if args.output in ['html', 'both']:
self.report_generator.generate_html_report(
review_result,
f"{base_name}_review.html"
)
print("\nโ
๋ฆฌ๋ทฐ ์๋ฃ!")
def run_explain(self, args):
"""๊ฐ๋
์ค๋ช
"""
if not self.assistant:
self.assistant = SmartReviewAssistant()
print(f"\n๐ก '{args.concept}' ์ค๋ช
:\n")
explanation = self.assistant.ai_engine.explain_concept(args.concept)
print(explanation)
def run(self):
"""CLI ์คํ"""
parser = self.setup_argparse()
args = parser.parse_args()
if not args.command:
parser.print_help()
return
if args.command == 'review':
self.run_review(args)
elif args.command == 'explain':
self.run_explain(args)
if __name__ == "__main__":
cli = CodeReviewCLI()
cli.run()
์น ์ธํฐํ์ด์ค (Flask) ๋ฒ์
from flask import Flask, render_template_string, request, jsonify
import json
app = Flask(__name__)
# HTML ํ
ํ๋ฆฟ
HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>๐ค AI ์ฝ๋ ๋ฆฌ๋ทฐ ๋์ฐ๋ฏธ</title>
<style>.tx-header{background:linear-gradient(135deg,#667eea 0%,#764ba2 100%);
color:#fff;
padding:40px 30px;
border-radius:16px;
text-align:center;
margin-bottom:40px;
box-shadow:0 8px 24px rgba(102,126,234,0.25);}
.tx-intro{background:#f8f9fa;
padding:25px;
border-radius:12px;
margin:30px 0;
border-left:5px solid #667eea;}
.tx-section{margin:40px 0;}
.tx-highlight{background:linear-gradient(120deg,#fef3c7 0%,#fde68a 100%);
padding:3px 8px;
border-radius:4px;
font-weight:600;}
.tx-box{background:#ffffff;
border:2px solid #e5e7eb;
padding:20px;
border-radius:12px;
margin:25px 0;
box-shadow:0 2px 8px rgba(0,0,0,0.05);}
.tx-tip{background:#ecfdf5;
border-left:4px solid #10b981;
padding:18px;
margin:25px 0;
border-radius:8px;}
.tx-warning{background:#fef2f2;
border-left:4px solid #ef4444;
padding:18px;
margin:25px 0;
border-radius:8px;}
.tx-step{background:#f0f9ff;
border-left:4px solid #3b82f6;
padding:18px;
margin:25px 0;
border-radius:8px;}
.tx-table{width:100%;
border-collapse:collapse;
margin:25px 0;}
.tx-table th{background:#667eea;
color:#fff;
padding:12px;
text-align:left;
font-weight:600;}
.tx-table td{padding:12px;
border-bottom:1px solid #e5e7eb;}
.tx-table tr:hover{background:#f9fafb;}
.tx-badge{display:inline-block;
background:#8b5cf6;
color:#fff;
padding:4px 12px;
border-radius:20px;
font-size:0.85em;
margin:5px;}
.tx-svg-container{text-align:center;
margin:40px 0;}</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>๐ค AI ์ฝ๋ ๋ฆฌ๋ทฐ ๋์ฐ๋ฏธ</h1>
<p>ํ์ด์ฌ ์ฝ๋๋ฅผ ์
๋ ฅํ๋ฉด AI๊ฐ ๋ฆฌ๋ทฐํด๋๋ฆฝ๋๋ค!</p>
</div>
<div class="content">
<div class="editor-section">
<div>
<h3>๐ ์ฝ๋ ์
๋ ฅ</h3>
<textarea id="codeInput" placeholder="์ฌ๊ธฐ์ ํ์ด์ฌ ์ฝ๋๋ฅผ ์
๋ ฅํ์ธ์...">def hello_world():
print("Hello, World!")
hello_world()</textarea>
</div>
<div>
<h3>โน๏ธ ์ค์ </h3>
<div style="padding: 15px;">
<label>
<input type="checkbox" id="useAI" checked>
AI ๋ฆฌ๋ทฐ ์ฌ์ฉ
</label>
<br><br>
<label>
AI ๋ชจ๋ธ:
<select id="modelSelect">
<option value="gpt-3.5-turbo">GPT-3.5 Turbo (๋น ๋ฆ)</option>
<option value="gpt-4">GPT-4 (์ ํํจ)</option>
</select>
</label>
</div>
</div>
</div>
<div class="button-group">
<button class="btn-primary" onclick="reviewCode()">
๐ ๋ฆฌ๋ทฐ ์์
</button>
<button class="btn-secondary" onclick="clearAll()">
๐๏ธ ์ด๊ธฐํ
</button>
</div>
<div class="loading" id="loading">
<div class="spinner"></div>
<p style="margin-top: 20px;">AI๊ฐ ์ฝ๋๋ฅผ ๋ถ์ํ๊ณ ์์ต๋๋ค...</p>
</div>
<div class="result-section" id="resultSection">
<h2>๐ ๋ฆฌ๋ทฐ ๊ฒฐ๊ณผ</h2>
<div id="resultContent"></div>
</div>
</div>
</div>
<script>
async function reviewCode() {
const code = document.getElementById('codeInput').value;
const useAI = document.getElementById('useAI').checked;
const model = document.getElementById('modelSelect').value;
if (!code.trim()) {
alert('์ฝ๋๋ฅผ ์
๋ ฅํด์ฃผ์ธ์!');
return;
}
// ๋ก๋ฉ ํ์
document.getElementById('loading').classList.add('show');
document.getElementById('resultSection').classList.remove('show');
try {
const response = await fetch('/api/review', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
code: code,
use_ai: useAI,
model: model
})
});
const result = await response.json();
// ๊ฒฐ๊ณผ ํ์
displayResult(result);
} catch (error) {
alert('๋ฆฌ๋ทฐ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: ' + error.message);
} finally {
document.getElementById('loading').classList.remove('show');
}
}
function displayResult(result) {
const resultContent = document.getElementById('resultContent');
const metrics = result.metrics;
const issues = result.issues;
let html = `
<div class="metric-card">
<h3>๐ ์ฝ๋ ๋ฉํธ๋ฆญ</h3>
<p>์ ์ฒด ๋ผ์ธ: ${metrics.total_lines} | ์ฝ๋ ๋ผ์ธ: ${metrics.code_lines}</p>
<p>ํจ์: ${metrics.functions.length}๊ฐ | ํด๋์ค: ${metrics.classes.length}๊ฐ</p>
<p>๋ณต์ก๋: ${metrics.complexity_score.toFixed(1)}</p>
</div>
<div class="metric-card">
<h3>๐ ๋ฐ๊ฒฌ๋ ์ด์ (${issues.length}๊ฐ)</h3>
`;
issues.forEach(issue => {
html += `
<div class="issue-item ${issue.severity}">
<strong>${issue.severity.toUpperCase()}</strong> - Line ${issue.line}<br>
${issue.message}
</div>
`;
});
html += `</div>`;
if (result.ai_review) {
html += `
<div class="metric-card">
<h3>๐ค AI ๋ฆฌ๋ทฐ</h3>
<pre style="white-space: pre-wrap;">${result.ai_review}</pre>
</div>
`;
}
resultContent.innerHTML = html;
document.getElementById('resultSection').classList.add('show');
}
function clearAll() {
document.getElementById('codeInput').value = '';
document.getElementById('resultSection').classList.remove('show');
}
</script>
</body>
</html>
"""
@app.route('/')
def index():
return render_template_string(HTML_TEMPLATE)
@app.route('/api/review', methods=['POST'])
def api_review():
data = request.json
code = data.get('code', '')
use_ai = data.get('use_ai', True)
model = data.get('model', 'gpt-3.5-turbo')
try:
# ์ฝ๋ ๋ฆฌ๋ทฐ ์คํ
assistant = SmartReviewAssistant()
result = assistant.review_code(code)
# JSON ์ง๋ ฌํ ๊ฐ๋ฅํ ํํ๋ก ๋ณํ
response = {
'success': True,
'metrics': result['metrics'],
'issues': [
{
'severity': issue.severity,
'line': issue.line,
'message': issue.message,
'rule': issue.rule
}
for issue in result['issues']
],
'ai_review': result.get('ai_review', '')
}
return jsonify(response)
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
}), 500
if __name__ == '__main__':
print("๐ ์น ์๋ฒ ์์!")
print("๐ ๋ธ๋ผ์ฐ์ ์์ http://localhost:5000 ์ ์ด์ด๋ณด์ธ์!")
app.run(debug=True, port=5000)
๐ ์น ๋ฒ์ ์คํํ๊ธฐ:
์ ์ฝ๋๋ฅผ app.py๋ก ์ ์ฅํ๊ณ python app.py๋ฅผ ์คํํ๋ฉด ์น ๋ธ๋ผ์ฐ์ ์์ ์ฝ๋ ๋ฆฌ๋ทฐ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ ์ ์์ด์! ํนํ ์ฌ๋ฅ๋ท์์ ํ๋ก๊ทธ๋๋ฐ ๊ฐ์๋ฅผ ํ์๋ ๋ถ๋ค์ ์ด ์น ๋ฒ์ ์ ํ์๋ค์๊ฒ ์ ๊ณตํ๋ฉด ์ ๋ง ์ ์ฉํ ๊ฑฐ์์! ๐
๐จ ๊ณ ๊ธ ๊ธฐ๋ฅ ์ถ๊ฐํ๊ธฐ
๊ธฐ๋ณธ ๊ธฐ๋ฅ์ ์์ฑํ์ผ๋, ์ด์ ๋ ๋ฉ์ง ๊ธฐ๋ฅ๋ค์ ์ถ๊ฐํด๋ด ์๋ค!
1. Git ํตํฉ ๊ธฐ๋ฅ
import git
from typing import List, Dict
class GitIntegration:
"""Git๊ณผ ํตํฉํ์ฌ ๋ณ๊ฒฝ๋ ํ์ผ๋ง ๋ฆฌ๋ทฐ"""
def __init__(self, repo_path: str = "."):
try:
self.repo = git.Repo(repo_path)
except:
raise ValueError("Git ์ ์ฅ์๊ฐ ์๋๋๋ค!")
def get_changed_files(self, branch: str = "HEAD") -> List[str]:
"""๋ณ๊ฒฝ๋ ํ์ด์ฌ ํ์ผ ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ"""
changed_files = []
# ์คํ
์ด์ง๋ ํ์ผ
for item in self.repo.index.diff("HEAD"):
if item.a_path.endswith('.py'):
changed_files.append(item.a_path)
# ์คํ
์ด์ง๋์ง ์์ ํ์ผ
for item in self.repo.index.diff(None):
if item.a_path.endswith('.py'):
changed_files.append(item.a_path)
return list(set(changed_files))
def get_file_diff(self, filepath: str) -> str:
"""ํ์ผ์ ๋ณ๊ฒฝ ๋ด์ฉ ๊ฐ์ ธ์ค๊ธฐ"""
try:
diff = self.repo.git.diff('HEAD', filepath)
return diff
except:
return ""
def review_commit(self, commit_hash: str = "HEAD") -> Dict:
"""ํน์ ์ปค๋ฐ์ ๋ณ๊ฒฝ์ฌํญ ๋ฆฌ๋ทฐ"""
commit = self.repo.commit(commit_hash)
results = {
'commit': commit_hash,
'message': commit.message,
'author': str(commit.author),
'files_reviewed': []
}
for item in commit.stats.files:
if item.endswith('.py'):
# ํ์ผ ๋ด์ฉ ๊ฐ์ ธ์ค๊ธฐ
file_content = (commit.tree / item).data_stream.read().decode('utf-8')
# ๋ฆฌ๋ทฐ ์คํ
assistant = SmartReviewAssistant()
review = assistant.review_code(file_content)
results['files_reviewed'].append({
'file': item,
'review': review
})
return results
# ์ฌ์ฉ ์์
if __name__ == "__main__":
git_integration = GitIntegration()
# ๋ณ๊ฒฝ๋ ํ์ผ ํ์ธ
changed_files = git_integration.get_changed_files()
print(f"๐ ๋ณ๊ฒฝ๋ ํ์ผ: {changed_files}")
# ๊ฐ ํ์ผ ๋ฆฌ๋ทฐ
for filepath in changed_files:
print(f"\n๐ {filepath} ๋ฆฌ๋ทฐ ์ค...")
with open(filepath, 'r') as f:
code = f.read()
assistant = SmartReviewAssistant()
result = assistant.review_code(code)
# ๋ฆฌํฌํธ ์์ฑ
generator = ReportGenerator()
generator.generate_markdown_report(result, f"{filepath}_review.md")
2. ์ฝ๋ ์๋ ์์ ๊ธฐ๋ฅ
import black
import autopep8
class CodeFixer:
"""์ฝ๋ ์๋ ์์ ๋๊ตฌ"""
def __init__(self, code: str):
self.original_code = code
self.fixed_code = code
def apply_black(self) -> str:
"""Black ํฌ๋งทํฐ ์ ์ฉ"""
try:
self.fixed_code = black.format_str(
self.fixed_code,
mode=black.FileMode()
)
print("โ
Black ํฌ๋งทํ
์๋ฃ")
except Exception as e:
print(f"โ ๏ธ Black ํฌ๋งทํ
์คํจ: {e}")
return self.fixed_code
def apply_autopep8(self) -> str:
"""autopep8 ์ ์ฉ"""
try:
self.fixed_code = autopep8.fix_code(
self.fixed_code,
options={'aggressive': 1}
)
print("โ
autopep8 ์ ์ฉ ์๋ฃ")
except Exception as e:
print(f"โ ๏ธ autopep8 ์ ์ฉ ์คํจ: {e}")
return self.fixed_code
def fix_common_issues(self) -> str:
"""์ผ๋ฐ์ ์ธ ์ด์ ์๋ ์์ """
lines = self.fixed_code.split('\n')
fixed_lines = []
for line in lines:
# ํํ ๊ณต๋ฐฑ ์ ๊ฑฐ
line = line.rstrip()
# ํญ์ ์คํ์ด์ค๋ก ๋ณํ
line = line.replace('\t', ' ')
fixed_lines.append(line)
self.fixed_code = '\n'.join(fixed_lines)
print("โ
์ผ๋ฐ ์ด์ ์์ ์๋ฃ")
return self.fixed_code
def get_diff(self) -> str:
"""์๋ณธ๊ณผ ์์ ๋ณธ์ ์ฐจ์ด ๋ณด๊ธฐ"""
import difflib
diff = difflib.unified_diff(
self.original_code.splitlines(keepends=True),
self.fixed_code.splitlines(keepends=True),
fromfile='original',
tofile='fixed'
)
return ''.join(diff)
def apply_all_fixes(self) -> str:
"""๋ชจ๋ ์์ ์ ์ฉ"""
print("๐ง ์ฝ๋ ์๋ ์์ ์์...\n")
self.fix_common_issues()
self.apply_autopep8()
self.apply_black()
print("\n๐ ์์ ์๋ฃ!")
print(f"์๋ณธ: {len(self.original_code)}์")
print(f"์์ : {len(self.fixed_code)}์")
return self.fixed_code
# ์ฌ์ฉ ์์
if __name__ == "__main__":
messy_code = """
def calculate(x,y):
result=x+y
return result
class myClass:
def __init__(self):
pass
"""
fixer = CodeFixer(messy_code)
fixed = fixer.apply_all_fixes()
print("\n" + "="*50)
print("์์ ๋ ์ฝ๋:")
print("="*50)
print(fixed)
print("\n" + "="*50)
print("๋ณ๊ฒฝ ์ฌํญ:")
print("="*50)
print(fixer.get_diff())
3. ํ์ต ๋ชจ๋ ๊ธฐ๋ฅ
class LearningMode:
"""์ด๋ณด์๋ฅผ ์ํ ํ์ต ๋ชจ๋"""
def __init__(self, ai_engine):
self.ai_engine = ai_engine
self.learning_history = []
def explain_issue(self, issue: CodeIssue) -> str:
"""์ด์์ ๋ํ ์์ธ ์ค๋ช
"""
prompt = f"""
๋ค์ ์ฝ๋ ์ด์๋ฅผ ์ด๋ณด์๋ ์ดํดํ ์ ์๊ฒ ์ค๋ช
ํด์ฃผ์ธ์:
์ด์: {issue.message}
๊ท์น: {issue.rule}
๋ค์ ๋ด์ฉ์ ํฌํจํด์ฃผ์ธ์:
1. ์ ์ด๊ฒ์ด ๋ฌธ์ ์ธ๊ฐ?
2. ์ด๋ป๊ฒ ์์ ํด์ผ ํ๋๊ฐ?
3. ์ค์ ์์ ์ฝ๋
4. ์ถ๊ฐ ํ์ต ์๋ฃ ์ถ์ฒ
"""
explanation = self.ai_engine.explain_concept(prompt)
self.learning_history.append({
'issue': issue.message,
'explanation': explanation
})
return explanation
def suggest_practice(self, topic: str) -> str:
"""์ฐ์ต ๋ฌธ์ ์ ์"""
prompt = f"""
'{topic}'์ ๋ํ ํ์ด์ฌ ์ฐ์ต ๋ฌธ์ ๋ฅผ 3๊ฐ ๋ง๋ค์ด์ฃผ์ธ์.
๋์ด๋: ์ด๊ธ, ์ค๊ธ, ๊ณ ๊ธ ๊ฐ 1๊ฐ์ฉ
๊ฐ ๋ฌธ์ ๋ ๋ค์์ ํฌํจํด์ผ ํฉ๋๋ค:
- ๋ฌธ์ ์ค๋ช
- ์
๋ ฅ/์ถ๋ ฅ ์์
- ํํธ
"""
return self.ai_engine.explain_concept(prompt)
def generate_study_plan(self, issues: List[CodeIssue]) -> str:
"""์ด์ ๊ธฐ๋ฐ ํ์ต ๊ณํ ์์ฑ"""
issue_categories = {}
for issue in issues:
category = issue.category
if category not in issue_categories:
issue_categories[category] = []
issue_categories[category].append(issue)
plan = "๐ ๋ง์ถคํ ํ์ต ๊ณํ\n\n"
for i, (category, category_issues) in enumerate(issue_categories.items(), 1):
plan += f"{i}. {category.upper()} ({len(category_issues)}๊ฐ ์ด์)\n"
plan += f" ์ถ์ฒ ํ์ต ์๊ฐ: {len(category_issues) * 30}๋ถ\n"
plan += f" ์ฐ์ ์์: {'๋์' if len(category_issues) > 3 else '๋ณดํต'}\n\n"
return plan
# ์ฌ์ฉ ์์
if __name__ == "__main__":
# AI ์์ง ์ด๊ธฐํ
ai_engine = AIReviewEngine()
learning_mode = LearningMode(ai_engine)
# ์ด์ ์ค๋ช
sample_issue = CodeIssue(
'warning',
10,
0,
'ํจ์๊ฐ ๋๋ฌด ๊น๋๋ค',
'function-too-long',
'maintainability'
)
explanation = learning_mode.explain_issue(sample_issue)
print("๐ก ์ด์ ์ค๋ช
:")
print(explanation)
# ์ฐ์ต ๋ฌธ์ ์ ์
print("\n๐ ์ฐ์ต ๋ฌธ์ :")
practice = learning_mode.suggest_practice("ํจ์ ๋ฆฌํฉํ ๋ง")
print(practice)
๐ ๋ฐฐํฌ ๋ฐ ์ค์ ํ์ฉ
์ด์ ๋ง๋ ๋๊ตฌ๋ฅผ ์ค์ ๋ก ํ์ฉํด๋ด ์๋ค!
๐ฆ ํจํค์ง ๊ตฌ์กฐ:
code-review-ai/
โโโ src/
โ โโโ __init__.py
โ โโโ parser.py # CodeParser ํด๋์ค
โ โโโ analyzer.py # StaticAnalyzer ํด๋์ค
โ โโโ ai_engine.py # AIReviewEngine ํด๋์ค
โ โโโ reporter.py # ReportGenerator ํด๋์ค
โ โโโ fixer.py # CodeFixer ํด๋์ค
โ โโโ learning.py # LearningMode ํด๋์ค
โโโ cli.py # CLI ์ธํฐํ์ด์ค
โโโ app.py # ์น ์ธํฐํ์ด์ค
โโโ requirements.txt # ์์กด์ฑ ๋ชฉ๋ก
โโโ .env.example # ํ๊ฒฝ๋ณ์ ์์
โโโ README.md # ์ฌ์ฉ ์ค๋ช
์
โโโ tests/ # ํ
์คํธ ์ฝ๋
โโโ test_parser.py
โโโ test_analyzer.py
โโโ test_ai_engine.py
Docker๋ก ๋ฐฐํฌํ๊ธฐ
# Dockerfile
FROM python:3.11-slim
WORKDIR /app
# ์์กด์ฑ ์ค์น
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# ์ ํ๋ฆฌ์ผ์ด์
๋ณต์ฌ
COPY . .
# ํ๊ฒฝ๋ณ์ ์ค์
ENV FLASK_APP=app.py
ENV FLASK_ENV=production
# ํฌํธ ๋
ธ์ถ
EXPOSE 5000
# ์คํ
CMD ["python", "app.py"]
# docker-compose.yml
version: '3.8'
services:
web:
build: .
ports:
- "5000:5000"
environment:
- OPENAI_API_KEY=${OPENAI_API_KEY}
volumes:
- ./reports:/app/reports
restart: unless-stopped
CI/CD ํตํฉ (GitHub Actions)
# .github/workflows/code-review.yml
name: AI Code Review
on:
pull_request:
types: [opened, synchronize]
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.11'
- name: Install dependencies
run: |
pip install -r requirements.txt
- name: Run AI Code Review
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
run: |
python cli.py review $(git diff --name-only HEAD^..HEAD | grep '.py$')
- name: Upload Review Report
uses: actions/upload-artifact@v2
with:
name: review-report
path: '*_review.md'
- name: Comment on PR
uses: actions/github-script@v5
with:
script: |
const fs = require('fs');
const report = fs.readFileSync('code_review_report.md', 'utf8');
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: report
});
๐ก ์ค์ ํ์ฉ ํ๊ณผ ๋ฒ ์คํธ ํ๋ํฐ์ค
1. API ๋น์ฉ ์ต์ ํ
โข ์บ์ฑ ํ์ฉ: ๊ฐ์ ์ฝ๋๋ ๋ค์ ๋ถ์ํ์ง ์๊ธฐ
โข ๋ฐฐ์น ์ฒ๋ฆฌ: ์ฌ๋ฌ ํ์ผ์ ํ ๋ฒ์ ์ฒ๋ฆฌ
โข ๋ชจ๋ธ ์ ํ: ๊ฐ๋จํ ๋ฆฌ๋ทฐ๋ GPT-3.5, ๋ณต์กํ ๋ฆฌ๋ทฐ๋ GPT-4
โข ํ๋กฌํํธ ์ต์ ํ: ๋ถํ์ํ ๋ด์ฉ ์ ๊ฑฐ
2. ํ ํ๊ฒฝ์์ ํ์ฉํ๊ธฐ
โข ์ฝ๋ฉ ์คํ์ผ ๊ฐ์ด๋ ํตํฉ
โข ํ ๊ท์น ์ปค์คํฐ๋ง์ด์ง
โข ๋ฆฌ๋ทฐ ๊ธฐ์ค ์ผ๊ด์ฑ ์ ์ง
โข ํ์ต ์๋ฃ๋ก ํ์ฉ
3. ๊ต์ก ๋ชฉ์ ์ผ๋ก ํ์ฉํ๊ธฐ
ํนํ ์ฌ๋ฅ๋ท์์ ํ๋ก๊ทธ๋๋ฐ์ ๊ฐ๋ฅด์น์๋ ๋ถ๋ค๊ป ์ ์ฉํ ํ์ด์์!
โข ํ์ ์ฝ๋ ์๋ ๋ฆฌ๋ทฐ
โข ์ฆ๊ฐ์ ์ธ ํผ๋๋ฐฑ ์ ๊ณต
โข ํ์ต ์ง๋ ์ถ์
โข ๋ง์ถคํ ์ฐ์ต ๋ฌธ์ ์์ฑ
โ ๏ธ ์ฃผ์์ฌํญ:
1. AI๋ ๋ณด์กฐ ๋๊ตฌ์
๋๋ค. ์ต์ข
ํ๋จ์ ์ฌ๋์ด ํด์ผ ํด์
2. ๋ฏผ๊ฐํ ์ฝ๋๋ ์ธ๋ถ API๋ก ๋ณด๋ด์ง ๋ง์ธ์
3. ๋ผ์ด์ ์ค๋ฅผ ํ์ธํ๊ณ ์ฌ์ฉํ์ธ์
4. ๊ณผ๋ํ ์์กด์ ํ์ต์ ๋ฐฉํด๊ฐ ๋ ์ ์์ด์
๐ ๋ ๋์๊ฐ๊ธฐ: ๊ณ ๊ธ ์ฃผ์ ๋ค
๊ธฐ๋ณธ ๊ธฐ๋ฅ์ ๋ง์คํฐํ๋ค๋ฉด, ์ด๋ฐ ๊ณ ๊ธ ๊ธฐ๋ฅ๋ค๋ ๋์ ํด๋ณด์ธ์!
๐ ํ์ฅ ์์ด๋์ด:
๋ค๊ตญ์ด ์ง์ ๋ค๋ฅธ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ ๋ฆฌ๋ทฐ
๋ณด์ ์ค์บ ์ทจ์ฝ์ ์๋ ํ์ง
์ฑ๋ฅ ๋ถ์ ์คํ ์๊ฐ ํ๋กํ์ผ๋ง
ํ
์คํธ ์์ฑ ์๋ ์ ๋ ํ
์คํธ ์์ฑ
๋ฌธ์ํ ์๋ docstring ์์ฑ
๋ฆฌํฉํ ๋ง ์ ์ ๋์์ธ ํจํด ์ ์ฉ
ํ์
๊ธฐ๋ฅ ํ ๋์๋ณด๋ ๊ตฌ์ถ
์์: ์๋ ํ ์คํธ ์์ฑ๊ธฐ
class TestGenerator:
"""ํจ์์ ๋ํ ์ ๋ ํ
์คํธ ์๋ ์์ฑ"""
def __init__(self, ai_engine):
self.ai_engine = ai_engine
def generate_tests(self, function_code: str, function_name: str) -> str:
"""ํจ์์ ๋ํ pytest ํ
์คํธ ์์ฑ"""
prompt = f"""
๋ค์ ํ์ด์ฌ ํจ์์ ๋ํ pytest ํ
์คํธ ์ฝ๋๋ฅผ ์์ฑํด์ฃผ์ธ์:
```python
{function_code}
```
๋ค์์ ํฌํจํด์ฃผ์ธ์:
1. ์ ์ ์ผ์ด์ค ํ
์คํธ
2. ๊ฒฝ๊ณ๊ฐ ํ
์คํธ
3. ์์ธ ์ํฉ ํ
์คํธ
4. ์ ์ ํ assert ๋ฌธ
5. ํ
์คํธ ํจ์๋ช
์ test_{function_name}_๋ก ์์
์์ ํ ํ
์คํธ ์ฝ๋๋ง ์์ฑํด์ฃผ์ธ์.
"""
response = self.ai_engine.explain_concept(prompt)
return response
# ์ฌ์ฉ ์์
if __name__ == "__main__":
function_code = """
def divide(a, b):
'''๋ ์ซ์๋ฅผ ๋๋๋๋ค'''
if b == 0:
raise ValueError("0์ผ๋ก ๋๋ ์ ์์ต๋๋ค")
return a / b
"""
ai_engine = AIReviewEngine()
test_gen = TestGenerator(ai_engine)
tests = test_gen.generate_tests(function_code, "divide")
print("๐งช ์์ฑ๋ ํ
์คํธ:")
print(tests)
๐ฏ ๋ง๋ฌด๋ฆฌํ๋ฉฐ
์! ์ ๋ง ๊ธด ์ฌ์ ์ด์์ฃ ? ๐ ์ฐ๋ฆฌ๋ ์ง๊ธ๊น์ง:
โ
์ฝ๋ ํ์๋ก ๊ตฌ์กฐ ๋ถ์ํ๊ธฐ
โ
์ ์ ๋ถ์์ผ๋ก ๋ฌธ์ ์ฐพ๊ธฐ
โ
AI๋ก ์ง๋ฅํ ๋ฆฌ๋ทฐ ์์ฑํ๊ธฐ
โ
์์ ๋ฆฌํฌํธ ๋ง๋ค๊ธฐ
โ
CLI์ ์น ์ธํฐํ์ด์ค ๊ตฌ์ถํ๊ธฐ
โ
Git ํตํฉ ๋ฐ ์๋ํํ๊ธฐ
โ
์ค์ ๋ฐฐํฌ๊น์ง!
์ด ๋ชจ๋ ๊ฑธ ๋ฐฐ์ ์ด์! ๐
๐ช ๋ค์ ๋จ๊ณ:
1. ์ง์ ์ฝ๋๋ฅผ ์์ฑํ๊ณ ์คํํด๋ณด์ธ์
2. ์์ ์ ํ๋ก์ ํธ์ ์ ์ฉํด๋ณด์ธ์
3. ํ์๋ค๊ณผ ๊ณต์ ํ๊ณ ํผ๋๋ฐฑ์ ๋ฐ์ผ์ธ์
4. ์ง์์ ์ผ๋ก ๊ฐ์ ํ๊ณ ํ์ฅํ์ธ์
๊ทธ๋ฆฌ๊ณ ํน์ ํ๋ก๊ทธ๋๋ฐ์ ๋ฐฐ์ฐ๊ณ ์ถ๊ฑฐ๋ ๊ฐ๋ฅด์น๊ณ ์ถ๋ค๋ฉด, ์ฌ๋ฅ๋ท์์ ๋ค์ํ ๊ฐ๋ฐ์๋ค๊ณผ ๊ต๋ฅํด๋ณด๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ด์์! ๐ค
์ฝ๋ ๋ฆฌ๋ทฐ๋ ๋จ์ํ ๋ฒ๊ทธ๋ฅผ ์ฐพ๋ ๊ฒ์ด ์๋๋ผ, ๋ ๋์ ๊ฐ๋ฐ์๋ก ์ฑ์ฅํ๋ ๊ณผ์ ์ด์์. AI ๋์ฐ๋ฏธ๋ ๊ทธ ์ฌ์ ์ ํจ๊ปํ๋ ๋ ๋ ํ ๋๋ฃ๊ฐ ๋์ด์ค ๊ฑฐ์์. ๐
์ฌ๋ฌ๋ถ์ ์ฝ๋ ๋ฆฌ๋ทฐ AI ๋์ฐ๋ฏธ๊ฐ ๋ฉ์ง๊ฒ ์์ฑ๋๊ธธ ๋ฐ๋๋๋ค! ๊ถ๊ธํ ์ ์ด ์๋ค๋ฉด ์ธ์ ๋ ์ปค๋ฎค๋ํฐ์ ๋ฌผ์ด๋ณด์ธ์. ์ฐ๋ฆฌ๋ ํจ๊ป ์ฑ์ฅํ๋ ๊ฐ๋ฐ์ ์ปค๋ฎค๋ํฐ๋๊น์! ๐
Happy Coding! ์ฆ๊ฑฐ์ด ์ฝ๋ฉ ๋์ธ์! ๐ปโจ
๐ ์ถํํฉ๋๋ค!
๋น์ ์ ์ด์ AI ์ฝ๋ ๋ฆฌ๋ทฐ ๋์ฐ๋ฏธ๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค!
๊ณ์ํด์ ๋ฐฐ์ฐ๊ณ , ์คํํ๊ณ , ๊ณต์ ํ์ธ์! ๐
๊ด๋ จ ํค์๋
๋๊ธ ์์ฑ
์ด ๊ธ์ ๋ํ ์ฌ๋ฌ๋ถ์ ์๊ฐ์ ๋ค๋ ค์ฃผ์ธ์
๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค
๋๊ธ์ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํด์ฃผ์ธ์.
๋๊ธ 0
์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์์ง ๋๊ธ์ด ์์ต๋๋ค.