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
19k stars 2.62k forks source link

how to intercept human_input callback #1046

Closed obaid closed 1 month ago

obaid commented 1 month ago

In my example below, the custom_ask_human_input method never gets triggered the when the agent goes into human_tool. Any ideas why?

import time
import requests
from langchain_openai import ChatOpenAI
from crewai import Agent, Task, Crew, Process
from crewai.agents import CrewAgentExecutor
from langchain_community.agent_toolkits.load_tools import load_tools

# Custom function to handle human input
def custom_ask_human_input(self, final_answer: dict) -> str:
    prompt = self._i18n.slice("getting_input").format(final_answer=final_answer)

    # Do some custom logic here....

    # For now, just return a placeholder response
    return "Human input handled by backend"

# Override the method in CrewAgentExecutor
CrewAgentExecutor._ask_human_input = custom_ask_human_input

# Define agents and tasks
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
human_tools = load_tools(["human"])

interviewer = Agent(
    role='Interviewer',
    goal='making questions about business to human',
    backstory="You are an interviewer of businessmen.",
    llm=llm,
    verbose=True,
    allow_delegation=False,
    tools=human_tools
)

writer = Agent(
    role='Writer',
    goal='Summarizing interview',
    backstory="You are a writer and your expertise lies in summarizing interviews.",
    llm=llm,
    verbose=True,
    allow_delegation=False
)

interviewer_task = Task(
    description="""
Your task is to interview successful entrepreneurs. 
Ask three business questions. Ask one question at a time.
When three questions have been answered by the interviewee, conclude by responding with a list of questions and answers, followed by "finish" on a new line.
To ask a question, use the "human" tool and provide the question to be asked as the "Action Input".
""",
    max_inter=3,
    expected_output="a coherent question",
    agent=interviewer,
    tools=human_tools,
    human_input=True  # Enable human input
)

writer_task = Task(
    description="Summarize the content of the provided interview",
    max_inter=3,
    agent=writer,
    expected_output="a coherent summary",
    context=[interviewer_task]
)

items_crew = Crew(
    agents=[interviewer, writer],
    tasks=[interviewer_task, writer_task],
    process=Process.sequential,
    memory=True,
    verbose=True,
    full_output=True
)

# Function to start the crew
def start_crew():
    response = items_crew.kickoff()
    print("## Final Result\n" + response)

# Start the crew
start_crew()