File size: 2,422 Bytes
e903db0 335610a e903db0 335610a e903db0 335610a e903db0 335610a e903db0 335610a e903db0 335610a e903db0 335610a e903db0 335610a e903db0 f39b638 e903db0 335610a e903db0 335610a e903db0 335610a | 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | """LLM-based fallback analyzer for languages without native linters."""
import json
from groq import Groq
from app.modules.analyzer_base import CodeAnalyzer, AnalysisResult
from app.config import GROQ_API_KEY, MODEL_NAME, MAX_TOKENS, TEMPERATURE
class LLMFallbackAnalyzer(CodeAnalyzer):
def __init__(self, language: str):
super().__init__(language)
self._client = None
def _get_client(self) -> Groq:
if self._client is None:
self._client = Groq(api_key=GROQ_API_KEY)
return self._client
def analyze(self, code: str) -> AnalysisResult:
try:
client = self._get_client()
prompt = f"""Analyze this {self.language} code for issues and return ONLY a JSON array with objects containing:
- line: estimated line number (1-based)
- symbol: issue code/identifier
- message: issue description
- severity: 'high', 'medium', or 'low'
- category: 'correctness', 'performance', 'security', or 'style'
Limit to top 5 issues. Return ONLY valid JSON array, no markdown.
Code:
{code}
JSON array:"""
resp = client.chat.completions.create(
model=MODEL_NAME,
messages=[{"role": "user", "content": prompt}],
max_tokens=MAX_TOKENS,
temperature=TEMPERATURE,
)
response_text = resp.choices[0].message.content.strip()
try:
start = response_text.find("[")
end = response_text.rfind("]") + 1
if start >= 0 and end > start:
issues_data = json.loads(response_text[start:end])
else:
issues_data = []
except json.JSONDecodeError:
return AnalysisResult(self.language, error="Failed to parse LLM response")
issues = []
for issue in issues_data[:5]:
formatted = self.format_issue(
line=issue.get("line", 0),
symbol=issue.get("symbol", "llm-issue"),
message=issue.get("message", ""),
category=issue.get("category", "style"),
severity=issue.get("severity", "low"),
)
issues.append(formatted)
return AnalysisResult(self.language, issues)
except Exception as e:
return AnalysisResult(self.language, error=str(e)) |