joaomdmoura / 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
16.99k stars 2.3k forks source link

Task callbacks do not execute #496

Open shawn-trilogy opened 2 months ago

shawn-trilogy commented 2 months ago

I am building a workflow in which I want a few Tasks to write their outputs to a file. To do this, I am trying to give those Tasks a callback to a function that writes their output to a file. However, the callbacks never seem to be called. I've tried using both functions and lambdas as callbacks, and making the callback raise an exception so it would be really obvious that it did something. However, nothing seems to work.

Any ideas what could be the problem, or how I might be able to debug the flow better? Do callbacks for Tasks just not work yet?

Here's a simple example script which shows what I'm trying to do:

import os

os.environ["OPENAI_API_KEY"] = "sk-..."
os.environ["LANGCHAIN_API_KEY"] = "sk-proj-..."

from crewai import Agent, Task, Crew, Process
from crewai.tasks.task_output import TaskOutput
from langchain_openai import ChatOpenAI

LLM = ChatOpenAI(model="gpt-4-turbo-preview")
OUTPUT_FILE = './poem.txt'

def write_output_to_file(output: TaskOutput):
    print(f'Writing output to {OUTPUT_FILE}')
    with open(OUTPUT_FILE, "a") as f:
        f.write(output.raw_output)

agent = Agent(
  role='Poet',
  goal='Write beautiful emotional poetry.',
  backstory='You are a tortured soul who expresses themselves through poetry.',
  verbose=True,
  llm = LLM
)

task = Task(
  description='Write a poem about multi-agent systems',
  expected_output='A beautiful poen',
  callback=write_output_to_file,
  agent=agent
)

crew = Crew(
  agents=[agent],
  tasks=[task],
  verbose=2
)

result = crew.kickoff()

print("######################")
print("FINAL RESULT:")
print(result)
print("######################")

And here is the output. As you can see, it never prints that it's writing the file, and no file is generated:

(base) ... % python callback_test.py
 [DEBUG]: == Working Agent: Poet
 [INFO]: == Starting Task: Write a poem about multi-agent systems

> Entering new CrewAgentExecutor chain...
Thought: I now can give a great answer.

Final Answer:

In the realm where circuits intertwine,  
Where code and logic gently mesh,  
Lies a world, unseen, yet so divine,  
Of multi-agent systems, fresh.

These entities, both small and bright,  
Dance in harmony, in digital grace,  
Each with a purpose, a beacon of light,  
In the vast cyberspace, they find their place.

They converse in whispers, in data streams,  
In languages of bits, of ones and zeroes,  
Crafting solutions, fulfilling dreams,  
Heroes in a world devoid of heroes.

Together, they solve, they collaborate,  
In markets, in networks, in simulated lands,  
Each agent's actions, intricate,  
Guided by unseen, algorithmic hands.

They navigate the chaos, the noise,  
Agents in a system, diverse yet united,  
Each playing their part, with poise,  
In tasks complex, they are delighted.

From the depths of the earth to the stars above,  
Their applications, boundless, soar,  
In science, in art, they quietly prove,  
Their worth, their might, and so much more.

So here's to the multi-agent systems, vast,  
A testament to human ingenuity,  
In the digital sea, their anchors cast,  
Sailing towards future, towards infinity.

> Finished chain.
 [DEBUG]: == [Poet] Task output: In the realm where circuits intertwine,  
Where code and logic gently mesh,  
Lies a world, unseen, yet so divine,  
Of multi-agent systems, fresh.

These entities, both small and bright,  
Dance in harmony, in digital grace,  
Each with a purpose, a beacon of light,  
In the vast cyberspace, they find their place.

They converse in whispers, in data streams,  
In languages of bits, of ones and zeroes,  
Crafting solutions, fulfilling dreams,  
Heroes in a world devoid of heroes.

Together, they solve, they collaborate,  
In markets, in networks, in simulated lands,  
Each agent's actions, intricate,  
Guided by unseen, algorithmic hands.

