Skip to content

Commit

Permalink
fix not fully defined error
Browse files Browse the repository at this point in the history
  • Loading branch information
shenchucheng committed Feb 2, 2024
1 parent 19b5ede commit caffcf0
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions metagpt/roles/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from __future__ import annotations

from enum import Enum
from typing import Iterable, Optional, Set, Type, Union
from typing import TYPE_CHECKING, Iterable, Optional, Set, Type, Union

from pydantic import BaseModel, ConfigDict, Field, SerializeAsAny, model_validator

Expand All @@ -39,6 +39,10 @@
from metagpt.utils.project_repo import ProjectRepo
from metagpt.utils.repair_llm_raw_output import extract_state_value_from_output

if TYPE_CHECKING:
from metagpt.environment import Environment # noqa: F401


PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}. """
CONSTRAINT_TEMPLATE = "the constraint is {constraints}. "

Expand Down Expand Up @@ -117,6 +121,13 @@ def important_memory(self) -> list[Message]:
def history(self) -> list[Message]:
return self.memory.get()

@classmethod
def model_rebuild(cls, **kwargs):
# https://stackoverflow.com/questions/63420889/fastapi-pydantic-circular-references-in-separate-files
from metagpt.environment import Environment # noqa: F401

super().model_rebuild(**kwargs)


class Role(SerializationMixin, ContextMixin, BaseModel):
"""Role/Agent"""
Expand Down Expand Up @@ -155,7 +166,6 @@ def validate_role_extra(self):
return self

def _process_role_extra(self):
self.pydantic_rebuild_model()
kwargs = self.model_extra or {}

if self.is_human:
Expand All @@ -168,14 +178,6 @@ def _process_role_extra(self):
if self.latest_observed_msg:
self.recovered = True

@staticmethod
def pydantic_rebuild_model():
"""Rebuild model to avoid `RecursionError: maximum recursion depth exceeded in comparison`"""
from metagpt.environment import Environment

Environment
Role.model_rebuild()

@property
def todo(self) -> Action:
"""Get action to do"""
Expand Down Expand Up @@ -559,3 +561,6 @@ def action_description(self) -> str:
if self.actions:
return any_to_name(self.actions[0])
return ""


RoleContext.model_rebuild()

0 comments on commit caffcf0

Please sign in to comment.