crewAIInc / crewAI

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
https://crewai.com
MIT License
20.25k stars 2.81k forks source link

[BUG] Open Source Homepage Example #1407

Closed LukeLalor closed 5 days ago

LukeLalor commented 1 week ago

Description

The example on https://www.crewai.com/open-source does not work. To be clear, the error is just a pydantic error meaning the example is likely out of date and just needs to be updated.

I am not asking how to make this example work, just pointing out that the example provided is broken.

Screenshot 2024-10-08 at 6 24 12 PM Screenshot 2024-10-08 at 6 24 09 PM Screenshot 2024-10-08 at 6 24 06 PM

python 3.12.4 crew 0.67.1

Combine this into a single example

from crewai import Agent, Task, Crew, Process

# Agent definition
researcher = Agent(
    role='Senior Researcher',
    goal='Uncover groundbreaking technologies in AI',
    backstory='Driven by curiosity, you explore and share the latest innovations.'
)

# Task definition
research_task = Task(
    description='Identify the next big trend in AI with pros and cons.',
    expected_output='A 3-paragraph report on emerging AI technologies.'
)

def main():
    print("forming crew")
    # Forming the crew and kicking off the process
    crew = Crew(
        agents=[researcher],
        tasks=[research_task],
        process=Process.sequential
    )

    print("running crew")
    result = crew.kickoff(inputs={'topic': 'AI in healthcare'})
    print(result)

if __name__ == "__main__":
    main()

and you get an error

pydantic_core._pydantic_core.ValidationError: 1 validation error for Crew
  Sequential process error: Agent is missing in the task with the following description: Identify the next big trend in AI with pros and cons. [type=missing_agent_in_task, input_value={'agents': [Agent(role=Se...quential: 'sequential'>}, input_type=dict]

Steps to Reproduce

  1. poetry new testcrew
  2. set version python = ">=3.10,<3.13"
  3. poetry add crewai
  4. vim testcrew script.py <- add example from description
  5. poetry run python crewtest/script.py

Expected behavior

Expected: It to work Actual:

Traceback (most recent call last):
  File "/Users/lukelalor/projects/crewtest/crewtest/script.py", line 33, in <module>
    main()
  File "/Users/lukelalor/projects/crewtest/crewtest/script.py", line 22, in main
    crew = Crew(
           ^^^^^
  File "/Users/lukelalor/Library/Caches/pypoetry/virtualenvs/crewtest-dgSFKtrv-py3.12/lib/python3.12/site-packages/pydantic/main.py", line 212, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for Crew
  Sequential process error: Agent is missing in the task with the following description: Identify the next big trend in AI with pros and cons. [type=missing_agent_in_task, input_value={'agents': [Agent(role=Se...quential: 'sequential'>}, input_type=dict]

Screenshots/Code snippets

from crewai import Agent, Task, Crew, Process

# Agent definition
researcher = Agent(
    role='Senior Researcher',
    goal='Uncover groundbreaking technologies in AI',
    backstory='Driven by curiosity, you explore and share the latest innovations.'
)

# Task definition
research_task = Task(
    description='Identify the next big trend in AI with pros and cons.',
    expected_output='A 3-paragraph report on emerging AI technologies.'
)

def main():
    print("forming crew")
    # Forming the crew and kicking off the process
    crew = Crew(
        agents=[researcher],
        tasks=[research_task],
        process=Process.sequential
    )

    print("running crew")
    result = crew.kickoff(inputs={'topic': 'AI in healthcare'})
    print(result)

if __name__ == "__main__":
    main()

Operating System

Sonoma 14.6.1

Python Version

3.12.4

crewAI Version

0.67.1

crewAI Tools Version

n/a

Virtual Environment

Poetry

Evidence

lukelalor@MacBook-Pro crewtest % poetry run python crewtest/script.py
/Users/lukelalor/Library/Caches/pypoetry/virtualenvs/crewtest-dgSFKtrv-py3.12/lib/python3.12/site-packages/pysbd/segmenter.py:66: SyntaxWarning: invalid escape sequence '\s'
  for match in re.finditer('{0}\s*'.format(re.escape(sent)), self.original_text):
/Users/lukelalor/Library/Caches/pypoetry/virtualenvs/crewtest-dgSFKtrv-py3.12/lib/python3.12/site-packages/pysbd/lang/arabic.py:29: SyntaxWarning: invalid escape sequence '\.'
  txt = re.sub('(?<={0})\.'.format(am), '∯', txt)
/Users/lukelalor/Library/Caches/pypoetry/virtualenvs/crewtest-dgSFKtrv-py3.12/lib/python3.12/site-packages/pysbd/lang/persian.py:29: SyntaxWarning: invalid escape sequence '\.'
  txt = re.sub('(?<={0})\.'.format(am), '∯', txt)
loading file
forming crew
Traceback (most recent call last):
  File "/Users/lukelalor/projects/crewtest/crewtest/script.py", line 33, in <module>
    main()
  File "/Users/lukelalor/projects/crewtest/crewtest/script.py", line 22, in main
    crew = Crew(
           ^^^^^
  File "/Users/lukelalor/Library/Caches/pypoetry/virtualenvs/crewtest-dgSFKtrv-py3.12/lib/python3.12/site-packages/pydantic/main.py", line 212, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for Crew
  Sequential process error: Agent is missing in the task with the following description: Identify the next big trend in AI with pros and cons. [type=missing_agent_in_task, input_value={'agents': [Agent(role=Se...quential: 'sequential'>}, input_type=dict]

Possible Solution

update the example with correct contract

Additional context

I am just pointing out that the example on the front page doesn't work, not necessarily trying to get this to work.

tonykipkemboi commented 5 days ago

Thanks for the feedback. We're updating the site with a working example.

For your code above try using this modified code which should work.

Just make sure to add your OPENAI_API_KEY and SERPER_API_KEY to your .env file in root.

from crewai import Agent, Task, Crew, Process, LLM
from crewai_tools import SerperDevTool
import os

llm = LLM(
    model="gpt-4o", 
    temperature=0.5, 
    api_key=os.environ.get("OPENAI_API_KEY")
)

# Agent definition
researcher = Agent(
    role='{topic} Senior Researcher',
    goal='Uncover groundbreaking technologies in {topic} for year 2024',
    backstory='Driven by curiosity, you explore and share the latest innovations.',
    tools=[SerperDevTool()],
    llm=llm
)

# Define a research task for the Senior Researcher agent
research_task = Task(
    description='Identify the next big trend in {topic} with pros and cons.',
    expected_output='A 3-paragraph report on emerging {topic} technologies.',
    agent=researcher
)

def main():
    # Forming the crew and kicking off the process
    crew = Crew(
        agents=[researcher],
        tasks=[research_task],
        process=Process.sequential,
        verbose=True
    )
    result = crew.kickoff(inputs={'topic': 'AI Agents'})
    print(result)

if __name__ == "__main__":
    main()