Removed the save_to_json parameter, incorporated the functionality directly with output_log_file

This commit is contained in:
Vidit-Ostwal
2025-02-05 22:51:28 +05:30
parent 89cf491f4b
commit 39a042285f
3 changed files with 21 additions and 42 deletions

View File

@@ -30,8 +30,7 @@ A crew in crewAI represents a collaborative group of agents working together to
| **Step Callback** _(optional)_ | `step_callback` | A function that is called after each step of every agent. This can be used to log the agent's actions or to perform other operations; it won't override the agent-specific `step_callback`. |
| **Task Callback** _(optional)_ | `task_callback` | A function that is called after the completion of each task. Useful for monitoring or additional operations post-task execution. |
| **Share Crew** _(optional)_ | `share_crew` | Whether you want to share the complete crew information and execution with the crewAI team to make the library better, and allow us to train models. |
| **Output Log File** _(optional)_ | `output_log_file` | Whether you want to have a file with the complete crew output and execution. You can set it using True and it will default to the folder you are currently in and it will be called logs.txt or passing a string with the full path and name of the file. |
| **Save Log File as JSON** _(optional)_| `save_as_json` | Whether you want to save the file as a list of JSON object. Output Log File parameter needs to be boolean `True` or a string. Defaults to `False`. |
| **Output Log File** _(optional)_ | `output_log_file` | Set to True to save logs as logs.txt in the current directory or provide a file path. Logs will be in JSON format if the filename ends in .json, otherwise .txt. Defautls to `None`. |
| **Manager Agent** _(optional)_ | `manager_agent` | `manager` sets a custom agent that will be used as a manager. |
| **Prompt File** _(optional)_ | `prompt_file` | Path to the prompt JSON file to be used for the crew. |
| **Planning** *(optional)* | `planning` | Adds planning ability to the Crew. When activated before each Crew iteration, all Crew data is sent to an AgentPlanner that will plan the tasks and this plan will be added to each task description. |
@@ -243,16 +242,17 @@ print(f"Token Usage: {crew_output.token_usage}")
## Accessing Crew Logs
You can see real time log of the crew execution, by setting `output_log_file` as a `True(Boolean)` or a `file_name(str)`. In case of `True(Boolean)` the file name which is saved will be `logs.txt`, in case of `(str)` it will be saved as `file_name.txt`
You can see real time log of the crew execution, by setting `output_log_file` as a `True(Boolean)` or a `file_name(str)`. Supports logging of events as both `file_name.txt` and `file_name.json`.
In case of `True(Boolean)` will save as `logs.txt`.
If you want to save the logs as a list of JSON events, set `save_as_json` parameter as `True(Boolean)`. In case of `output_log_file` is set as `True(Boolean)` it will be saved as `logs.json`, in case of `file_name(str)` it will be saved as `file_name.json`. In case of `output_log_file` is set as `False(Booelan)` or `None`, the logs will not be populated.
In case of `output_log_file` is set as `False(Booelan)` or `None`, the logs will not be populated.
```python Code
# Save crew logs
crew = Crew(output_log_file = True, save_as_json = False) # Logs will be saved as logs.txt
crew = Crew(output_log_file = file_name, save_as_json = False) # Logs will be saved as file_name.txt
crew = Crew(output_log_file = True, save_as_json = True) # Logs will be saved as logs.json
crew = Crew(output_log_file = file_name, save_as_json = True) # Logs will be saved as file_name.json
crew = Crew(output_log_file = True) # Logs will be saved as logs.txt
crew = Crew(output_log_file = file_name) # Logs will be saved as file_name.txt
crew = Crew(output_log_file = file_name.txt) # Logs will be saved as file_name.txt
crew = Crew(output_log_file = file_name.json) # Logs will be saved as file_name.json
```

View File

@@ -183,13 +183,9 @@ class Crew(BaseModel):
default=None,
description="Path to the prompt json file to be used for the crew.",
)
output_log_file: Optional[str] = Field(
output_log_file: Optional[Union[bool, str]] = Field(
default=None,
description="output_log_file",
),
save_as_json: Optional[bool] = Field(
default=False,
description="If true saves the logs in JSON format",
description="Path to the log file to be saved",
)
planning: Optional[bool] = Field(
default=False,
@@ -250,7 +246,7 @@ class Crew(BaseModel):
self._cache_handler = CacheHandler()
self._logger = Logger(verbose=self.verbose)
if self.output_log_file:
self._file_handler = FileHandler(self.output_log_file, self.save_as_json)
self._file_handler = FileHandler(self.output_log_file)
self._rpm_controller = RPMController(max_rpm=self.max_rpm, logger=self._logger)
if self.function_calling_llm and not isinstance(self.function_calling_llm, LLM):
self.function_calling_llm = create_llm(self.function_calling_llm)
@@ -442,13 +438,7 @@ class Crew(BaseModel):
f"Task '{task.description}' has a context dependency on a future task '{context_task.description}', which is not allowed."
)
return self
@model_validator(mode="after")
def validate_output_log_file_and_save_as_json_compatibility(self):
"""Validates that saving as JSON is not enabled when output logging is disabled, as this would be an invalid configuration."""
if not self.output_log_file and self.save_as_json: # handles False, None, empty string
raise PydanticCustomError("Cannot save as JSON when output logging is disabled")
return self
@property
def key(self) -> str:

View File

@@ -13,29 +13,21 @@ class FileHandler:
save_as_json (bool): If True, saves logs in JSON format
"""
def __init__(self, file_path: Union[bool, str], save_as_json: bool):
self.save_as_json = save_as_json
self._path = self._initialize_path(file_path, save_as_json)
def __init__(self, file_path: Union[bool, str]):
self._initialize_path(file_path)
def _initialize_path(self, file_path: Union[bool, str], save_as_json: bool) -> str:
def _initialize_path(self, file_path: Union[bool, str]) -> str:
if file_path is True: # File path is boolean True
if save_as_json:
self._path = os.path.join(os.curdir, "logs.json")
else:
self._path = os.path.join(os.curdir, "logs.txt")
elif isinstance(file_path, str): # File path is a string
if save_as_json:
if not file_path.endswith(".json"):
file_path += ".json"
if file_path.endswith((".json", ".txt")):
pass
else:
if not file_path.endswith(".txt"):
file_path += ".txt"
self._path = file_path
else:
raise ValueError("file_path must be either a boolean or a string.")
file_path += ".txt"
self._path = file_path
def log(self, **kwargs):
try:
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
@@ -67,9 +59,6 @@ class FileHandler:
except Exception as e:
raise ValueError(f"Failed to log message: {str(e)}")
class PickleHandler:
def __init__(self, file_name: str) -> None:
"""