crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

Agents llm log enhancer

Open orcema opened this issue 8 months ago • 4 comments

orcema avatar May 04 '25 05:05 orcema

Disclaimer: This review was made by a crew of AI Agents.

Code Review Comment for PR #2750 - Log Agent Specific LLM Assignment

Overview

The modifications introduced in this pull request significantly enhance the logging functionality by displaying LLM model information during the execution of agent executors. This is a valuable addition that supports debugging and performance tracking.

Detailed Findings & Suggestions

1. src/crewai/agents/crew_agent_executor.py

Code Improvements:

  • Spacing Consistency: Adjust spacing around function definitions for readability. For instance:

    def _show_logs(self, formatted_answer: Union[AgentAction, AgentFinish]):
        """Show logs for the agent's execution."""
        ...
    # Add a newline here for better readability
    def _summarize_messages(self) -> None:
    
  • Optimizing Import Order: Reorganize imports to follow PEP 8 standards. Example:

    # Standard library imports
    
    # Third-party imports
    from typing import Union
    
    # Local application imports
    from crewai.utilities.agent_utils import ...
    

2. src/crewai/utilities/agent_utils.py

Code Improvements:

  • Enhanced Type Hints: Ensure all functions, especially show_agent_llm_model, have appropriate type hints. Revise it as:

    def show_agent_llm_model(self: Any) -> None:
        ...
    
  • Error Handling: Introduce error handling around LLM model access to prevent runtime errors:

    try:
        if hasattr(self, "llm") and getattr(self.llm, "model", None):
            ...
    except AttributeError as e:
        logger.debug(f"Unable to display LLM model information: {str(e)}")
    

General Recommendations:

  1. Robust Error Handling: Ensure all attribute accesses are enclosed in try-catch blocks to avoid exceptions disrupting the flow, especially in show_agent_llm_model.

  2. Documentation Enhancements: Expand docstrings to include information about return values, exceptions, and usage examples, especially in utility functions.

  3. Move Color Codes to Constants: Consider defining color codes at the top of the file for reusability:

    ANSI_PURPLE = "\033[95m"
    ANSI_GREEN = "\033[92m"
    
  4. Unit Testing: Implement tests for the new logging functionality to cover various scenarios, including different configurations of the LLM and cases where attributes might be missing.

Historical Context

While I couldn't retrieve insights from previous PRs due to access issues, it's often beneficial to reference changes related to logging modifications. Reviewing how prior PRs have handled similar enhancements could yield useful patterns for error management and logging consistency.

Conclusion

Overall, the changes in this pull request are a positive step towards improving the logging mechanisms within the agent executors. Addressing the points highlighted in this review will significantly enhance code maintainability and robustness. Thank you for your contributions to improving our code quality!

joaomdmoura avatar May 04 '25 05:05 joaomdmoura

nice, but what about putting it console.formatter.py ?

lorenzejay avatar May 05 '25 22:05 lorenzejay

i don't get exactly the point about console.formatter.py ? why should this file be updated too with the enhanced logging ?

orcema avatar May 06 '25 18:05 orcema

Going to deprecate the old logger for the console_formatter.py as they are duplicating as all the logs are now deriving from event_listener. which is the tree structure

lorenzejay avatar May 06 '25 21:05 lorenzejay

i can't figure out why it blocs passing the tests for python 3.11 and i do not get an error message image

orcema avatar May 09 '25 10:05 orcema

i even tried to push a new branch https://github.com/crewAIInc/crewAI/pull/2800 but this one failed too on tests for the python version 3.11

orcema avatar May 11 '25 14:05 orcema

@orcema would you mind to address linter issues?

lucasgomide avatar May 12 '25 12:05 lucasgomide

@orcema would you mind to address linter issues?

I would like but i don't know how to address the issue in a correct manner.

My point is that when i try to run the tests according to crewai instructions https://github.com/crewAIInc/crewAI?tab=readme-ov-file#running-tests i get a lot of failed tests 7 errors

= 153 failed, 623 passed, 4 skipped, 116 warnings, 7 errors in 262.22s (0:04:22) = [274042 ms] postCreateCommand from devcontainer.json failed with exit code 1. Skipping any further user-provided commands.

I'm using .devcontainers with the with settings as here below:

{ "name": "CrewAI Development", "image": "mcr.microsoft.com/devcontainers/python:3.10", "features": { "ghcr.io/devcontainers-contrib/features/poetry:2": {} }, "remoteUser": "vscode", "postCreateCommand": "mkdir -p /home/vscode/.local/lib && pip install uv && uv lock && uv sync --dev --all-extras && uv run pytest --block-network --timeout=60 -vv", "customizations": { "vscode": { "extensions": [ "ms-python.python", "ms-python.vscode-pylance", "njpwerner.autodocstring" ], "settings": {} } }

orcema avatar May 13 '25 19:05 orcema

@orcema it's a flaky test

Regarding linter, it seems you have a single file with issues.. you can solve by running:

uv run ruff check --fix src/crewai/agents/crew_agent_executor.py

lucasgomide avatar May 14 '25 13:05 lucasgomide

This PR is stale because it has been open for 45 days with no activity.

github-actions[bot] avatar Aug 14 '25 12:08 github-actions[bot]

This PR is stale because it has been open for 45 days with no activity.

github-actions[bot] avatar Sep 30 '25 12:09 github-actions[bot]

Closing as stale

greysonlalonde avatar Oct 05 '25 17:10 greysonlalonde