crewAIInc / crewAI-examples

A collection of examples that show how to use CrewAI framework to automate workflows.
2.86k stars 1.09k forks source link

How to pass agent's output to another agent's input #168

Closed thanhtran0302 closed 3 weeks ago

thanhtran0302 commented 1 month ago

Hi everyone, I'm new to CrewAI, and I juste generated a project with CrewAI CLI. And I'm trying to pass the result of the first agent's task, to another agent's input unsuccessfully.

Here is my crew.py file

from crewai import Agent, Crew, Process, Task
from crewai.project import CrewBase, agent, crew, task

@CrewBase
class RefinaCrew():
    """Refina crew"""
    agents_config = 'config/agents.yaml'
    tasks_config = 'config/tasks.yaml'

    @agent
    def ternary_developer(self) -> Agent:
        return Agent(
            config=self.agents_config['ternary_developer'],
            verbose=True
        )

    @agent
    def reviewer_developer(self) -> Agent:
        return Agent(
            config=self.agents_config['reviewer_developer'],
            verbose=True
        )

    @task
    def transform_ugly_ternary(self) -> Task:
        return Task(
            config=self.tasks_config['transform_ugly_ternary'],
        )

    @task
    def review_code_quality(self) -> Task:
        return Task(
            config=self.tasks_config['review_code_quality'],
            agent=self.reviewer_developer()
        )

    @crew
    def crew(self) -> Crew:
        """Creates the Refina crew"""
        return Crew(
            agents=self.agents,
            tasks=self.tasks,
            process=Process.sequential,
            verbose=True,
            # process=Process.hierarchical, # In case you wanna use that instead https://docs.crewai.com/how-to/Hierarchical/
        )

Thank you !

joaomdmoura commented 1 month ago

Hey @thanhtran0302 result from one task is automatically passed into the next, but you can force specific context to be passed by using the context attribute.

@task
    def review_code_quality(self) -> Task:
        return Task(
            config=self.tasks_config['review_code_quality'],
            agent=self.reviewer_developer()
            context=[self. transform_ugly_ternary()]
        )
thanhtran0302 commented 3 weeks ago

This is perfect ! Thank you !