Call LLM for code analysis with retry mechanism and debugging support
(
self, prompt: str, system_prompt: str = None, max_tokens: int = None
)
| 381 | ) |
| 382 | |
| 383 | async def _call_llm( |
| 384 | self, prompt: str, system_prompt: str = None, max_tokens: int = None |
| 385 | ) -> str: |
| 386 | """Call LLM for code analysis with retry mechanism and debugging support""" |
| 387 | if system_prompt is None: |
| 388 | system_prompt = self.llm_system_prompt |
| 389 | if max_tokens is None: |
| 390 | max_tokens = self.llm_max_tokens |
| 391 | |
| 392 | # Mock response for testing |
| 393 | if self.mock_llm_responses: |
| 394 | mock_response = self._generate_mock_response(prompt) |
| 395 | if self.save_raw_responses: |
| 396 | self._save_debug_response("mock", prompt, mock_response) |
| 397 | return mock_response |
| 398 | |
| 399 | last_error = None |
| 400 | |
| 401 | # Retry mechanism |
| 402 | for attempt in range(self.max_retries): |
| 403 | try: |
| 404 | if self.verbose_output and attempt > 0: |
| 405 | self.logger.info( |
| 406 | f"LLM call attempt {attempt + 1}/{self.max_retries}" |
| 407 | ) |
| 408 | |
| 409 | client, client_type = await self._initialize_llm_client() |
| 410 | |
| 411 | if client_type == "anthropic": |
| 412 | response = await client.messages.create( |
| 413 | model=self.default_models["anthropic"], |
| 414 | system=system_prompt, |
| 415 | messages=[{"role": "user", "content": prompt}], |
| 416 | max_tokens=max_tokens, |
| 417 | temperature=self.llm_temperature, |
| 418 | ) |
| 419 | |
| 420 | content = "" |
| 421 | for block in response.content: |
| 422 | if block.type == "text": |
| 423 | content += block.text |
| 424 | |
| 425 | # Save debug response if enabled |
| 426 | if self.save_raw_responses: |
| 427 | self._save_debug_response("anthropic", prompt, content) |
| 428 | |
| 429 | return content |
| 430 | |
| 431 | elif client_type == "openai": |
| 432 | messages = [ |
| 433 | {"role": "system", "content": system_prompt}, |
| 434 | {"role": "user", "content": prompt}, |
| 435 | ] |
| 436 | |
| 437 | response = await client.chat.completions.create( |
| 438 | model=self.default_models["openai"], |
| 439 | messages=messages, |
| 440 | max_tokens=max_tokens, |
no test coverage detected