crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

Add prompt_overrides to Crew

Open gabriels1234 opened this issue 1 year ago • 2 comments

This PR allows the Crew to receive prompt overrides for specific prompts (instead of one having to handle the entire json to the crew). This proves useful for:

  • Tweaking specific prompts for specific use cases.
  • Easier to test many prompt optimization assumptions on-the-fly.

        crew = Crew(
            agents=agents,
            tasks=tasks,
            process=Process.hierarchical,  # Using hierarchical process
            manager_agent=manager,  # Assigning the manager agent
            verbose=True,
            prompt_overrides={
                "slices": {"format": "Some custom prompt"}}
        )

gabriels1234 avatar Jun 02 '24 06:06 gabriels1234

Any help needed to move forward with this? Thanks!

gabriels1234 avatar Jun 10 '24 04:06 gabriels1234

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

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

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

Code Review Comment for PR #720: Add prompt_overrides

Overview

Thank you for your work on implementing the prompt_overrides feature within CrewAI, which significantly enhances our framework by allowing dynamic runtime customizations of prompts. This feature integrates into the existing architecture seamlessly but would benefit from a few improvements for robustness and clarity.

File-by-File Insights

1. src/crewai/crew.py

  • Positive Aspects:

    • The addition of the prompt_overrides field is well-typed and documented clearly, which aids developer understanding.
  • Specific Improvements:

    • Documentation: The existing description could be more detailed. Consider implementing this change for clarity:
      prompt_overrides: Optional[dict] = Field(
          default=None,
          description="Dictionary containing overrides for default prompts or prompt_file contents. Format: {'category': {'key': 'value'}}"
      )
      
    • Type Specification: Enhance the type hint to better reflect expected usage:
      from typing import Dict, Any
      prompt_overrides: Optional[Dict[str, Dict[str, str]]] = Field(
          default=None,
          description="overrides to prompt_file or default prompts.",
      )
      

2. src/crewai/utilities/i18n.py

  • Positive Aspects:

    • Excellent implementation of recursive merging for dictionaries, allowing for fine control over prompt customization.
  • Specific Improvements:

    • Method Documentation: Expand documentation for clarity on method functionality:
      @classmethod
      def merge_dicts(cls, d1: Dict[str, Any], d2: Dict[str, Any]) -> Dict[str, Any]:
          """
          Recursively merge two dictionaries, with d2 values taking precedence.
      
          Args:
              d1: Base dictionary to merge into
              d2: Dictionary whose values take precedence
          Returns:
              Merged dictionary
          """
          ...
      
    • Input Validation: Consider adding structured validation to ensure prompt_overrides conforms to expected formats:
      @model_validator(mode="after")
      def validate_prompt_overrides(self) -> "I18N":
          if self.prompt_overrides:
              if not isinstance(self.prompt_overrides, dict):
                  raise ValueError("prompt_overrides must be a dictionary")
              # Validate structure
              for category, prompts in self.prompt_overrides.items():
                  if not isinstance(prompts, dict):
                      raise ValueError(f"Category '{category}' must contain a dictionary of prompts")
          return self
      

3. tests/utilities/test_i18n.py

  • Positive Aspects:

    • The tests showcase good coverage of the new functionality and the expected behavior of overrides.
  • Specific Improvements:

    • Edge Cases: Incorporate additional tests that cover unexpected scenarios, such as deeply nested and invalid structures. Example:
      def test_prompt_overrides_edge_cases():
          ...
      

General Recommendations

  • Documentation: Expand the overall documentation, especially concerning the prompt_overrides implementation.
  • Error Handling: Add explicit error handling to address potential misconfigurations within prompt_overrides.
  • Performance Consideration: Review potential performance bottlenecks, especially around merging logic for large dictionaries.

Conclusion

Overall, this pull request introduces a valuable feature that enhances the functionality of CrewAI while allowing for improved flexibility in runtime prompt management. Adopting the suggestions for clearer documentation, robust validation, and comprehensive testing will enhance maintainability and usability. Great work, and looking forward to seeing these improvements integrated!

joaomdmoura avatar Dec 05 '24 17:12 joaomdmoura

Hey @gabriels1234, thanks for the Pull request.

Could you let me know if this PR is still relevant to your use case? Also, I’m a bit confused about the benefits of adding "prompt_overrides" since it seems like you already have that capability with the: prompt_file: Path to the prompt json file to be used for the crew.

Thanks

pythonbyte avatar Dec 09 '24 13:12 pythonbyte

You can overwrite the default prompts by updating the prompt_file property in a crew.

bhancockio avatar Dec 13 '24 21:12 bhancockio