Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(agent): Fix load db models error #2290

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 57 additions & 41 deletions dbgpt/agent/resource/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
from typing import Any, Dict, List, Optional, Tuple, Type, cast

from dbgpt.agent import AgentMessage, ConversableAgent
from dbgpt.serve.agent.agents.app_agent_manage import get_app_manager
from dbgpt.util import ParameterDescription

from .base import Resource, ResourceParameters, ResourceType


def _get_app_list():
# TODO: Don't import dbgpt.serve in dbgpt.agent module
from dbgpt.serve.agent.agents.app_agent_manage import get_app_manager

# Only call this function when the system app is initialized
apps = get_app_manager().get_dbgpts()
results = [
{
Expand All @@ -24,53 +27,63 @@ 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):
"""Initialize AppResource resource."""
# TODO: Don't import dbgpt.serve in dbgpt.agent module
from dbgpt.serve.agent.agents.app_agent_manage import get_app_manager

self._resource_name = name
self._app_code = app_code

Expand Down Expand Up @@ -101,7 +114,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 Expand Up @@ -172,6 +185,9 @@ async def _start_app(
conv_uid: Optional[str] = None,
) -> AgentMessage:
"""Start App By AppResource."""
# TODO: Don't import dbgpt.serve in dbgpt.agent module
from dbgpt.serve.agent.agents.app_agent_manage import get_app_manager

conv_uid = str(uuid.uuid4()) if conv_uid is None else conv_uid
gpts_app = get_app_manager().get_app(app_code)
app_agent = await get_app_manager().create_agent_by_app_code(
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
Loading