Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add validation fix output_file issue when have '/' #585

Merged
merged 3 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my formatter adding 2 more spaces to properly ident this past code

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
Loading