How to access inputs in Task callbacks
carvalhorafael opened this issue · comments
I would like to access the value of an input inside a task's callback function.
Let me explain a little better.
Here is my main.py
starting the crew and passing a hash (my_hash
) as an input:
MyCrew().crew().kickoff(inputs={'my_hash': 'my hash here' )
My crew has several agents and several tasks. Here I will only bring one of them:
# Callback function
def my_callback(output: TaskOutput):
content = output.raw_output
print(f"Task output: {content}")
print(f"My Hash: {my_hash}") # I would like to access the input here
@CrewBase
class MyCrew():
...
@task
def my_task(self) -> Task:
return Task(
config=self.tasks_config['my_task'],
agent=self.my_agent(),
callback=my_callback # Callback function here
)
...
@crew
def crew(self) -> Crew:
return Crew(
agents=self.agents,
tasks=self.tasks,
process=Process.sequential,
verbose=2
)
As you can see in the code, I would like to access the value of my_hash
passed as input from the crew inside the task callback function.
How can I do this?
Here is another issue that could benefit from an answer #986
I found a solution, but I don't know if it's the best approach. I'll share it with you:
In my_crew.py
I did:
...
# Callback function receiving my_hash as parameter
def my_callback(output: TaskOutput, my_hash: str):
content = output.raw_output
print(f"Task output: {content}")
print(f"My Hash: {my_hash}")
@CrewBase
class MyCrew():
# saving the inputs in Class init
def __init__(self, inputs):
self.inputs = inputs
....
@task
def my_task(self) -> Task:
my_hash = self.inputs['my_hash']
return Task(
config=self.tasks_config['my_task'],
agent=self.my_agent(),
callback=self.create_callback(my_callback, my_hash)
)
# Wraps the callback to pass my_hash as parameter
def create_callback(self, my_callback, my_hash):
def wrapper(output: TaskOutput):
return my_callback(output, my_hash)
return wrapper
....
And in the main.py
file I did:
my_crew = MyCrew(inputs={'my_hash': 'my hash here'}) # to inputs to be saved at class init
my_crew.crew().kickoff(inputs={'my_hash': 'my hash here'})
As I said, with this approach I was able to pass the hash in input to a callback function.
Any feedback would be greatly appreciated.