Spaces:
Build error
Build error
Create reviewer_agent.py
Browse files- reviewer_agent.py +48 -0
reviewer_agent.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
import tempfile
|
| 3 |
+
import openai
|
| 4 |
+
import os
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 10 |
+
|
| 11 |
+
def review_agent_with_pylint(code):
|
| 12 |
+
"""
|
| 13 |
+
Review code using pylint.
|
| 14 |
+
"""
|
| 15 |
+
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
|
| 16 |
+
f.write(code)
|
| 17 |
+
f.flush()
|
| 18 |
+
result = subprocess.run(['pylint', f.name], capture_output=True, text=True)
|
| 19 |
+
os.unlink(f.name)
|
| 20 |
+
return result.stdout
|
| 21 |
+
|
| 22 |
+
def review_agent_with_llm(code):
|
| 23 |
+
"""
|
| 24 |
+
Use an LLM to review the code for logical errors and improvements.
|
| 25 |
+
"""
|
| 26 |
+
prompt = f"Review the following Python code for logical errors, style, and potential improvements:\n\n```python\n{code}\n```"
|
| 27 |
+
|
| 28 |
+
response = openai.ChatCompletion.create(
|
| 29 |
+
model="gpt-4",
|
| 30 |
+
messages=[
|
| 31 |
+
{"role": "system", "content": "You are a code reviewer. Provide feedback on the code."},
|
| 32 |
+
{"role": "user", "content": prompt}
|
| 33 |
+
],
|
| 34 |
+
temperature=0.3,
|
| 35 |
+
max_tokens=300
|
| 36 |
+
)
|
| 37 |
+
return response.choices[0].message.content
|
| 38 |
+
|
| 39 |
+
def review_agent(code):
|
| 40 |
+
"""
|
| 41 |
+
Combine pylint and LLM review.
|
| 42 |
+
"""
|
| 43 |
+
pylint_output = review_agent_with_pylint(code)
|
| 44 |
+
llm_output = review_agent_with_llm(code)
|
| 45 |
+
return {
|
| 46 |
+
"pylint": pylint_output,
|
| 47 |
+
"llm_review": llm_output
|
| 48 |
+
}
|