Code Amélioration AI
Создано: 22 декабря 2024 г.
Создано: 22 декабря 2024 г.
Pour optimiser un code Python d'ingénieur IA, je suis en train de peaufiner la structure, je vais bien suivre l'importation des modules nécessaires, et je m'assure que chaque classe ou module est bien défini et gère correctement les requêtes.
Réflexion pendant quelques secondes
Votre code pour l'AIEngineer
est déjà bien structuré et utilise des pratiques modernes telles qu'asyncio
pour la gestion asynchrone et Rich
pour une interface utilisateur enrichie. Cependant, il y a plusieurs améliorations possibles pour augmenter la robustesse, la maintenabilité et les fonctionnalités. Voici une version améliorée de votre code avec des explications détaillées sur les modifications apportées.
logging
pour remplacer les impressions directes avec Rich
, permettant une meilleure gestion des logs.dataclasses
: Pour une meilleure gestion des structures de données complexes.Voici la version améliorée de votre code :
from typing import Dict, Any, List, Optional, Union, Callable from pathlib import Path from rich.console import Console from rich.panel import Panel from rich.markdown import Markdown from rich.prompt import Confirm, Prompt from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn, TimeElapsedColumn import asyncio import json import logging from .groq_service import GroqService from .file_manager import FileManager from .context_manager import ContextManager from .settings_manager import SettingsManager from workspace.src.components.task_manager import TaskManager, TaskType from workspace.src.components.code_generator import CodeGenerator from workspace.project_structure import ProjectStructure # Configuration du logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', handlers=[ logging.FileHandler("ai_engineer.log"), logging.StreamHandler() ] ) logger = logging.getLogger(__name__) console = Console() # Exceptions personnalisées class AIEngineerError(Exception): """Base exception class for AIEngineer.""" pass class RequestUnderstandingError(AIEngineerError): """Exception raised when understanding the request fails.""" pass class ImplementationPlanError(AIEngineerError): """Exception raised when creating the implementation plan fails.""" pass class ExecutionError(AIEngineerError): """Exception raised during the execution of the implementation.""" pass class ValidationError(AIEngineerError): """Exception raised during the validation of the implementation.""" pass class AIEngineer: """Advanced AI Software Engineer that can understand, plan, and execute complex development tasks.""" def __init__( self, groq_service: GroqService, file_manager: FileManager, context_manager: ContextManager, settings_manager: SettingsManager ): self.groq = groq_service self.file_manager = file_manager self.context_manager = context_manager self.settings = settings_manager # Define task types the AI can handle self.task_types: Dict[str, Callable[[Dict[str, Any], Progress, int], Any]] = { "implement": self._implement_feature, "create": self._create_component, "modify": self._modify_code, "refactor": self._refactor_code, "test": self._create_tests, "fix": self._fix_bugs, "explain": self._explain_code, "optimize": self._optimize_code, "document": self._add_documentation } # Initialize new components self.project_structure = ProjectStructure(self.file_manager.workspace.workspace_dir) self.task_manager = TaskManager() self.code_generator = CodeGenerator(self.file_manager.workspace.workspace_dir) async def process_request(self, request: str) -> Dict[str, Any]: """Process a user request through multiple stages.""" try: # 1. Understand the request understanding = await self._understand_request(request) if not understanding["success"]: raise RequestUnderstandingError(understanding["message"]) # 2. Create tasks from understanding tasks = await self._create_tasks(understanding["analysis"]) if not tasks["success"]: raise AIEngineerError(tasks["message"]) # 3. Plan implementation implementation = await self._plan_implementation(tasks["tasks"]) if not implementation["success"]: raise ImplementationPlanError(implementation["message"]) # 4. Get user confirmation if not await self._confirm_implementation(implementation["plan"]): return {"success": False, "message": "Operation cancelled"} # 5. Execute implementation async with Progress( SpinnerColumn(), TextColumn("[progress.description]{task.description}"), BarColumn(), TimeElapsedColumn(), console=console ) as progress: result = await self._execute_implementation( implementation["plan"], progress ) # 6. Validate and test validation = await self._validate_implementation(result) return validation except AIEngineerError as e: logger.error(f"AIEngineerError: {str(e)}") return {"success": False, "message": str(e)} except Exception as e: logger.exception("Unexpected error occurred") return {"success": False, "message": "An unexpected error occurred."} async def _understand_request(self, request: str) -> Dict[str, Any]: """Deeply understand what the user wants to achieve.""" understanding_prompt = f"""Analyze this software engineering request: {request} Provide a detailed analysis including: 1. Main Goal: What is the user trying to achieve? 2. Technical Requirements: What needs to be implemented? 3. Dependencies: What existing code or new components are needed? 4. Potential Challenges: What technical challenges might we face? 5. Best Practices: What patterns and practices should we follow? Return analysis in markdown format.""" try: analysis = await self.groq.send_message(understanding_prompt, include_context=True) console.print(Panel(Markdown(analysis), title="🤔 Understanding", border_style="blue")) logger.info("Request understood successfully.") return {"success": True, "analysis": analysis} except Exception as e: logger.error(f"Failed to understand request: {str(e)}") return {"success": False, "message": "Failed to understand the request."} async def _create_tasks(self, analysis: str) -> Dict[str, Any]: """Create tasks based on the analysis.""" # Implementation spécifique pour créer des tâches à partir de l'analyse # Cela pourrait impliquer de parser le markdown, extraire des informations, etc. # Pour l'instant, supposons que nous retournons une liste de tâches simulées try: tasks = self.task_manager.parse_analysis(analysis) logger.info("Tasks created successfully.") return {"success": True, "tasks": tasks} except Exception as e: logger.error(f"Failed to create tasks: {str(e)}") return {"success": False, "message": "Failed to create tasks from analysis."} async def _plan_implementation(self, tasks: List[TaskType]) -> Dict[str, Any]: """Create a detailed implementation plan.""" planning_prompt = f"""Based on these tasks: {json.dumps(tasks, indent=2)} Create a detailed implementation plan that includes: 1. Task Type: What kind of task is this (implement/create/modify/etc.)? 2. Components: What files and components need to be created or modified? 3. Steps: What are the specific implementation steps? 4. Testing: How should we validate the implementation? Return the plan in this JSON format: {{ "task_type": "implement/create/modify/etc.", "components": ["list", "of", "files"], "steps": [ {{ "type": "action_type", "description": "step description", "target": "file_or_component", "details": "specific details" }} ], "tests": ["list", "of", "test", "cases"] }}""" try: response = await self.groq.send_message(planning_prompt, include_context=True) plan = self.groq.extract_json(response) console.print("\n[bold blue]📋 Implementation Plan:[/bold blue]") console.print(f"Task Type: [yellow]{plan['task_type']}[/yellow]") console.print("\nComponents:") for component in plan['components']: console.print(f" • {component}") console.print("\nSteps:") for i, step in enumerate(plan['steps'], 1): console.print(f" {i}. [yellow]{step['type']}[/yellow]: {step['description']}") logger.info("Implementation plan created successfully.") return {"success": True, "plan": plan} except json.JSONDecodeError as e: logger.error(f"JSON decoding failed: {str(e)}") return {"success": False, "message": "Failed to parse the implementation plan."} except Exception as e: logger.error(f"Failed to plan implementation: {str(e)}") return {"success": False, "message": "Failed to create the implementation plan."} async def _confirm_implementation(self, plan: Dict[str, Any]) -> bool: """Get user confirmation for the implementation plan.""" try: confirmation = Confirm.ask("\n[bold yellow]Proceed with this implementation?[/bold yellow]") logger.info(f"User confirmation: {confirmation}") return confirmation except Exception as e: logger.error(f"Failed to get user confirmation: {str(e)}") return False async def _execute_implementation(self, plan: Dict[str, Any], progress: Progress) -> Dict[str, Any]: """Execute the implementation plan.""" try: task_type = plan['task_type'] if task_type in self.task_types: task_id = progress.add_task( f"[blue]Executing {task_type} task...[/blue]", total=len(plan['steps']) ) result = await self.task_types[task_type](plan, progress, task_id) if result["success"]: await self._run_tests(plan.get('tests', []), progress) logger.info("Implementation executed successfully.") return result else: logger.error(f"Unknown task type: {task_type}") return { "success": False, "message": f"Unknown task type: {task_type}" } except Exception as e: logger.error(f"Failed to execute implementation: {str(e)}") return {"success": False, "message": "Failed to execute the implementation."} async def _implement_feature(self, plan: Dict[str, Any], progress: Progress, task_id: int) -> Dict[str, Any]: """Implement a new feature.""" results = [] for step in plan['steps']: progress.update(task_id, description=f"[blue]{step['description']}[/blue]") try: if step['type'] == 'create_file': result = await self.groq.handle_action( f"Create {step['target']} with {step['details']}", "create_file" ) elif step['type'] == 'modify_file': result = await self.groq.handle_action( f"Modify {step['target']} to {step['details']}", "edit_file" ) else: result = {"success": False, "message": f"Unknown step type: {step['type']}"} results.append(result) progress.advance(task_id) except Exception as e: logger.error(f"Error during step '{step['description']}': {str(e)}") results.append({"success": False, "message": str(e)}) progress.advance(task_id) return { "success": all(r["success"] for r in results), "results": results } # Implémentation des autres gestionnaires de tâches async def _create_component(self, plan: Dict[str, Any], progress: Progress, task_id: int) -> Dict[str, Any]: """Create a new component.""" # Exemple d'implémentation results = [] for step in plan['steps']: progress.update(task_id, description=f"[green]{step['description']}[/green]") try: if step['type'] == 'create_component': result = await self.groq.handle_action( f"Create component {step['target']} with details {step['details']}", "create_component" ) else: result = {"success": False, "message": f"Unknown step type: {step['type']}"} results.append(result) progress.advance(task_id) except Exception as e: logger.error(f"Error during step '{step['description']}': {str(e)}") results.append({"success": False, "message": str(e)}) progress.advance(task_id) return { "success": all(r["success"] for r in results), "results": results } async def _modify_code(self, plan: Dict[str, Any], progress: Progress, task_id: int) -> Dict[str, Any]: """Modify existing code.""" results = [] for step in plan['steps']: progress.update(task_id, description=f"[cyan]{step['description']}[/cyan]") try: if step['type'] == 'modify_code': result = await self.groq.handle_action( f"Modify code in {step['target']} to {step['details']}", "modify_code" ) else: result = {"success": False, "message": f"Unknown step type: {step['type']}"} results.append(result) progress.advance(task_id) except Exception as e: logger.error(f"Error during step '{step['description']}': {str(e)}") results.append({"success": False, "message": str(e)}) progress.advance(task_id) return { "success": all(r["success"] for r in results), "results": results } async def _refactor_code(self, plan: Dict[str, Any], progress: Progress, task_id: int) -> Dict[str, Any]: """Refactor existing code.""" results = [] for step in plan['steps']: progress.update(task_id, description=f"[magenta]{step['description']}[/magenta]") try: if step['type'] == 'refactor_code': result = await self.groq.handle_action( f"Refactor {step['target']} to {step['details']}", "refactor_code" ) else: result = {"success": False, "message": f"Unknown step type: {step['type']}"} results.append(result) progress.advance(task_id) except Exception as e: logger.error(f"Error during step '{step['description']}': {str(e)}") results.append({"success": False, "message": str(e)}) progress.advance(task_id) return { "success": all(r["success"] for r in results), "results": results } async def _create_tests(self, plan: Dict[str, Any], progress: Progress, task_id: int) -> Dict[str, Any]: """Create test cases.""" results = [] for test in plan['tests']: progress.update(task_id, description=f"[green]Creating test: {test}[/green]") try: result = await self.groq.handle_action( f"Create test case for {test}", "create_test" ) results.append(result) progress.advance(task_id) except Exception as e: logger.error(f"Error creating test '{test}': {str(e)}") results.append({"success": False, "message": str(e)}) progress.advance(task_id) return { "success": all(r["success"] for r in results), "results": results } async def _fix_bugs(self, plan: Dict[str, Any], progress: Progress, task_id: int) -> Dict[str, Any]: """Fix bugs in code.""" results = [] for step in plan['steps']: progress.update(task_id, description=f"[red]{step['description']}[/red]") try: if step['type'] == 'fix_bug': result = await self.groq.handle_action( f"Fix bug in {step['target']} with details {step['details']}", "fix_bug" ) else: result = {"success": False, "message": f"Unknown step type: {step['type']}"} results.append(result) progress.advance(task_id) except Exception as e: logger.error(f"Error fixing bug '{step['description']}': {str(e)}") results.append({"success": False, "message": str(e)}) progress.advance(task_id) return { "success": all(r["success"] for r in results), "results": results } async def _explain_code(self, plan: Dict[str, Any], progress: Progress, task_id: int) -> Dict[str, Any]: """Explain code functionality.""" results = [] for step in plan['steps']: progress.update(task_id, description=f"[yellow]{step['description']}[/yellow]") try: if step['type'] == 'explain_code': explanation = await self.groq.handle_action( f"Explain the functionality of {step['target']}", "explain_code" ) results.append({"success": True, "explanation": explanation}) else: result = {"success": False, "message": f"Unknown step type: {step['type']}"} results.append(result) progress.advance(task_id) except Exception as e: logger.error(f"Error explaining code '{step['description']}': {str(e)}") results.append({"success": False, "message": str(e)}) progress.advance(task_id) return { "success": all(r["success"] for r in results), "results": results } async def _optimize_code(self, plan: Dict[str, Any], progress: Progress, task_id: int) -> Dict[str, Any]: """Optimize code performance.""" results = [] for step in plan['steps']: progress.update(task_id, description=f"[cyan]{step['description']}[/cyan]") try: if step['type'] == 'optimize_code': result = await self.groq.handle_action( f"Optimize {step['target']} for performance with details {step['details']}", "optimize_code" ) else: result = {"success": False, "message": f"Unknown step type: {step['type']}"} results.append(result) progress.advance(task_id) except Exception as e: logger.error(f"Error optimizing code '{step['description']}': {str(e)}") results.append({"success": False, "message": str(e)}) progress.advance(task_id) return { "success": all(r["success"] for r in results), "results": results } async def _add_documentation(self, plan: Dict[str, Any], progress: Progress, task_id: int) -> Dict[str, Any]: """Add or improve documentation.""" results = [] for step in plan['steps']: progress.update(task_id, description=f"[blue]{step['description']}[/blue]") try: if step['type'] == 'add_documentation': result = await self.groq.handle_action( f"Add documentation to {step['target']} with details {step['details']}", "add_documentation" ) else: result = {"success": False, "message": f"Unknown step type: {step['type']}"} results.append(result) progress.advance(task_id) except Exception as e: logger.error(f"Error adding documentation '{step['description']}': {str(e)}") results.append({"success": False, "message": str(e)}) progress.advance(task_id) return { "success": all(r["success"] for r in results), "results": results } async def _run_tests(self, tests: List[str], progress: Progress) -> Dict[str, Any]: """Run tests to validate implementation.""" if not tests: logger.info("No tests to run.") return {"success": True, "message": "No tests to run."} test_task = progress.add_task("[blue]Running tests...[/blue]", total=len(tests)) results = [] for test in tests: progress.update(test_task, description=f"[blue]Testing: {test}[/blue]") try: # Implémenter l'exécution réelle des tests ici test_result = await self.groq.run_test(test) results.append(test_result) progress.advance(test_task) logger.info(f"Test '{test}' executed successfully.") except Exception as e: logger.error(f"Error running test '{test}': {str(e)}") results.append({"success": False, "message": str(e)}) progress.advance(test_task) overall_success = all(r["success"] for r in results) if overall_success: logger.info("All tests passed successfully.") return {"success": True, "test_results": results} else: logger.warning("Some tests failed.") return {"success": False, "test_results": results} async def _validate_implementation(self, result: Dict[str, Any]) -> Dict[str, Any]: """Validate the implementation results.""" if result.get("success"): validation_message = "Implementation completed and validated successfully." console.print(f"[green]{validation_message}[/green]") logger.info(validation_message) return {"success": True, "message": validation_message} else: validation_message = f"Implementation failed: {result.get('message')}" console.print(f"[red]{validation_message}[/red]") logger.error(validation_message) return {"success": False, "message": validation_message}
Gestion des Exceptions Personnalisées:
AIEngineerError
, RequestUnderstandingError
, etc.) pour une gestion plus fine et spécifique des erreurs.Utilisation du Module logging
:
console.print
par des appels au module logging
.ai_engineer.log
) et dans la console.INFO
, ERROR
, WARNING
, etc.) permettent de filtrer et de prioriser les messages.Amélioration des Docstrings et des Annotations de Type:
Optimisation Asynchrone:
async with
pour la gestion du contexte de Progress
, garantissant que les ressources sont correctement libérées.Implémentation Complète des Handlers de Tâches:
_create_component
, _modify_code
, etc.).Séparation des Responsabilités:
Validation et Exécution des Tests:
_run_tests
améliorée pour gérer l'exécution des tests de manière robuste, avec gestion des erreurs et logs appropriés.Utilisation de dataclasses
(Optionnel):
dataclasses
pourrait simplifier la gestion et la manipulation des données.Tests Unitaires:
AIEngineer
afin de garantir le bon fonctionnement et faciliter les futures modifications.Configuration Externe:
Documentation:
Gestion des Concurrences:
asyncio.gather
).Sécurité:
En intégrant ces améliorations, votre classe AIEngineer
sera plus robuste, maintenable et prête à gérer des tâches de développement complexes de manière efficace. N'hésitez pas à adapter ces suggestions en fonction des besoins spécifiques de votre projet.