mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-09 08:08:32 +00:00
Update to Pydantic v2: Transitioned all references from pydantic.v1 to pydantic (v2), ensuring compatibility with the latest Pydantic features and improvements. Affected components include agent tools, prompts, crew, and task modules. Refactoring & Alignment with Pydantic Standards: Refactored the agent module away from traditional __init__ to align more closely with Pydantic best practices. Updated the crew module to Pydantic v2 and enhanced configurations, allowing JSON and dictionary inputs. Additionally, some (not all) exceptions have been migrated to leverage Pydantic's error-handling capabilities. Enhancements to Validators and Typings: Improved validators and type annotations across multiple modules, enhancing code readability and maintainability. Streamlined the validation process in line with Pydantic v2's methodologies. Import and Configuration Adjustments: Updated to test-related absolute imports due to issues with Pytest finding packages through relative imports.
40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
from typing import Any, List, Optional
|
|
|
|
from pydantic import BaseModel, Field, model_validator
|
|
|
|
from .agent import Agent
|
|
|
|
|
|
class Task(BaseModel):
|
|
"""Class that represent a task to be executed."""
|
|
|
|
description: str = Field(description="Description of the actual task.")
|
|
agent: Optional[Agent] = Field(
|
|
description="Agent responsible for the task.", default=None
|
|
)
|
|
tools: List[Any] = Field(
|
|
default_factory=list,
|
|
description="Tools the agent are limited to use for this task.",
|
|
)
|
|
|
|
@model_validator(mode="after")
|
|
def check_tools(self):
|
|
if not self.tools and (self.agent and self.agent.tools):
|
|
self.tools.extend(self.agent.tools)
|
|
return self
|
|
|
|
def execute(self, context: str = None) -> str:
|
|
"""Execute the task.
|
|
|
|
Returns:
|
|
Output of the task.
|
|
"""
|
|
if self.agent:
|
|
return self.agent.execute_task(
|
|
task=self.description, context=context, tools=self.tools
|
|
)
|
|
else:
|
|
raise Exception(
|
|
f"The task '{self.description}' has no agent assigned, therefore it can't be executed directly and should be executed in a Crew using a specific process that support that, either consensual or hierarchical."
|
|
)
|