Hi everyone! I’m a beginner and still learning how to use Crew.AI.
Problem:
I have a team that needs to process many requests. Each validation task saves its result to a dictionary called results
. After all tasks are complete, I want to combine the contents of results
and save them to a file with a custom name.
However, I’m not sure how to implement this properly.
Example code:
File crew.py
:
results = {}
def save_temp_result(task_name):
return lambda output: results.update({task_name: output})
@CrewBase
...
@task
def validator_1_task(self) -> Task:
return Task(
config=self.tasks_config['validator_1_task'],
callback=save_temp_result("validator_1_task")
)
...
@crew
def crew(self) -> Crew:
return Crew(
agents=self.agents,
tasks=self.tasks,
process=Process.sequential,
verbose=True
)
File main.py
:
import warnings
from clear_data.crew import ClearData
warnings.filterwarnings("ignore", category=SyntaxWarning, module="pysbd")
def run():
inputs = [
{'topic': '....', 'file_name': 'document_1'},
{'topic': '....', 'file_name': 'document_2'},
{'topic': '....', 'file_name': 'document_3'},
...
]
ClearData().crew().kickoff_for_each(inputs=inputs)
def test():
...
What I want:
The final task should combine all results from the results
dictionary and save them to a file. I don’t want to use AI for this because it takes too long, and I haven’t figured out how to pass the results
dictionary to the AI.
I tried the following approach:
@task
def validator_n_task(self) -> Task:
return Task(
config=self.tasks_config['validator_n_task'],
callback=save_temp_result("validator_n_task"),
callback=lambda _: self.combine_results()
)
def combine_results(??????):
file_name = ??????
combined_text = "\n".join(
f"{task}: {output}" for task, output in results.items()
)
with open(f"{file_name}.txt", "w", encoding="utf-8") as file:
file.write(combined_text)
print(f"Combined results saved in '{file_name}.md'.")
??????
— here I tried using self
, inputs
, and various combinations, but nothing worked.
How can I properly combine the results and save them to a file with a custom name?
Looking forward to any advice or suggestions. Thanks in advance!