mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-08 23:58:34 +00:00
65 lines
2.2 KiB
Python
65 lines
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from typing import Any
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
from crewai.tasks.output_format import OutputFormat
|
|
from crewai.tasks.task_output import TaskOutput
|
|
from crewai.types.usage_metrics import UsageMetrics, WorkflowTokenMetrics
|
|
|
|
|
|
class CrewOutput(BaseModel):
|
|
"""Class that represents the result of a crew."""
|
|
|
|
raw: str = Field(description="Raw output of crew", default="")
|
|
pydantic: BaseModel | None = Field(
|
|
description="Pydantic output of Crew", default=None
|
|
)
|
|
json_dict: dict[str, Any] | None = Field(
|
|
description="JSON dict output of Crew", default=None
|
|
)
|
|
tasks_output: list[TaskOutput] = Field(
|
|
description="Output of each task", default=[]
|
|
)
|
|
token_usage: UsageMetrics = Field(
|
|
description="Processed token summary", default_factory=UsageMetrics
|
|
)
|
|
token_metrics: WorkflowTokenMetrics | None = Field(
|
|
description="Detailed per-agent and per-task token metrics",
|
|
default=None
|
|
)
|
|
|
|
@property
|
|
def json(self) -> str | None: # type: ignore[override]
|
|
if self.tasks_output[-1].output_format != OutputFormat.JSON:
|
|
raise ValueError(
|
|
"No JSON output found in the final task. Please make sure to set the output_json property in the final task in your crew."
|
|
)
|
|
|
|
return json.dumps(self.json_dict)
|
|
|
|
def to_dict(self) -> dict[str, Any]:
|
|
"""Convert json_output and pydantic_output to a dictionary."""
|
|
output_dict = {}
|
|
if self.json_dict:
|
|
output_dict.update(self.json_dict)
|
|
elif self.pydantic:
|
|
output_dict.update(self.pydantic.model_dump())
|
|
return output_dict
|
|
|
|
def __getitem__(self, key):
|
|
if self.pydantic and hasattr(self.pydantic, key):
|
|
return getattr(self.pydantic, key)
|
|
if self.json_dict and key in self.json_dict:
|
|
return self.json_dict[key]
|
|
raise KeyError(f"Key '{key}' not found in CrewOutput.")
|
|
|
|
def __str__(self):
|
|
if self.pydantic:
|
|
return str(self.pydantic)
|
|
if self.json_dict:
|
|
return str(self.json_dict)
|
|
return self.raw
|