Implement CrewAIBaseModel and Update to ConfigDict (#29)

New CrewAIBaseModel:

Base for Agent, Crew, Task.
Includes generated, frozen UUID.
Adds hashing capability
Migrate to ConfigDict:

Replaces class Config with model_config, see this deprecation note .
Benefits:
Adds auditing capability with frozen UUIDs.
This commit is contained in:
Greyson LaLonde
2023-12-30 19:52:04 -05:00
committed by GitHub
parent 469874d858
commit 1f0001b644
5 changed files with 35 additions and 12 deletions

24
crewai/base/model.py Normal file
View File

@@ -0,0 +1,24 @@
import uuid
from typing import Optional
from pydantic import UUID4, BaseModel, Field, field_validator
from pydantic_core import PydanticCustomError
class CrewAIBaseModel(BaseModel):
"""Base model with unique identifier."""
__hash__ = object.__hash__
id: UUID4 = Field(
default_factory=uuid.uuid4,
frozen=True,
description="Unique identifier for the object, not set by user.",
)
@field_validator("id", mode="before")
@classmethod
def _deny_user_set_id(cls, v: Optional[UUID4]) -> None:
if v:
raise PydanticCustomError(
"may_not_set_field", "This field is not to be set by the user.", {}
)