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?
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.
######################
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:
And here is the output. As you can see, it never prints that it's writing the file, and no file is generated: