Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support LLM managers that use model instead of model_name #1887

Closed
wants to merge 2 commits into from

Conversation

y4izus
Copy link

@y4izus y4izus commented Jan 13, 2025

When trying to use a hierarchical process with ChatOllama from langchain_ollama as manager_llm I get this error: litellm.exceptions.BadRequestError: litellm.BadRequestError: LLM Provider NOT provided. Pass in the LLM provider you are trying to call. You passed model=model='ollama/llama3'.

ChatOllama requires to pass the model using the param model, while _create_manager_agent expects a model_name. As a result, all the param, included the model= part is passed as a provider.

This PR adds the options to get the model if the name of the param used is model

@joaomdmoura
Copy link
Collaborator

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

Code Review Comment for PR #1887

Overview

This pull request enhances the manager agent creation functionality in crew.py by adding support for an additional model attribute lookup. The changes are primarily within the _create_manager_agent method.

Changes Made

The new code introduces a refined attribute lookup mechanism:

self.manager_llm = (
    getattr(self.manager_llm, "model_name", None)
    or getattr(self.manager_llm, "model", None)  # New addition
    or getattr(self.manager_llm, "deployment_name", None)
    or self.manager_llm
)

Positive Aspects

  • Backward Compatibility: Maintains existing functionality by preserving the original fallback method.
  • Pattern Adherence: Utilizes established attribute access patterns congruent with the existing codebase.
  • Fallback Resilience: The use of or operator ensures that if one attribute is nonexistent, the next is checked, preventing errors during agent creation.
  • Flexibility for LLM Implementations: By including the "model" attribute, it addresses potential use cases where different implementations of LLMs might use various attribute names.

Suggestions for Improvement

  1. Documentation Update: It is advised to enhance the docstring of the _create_manager_agent method to clearly describe the newly checked attributes:

    def _create_manager_agent(self):
        """
        Creates the manager agent for the crew.
        
        Attempts to retrieve the model identifier in the following order:
        1. model_name
        2. model
        3. deployment_name
        4. falls back to the original manager_llm
        """
  2. Validation Enhancements: The proposed validation mechanism ensures that the attributes not only exist but also have valid values:

    def _create_manager_agent(self):
        model_attrs = ["model_name", "model", "deployment_name"]
        for attr in model_attrs:
            if hasattr(self.manager_llm, attr):
                value = getattr(self.manager_llm, attr)
                if value is not None:
                    self.manager_llm = value
                    break
        else:
            self.manager_llm = self.manager_llm
  3. Adding Type Hints: Incorporating type hints for better readability and maintainability:

    def _create_manager_agent(self) -> Agent:
        """
        Creates and returns the manager agent.
        
        Returns:
            Agent: The configured manager agent instance
        """

Potential Issues to Consider

  • Model Identifier Validation: The absence of checks on the type/format of retrieved model identifiers can lead to inconsistencies.
  • Lack of Logging: Implementing logging to indicate which model attribute was successfully utilized would enhance debugging.
  • Error Handling: Explicit error handling for invalid configurations is recommended to avoid runtime discrepancies.

Testing Recommendations

  • Unit Tests: Add tests specifically for the introduced "model" attribute.
  • Diverse Use Cases: Test with various LLM implementations that may use different attribute names.
  • Edge Case Scenarios: Incorporate tests to handle cases where attribute values might be None or empty.

Summary

While the changes introduced in PR #1887 are beneficial and maintain existing operations, follow-up actions on validation, documentation enhancements, and testing will bolster the robustness and maintainability of the code. This PR is recommended for approval with the above improvements considered.

Conclusion

The modifications improve compatibility with different LLM implementations without compromising the existing functionality of the codebase. Implementing the suggested improvements will ensure high code quality and resilience against future changes or bugs.

@joaomdmoura
Copy link
Collaborator

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

Code Review Comment for PR #1887

Overview

This pull request proposes a modification to the _create_manager_agent method in src/crewai/crew.py, introducing support for an additional attribute, model, in the LLM object during manager agent creation. This enhancement allows for greater flexibility in the integration of varied LLM implementations.

Functionality Change

The change implements another fallback in the attribute lookup for initializing self.manager_llm:

self.manager_llm = (
    getattr(self.manager_llm, "model_name", None)
    or getattr(self.manager_llm, "model", None)  # New addition
    or getattr(self.manager_llm, "deployment_name", None)
    or self.manager_llm
)

The intention here is clear—enabling the code to work seamlessly across different configurations of LLMs.

Code Quality Assessment

Positive Aspects

  • The code maintains an organized fallback pattern, which enhances robustness.
  • The modification remains minimal yet impactful, preserving existing coding standards and conventions.

Suggestions for Improvement

  1. Documentation:
    Adding inline comments would improve readability and understanding. For instance:

    self.manager_llm = (
        # Try to get model name from different possible attribute names
        getattr(self.manager_llm, "model_name", None)  # Standard model_name attribute
        or getattr(self.manager_llm, "model", None)    # Alternative model attribute
        or getattr(self.manager_llm, "deployment_name", None)  # Azure deployment name
        or self.manager_llm  # Fallback to original value
    )
  2. Type Hints:
    Implementing type hints can clarify expected data types and assist in both development and maintenance. For example:

    def _create_manager_agent(self) -> Agent:
        """
        Creates the manager agent with appropriate model configuration.
        Returns:
            Agent: The configured manager agent
        """
  3. Error Handling:
    To improve the resilience of the code, consider validating the type of manager_llm:

    def _create_manager_agent(self):
        if not hasattr(self, 'manager_llm'):
            raise ValueError("manager_llm attribute is required")
  4. Testing Considerations:
    Ensure the introduction of the new attribute lookup is covered by unit tests, focusing on:

    • Verification that the new fallback successfully retrieves expected attributes.
    • Edge cases when none of the attributes are found.

Final Recommendations

  1. I recommend accepting this pull request as it accurately enhances functionality without disrupting existing features.
  2. Follow-up tasks should be created to:
    • Improve the documentation surrounding the attribute checks.
    • Include type hints where applicable.
    • Develop unit tests for the new changes proposed.
    • Ensure robust error handling for misconfigured states.

This change enriches the flexibility of our system regarding LLM configurations while maintaining a clean, organized code structure. Implementing the proposed improvements will further enhance maintainability and robustness.

@bhancockio
Copy link
Collaborator

Good fix @y4izus!

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants