Can we pass extra parameters in step_callback handler? #1487
-
I want to pass extra parameters to the step_callback handler to process the verbose data I got def write_callback_to_file(step_output, file_path: str = "output.txt"):
with open(file_path, 'a') as file:
# my logic
agent = Agent(
role=task_info.agent.role,
goal=task_info.agent.goal,
backstory=task_info.agent.backstory,
verbose=False,
llm=get_llm(),
step_callback=write_callback_to_file(file_path="my_custom_name.txt")
) is it possible to do something like this? |
Beta Was this translation helpful? Give feedback.
Answered by
pip-install-skills
Oct 24, 2024
Replies: 2 comments
-
from functools import partial
def write_callback_to_file(step_output, file_path: str = "output.txt"):
with open(file_path, 'a') as file:
# my logic
callback_partial_function = partial(write_callback_to_file, file_path="output1.txt")
agent = Agent(
role=task_info.agent.role,
goal=task_info.agent.goal,
backstory=task_info.agent.backstory,
verbose=False,
llm=get_llm(),
step_callback=callback_partial_function
) This worked for me, I needed to use partial from functools |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
pip-install-skills
-
This can be used for newer versions of crewai from crewai.agents.parser import AgentFinish
def write_output_to_file_callback(step_output: AgentFinish, file_path: str = "agent_outputs.log") -> None:
"""
Writes the output of an agent step to a file in markdown format.
Parameters:
step_output (AgentFinish): The output of the agent step containing 'thought', 'output', and 'text'.
file_path (str): Path to the file where the output will be written. Defaults to "agent_outputs.log".
Raises:
ValueError: If the step_output is not an instance of AgentFinish.
IOError: If there is an error while writing to the file.
"""
# Validate the input
if not isinstance(step_output, AgentFinish):
raise ValueError("step_output must be an instance of AgentFinish.")
# Create the markdown-formatted data
markdown_data = (
f"# Agent Step\n\n"
f"## Thought\n"
f"{step_output.thought}\n\n"
f"## Output\n"
f"{step_output.output}\n\n"
f"## Text\n"
f"{step_output.text}\n"
f"---\n"
)
# Write the markdown data to the file
try:
with open(file_path, "a", encoding="utf-8") as file:
file.write(markdown_data)
file.write("-" * 50 + "\n")
print(f"Output successfully written to {file_path}")
except IOError as e:
print(f"Failed to write to file {file_path}: {e}")
raise |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This worked for me, I needed to use partial from functools