mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-27 09:08:14 +00:00
fix: interpolate output_file attribute from YAML
Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -213,8 +213,14 @@ class Task(BaseModel):
|
|||||||
|
|
||||||
@field_validator("output_file")
|
@field_validator("output_file")
|
||||||
@classmethod
|
@classmethod
|
||||||
def output_file_validation(cls, value: str) -> str:
|
def output_file_validation(cls, value: Optional[str]) -> Optional[str]:
|
||||||
"""Validate the output file path by removing the / from the beginning of the path."""
|
"""Validate the output file path."""
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
|
# Don't strip leading slash if it's a template path with variables
|
||||||
|
if "{" in value or "}" in value:
|
||||||
|
return value
|
||||||
|
# Strip leading slash for regular paths
|
||||||
if value.startswith("/"):
|
if value.startswith("/"):
|
||||||
return value[1:]
|
return value[1:]
|
||||||
return value
|
return value
|
||||||
@@ -394,17 +400,23 @@ class Task(BaseModel):
|
|||||||
return "\n".join(tasks_slices)
|
return "\n".join(tasks_slices)
|
||||||
|
|
||||||
def interpolate_inputs(self, inputs: Dict[str, Any]) -> None:
|
def interpolate_inputs(self, inputs: Dict[str, Any]) -> None:
|
||||||
"""Interpolate inputs into the task description and expected output."""
|
"""Interpolate inputs into the task description, expected output, and output file path."""
|
||||||
if self._original_description is None:
|
if self._original_description is None:
|
||||||
self._original_description = self.description
|
self._original_description = self.description
|
||||||
if self._original_expected_output is None:
|
if self._original_expected_output is None:
|
||||||
self._original_expected_output = self.expected_output
|
self._original_expected_output = self.expected_output
|
||||||
|
if self.output_file is not None and not hasattr(self, "_original_output_file"):
|
||||||
|
self._original_output_file = self.output_file
|
||||||
|
|
||||||
if inputs:
|
if inputs:
|
||||||
self.description = self._original_description.format(**inputs)
|
self.description = self._original_description.format(**inputs)
|
||||||
self.expected_output = self.interpolate_only(
|
self.expected_output = self.interpolate_only(
|
||||||
input_string=self._original_expected_output, inputs=inputs
|
input_string=self._original_expected_output, inputs=inputs
|
||||||
)
|
)
|
||||||
|
if self.output_file is not None:
|
||||||
|
self.output_file = self.interpolate_only(
|
||||||
|
input_string=self._original_output_file, inputs=inputs
|
||||||
|
)
|
||||||
|
|
||||||
def interpolate_only(self, input_string: str, inputs: Dict[str, Any]) -> str:
|
def interpolate_only(self, input_string: str, inputs: Dict[str, Any]) -> str:
|
||||||
"""Interpolate placeholders (e.g., {key}) in a string while leaving JSON untouched."""
|
"""Interpolate placeholders (e.g., {key}) in a string while leaving JSON untouched."""
|
||||||
|
|||||||
@@ -719,21 +719,24 @@ def test_interpolate_inputs():
|
|||||||
task = Task(
|
task = Task(
|
||||||
description="Give me a list of 5 interesting ideas about {topic} to explore for an article, what makes them unique and interesting.",
|
description="Give me a list of 5 interesting ideas about {topic} to explore for an article, what makes them unique and interesting.",
|
||||||
expected_output="Bullet point list of 5 interesting ideas about {topic}.",
|
expected_output="Bullet point list of 5 interesting ideas about {topic}.",
|
||||||
|
output_file="/tmp/{topic}/output_{date}.txt"
|
||||||
)
|
)
|
||||||
|
|
||||||
task.interpolate_inputs(inputs={"topic": "AI"})
|
task.interpolate_inputs(inputs={"topic": "AI", "date": "2024"})
|
||||||
assert (
|
assert (
|
||||||
task.description
|
task.description
|
||||||
== "Give me a list of 5 interesting ideas about AI to explore for an article, what makes them unique and interesting."
|
== "Give me a list of 5 interesting ideas about AI to explore for an article, what makes them unique and interesting."
|
||||||
)
|
)
|
||||||
assert task.expected_output == "Bullet point list of 5 interesting ideas about AI."
|
assert task.expected_output == "Bullet point list of 5 interesting ideas about AI."
|
||||||
|
assert task.output_file == "/tmp/AI/output_2024.txt"
|
||||||
|
|
||||||
task.interpolate_inputs(inputs={"topic": "ML"})
|
task.interpolate_inputs(inputs={"topic": "ML", "date": "2025"})
|
||||||
assert (
|
assert (
|
||||||
task.description
|
task.description
|
||||||
== "Give me a list of 5 interesting ideas about ML to explore for an article, what makes them unique and interesting."
|
== "Give me a list of 5 interesting ideas about ML to explore for an article, what makes them unique and interesting."
|
||||||
)
|
)
|
||||||
assert task.expected_output == "Bullet point list of 5 interesting ideas about ML."
|
assert task.expected_output == "Bullet point list of 5 interesting ideas about ML."
|
||||||
|
assert task.output_file == "/tmp/ML/output_2025.txt"
|
||||||
|
|
||||||
|
|
||||||
def test_interpolate_only():
|
def test_interpolate_only():
|
||||||
|
|||||||
Reference in New Issue
Block a user