Skip to content

Commit

Permalink
fix: Add validation fix output_file issue when have '/' (#585)
Browse files Browse the repository at this point in the history
* fix: Add validation fix output_file issue when have /

* fix: run black to format code

* fix: run black to format code
  • Loading branch information
pythonbyte authored May 9, 2024
1 parent 809b4b2 commit 7eb4fcd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/crewai/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,9 @@ def create_agent_executor(self, tools=None) -> None:
}

if self._rpm_controller:
executor_args[
"request_within_rpm_limit"
] = self._rpm_controller.check_or_wait
executor_args["request_within_rpm_limit"] = (
self._rpm_controller.check_or_wait
)

prompt = Prompts(
i18n=self.i18n,
Expand Down
4 changes: 3 additions & 1 deletion src/crewai/cli/templates/tools/custom_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

class MyCustomTool(BaseTool):
name: str = "Name of my tool"
description: str = "Clear description for what this tool is useful for, you agent will need this information to use it."
description: str = (
"Clear description for what this tool is useful for, you agent will need this information to use it."
)

def _run(self, argument: str) -> str:
# Implementation goes here
Expand Down
32 changes: 20 additions & 12 deletions src/crewai/task.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import re
import threading
import uuid
from typing import Any, Dict, List, Optional, Type
import os

from langchain_openai import ChatOpenAI
from pydantic import UUID4, BaseModel, Field, field_validator, model_validator
Expand Down Expand Up @@ -109,6 +109,14 @@ def _deny_user_set_id(cls, v: Optional[UUID4]) -> None:
"may_not_set_field", "This field is not to be set by the user.", {}
)

@field_validator("output_file")
@classmethod
def output_file_validattion(cls, value: str) -> str:
"""Validate the output file path by removing the / from the beginning of the path."""
if value.startswith("/"):
return value[1:]
return value

@model_validator(mode="after")
def set_attributes_based_on_config(self) -> "Task":
"""Set attributes based on the agent configuration."""
Expand Down Expand Up @@ -247,16 +255,16 @@ def _export_output(self, result: str) -> Any:
return exported_result.model_dump()
return exported_result
except Exception:
# sometimes the response contains valid JSON in the middle of text
match = re.search(r"({.*})", result, re.DOTALL)
if match:
try:
exported_result = model.model_validate_json(match.group(0))
if self.output_json:
return exported_result.model_dump()
return exported_result
except Exception:
pass
# sometimes the response contains valid JSON in the middle of text
match = re.search(r"({.*})", result, re.DOTALL)
if match:
try:
exported_result = model.model_validate_json(match.group(0))
if self.output_json:
return exported_result.model_dump()
return exported_result
except Exception:
pass

llm = self.agent.function_calling_llm or self.agent.llm

Expand Down Expand Up @@ -294,7 +302,7 @@ def _is_gpt(self, llm) -> bool:
def _save_file(self, result: Any) -> None:
directory = os.path.dirname(self.output_file)

if not os.path.exists(directory):
if directory and not os.path.exists(directory):
os.makedirs(directory)

with open(self.output_file, "w") as file:
Expand Down
8 changes: 5 additions & 3 deletions src/crewai/telemetry/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,11 @@ def crew_execution_span(self, crew):
"async_execution?": task.async_execution,
"output": task.expected_output,
"agent_role": task.agent.role if task.agent else "None",
"context": [task.description for task in task.context]
if task.context
else "None",
"context": (
[task.description for task in task.context]
if task.context
else "None"
),
"tools_names": [
tool.name.casefold() for tool in task.tools
],
Expand Down

0 comments on commit 7eb4fcd

Please sign in to comment.