Regenerate all MDC rules with fresh Exa data and December 2025 best practices

- Refreshed all 241 library rules with current best practices
- Updated Exa search results for all libraries with 2025 content
- Fixed --regenerate-all flag logic to properly process all libraries
- Removed duplicate astro entry in rules.json (merged tags)
- Added missing docker.mdc and vim.mdc files
- All rules now include modern patterns and anti-patterns
- Updated with framework-specific recommendations for 2025

Technical improvements:
- Fixed retry_failed_only logic in generate_mdc_files.py
- Improved progress tracking for regeneration scenarios
- Ensured proper handling of --refresh-exa flag
This commit is contained in:
sanjeed5
2025-12-17 22:40:28 +05:30
parent 331fb6ffac
commit 8fbf269085
244 changed files with 66701 additions and 52708 deletions
+252 -155
View File
@@ -1,166 +1,263 @@
---
description: Provides coding standards, best practices, and guidance specific to the AutoGen library, covering aspects from code structure to security and performance.
globs: *.py
description: This guide provides opinionated best practices for structuring, optimizing, and testing autogen multi-agent systems to ensure maintainability, reproducibility, and cost-efficiency.
globs: **/*.py
---
- Best practice 1: Employ Modular Design. Utilize AutoGen's multi-agent framework to create modular agents with specific roles. This enhances code reusability and simplifies the orchestration of complex workflows. Example: Create separate agents for data fetching, code execution, and response formatting.
- Best practice 2: Define Customizable Workflows. Leverage AutoGen's capabilities to define customizable workflows that integrate LLMs, human inputs, and tools. This allows for flexible agent interactions and enhances the performance of LLM applications. Implement a workflow manager to define and control agent interactions.
- Best practice 3: Ensure Code Quality and Consistency. Implement automatic code formatting and linting features provided by AutoGen to ensure consistent coding styles and practices. This reduces human error in code generation and maintains high code quality. Integrate linters like `pylint` or `flake8` in your CI/CD pipeline.
- Best practice 4: Integrate Human Feedback. Incorporate a Human Proxy Agent to facilitate human feedback in the workflow, ensuring that the AI systems remain aligned with user expectations and can adapt based on real-world input. Use AutoGen's `HumanProxyAgent` to gather and incorporate human feedback in the loop.
- Best practice 5: Use UV for dependency management for faster and more reliable installations.
- Best practice 6: Always use the latest stable version of Python (currently 3.12) for better performance and security.
- Best practice 7: Prefer using classes over standalone functions to encapsulate state and behavior, particularly for agents and tools.
- Best practice 8: Separate configuration from code using environment variables and configuration files to allow easy modification without altering the code base
# autogen Best Practices
## Library Information:
- Name: autogen
- Tags: ai, ml, llm, python, agent-framework, multi-agent
Building robust `autogen` applications requires disciplined adherence to specific patterns. This guide outlines the definitive best practices for our team, focusing on modularity, performance, and testability.
## Comprehensive Guide to AutoGen Best Practices
## 1. Code Organization and Agent Structure
### 1. Code Organization and Structure:
- **Directory Structure Best Practices:**
- `src/`: Contains the core AutoGen application code.
- `src/agents/`: Holds individual agent implementations (e.g., `code_writer_agent.py`, `data_analyst_agent.py`).
- `src/workflows/`: Defines workflows or agent interaction patterns (e.g., `code_generation_workflow.py`).
- `src/tools/`: Contains custom tools and utilities used by agents (e.g., `web_scraper.py`, `api_client.py`).
- `config/`: Stores configuration files (e.g., `agent_config.yaml`, `llm_config.json`).
- `tests/`: Contains unit and integration tests.
- `docs/`: Holds documentation.
- `examples/`: Example scripts and notebooks showcasing AutoGen's capabilities.
- **File Naming Conventions:**
- Use descriptive names with snake_case (e.g., `code_executor_agent.py`, `data_processing_tool.py`).
- Configuration files should use `.yaml` or `.json` extensions (e.g., `agent_config.yaml`).
- Test files should be named `test_<module_name>.py` (e.g., `test_code_executor.py`).
- **Module Organization Best Practices:**
- Group related functionalities into modules (e.g., `agents`, `workflows`, `tools`).
- Use `__init__.py` files to define packages and control imports.
- Implement clear and concise module-level documentation.
- **Component Architecture Recommendations:**
- Separate agents, tools, and workflows into distinct components.
- Use interfaces or abstract base classes to define contracts between components.
- Implement a central registry or dependency injection mechanism to manage component dependencies.
- **Code Splitting Strategies:**
- Split large agent implementations into smaller, more manageable classes or functions.
- Use lazy loading or dynamic imports to load components only when needed.
- Implement a plugin system to allow external components to be added to the application.
Organize your agents into dedicated modules, ensuring each agent has a single, well-defined responsibility. This mirrors traditional software architecture and enhances maintainability.
### 2. Common Patterns and Anti-patterns:
- **Design Patterns:**
- **Factory Pattern:** Create agents and tools dynamically based on configuration or user input.
- **Strategy Pattern:** Implement different agent behaviors or tool execution strategies based on the current context.
- **Observer Pattern:** Allow agents to subscribe to events or notifications from other agents or tools.
- **Recommended Approaches for Common Tasks:**
- **Code Generation:** Use AutoGen's code generation capabilities to automate boilerplate code creation or data transformation tasks. Example: Automatically generate data validation functions based on schema definitions.
- **Data Analysis:** Implement agents that can analyze data, generate visualizations, and extract insights. Example: Create an agent that analyzes website traffic data and identifies potential areas for improvement.
- **Workflow Orchestration:** Define workflows that automate complex multi-step tasks involving multiple agents and tools. Example: Implement a workflow that automatically generates, tests, and deploys code changes.
- **Anti-patterns and Code Smells:**
- **God Agent:** Avoid creating a single agent that performs too many tasks. Decompose complex tasks into smaller, more manageable agents.
- **Tight Coupling:** Minimize dependencies between agents and tools. Use interfaces or abstract base classes to decouple components.
- **Code Duplication:** Extract common functionalities into reusable functions or classes.
- **State Management Best Practices:**
- Use a central state management system to store and manage the application's state.
- Implement versioning or snapshotting to track state changes over time.
- Use immutable data structures to prevent unintended state modifications.
- **Error Handling Patterns:**
- Implement robust error handling mechanisms to catch and handle exceptions.
- Use logging to record errors and debug issues.
- Implement retry logic to handle transient errors.
❌ BAD: Monolithic agent definitions or generic names.
```python
# agents.py
from autogen import AssistantAgent, UserProxyAgent
### 3. Performance Considerations:
- **Optimization Techniques:**
- **Caching:** Cache frequently accessed data to reduce latency and improve throughput.
- **Parallelization:** Use asynchronous programming or multithreading to parallelize tasks and improve performance.
- **Code Optimization:** Profile and optimize code to reduce execution time.
- **Memory Management:**
- Use generators or iterators to process large datasets in chunks.
- Release resources as soon as they are no longer needed.
- Avoid creating unnecessary copies of data.
- **Bundle Size Optimization (if applicable):** N/A for a Python Library, but if creating web apps with autogen, use tree-shaking to remove unused code and minimize bundle size.
- **Lazy Loading:** Load modules and components only when needed to reduce startup time and memory usage.
# Too many responsibilities, unclear role
def create_complex_agent(llm_config):
agent = AssistantAgent(
name="GenericAgent",
llm_config=llm_config,
system_message="I can do anything you ask."
)
return agent
```
### 4. Security Best Practices:
- **Common Vulnerabilities:**
- **Prompt Injection:** Sanitize user inputs to prevent malicious code from being injected into agent prompts.
- **Unauthorized Access:** Implement access controls to restrict access to sensitive data and functionalities.
- **Data Leaks:** Protect sensitive data from being exposed in logs or error messages.
- **Input Validation:**
- Validate all user inputs to ensure that they conform to the expected format and range.
- Use allow lists to restrict the types of inputs that are allowed.
- Sanitize inputs to remove potentially harmful characters or code.
- **Authentication and Authorization:**
- Implement authentication mechanisms to verify the identity of users or agents.
- Use authorization policies to control access to resources and functionalities.
- Use role-based access control (RBAC) to simplify access management.
- **Data Protection:**
- Encrypt sensitive data at rest and in transit.
- Use secure storage mechanisms to protect data from unauthorized access.
- Implement data masking or anonymization techniques to protect sensitive data in logs or error messages.
- **Secure API Communication:**
- Use HTTPS to encrypt API communication.
- Authenticate and authorize API requests.
- Validate API responses to prevent data tampering.
✅ GOOD: Dedicated modules, clear roles, and explicit system messages.
```python
# agents/planner.py
from autogen import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient
### 5. Testing Approaches:
- **Unit Testing:**
- Write unit tests for individual agents, tools, and workflows.
- Use mocking or stubbing to isolate components during testing.
- Test different scenarios and edge cases.
- **Integration Testing:**
- Write integration tests to verify the interaction between different components.
- Test the integration with external systems or APIs.
- Use test data that is representative of real-world data.
- **End-to-End Testing:**
- Write end-to-end tests to verify the overall functionality of the application.
- Simulate user interactions and verify that the application behaves as expected.
- Use automated testing frameworks to automate the testing process.
- **Test Organization:**
- Organize tests into separate directories or files based on the component being tested.
- Use descriptive names for test functions or classes.
- Follow a consistent testing convention.
- **Mocking and Stubbing:**
- Use mocking to replace external dependencies with mock objects.
- Use stubbing to replace complex functionalities with simple stubs.
- Use mocking frameworks to simplify the mocking process.
def create_planner_agent(model_client: OpenAIChatCompletionClient) -> AssistantAgent:
"""
Creates an agent responsible for breaking down complex tasks into actionable steps.
"""
return AssistantAgent(
name="TaskPlanner",
model_client=model_client,
description="An expert in breaking down complex problems into a sequence of manageable sub-tasks.",
system_message=(
"You are a meticulous TaskPlanner. Your sole responsibility is to decompose user requests "
"into a clear, ordered list of steps. Do not execute tasks, only plan them."
)
)
### 6. Common Pitfalls and Gotchas:
- **Frequent Mistakes:**
- Not properly configuring LLM parameters (e.g., temperature, max tokens).
- Overly complex prompts that confuse the LLM.
- Ignoring error messages and logs.
- Not validating user inputs.
- **Edge Cases:**
- Handling unexpected LLM responses.
- Dealing with rate limits or API outages.
- Handling large datasets or complex workflows.
- **Version-Specific Issues:**
- Be aware of breaking changes in AutoGen updates.
- Check the release notes for known issues or workarounds.
- **Compatibility Concerns:**
- Ensure that AutoGen is compatible with other libraries or frameworks being used in the project.
- Check for conflicts between different versions of AutoGen.
- **Debugging Strategies:**
- Use logging to track the execution flow and identify errors.
- Use a debugger to step through the code and inspect variables.
- Use print statements to output intermediate results.
# agents/coder.py
from autogen import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient
### 7. Tooling and Environment:
- **Recommended Development Tools:**
- Python IDE (e.g., VS Code, PyCharm).
- Version control system (e.g., Git).
- Dependency management tool (e.g., pip, uv).
- **Build Configuration:**
- Use a build system to automate the build process.
- Configure the build system to run linters and tests.
- Use a virtual environment to isolate dependencies.
- **Linting and Formatting:**
- Use linters to enforce coding style guidelines.
- Use formatters to automatically format code.
- Integrate linters and formatters into the development workflow.
- **Deployment:**
- Use a deployment platform to deploy the application to a production environment.
- Configure the deployment platform to automatically scale the application.
- Use a monitoring system to track the application's performance.
- **CI/CD Integration:**
- Use a CI/CD pipeline to automate the build, test, and deployment process.
- Integrate the CI/CD pipeline with the version control system.
- Use automated testing to ensure code quality.
def create_coder_agent(model_client: OpenAIChatCompletionClient) -> AssistantAgent:
"""
Creates an agent capable of writing and executing Python code.
"""
return AssistantAgent(
name="PythonCoder",
model_client=model_client,
description="An expert Python programmer capable of writing, executing, and debugging code.",
system_message=(
"You are an expert Python programmer. You write clean, efficient, and well-tested code. "
"When asked to solve a problem, provide the Python code in a markdown block. "
"If execution is needed, state 'EXECUTE' after the code block."
)
)
```
## 2. LLM Caching for Reproducibility and Cost Efficiency
ALWAYS wrap your agent interactions in a caching context manager. This is non-negotiable for reproducible results and to prevent excessive API costs during development and testing.
❌ BAD: No caching, leading to variable outputs and repeated API calls.
```python
# In your main script
user_proxy.initiate_chat(assistant, message="Analyze data.") # Each run hits the LLM
```
✅ GOOD: Explicit caching with `Cache.disk()` or `Cache.redis()`.
```python
from autogen import Cache
from autogen_ext.models.openai import OpenAIChatCompletionClient
import os
# Configure your LLM client once
llm_client = OpenAIChatCompletionClient(
model="gpt-4o-mini",
api_key=os.environ.get("OPENAI_API_KEY")
)
# Create agents using the configured client
# (Assuming create_planner_agent and UserProxyAgent are defined)
planner = create_planner_agent(llm_client)
user_proxy = UserProxyAgent(
name="User",
human_input_mode="NEVER",
code_execution_config={"use_docker": False} # Or True with proper setup
)
# Use disk caching for development and testing
with Cache.disk(cache_path_root=".autogen_cache") as cache:
user_proxy.initiate_chat(
planner,
message="Develop a Python script to calculate the factorial of 5.",
cache=cache # Pass the cache object
)
# For production or shared environments, consider RedisCache
# with Cache.redis(redis_url="redis://localhost:6379/0") as cache:
# user_proxy.initiate_chat(planner, message="...", cache=cache)
```
**Note**: Set `cache_seed=None` in `llm_config` to explicitly disable caching for an agent, but this should be rare and justified.
## 3. Type Hints for Clarity and Maintainability
Utilize Python type hints extensively. This improves code readability, enables static analysis, and reduces bugs in complex multi-agent systems.
❌ BAD: Untyped function signatures.
```python
def setup_conversation(agent1, agent2, task):
# ... logic ...
pass
```
✅ GOOD: Fully typed function signatures.
```python
from autogen import Agent, GroupChatManager, UserProxyAgent
from typing import List
def setup_conversation(
agents: List[Agent],
manager: GroupChatManager,
initial_message: str
) -> None:
"""
Initiates a multi-agent conversation with a given manager and agents.
"""
user_proxy = UserProxyAgent(
name="User",
human_input_mode="NEVER",
code_execution_config={"use_docker": False}
)
user_proxy.initiate_chat(manager, message=initial_message)
```
## 4. Robust Testing Approaches
Implement a multi-layered testing strategy:
1. **Unit Tests**: For individual tool functions and agent helper methods.
2. **Integration Tests**: For multi-agent conversations, ensuring workflows complete as expected. Leverage caching to make these tests fast and deterministic.
```python
# tests/test_tools.py
import pytest
# Example tool function
def calculate_square(number: int) -> int:
return number * number
def test_calculate_square():
assert calculate_square(5) == 25
assert calculate_square(0) == 0
assert calculate_square(-2) == 4
# tests/test_workflow.py
import pytest
from autogen import Cache, GroupChatManager, UserProxyAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient
import os
from agents.planner import create_planner_agent
from agents.coder import create_coder_agent
@pytest.fixture(scope="module")
def llm_client():
return OpenAIChatCompletionClient(
model="gpt-4o-mini",
api_key=os.environ.get("OPENAI_API_KEY")
)
@pytest.fixture(scope="module")
def planner_agent(llm_client):
return create_planner_agent(llm_client)
@pytest.fixture(scope="module")
def coder_agent(llm_client):
return create_coder_agent(llm_client)
def test_factorial_workflow(planner_agent, coder_agent):
user_proxy = UserProxyAgent(
name="User",
human_input_mode="NEVER",
code_execution_config={"use_docker": False}
)
manager = GroupChatManager(
agents=[user_proxy, planner_agent, coder_agent],
messages=[],
llm_config={"model": "gpt-4o-mini", "api_key": os.environ.get("OPENAI_API_KEY")}
)
with Cache.disk(cache_path_root=".autogen_test_cache") as cache:
user_proxy.initiate_chat(
manager,
message="Calculate the factorial of 5 using Python.",
cache=cache
)
# Assert that the final message contains the correct answer
final_message = manager.last_message()["content"]
assert "120" in final_message
```
## 5. Environment Management and Packaging
While not `autogen`-specific, these are crucial for any Python project.
* **Virtual Environments**: ALWAYS use `venv` or `conda` for dependency isolation.
```bash
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```
* **Packaging**: For reusable agents or applications, define `pyproject.toml` (Poetry/Rye) or `setup.py` for proper distribution.
```toml
# pyproject.toml (example snippet)
[project]
name = "my-autogen-app"
version = "0.1.0"
dependencies = [
"pyautogen~=0.2.8", # Pin to specific versions
"autogen-ext[openai]",
"diskcache",
"python-dotenv",
# ... other dependencies
]
```
## 6. Secure API Key Handling
NEVER hardcode API keys. Use environment variables.
❌ BAD: Hardcoded API key.
```python
llm_config = {"model": "gpt-4", "api_key": "sk-YOUR_HARDCODED_KEY"}
```
✅ GOOD: Environment variables.
```python
import os
from dotenv import load_dotenv
load_dotenv() # Load from .env file in development
llm_config = {
"model": "gpt-4",
"api_key": os.environ.get("OPENAI_API_KEY")
}
if not llm_config["api_key"]:
raise ValueError("OPENAI_API_KEY environment variable not set.")
```
```
# .env file (add to .gitignore)
OPENAI_API_KEY=sk-your-actual-openai-key-here
```
By adhering to these guidelines, our `autogen` projects will be maintainable, performant, and reliable.