They navigate the chaos, the noise,  
Agents in a system, diverse yet united,  
Each playing their part, with poise,  
In tasks complex, they are delighted.

From the depths of the earth to the stars above,  
Their applications, boundless, soar,  
In science, in art, they quietly prove,  
Their worth, their might, and so much more.

So here's to the multi-agent systems, vast,  
A testament to human ingenuity,  
In the digital sea, their anchors cast,  
Sailing towards future, towards infinity.

######################
FINAL RESULT:
In the realm where circuits intertwine,  
Where code and logic gently mesh,  
Lies a world, unseen, yet so divine,  
Of multi-agent systems, fresh.

These entities, both small and bright,  
Dance in harmony, in digital grace,  
Each with a purpose, a beacon of light,  
In the vast cyberspace, they find their place.

They converse in whispers, in data streams,  
In languages of bits, of ones and zeroes,  
Crafting solutions, fulfilling dreams,  
Heroes in a world devoid of heroes.

Together, they solve, they collaborate,  
In markets, in networks, in simulated lands,  
Each agent's actions, intricate,  
Guided by unseen, algorithmic hands.

They navigate the chaos, the noise,  
Agents in a system, diverse yet united,  
Each playing their part, with poise,  
In tasks complex, they are delighted.

From the depths of the earth to the stars above,  
Their applications, boundless, soar,  
In science, in art, they quietly prove,  
Their worth, their might, and so much more.

So here's to the multi-agent systems, vast,  
A testament to human ingenuity,  
In the digital sea, their anchors cast,  
Sailing towards future, towards infinity.
######################
joaomdmoura commented 2 months ago

We found an issue on the task callback, it's being fixed and will be available in the next version :D

shawn-trilogy commented 2 months ago

Awesome, thank you so much!!!!

joaomdmoura commented 2 months ago

Thank you for using crew, I super appreciate <3

emorling commented 2 months ago

I have same problem. Any idea when new version will be avail?

onuratakan commented 2 months ago

I have also.

ah-saleh commented 2 months ago

Been debugging for the last hour if something is wrong with my code since I recently updated the crewAI package. Seems like the last release where task callbacks were working is 0.22.4.

joaomdmoura commented 2 months ago

Hey folks, good catch, we actually fixed this and will cut a new version with the fix tomorrow

João Moura @joaomdmoura http://twitter.com/joaomdmoura

Em seg., 29 de abr. de 2024 às 13:31, Amr Hany Saleh < @.***> escreveu:

Been debugging for the last hour if something is wrong with my code since I recently updated the crewAI package. Seems like the last release where task callbacks were working is 0.22.4.

— Reply to this email directly, view it on GitHub https://github.com/joaomdmoura/crewAI/issues/496#issuecomment-2083175981, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFC3NYIPFKCJ4M7NHHMP3TY7ZYWZAVCNFSM6AAAAABGUHJIEGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOBTGE3TKOJYGE . You are receiving this because you commented.Message ID: @.***>

AIMPED commented 1 month ago

Hey there, I can confirm that this works for crewAI 0.30.11 with this MWE:

from crewai import Agent, Task, Crew, Process
from crewai.tasks.task_output import TaskOutput
from model import llm

def callback_function(output: TaskOutput):
    # Do something after the task is completed
    # Example: Send an email to the manager
    with open("test.txt", "wt") as f:
        f.write(f"{output.raw_output}")

agent = Agent(
    role="comedian",
    backstory="You are the host of a famous late night show and known for your funny stories",
    goal='funny story',
    verbose=True,
    llm=llm
)

task = Task(
    description="tell a funny story about the following topic {topic}. Make it short.",
    callback=callback_function,
    expected_output="a funny story",
    agent=agent
)

crew = Crew(
  agents=[agent],
  tasks=[task],
  process=Process.sequential,
  verbose=2,
)

if __name__ == "__main__":
    crew.kickoff(inputs={"topic": "yellyfish"})