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
21.11k stars 2.93k forks source link

How can you make a crew start a new task when the old one is completed ? #1002

Closed P7201 closed 2 months ago

P7201 commented 3 months ago

How can you make a crew start a new task when the old one is completed ? I have a crew that I've built that does a research paper literature review, takes in a query, combines the papers and answers the query. When that task is completed and the report is submitted to the user by the final agent it would be great if the user were asked if they would like to submit more papers for additional queries. How do you have the crew kickoff again after its task is complete for a new task without manually ending the present run and restarting the application? Sort of how in a simple python app you can ask the user for input if they want to repeat a task or end it.

shnoman97 commented 3 months ago

To achieve this behavior in CrewAI, you can set up a loop within the kickoff process that asks the user if they want to continue with a new task once the current task is completed. This would involve handling the user input dynamically and reinitializing the crew with new tasks based on the user's response.

Here's a high-level plan for how you can implement this:

  1. Create a function to handle user input for continuing or ending the process.
  2. Wrap the crew kickoff process in a loop that checks the user's input.
  3. Reinitialize the crew with new tasks if the user wants to continue.

Below is an example implementation in Python:

Step 1: Create a function to handle user input

This function will ask the user if they want to continue with a new task and return their response.

def ask_user_to_continue():
    while True:
        user_input = input("Would you like to submit more papers for additional queries? (yes/no): ").strip().lower()
        if user_input in ["yes", "no"]:
            return user_input == "yes"
        else:
            print("Please enter 'yes' or 'no'.")

Step 2: Initialize the crew and tasks

Set up your crew and initial tasks as you normally would.

Step 3: Wrap the kickoff process in a loop

Create a loop that kicks off the crew, waits for completion, and then checks if the user wants to continue.

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

# Assuming you have a SerperDevTool configured
search_tool = SerperDevTool()

def create_crew_and_tasks(query):
    researcher = Agent(
        role='Researcher',
        goal='Conduct literature review on {query}',
        verbose=True,
        memory=True,
        backstory=(
            "You are an expert in literature review, skilled at finding and synthesizing information from various sources."
        ),
        tools=[search_tool]
    )

    review_task = Task(
        description=(
            "Perform a thorough literature review on the topic '{query}'."
            "Compile the most relevant papers and provide a comprehensive report."
        ),
        expected_output='A detailed report on the literature review for {query}.',
        tools=[search_tool],
        agent=researcher,
    )

    crew = Crew(
        agents=[researcher],
        tasks=[review_task],
        process=Process.sequential
    )

    return crew

def main():
    os.environ["SERPER_API_KEY"] = "Your_SerperDevTool_API_Key"
    os.environ["OPENAI_API_KEY"] = "Your_OpenAI_API_Key"

    while True:
        query = input("Enter the query for literature review: ")
        crew = create_crew_and_tasks(query)
        result = crew.kickoff(inputs={'query': query})
        print(result)

        if not ask_user_to_continue():
            print("Ending the process.")
            break

if __name__ == "__main__":
    main()

Explanation:

P7201 commented 3 months ago

Thanks for responding. Where are you calling th eask_user_to_continue() function in the main file? I'm going to have to really think this out, I have several agents with many custom tools I've written. At the moment I'm using Holoviz Panel for a front end so I'm having the added complexity of passing the messages and user input back and forth via a front-end interface. I'm going to put all the crewai code behind a Flask api and write a nextjs front-end. One other thing is that crewai really needs an interactive proxy agent like Autogen.