mirror of
https://github.com/csunny/DB-GPT.git
synced 2025-07-23 12:21:08 +00:00
fix(agent): format problem (#2275)
Co-authored-by: cinjospeh <joseph.cjn@alibaba-inc.com>
This commit is contained in:
parent
56670f0084
commit
1a1fec02ef
@ -1,20 +1,23 @@
|
|||||||
|
"""Application Resources for the agent."""
|
||||||
|
|
||||||
import dataclasses
|
import dataclasses
|
||||||
import uuid
|
import uuid
|
||||||
from typing import Optional, Tuple, Dict, Type, Any, List, cast
|
from typing import Any, Dict, List, Optional, Tuple, Type, cast
|
||||||
|
|
||||||
from dbgpt.agent import ConversableAgent, AgentMessage, AgentContext
|
from dbgpt.agent import AgentMessage, ConversableAgent
|
||||||
from dbgpt.serve.agent.agents.app_agent_manage import get_app_manager
|
from dbgpt.serve.agent.agents.app_agent_manage import get_app_manager
|
||||||
from dbgpt.util import ParameterDescription
|
from dbgpt.util import ParameterDescription
|
||||||
|
|
||||||
from .base import Resource, ResourceParameters, ResourceType
|
from .base import Resource, ResourceParameters, ResourceType
|
||||||
|
|
||||||
|
|
||||||
def get_app_list():
|
def _get_app_list():
|
||||||
apps = get_app_manager().get_dbgpts()
|
apps = get_app_manager().get_dbgpts()
|
||||||
results = [
|
results = [
|
||||||
{
|
{
|
||||||
"label": f"{app.app_name}({app.app_code})",
|
"label": f"{app.app_name}({app.app_code})",
|
||||||
"key": app.app_code,
|
"key": app.app_code,
|
||||||
"description": app.app_describe
|
"description": app.app_describe,
|
||||||
}
|
}
|
||||||
for app in apps
|
for app in apps
|
||||||
]
|
]
|
||||||
@ -23,20 +26,21 @@ def get_app_list():
|
|||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass
|
||||||
class AppResourceParameters(ResourceParameters):
|
class AppResourceParameters(ResourceParameters):
|
||||||
|
"""Application resource class."""
|
||||||
|
|
||||||
app_code: str = dataclasses.field(
|
app_code: str = dataclasses.field(
|
||||||
default=None,
|
|
||||||
metadata={
|
metadata={
|
||||||
"help": "app code",
|
"help": "app code",
|
||||||
"valid_values": get_app_list(),
|
"valid_values": _get_app_list(),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def to_configurations(
|
def to_configurations(
|
||||||
cls,
|
cls,
|
||||||
parameters: Type["AppResourceParameters"],
|
parameters: Type["AppResourceParameters"],
|
||||||
version: Optional[str] = None,
|
version: Optional[str] = None,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> Any:
|
) -> Any:
|
||||||
"""Convert the parameters to configurations."""
|
"""Convert the parameters to configurations."""
|
||||||
conf: List[ParameterDescription] = cast(
|
conf: List[ParameterDescription] = cast(
|
||||||
@ -53,7 +57,7 @@ class AppResourceParameters(ResourceParameters):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_dict(
|
def from_dict(
|
||||||
cls, data: dict, ignore_extra_fields: bool = True
|
cls, data: dict, ignore_extra_fields: bool = True
|
||||||
) -> ResourceParameters:
|
) -> ResourceParameters:
|
||||||
"""Create a new instance from a dictionary."""
|
"""Create a new instance from a dictionary."""
|
||||||
copied_data = data.copy()
|
copied_data = data.copy()
|
||||||
@ -66,6 +70,7 @@ class AppResource(Resource[AppResourceParameters]):
|
|||||||
"""AppResource resource class."""
|
"""AppResource resource class."""
|
||||||
|
|
||||||
def __init__(self, name: str, app_code: str, **kwargs):
|
def __init__(self, name: str, app_code: str, **kwargs):
|
||||||
|
"""Initialize AppResource resource."""
|
||||||
self._resource_name = name
|
self._resource_name = name
|
||||||
self._app_code = app_code
|
self._app_code = app_code
|
||||||
|
|
||||||
@ -73,12 +78,24 @@ class AppResource(Resource[AppResourceParameters]):
|
|||||||
self._app_name = app.app_name
|
self._app_name = app.app_name
|
||||||
self._app_desc = app.app_describe
|
self._app_desc = app.app_describe
|
||||||
|
|
||||||
|
@property
|
||||||
|
def app_desc(self):
|
||||||
|
"""Return the app description."""
|
||||||
|
return self._app_desc
|
||||||
|
|
||||||
|
@property
|
||||||
|
def app_name(self):
|
||||||
|
"""Return the app name."""
|
||||||
|
return self._app_name
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def type(cls) -> ResourceType:
|
def type(cls) -> ResourceType:
|
||||||
|
"""Return the resource type."""
|
||||||
return ResourceType.App
|
return ResourceType.App
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
|
"""Return the resource name."""
|
||||||
return self._resource_name
|
return self._resource_name
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -86,13 +103,18 @@ class AppResource(Resource[AppResourceParameters]):
|
|||||||
"""Return the resource parameters class."""
|
"""Return the resource parameters class."""
|
||||||
return AppResourceParameters
|
return AppResourceParameters
|
||||||
|
|
||||||
async def get_prompt(self, *, lang: str = "en", prompt_type: str = "default", question: Optional[str] = None,
|
async def get_prompt(
|
||||||
resource_name: Optional[str] = None, **kwargs) -> Tuple[str, Optional[Dict]]:
|
self,
|
||||||
|
*,
|
||||||
|
lang: str = "en",
|
||||||
|
prompt_type: str = "default",
|
||||||
|
question: Optional[str] = None,
|
||||||
|
resource_name: Optional[str] = None,
|
||||||
|
**kwargs,
|
||||||
|
) -> Tuple[str, Optional[Dict]]:
|
||||||
"""Get the prompt."""
|
"""Get the prompt."""
|
||||||
|
|
||||||
prompt_template_zh = (
|
prompt_template_zh = (
|
||||||
"{name}:调用此资源与应用 {app_name} 进行交互。"
|
"{name}:调用此资源与应用 {app_name} 进行交互。" "应用 {app_name} 有什么用?{description}"
|
||||||
"应用 {app_name} 有什么用?{description}"
|
|
||||||
)
|
)
|
||||||
prompt_template_en = (
|
prompt_template_en = (
|
||||||
"{name}:Call this resource to interact with the application {app_name} ."
|
"{name}:Call this resource to interact with the application {app_name} ."
|
||||||
@ -102,9 +124,7 @@ class AppResource(Resource[AppResourceParameters]):
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
template.format(
|
template.format(
|
||||||
name=self.name,
|
name=self.name, app_name=self._app_name, description=self._app_desc
|
||||||
app_name=self._app_name,
|
|
||||||
description=self._app_desc
|
|
||||||
),
|
),
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
@ -114,15 +134,16 @@ class AppResource(Resource[AppResourceParameters]):
|
|||||||
"""Return whether the tool is asynchronous."""
|
"""Return whether the tool is asynchronous."""
|
||||||
return True
|
return True
|
||||||
|
|
||||||
async def execute(self, *args, resource_name: Optional[str] = None, **kwargs) -> Any:
|
def execute(self, *args, resource_name: Optional[str] = None, **kwargs) -> Any:
|
||||||
|
"""Execute the resource."""
|
||||||
if self.is_async:
|
if self.is_async:
|
||||||
raise RuntimeError("Async execution is not supported")
|
raise RuntimeError("Sync execution is not supported")
|
||||||
|
|
||||||
async def async_execute(
|
async def async_execute(
|
||||||
self,
|
self,
|
||||||
*args,
|
*args,
|
||||||
resource_name: Optional[str] = None,
|
resource_name: Optional[str] = None,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> Any:
|
) -> Any:
|
||||||
"""Execute the tool asynchronously.
|
"""Execute the tool asynchronously.
|
||||||
|
|
||||||
@ -132,35 +153,41 @@ class AppResource(Resource[AppResourceParameters]):
|
|||||||
specific tool).
|
specific tool).
|
||||||
**kwargs: The keyword arguments.
|
**kwargs: The keyword arguments.
|
||||||
"""
|
"""
|
||||||
|
user_input: Optional[str] = kwargs.get("user_input")
|
||||||
|
parent_agent: Optional[ConversableAgent] = kwargs.get("parent_agent")
|
||||||
|
|
||||||
user_input = kwargs.get("user_input")
|
if user_input is None:
|
||||||
parent_agent = kwargs.get("parent_agent")
|
raise RuntimeError("AppResource async execution user_input is None")
|
||||||
|
if parent_agent is None:
|
||||||
|
raise RuntimeError("AppResource async execution parent_agent is None")
|
||||||
|
|
||||||
reply_message = await self.chat_2_app_once(self._app_code, user_input=user_input, sender=parent_agent)
|
reply_message = await _start_app(self._app_code, user_input, parent_agent)
|
||||||
return reply_message.content
|
return reply_message.content
|
||||||
|
|
||||||
async def chat_2_app_once(self,
|
|
||||||
app_code: str,
|
|
||||||
user_input: str,
|
|
||||||
conv_uid: str = None,
|
|
||||||
sender: ConversableAgent = None) -> AgentMessage:
|
|
||||||
# create a new conv_uid
|
|
||||||
conv_uid = str(uuid.uuid4()) if conv_uid is None else conv_uid
|
|
||||||
|
|
||||||
gpts_app = get_app_manager().get_app(app_code)
|
async def _start_app(
|
||||||
|
app_code: str,
|
||||||
|
user_input: str,
|
||||||
|
sender: ConversableAgent,
|
||||||
|
conv_uid: Optional[str] = None,
|
||||||
|
) -> AgentMessage:
|
||||||
|
"""Start App By AppResource."""
|
||||||
|
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(
|
||||||
|
gpts_app, conv_uid=conv_uid
|
||||||
|
)
|
||||||
|
|
||||||
app_agent = await get_app_manager().create_agent_by_app_code(gpts_app, conv_uid=conv_uid)
|
agent_message = AgentMessage(
|
||||||
|
content=user_input,
|
||||||
|
current_goal=user_input,
|
||||||
|
context={
|
||||||
|
"conv_uid": conv_uid,
|
||||||
|
},
|
||||||
|
rounds=0,
|
||||||
|
)
|
||||||
|
reply_message: AgentMessage = await app_agent.generate_reply(
|
||||||
|
received_message=agent_message, sender=sender
|
||||||
|
)
|
||||||
|
|
||||||
agent_message = AgentMessage(
|
return reply_message
|
||||||
content=user_input,
|
|
||||||
current_goal=user_input,
|
|
||||||
context={
|
|
||||||
"conv_uid": conv_uid,
|
|
||||||
},
|
|
||||||
rounds=0,
|
|
||||||
)
|
|
||||||
|
|
||||||
reply_message: AgentMessage = await app_agent.generate_reply(received_message=agent_message,
|
|
||||||
sender=sender)
|
|
||||||
|
|
||||||
return reply_message
|
|
||||||
|
@ -106,12 +106,12 @@ def _initialize_resource_manager(system_app: SystemApp):
|
|||||||
get_current_host_system_load,
|
get_current_host_system_load,
|
||||||
)
|
)
|
||||||
from dbgpt.agent.expand.resources.search_tool import baidu_search
|
from dbgpt.agent.expand.resources.search_tool import baidu_search
|
||||||
|
from dbgpt.agent.resource.app import AppResource
|
||||||
from dbgpt.agent.resource.base import ResourceType
|
from dbgpt.agent.resource.base import ResourceType
|
||||||
from dbgpt.agent.resource.manage import get_resource_manager, initialize_resource
|
from dbgpt.agent.resource.manage import get_resource_manager, initialize_resource
|
||||||
from dbgpt.serve.agent.resource.datasource import DatasourceResource
|
from dbgpt.serve.agent.resource.datasource import DatasourceResource
|
||||||
from dbgpt.serve.agent.resource.knowledge import KnowledgeSpaceRetrieverResource
|
from dbgpt.serve.agent.resource.knowledge import KnowledgeSpaceRetrieverResource
|
||||||
from dbgpt.serve.agent.resource.plugin import PluginToolPack
|
from dbgpt.serve.agent.resource.plugin import PluginToolPack
|
||||||
from dbgpt.agent.resource.app import AppResource
|
|
||||||
|
|
||||||
initialize_resource(system_app)
|
initialize_resource(system_app)
|
||||||
rm = get_resource_manager(system_app)
|
rm = get_resource_manager(system_app)
|
||||||
|
@ -6,28 +6,29 @@ from typing import List, Type
|
|||||||
from dbgpt.agent import (
|
from dbgpt.agent import (
|
||||||
AgentContext,
|
AgentContext,
|
||||||
AgentMemory,
|
AgentMemory,
|
||||||
|
AutoPlanChatManager,
|
||||||
ConversableAgent,
|
ConversableAgent,
|
||||||
DefaultAWELLayoutManager,
|
DefaultAWELLayoutManager,
|
||||||
GptsMemory,
|
GptsMemory,
|
||||||
LLMConfig,
|
LLMConfig,
|
||||||
UserProxyAgent, get_agent_manager,
|
UserProxyAgent,
|
||||||
|
get_agent_manager,
|
||||||
)
|
)
|
||||||
from dbgpt.agent.core.schema import Status
|
from dbgpt.agent.core.schema import Status
|
||||||
from dbgpt.agent.resource import get_resource_manager
|
from dbgpt.agent.resource import get_resource_manager
|
||||||
from dbgpt.agent.util.llm.llm import LLMStrategyType
|
from dbgpt.agent.util.llm.llm import LLMStrategyType
|
||||||
from dbgpt.app.component_configs import CFG
|
from dbgpt.app.component_configs import CFG
|
||||||
from dbgpt.component import BaseComponent, ComponentType, SystemApp
|
from dbgpt.component import BaseComponent, ComponentType, SystemApp
|
||||||
from dbgpt.core import LLMClient
|
from dbgpt.core import LLMClient, PromptTemplate
|
||||||
from dbgpt.core import PromptTemplate
|
|
||||||
from dbgpt.model.cluster import WorkerManagerFactory
|
from dbgpt.model.cluster import WorkerManagerFactory
|
||||||
from dbgpt.model.cluster.client import DefaultLLMClient
|
from dbgpt.model.cluster.client import DefaultLLMClient
|
||||||
from dbgpt.serve.prompt.api.endpoints import get_service
|
from dbgpt.serve.prompt.api.endpoints import get_service
|
||||||
from .db_gpts_memory import MetaDbGptsMessageMemory, MetaDbGptsPlansMemory
|
|
||||||
from ..db import GptsMessagesDao
|
from ..db import GptsMessagesDao
|
||||||
from ..db.gpts_app import GptsApp, GptsAppDao, GptsAppQuery
|
from ..db.gpts_app import GptsApp, GptsAppDao, GptsAppDetail, GptsAppQuery
|
||||||
from ..db.gpts_app import GptsAppDetail
|
|
||||||
from ..db.gpts_conversations_db import GptsConversationsDao
|
from ..db.gpts_conversations_db import GptsConversationsDao
|
||||||
from ..team.base import TeamMode
|
from ..team.base import TeamMode
|
||||||
|
from .db_gpts_memory import MetaDbGptsMessageMemory, MetaDbGptsPlansMemory
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -63,16 +64,16 @@ class AppManager(BaseComponent, ABC):
|
|||||||
return self.gpts_app.app_detail(app_code)
|
return self.gpts_app.app_detail(app_code)
|
||||||
|
|
||||||
async def user_chat_2_app(
|
async def user_chat_2_app(
|
||||||
self,
|
self,
|
||||||
user_query: str,
|
user_query: str,
|
||||||
conv_uid: str,
|
conv_uid: str,
|
||||||
gpts_app: GptsApp,
|
gpts_app: GptsApp,
|
||||||
agent_memory: AgentMemory,
|
agent_memory: AgentMemory,
|
||||||
is_retry_chat: bool = False,
|
is_retry_chat: bool = False,
|
||||||
last_speaker_name: str = None,
|
last_speaker_name: str = None,
|
||||||
init_message_rounds: int = 0,
|
init_message_rounds: int = 0,
|
||||||
enable_verbose: bool = True,
|
enable_verbose: bool = True,
|
||||||
**ext_info,
|
**ext_info,
|
||||||
) -> Status:
|
) -> Status:
|
||||||
context: AgentContext = AgentContext(
|
context: AgentContext = AgentContext(
|
||||||
conv_id=conv_uid,
|
conv_id=conv_uid,
|
||||||
@ -107,41 +108,41 @@ class AppManager(BaseComponent, ABC):
|
|||||||
return Status.COMPLETE
|
return Status.COMPLETE
|
||||||
|
|
||||||
async def create_app_agent(
|
async def create_app_agent(
|
||||||
self,
|
self,
|
||||||
gpts_app: GptsApp,
|
gpts_app: GptsApp,
|
||||||
agent_memory: AgentMemory,
|
agent_memory: AgentMemory,
|
||||||
context: AgentContext,
|
context: AgentContext,
|
||||||
) -> ConversableAgent:
|
) -> ConversableAgent:
|
||||||
# init default llm provider
|
# init default llm provider
|
||||||
llm_provider = DefaultLLMClient(
|
llm_provider = DefaultLLMClient(
|
||||||
self.system_app.get_component(ComponentType.WORKER_MANAGER_FACTORY, WorkerManagerFactory).create(),
|
self.system_app.get_component(
|
||||||
auto_convert_message=True
|
ComponentType.WORKER_MANAGER_FACTORY, WorkerManagerFactory
|
||||||
|
).create(),
|
||||||
|
auto_convert_message=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
# init team employees
|
# init team employees
|
||||||
# TODO employee has it own llm provider
|
# TODO employee has it own llm provider
|
||||||
employees: List[ConversableAgent] = []
|
employees: List[ConversableAgent] = []
|
||||||
for record in gpts_app.details:
|
for record in gpts_app.details:
|
||||||
agent = (await create_agent_from_gpt_detail(record, llm_provider, context, agent_memory))
|
agent = await create_agent_from_gpt_detail(
|
||||||
|
record, llm_provider, context, agent_memory
|
||||||
|
)
|
||||||
agent.name_prefix = gpts_app.app_name
|
agent.name_prefix = gpts_app.app_name
|
||||||
employees.append(agent)
|
employees.append(agent)
|
||||||
|
|
||||||
app_agent: ConversableAgent = (
|
app_agent: ConversableAgent = await create_agent_of_gpts_app(
|
||||||
await create_agent_of_gpts_app(gpts_app,
|
gpts_app, llm_provider, context, agent_memory, employees
|
||||||
llm_provider,
|
|
||||||
context,
|
|
||||||
agent_memory,
|
|
||||||
employees)
|
|
||||||
)
|
)
|
||||||
app_agent.name_prefix = gpts_app.app_name
|
app_agent.name_prefix = gpts_app.app_name
|
||||||
return app_agent
|
return app_agent
|
||||||
|
|
||||||
async def create_agent_by_app_code(
|
async def create_agent_by_app_code(
|
||||||
self,
|
self,
|
||||||
gpts_app: GptsApp,
|
gpts_app: GptsApp,
|
||||||
conv_uid: str = None,
|
conv_uid: str = None,
|
||||||
agent_memory: AgentMemory = None,
|
agent_memory: AgentMemory = None,
|
||||||
context: AgentContext = None,
|
context: AgentContext = None,
|
||||||
) -> ConversableAgent:
|
) -> ConversableAgent:
|
||||||
"""
|
"""
|
||||||
Create a conversable agent by application code.
|
Create a conversable agent by application code.
|
||||||
@ -157,7 +158,10 @@ class AppManager(BaseComponent, ABC):
|
|||||||
"""
|
"""
|
||||||
conv_uid = str(uuid.uuid4()) if conv_uid is None else conv_uid
|
conv_uid = str(uuid.uuid4()) if conv_uid is None else conv_uid
|
||||||
|
|
||||||
from dbgpt.agent.core.memory.gpts import DefaultGptsPlansMemory, DefaultGptsMessageMemory
|
from dbgpt.agent.core.memory.gpts import (
|
||||||
|
DefaultGptsMessageMemory,
|
||||||
|
DefaultGptsPlansMemory,
|
||||||
|
)
|
||||||
|
|
||||||
if agent_memory is None:
|
if agent_memory is None:
|
||||||
gpt_memory = GptsMemory(
|
gpt_memory = GptsMemory(
|
||||||
@ -179,24 +183,23 @@ class AppManager(BaseComponent, ABC):
|
|||||||
context.gpts_app_name = gpts_app.app_name
|
context.gpts_app_name = gpts_app.app_name
|
||||||
context.language = gpts_app.language
|
context.language = gpts_app.language
|
||||||
|
|
||||||
agent: ConversableAgent = (
|
agent: ConversableAgent = await self.create_app_agent(
|
||||||
await self.create_app_agent(gpts_app, agent_memory, context)
|
gpts_app, agent_memory, context
|
||||||
)
|
)
|
||||||
return agent
|
return agent
|
||||||
|
|
||||||
|
|
||||||
async def create_agent_from_gpt_detail(
|
async def create_agent_from_gpt_detail(
|
||||||
record: GptsAppDetail,
|
record: GptsAppDetail,
|
||||||
llm_client: LLMClient,
|
llm_client: LLMClient,
|
||||||
agent_context: AgentContext,
|
agent_context: AgentContext,
|
||||||
agent_memory: AgentMemory) -> ConversableAgent:
|
agent_memory: AgentMemory,
|
||||||
|
) -> ConversableAgent:
|
||||||
"""
|
"""
|
||||||
Get the agent object from the GPTsAppDetail object.
|
Get the agent object from the GPTsAppDetail object.
|
||||||
"""
|
"""
|
||||||
agent_manager = get_agent_manager()
|
agent_manager = get_agent_manager()
|
||||||
agent_cls: Type[ConversableAgent] = agent_manager.get_by_name(
|
agent_cls: Type[ConversableAgent] = agent_manager.get_by_name(record.agent_name)
|
||||||
record.agent_name
|
|
||||||
)
|
|
||||||
llm_config = LLMConfig(
|
llm_config = LLMConfig(
|
||||||
llm_client=llm_client,
|
llm_client=llm_client,
|
||||||
llm_strategy=LLMStrategyType(record.llm_strategy),
|
llm_strategy=LLMStrategyType(record.llm_strategy),
|
||||||
@ -208,25 +211,29 @@ async def create_agent_from_gpt_detail(
|
|||||||
prompt_code=record.prompt_template
|
prompt_code=record.prompt_template
|
||||||
)
|
)
|
||||||
|
|
||||||
depend_resource = get_resource_manager().build_resource(record.resources, version="v1")
|
depend_resource = get_resource_manager().build_resource(
|
||||||
|
record.resources, version="v1"
|
||||||
|
)
|
||||||
|
|
||||||
agent = (await agent_cls()
|
agent = (
|
||||||
.bind(agent_context)
|
await agent_cls()
|
||||||
.bind(agent_memory)
|
.bind(agent_context)
|
||||||
.bind(llm_config)
|
.bind(agent_memory)
|
||||||
.bind(depend_resource)
|
.bind(llm_config)
|
||||||
.bind(prompt_template)
|
.bind(depend_resource)
|
||||||
.build())
|
.bind(prompt_template)
|
||||||
|
.build()
|
||||||
|
)
|
||||||
|
|
||||||
return agent
|
return agent
|
||||||
|
|
||||||
|
|
||||||
async def create_agent_of_gpts_app(
|
async def create_agent_of_gpts_app(
|
||||||
gpts_app: GptsApp,
|
gpts_app: GptsApp,
|
||||||
llm_client: LLMClient,
|
llm_client: LLMClient,
|
||||||
context: AgentContext,
|
context: AgentContext,
|
||||||
memory: AgentMemory,
|
memory: AgentMemory,
|
||||||
employees: List[ConversableAgent],
|
employees: List[ConversableAgent],
|
||||||
) -> ConversableAgent:
|
) -> ConversableAgent:
|
||||||
llm_config = LLMConfig(
|
llm_config = LLMConfig(
|
||||||
llm_client=llm_client,
|
llm_client=llm_client,
|
||||||
@ -240,13 +247,10 @@ async def create_agent_of_gpts_app(
|
|||||||
agent_of_app: ConversableAgent = employees[0]
|
agent_of_app: ConversableAgent = employees[0]
|
||||||
else:
|
else:
|
||||||
if TeamMode.AUTO_PLAN == team_mode:
|
if TeamMode.AUTO_PLAN == team_mode:
|
||||||
if not employees or len(employees) < 0:
|
if not gpts_app.details or len(gpts_app.details) < 0:
|
||||||
raise ValueError("APP exception no available agent!")
|
raise ValueError("APP exception no available agent!")
|
||||||
from dbgpt.agent.v2 import AutoPlanChatManagerV2, MultiAgentTeamPlanner
|
llm_config = employees[0].llm_config
|
||||||
planner = MultiAgentTeamPlanner()
|
manager = AutoPlanChatManager()
|
||||||
planner.name_prefix = gpts_app.app_name
|
|
||||||
manager = AutoPlanChatManagerV2(planner)
|
|
||||||
manager.name_prefix = gpts_app.app_name
|
|
||||||
elif TeamMode.AWEL_LAYOUT == team_mode:
|
elif TeamMode.AWEL_LAYOUT == team_mode:
|
||||||
if not awel_team_context:
|
if not awel_team_context:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
@ -257,12 +261,7 @@ async def create_agent_of_gpts_app(
|
|||||||
raise ValueError(f"Native APP chat not supported!")
|
raise ValueError(f"Native APP chat not supported!")
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Unknown Agent Team Mode!{team_mode}")
|
raise ValueError(f"Unknown Agent Team Mode!{team_mode}")
|
||||||
manager = (
|
manager = await manager.bind(context).bind(memory).bind(llm_config).build()
|
||||||
await manager.bind(context)
|
|
||||||
.bind(memory)
|
|
||||||
.bind(llm_config)
|
|
||||||
.build()
|
|
||||||
)
|
|
||||||
manager.hire(employees)
|
manager.hire(employees)
|
||||||
agent_of_app: ConversableAgent = manager
|
agent_of_app: ConversableAgent = manager
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
from dbgpt.serve.agent.agents.expand.app_resource_start_assisant_agent import AppStarterAgent
|
from dbgpt.serve.agent.agents.expand.app_resource_start_assisant_agent import (
|
||||||
|
AppStarterAgent,
|
||||||
|
)
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"AppStarterAgent",
|
"AppStarterAgent",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
from typing import Any, Dict, List
|
from typing import Any, Dict, List, Optional
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
from dbgpt._private.pydantic import BaseModel, Field
|
from dbgpt._private.pydantic import BaseModel, Field
|
||||||
from dbgpt.agent import Action, ActionOutput, AgentResource, AgentMessage, ResourceType
|
|
||||||
from dbgpt.agent import (
|
from dbgpt.agent import (
|
||||||
|
Action,
|
||||||
|
ActionOutput,
|
||||||
Agent,
|
Agent,
|
||||||
|
AgentMessage,
|
||||||
|
AgentResource,
|
||||||
ConversableAgent,
|
ConversableAgent,
|
||||||
|
Resource,
|
||||||
|
ResourceType,
|
||||||
get_agent_manager,
|
get_agent_manager,
|
||||||
)
|
)
|
||||||
from dbgpt.agent.core.profile import DynConfig, ProfileConfig
|
from dbgpt.agent.core.profile import DynConfig, ProfileConfig
|
||||||
@ -23,7 +27,7 @@ class AppResourceInput(BaseModel):
|
|||||||
app_name: str = Field(
|
app_name: str = Field(
|
||||||
...,
|
...,
|
||||||
description="The name of a application that can be used to answer the current question"
|
description="The name of a application that can be used to answer the current question"
|
||||||
" or solve the current task.",
|
" or solve the current task.",
|
||||||
)
|
)
|
||||||
|
|
||||||
app_query: str = Field(
|
app_query: str = Field(
|
||||||
@ -60,7 +64,7 @@ class AppResourceAction(Action[AppResourceInput]):
|
|||||||
"""Return the AI output schema."""
|
"""Return the AI output schema."""
|
||||||
out_put_schema = {
|
out_put_schema = {
|
||||||
"app_name": "the agent name you selected",
|
"app_name": "the agent name you selected",
|
||||||
"app_query": "the query to the selected agent, must input a str, base on the natural language "
|
"app_query": "the query to the selected agent, must input a str, base on the natural language ",
|
||||||
}
|
}
|
||||||
|
|
||||||
return f"""Please response in the following json format:
|
return f"""Please response in the following json format:
|
||||||
@ -69,12 +73,12 @@ class AppResourceAction(Action[AppResourceInput]):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
async def run(
|
async def run(
|
||||||
self,
|
self,
|
||||||
ai_message: str,
|
ai_message: str,
|
||||||
resource: Optional[AgentResource] = None,
|
resource: Optional[AgentResource] = None,
|
||||||
rely_action_out: Optional[ActionOutput] = None,
|
rely_action_out: Optional[ActionOutput] = None,
|
||||||
need_vis_render: bool = True,
|
need_vis_render: bool = True,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> ActionOutput:
|
) -> ActionOutput:
|
||||||
"""Perform the plugin action.
|
"""Perform the plugin action.
|
||||||
|
|
||||||
@ -92,7 +96,9 @@ class AppResourceAction(Action[AppResourceInput]):
|
|||||||
err_msg = None
|
err_msg = None
|
||||||
app_result = None
|
app_result = None
|
||||||
try:
|
try:
|
||||||
param: AppResourceInput = self._input_convert(ai_message, AppResourceInput)
|
param: AppResourceInput = self._input_convert(
|
||||||
|
ai_message, AppResourceInput
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception((str(e)))
|
logger.exception((str(e)))
|
||||||
return ActionOutput(
|
return ActionOutput(
|
||||||
@ -126,7 +132,9 @@ class AppResourceAction(Action[AppResourceInput]):
|
|||||||
is_exe_success=False, content=f"App action run failed!{str(e)}"
|
is_exe_success=False, content=f"App action run failed!{str(e)}"
|
||||||
)
|
)
|
||||||
|
|
||||||
async def __get_plugin_view(self, param: AppResourceInput, app_result: Any, err_msg: str):
|
async def __get_plugin_view(
|
||||||
|
self, param: AppResourceInput, app_result: Any, err_msg: str
|
||||||
|
):
|
||||||
if not self.render_protocol:
|
if not self.render_protocol:
|
||||||
return None
|
return None
|
||||||
# raise NotImplementedError("The render_protocol should be implemented.")
|
# raise NotImplementedError("The render_protocol should be implemented.")
|
||||||
@ -181,11 +189,8 @@ class AppStarterAgent(ConversableAgent):
|
|||||||
constraints=DynConfig(
|
constraints=DynConfig(
|
||||||
[
|
[
|
||||||
"请一步一步思考参为用户问题选择一个最匹配的应用来进行用户问题回答,可参考给出示例的应用选择逻辑.",
|
"请一步一步思考参为用户问题选择一个最匹配的应用来进行用户问题回答,可参考给出示例的应用选择逻辑.",
|
||||||
"请阅读用户问题,确定问题所属领域和问题意图,按领域和意图匹配应用,如果用户问题意图缺少操作类应用需要的参数,优先使用咨询类型应用,有明确操作目标才使用操作类应用.",
|
|
||||||
"必须从已知的应用中选出一个可用的应用来进行回答,不要瞎编应用的名称",
|
"必须从已知的应用中选出一个可用的应用来进行回答,不要瞎编应用的名称",
|
||||||
"仅选择可回答问题的应用即可,不要直接回答用户问题.",
|
"如果选择出了合适的应用,传递给应用的请求和用户输入的请求中的信息必须完全对等,不能丢失或增加任何信息",
|
||||||
"如果用户的问题和提供的所有应用全都不相关,则应用code和name都输出为空",
|
|
||||||
"注意应用意图定义中如果有槽位信息,再次阅读理解用户输入信息,将对应的内容填入对应槽位参数定义中.",
|
|
||||||
],
|
],
|
||||||
category="agent",
|
category="agent",
|
||||||
key="dbgpt_ant_agent_agents_app_resource_starter_assistant_agent_profile_constraints",
|
key="dbgpt_ant_agent_agents_app_resource_starter_assistant_agent_profile_constraints",
|
||||||
@ -203,11 +208,11 @@ class AppStarterAgent(ConversableAgent):
|
|||||||
self._init_actions([AppResourceAction])
|
self._init_actions([AppResourceAction])
|
||||||
|
|
||||||
def prepare_act_param(
|
def prepare_act_param(
|
||||||
self,
|
self,
|
||||||
received_message: Optional[AgentMessage],
|
received_message: Optional[AgentMessage],
|
||||||
sender: Agent,
|
sender: Agent,
|
||||||
rely_messages: Optional[List[AgentMessage]] = None,
|
rely_messages: Optional[List[AgentMessage]] = None,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
return {
|
return {
|
||||||
"user_input": received_message.content,
|
"user_input": received_message.content,
|
||||||
@ -215,6 +220,23 @@ class AppStarterAgent(ConversableAgent):
|
|||||||
"parent_agent": self,
|
"parent_agent": self,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def desc(self) -> Optional[str]:
|
||||||
|
resources: List[AppResource] = get_app_resource_list(self.resource)
|
||||||
|
|
||||||
|
return f"根据用户问题匹配合适的应用来进行回答. AppStart下的应用中可以解决下列问题:{[f' {res.app_desc}' for res in resources]}"
|
||||||
|
|
||||||
|
|
||||||
|
def get_app_resource_list(resource: Resource) -> List[AppResource]:
|
||||||
|
app_resource_list: List[AppResource] = []
|
||||||
|
if resource.type() == ResourceType.Pack:
|
||||||
|
for sub_resource in resource.sub_resources:
|
||||||
|
if sub_resource.type() == ResourceType.App:
|
||||||
|
app_resource_list.extend(AppResource.from_resource(sub_resource))
|
||||||
|
if resource.type() == ResourceType.App:
|
||||||
|
app_resource_list.extend(AppResource.from_resource(resource))
|
||||||
|
return app_resource_list
|
||||||
|
|
||||||
|
|
||||||
agent_manage = get_agent_manager()
|
agent_manage = get_agent_manager()
|
||||||
agent_manage.register_agent(AppStarterAgent)
|
agent_manage.register_agent(AppStarterAgent)
|
||||||
|
Loading…
Reference in New Issue
Block a user