Skip to content

Commit

Permalink
fix(agent): Fix load db models error
Browse files Browse the repository at this point in the history
  • Loading branch information
fangyinc committed Jan 9, 2025
1 parent 2224984 commit bfb294d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 40 deletions.
88 changes: 48 additions & 40 deletions dbgpt/agent/resource/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


def _get_app_list():
# Only call this function when the system app is initialized
apps = get_app_manager().get_dbgpts()
results = [
{
Expand All @@ -24,49 +25,56 @@ def _get_app_list():
return results


@dataclasses.dataclass
class AppResourceParameters(ResourceParameters):
"""Application resource class."""
def _create_app_resource_parameters() -> Type[ResourceParameters]:
"""Create AppResourceParameters."""

app_code: str = dataclasses.field(
metadata={
"help": "app code",
"valid_values": _get_app_list(),
},
)
@dataclasses.dataclass
class _DynAppResourceParameters(ResourceParameters):
"""Application resource class."""

@classmethod
def to_configurations(
cls,
parameters: Type["AppResourceParameters"],
version: Optional[str] = None,
**kwargs,
) -> Any:
"""Convert the parameters to configurations."""
conf: List[ParameterDescription] = cast(
List[ParameterDescription], super().to_configurations(parameters)
app_code: str = dataclasses.field(
metadata={
"help": "app code",
"valid_values": _get_app_list(),
},
)
version = version or cls._resource_version()
if version != "v1":
return conf
# Compatible with old version
for param in conf:
if param.param_name == "app_code":
return param.valid_values or []
return []

@classmethod
def from_dict(
cls, data: dict, ignore_extra_fields: bool = True
) -> ResourceParameters:
"""Create a new instance from a dictionary."""
copied_data = data.copy()
if "app_code" not in copied_data and "value" in copied_data:
copied_data["app_code"] = copied_data.pop("value")
return super().from_dict(copied_data, ignore_extra_fields=ignore_extra_fields)


class AppResource(Resource[AppResourceParameters]):
@classmethod
def to_configurations(
cls,
parameters: Type["ResourceParameters"],
version: Optional[str] = None,
**kwargs,
) -> Any:
"""Convert the parameters to configurations."""
conf: List[ParameterDescription] = cast(
List[ParameterDescription], super().to_configurations(parameters)
)
version = version or cls._resource_version()
if version != "v1":
return conf
# Compatible with old version
for param in conf:
if param.param_name == "app_code":
return param.valid_values or []
return []

@classmethod
def from_dict(
cls, data: dict, ignore_extra_fields: bool = True
) -> ResourceParameters:
"""Create a new instance from a dictionary."""
copied_data = data.copy()
if "app_code" not in copied_data and "value" in copied_data:
copied_data["app_code"] = copied_data.pop("value")
return super().from_dict(
copied_data, ignore_extra_fields=ignore_extra_fields
)

return _DynAppResourceParameters


class AppResource(Resource[ResourceParameters]):
"""AppResource resource class."""

def __init__(self, name: str, app_code: str, **kwargs):
Expand Down Expand Up @@ -101,7 +109,7 @@ def name(self) -> str:
@classmethod
def resource_parameters_class(cls, **kwargs) -> Type[ResourceParameters]:
"""Return the resource parameters class."""
return AppResourceParameters
return _create_app_resource_parameters()

async def get_prompt(
self,
Expand Down
12 changes: 12 additions & 0 deletions dbgpt/serve/agent/agents/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ def __init__(self, system_app: SystemApp):
super().__init__(system_app)
self.system_app = system_app

def on_init(self):
"""Called when init the application.
Import your own module here to ensure the module is loaded before the application starts
"""
from ..db.gpts_app import (
GptsAppCollectionEntity,
GptsAppDetailEntity,
GptsAppEntity,
UserRecentAppsEntity,
)

def get_dbgpts(self, user_code: str = None, sys_code: str = None):
apps = self.gpts_app.app_list(
GptsAppQuery(user_code=user_code, sys_code=sys_code)
Expand Down

0 comments on commit bfb294d

Please sign in to comment.