Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task callbacks do not execute #496

Open
shawn-trilogy opened this issue Apr 23, 2024 · 7 comments
Open

Task callbacks do not execute #496

shawn-trilogy opened this issue Apr 23, 2024 · 7 comments

Comments

@shawn-trilogy
Copy link

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
Copy link
Owner

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

@shawn-trilogy
Copy link
Author

Awesome, thank you so much!!!!

@joaomdmoura
Copy link
Owner

joaomdmoura commented Apr 23, 2024

Thank you for using crew, I super appreciate <3

@emorling
Copy link

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

@onuratakan
Copy link

I have also.

@ah-saleh
Copy link

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
Copy link
Owner

joaomdmoura commented May 1, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants