Spaces:
Sleeping
Sleeping
File size: 1,392 Bytes
80f6bb8 339ddc2 80f6bb8 339ddc2 80f6bb8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import os
from groq import Groq
class RefactorAgent:
def __init__(self, api_key: str = None):
from config import GROQ_API_KEY
self.api_key = api_key or GROQ_API_KEY
self.client = Groq(api_key=self.api_key) if self.api_key else None
def refactor_code(self, code: str, context: str) -> str:
if not self.client:
return "Groq API Key not configured."
from dotenv import dotenv_values
from config import BASE_DIR
env_vars = dotenv_values(BASE_DIR / ".env")
current_model = env_vars.get("LLM_MODEL", "llama-3.1-8b-instant")
prompt = f"""You are an Expert Python Refactoring Agent.
Please refactor the following function to be more readable, efficient, and Pythonic.
- Reduce cyclomatic complexity if possible.
- Include Python type hints.
- Add a brief docstring.
Return ONLY the refactored code without conversational text.
Context where this is used: {context}
CODE TO REFACTOR:
```python
{code}
```
"""
try:
response = self.client.chat.completions.create(
model=current_model,
messages=[{"role": "user", "content": prompt}],
temperature=0.1
)
return response.choices[0].message.content.strip("`").replace("python\n", "")
except Exception as e:
return f"Error: {e}"
|