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

fix: missing key name when running with ollama provider #1638

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

bowenliang123
Copy link

@bowenliang123 bowenliang123 commented Nov 21, 2024

Move the existance in unnacepted_attributes after ensuring key name not null, to fix the error when running with model provider ollama:

Traceback (most recent call last):
  File "/Users/bw/dev/play-crewai/ai_team/.venv/bin/run_crew", line 8, in <module>
    sys.exit(run())
  File "/Users/bw/dev/play-crewai/ai_team/src/ai_team/main.py", line 21, in run
    AiTeam().crew().kickoff(inputs=inputs)
  File "/Users/bw/dev/play-crewai/ai_team/.venv/lib/python3.10/site-packages/crewai/project/crew_base.py", line 35, in __init__
    self.map_all_task_variables()
  File "/Users/bw/dev/play-crewai/ai_team/.venv/lib/python3.10/site-packages/crewai/project/crew_base.py", line 145, in map_all_task_variables
    self._map_task_variables(
  File "/Users/bw/dev/play-crewai/ai_team/.venv/lib/python3.10/site-packages/crewai/project/crew_base.py", line 178, in _map_task_variables
    self.tasks_config[task_name]["agent"] = agents[agent_name]()
  File "/Users/bw/dev/play-crewai/ai_team/.venv/lib/python3.10/site-packages/crewai/project/utils.py", line 7, in memoized_func
    cache[key] = func(*args, **kwargs)
  File "/Users/bw/dev/play-crewai/ai_team/src/ai_team/crew.py", line 19, in researcher
    return Agent(
  File "/Users/bw/dev/play-crewai/ai_team/.venv/lib/python3.10/site-packages/pydantic/main.py", line 212, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
  File "/Users/bw/dev/play-crewai/ai_team/.venv/lib/python3.10/site-packages/crewai/agent.py", line 160, in post_init_setup
    if env_var["key_name"] in unnacepted_attributes:
KeyError: 'key_name'

The root cause is the line 160 of crewai/agent.py where it's getting value from env_var by attribute name ,

for env_var in env_vars:
    if env_var["key_name"] in unnacepted_attributes:

But ENV_VARS in crewai/cli/constants.py for ollama does not has a attribute name of key_name,

ENV_VARS = {

    "ollama": [
        {
            "default": True,
            "API_BASE": "http://localhost:11434",
        }
    ]

@joaomdmoura
Copy link
Collaborator

This is a review from a crew. I'm testing a crew for PRs, so if it sounds weird that is the reason, you are one of the first ones to see this. :) I'll try to personally look into it as well

Overview

This Pull Request introduces significant improvements to src/crewai/agent.py, primarily focusing on the validation of key names and formatting enhancements. The changes aim to alleviate potential issues related to environment variable handling while improving code readability.

Specific Code Improvements Suggested

  • Implement comprehensive error handling around the environment variable processing to log errors effectively, improving debugging capabilities.

    def post_init_setup(self):
        try:
            # Process environment variables
        except KeyError as e:
            logger.error(f"Invalid environment variable configuration: {e}")
        except Exception as e:
            logger.error(f"Error during initialization: {e}")
  • Add Type Hints across methods to enhance code clarity and facilitate static analysis tools to catch potential issues early.

    def execute_task(
        self,
        task: Task,
        context: Optional[str] = None,
    ) -> str:
        # ... rest of the code
  • Ensure consistent removal of trailing whitespace throughout the code.

  • Recommend adding logging for critical operations to assist in future debugging efforts.

  • Provide docstring documentation for all modified methods to improve code maintainability and clarity for future developers.

Historical Context

Although specific related PRs could not be retrieved for further insights, earlier pull requests (#1567, #1552, #1535, #1514, #1512) have previously addressed key areas relevant to agent functionality and environment variable management. Monitoring trends from these past changes can enhance understanding of ongoing improvements and issues relevant to this file.

Conclusion

The changes made in PR #1638 represent positive steps towards robust environment variable management within the crewAI framework. By incorporating additional recommendations for error handling, type hinting, and documentation, the project will greatly benefit from improved maintainability and reliability. Thank you for your contributions!

@bowenliang123
Copy link
Author

Hi @joaomdmoura, thanks for reviewing this PR.
I encoutered this problem with crewai 0.80.0 , creating a sample project as in get started docs but with ollama set MODEL=ollama/llama3.1 in .env file.

With this PR patched, it successfully runing with crewai run command.

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.

2 participants