From b951b50689cedaae23c22dcfbf174cce445cb00a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=8E=E5=A4=A9?= <460342015@qq.com> Date: Wed, 4 Sep 2024 10:59:03 +0800 Subject: [PATCH] feat(agent):Fix agent bug (#1953) Co-authored-by: aries_ckt <916701291@qq.com> --- dbgpt/agent/core/action/base.py | 4 ++-- dbgpt/agent/core/action/blank_action.py | 6 ++--- dbgpt/agent/core/agent_manage.py | 4 ---- dbgpt/agent/core/base_agent.py | 2 +- dbgpt/agent/core/plan/plan_action.py | 2 +- dbgpt/agent/expand/actions/chart_action.py | 6 ++--- dbgpt/agent/expand/actions/code_action.py | 6 ++--- .../agent/expand/actions/dashboard_action.py | 6 ++--- .../agent/expand/actions/indicator_action.py | 6 ++--- dbgpt/agent/expand/actions/tool_action.py | 6 ++--- dbgpt/agent/resource/knowledge.py | 4 ++-- dbgpt/app/openapi/api_v1/api_v1.py | 11 +++++++++ .../chat_excel/excel_analyze/chat.py | 2 ++ .../chat_data/chat_excel/excel_reader.py | 2 +- dbgpt/datasource/manages/connect_config_db.py | 9 +++++-- .../agents/expand/actions/app_start_action.py | 3 ++- .../actions/intent_recognition_action.py | 24 +++++++++++++------ .../community/community_store.py | 10 +++----- 18 files changed, 67 insertions(+), 46 deletions(-) diff --git a/dbgpt/agent/core/action/base.py b/dbgpt/agent/core/action/base.py index 04733a13a..9061efdfa 100644 --- a/dbgpt/agent/core/action/base.py +++ b/dbgpt/agent/core/action/base.py @@ -82,10 +82,10 @@ def to_dict(self) -> Dict[str, Any]: class Action(ABC, Generic[T]): """Base Action class for defining agent actions.""" - def __init__(self): + def __init__(self, language: str = "en"): """Create an action.""" self.resource: Optional[Resource] = None - self.language: str = "en" + self.language: str = language def init_resource(self, resource: Optional[Resource]): """Initialize the resource.""" diff --git a/dbgpt/agent/core/action/blank_action.py b/dbgpt/agent/core/action/blank_action.py index e084b0738..cd19dce6b 100644 --- a/dbgpt/agent/core/action/blank_action.py +++ b/dbgpt/agent/core/action/blank_action.py @@ -12,9 +12,9 @@ class BlankAction(Action): """Blank action class.""" - def __init__(self): - """Create a blank action.""" - super().__init__() + def __init__(self, **kwargs): + """Blank action init.""" + super().__init__(**kwargs) @property def ai_out_schema(self) -> Optional[str]: diff --git a/dbgpt/agent/core/agent_manage.py b/dbgpt/agent/core/agent_manage.py index a00417566..401c36fb9 100644 --- a/dbgpt/agent/core/agent_manage.py +++ b/dbgpt/agent/core/agent_manage.py @@ -127,9 +127,6 @@ def all_agents(self) -> Dict[str, str]: def list_agents(self): """Return a list of all registered agents and their descriptions.""" result = [] - from datetime import datetime - - logger.info(f"List Agent Begin:{datetime.now()}") for name, value in self._agents.items(): result.append( { @@ -137,7 +134,6 @@ def list_agents(self): "desc": value[1].goal, } ) - logger.info(f"List Agent End:{datetime.now()}") return result diff --git a/dbgpt/agent/core/base_agent.py b/dbgpt/agent/core/base_agent.py index 48670a7bf..f1be80c71 100644 --- a/dbgpt/agent/core/base_agent.py +++ b/dbgpt/agent/core/base_agent.py @@ -673,7 +673,7 @@ def _init_actions(self, actions: List[Type[Action]]): self.actions = [] for idx, action in enumerate(actions): if issubclass(action, Action): - self.actions.append(action()) + self.actions.append(action(language=self.language)) async def _a_append_message( self, message: AgentMessage, role, sender: Agent diff --git a/dbgpt/agent/core/plan/plan_action.py b/dbgpt/agent/core/plan/plan_action.py index 41e8dbc7a..4e6f1b1f1 100644 --- a/dbgpt/agent/core/plan/plan_action.py +++ b/dbgpt/agent/core/plan/plan_action.py @@ -40,7 +40,7 @@ class PlanAction(Action[List[PlanInput]]): def __init__(self, **kwargs): """Create a plan action.""" - super().__init__() + super().__init__(**kwargs) self._render_protocol = VisAgentPlans() @property diff --git a/dbgpt/agent/expand/actions/chart_action.py b/dbgpt/agent/expand/actions/chart_action.py index 8783f517b..b2f9386a0 100644 --- a/dbgpt/agent/expand/actions/chart_action.py +++ b/dbgpt/agent/expand/actions/chart_action.py @@ -31,9 +31,9 @@ class SqlInput(BaseModel): class ChartAction(Action[SqlInput]): """Chart action class.""" - def __init__(self): - """Create a chart action.""" - super().__init__() + def __init__(self, **kwargs): + """Chart action init.""" + super().__init__(**kwargs) self._render_protocol = VisChart() @property diff --git a/dbgpt/agent/expand/actions/code_action.py b/dbgpt/agent/expand/actions/code_action.py index c8d206932..0213c85f9 100644 --- a/dbgpt/agent/expand/actions/code_action.py +++ b/dbgpt/agent/expand/actions/code_action.py @@ -16,9 +16,9 @@ class CodeAction(Action[None]): """Code Action Module.""" - def __init__(self): - """Create a code action.""" - super().__init__() + def __init__(self, **kwargs): + """Code action init.""" + super().__init__(**kwargs) self._render_protocol = VisCode() self._code_execution_config = {} diff --git a/dbgpt/agent/expand/actions/dashboard_action.py b/dbgpt/agent/expand/actions/dashboard_action.py index 0a1d909df..3d0cb127b 100644 --- a/dbgpt/agent/expand/actions/dashboard_action.py +++ b/dbgpt/agent/expand/actions/dashboard_action.py @@ -39,9 +39,9 @@ def to_dict(self): class DashboardAction(Action[List[ChartItem]]): """Dashboard action class.""" - def __init__(self): - """Create a dashboard action.""" - super().__init__() + def __init__(self, **kwargs): + """Dashboard action init.""" + super().__init__(**kwargs) self._render_protocol = VisDashboard() @property diff --git a/dbgpt/agent/expand/actions/indicator_action.py b/dbgpt/agent/expand/actions/indicator_action.py index 407d51016..314eb9dbe 100644 --- a/dbgpt/agent/expand/actions/indicator_action.py +++ b/dbgpt/agent/expand/actions/indicator_action.py @@ -41,9 +41,9 @@ class IndicatorInput(BaseModel): class IndicatorAction(Action[IndicatorInput]): """Indicator Action.""" - def __init__(self): - """Init Indicator Action.""" - super().__init__() + def __init__(self, **kwargs): + """Init indicator action.""" + super().__init__(**kwargs) self._render_protocol = VisApiResponse() @property diff --git a/dbgpt/agent/expand/actions/tool_action.py b/dbgpt/agent/expand/actions/tool_action.py index 4c49d3a61..1f8dd63d4 100644 --- a/dbgpt/agent/expand/actions/tool_action.py +++ b/dbgpt/agent/expand/actions/tool_action.py @@ -34,9 +34,9 @@ class ToolInput(BaseModel): class ToolAction(Action[ToolInput]): """Tool action class.""" - def __init__(self): - """Create a plugin action.""" - super().__init__() + def __init__(self, **kwargs): + """Tool action init.""" + super().__init__(**kwargs) self._render_protocol = VisPlugin() @property diff --git a/dbgpt/agent/resource/knowledge.py b/dbgpt/agent/resource/knowledge.py index c3f5175e0..ca651d012 100644 --- a/dbgpt/agent/resource/knowledge.py +++ b/dbgpt/agent/resource/knowledge.py @@ -73,8 +73,8 @@ async def get_prompt( prompt_template = f"\nResources-{self.name}:\n {content}" prompt_template_zh = f"\n资源-{self.name}:\n {content}" if lang == "en": - return prompt_template.format(content=content), self._get_references(chunks) - return prompt_template_zh.format(content=content), self._get_references(chunks) + return prompt_template, self._get_references(chunks) + return prompt_template_zh, self._get_references(chunks) async def get_resources( self, diff --git a/dbgpt/app/openapi/api_v1/api_v1.py b/dbgpt/app/openapi/api_v1/api_v1.py index 05e052bed..4de2666bf 100644 --- a/dbgpt/app/openapi/api_v1/api_v1.py +++ b/dbgpt/app/openapi/api_v1/api_v1.py @@ -548,6 +548,17 @@ async def chat_completions( headers=headers, media_type="text/plain", ) + except Exception as e: + logger.exception(f"Chat Exception!{dialogue}", e) + + async def error_text(err_msg): + yield f"data:{err_msg}\n\n" + + return StreamingResponse( + error_text(str(e)), + headers=headers, + media_type="text/plain", + ) finally: # write to recent usage app. if dialogue.user_name is not None and dialogue.app_code is not None: diff --git a/dbgpt/app/scene/chat_data/chat_excel/excel_analyze/chat.py b/dbgpt/app/scene/chat_data/chat_excel/excel_analyze/chat.py index e12574c8d..b78f8b6de 100644 --- a/dbgpt/app/scene/chat_data/chat_excel/excel_analyze/chat.py +++ b/dbgpt/app/scene/chat_data/chat_excel/excel_analyze/chat.py @@ -34,6 +34,8 @@ def __init__(self, chat_param: Dict): """ self.select_param = chat_param["select_param"] + if not self.select_param: + raise ValueError("Please upload the Excel document you want to talk to!") self.model_name = chat_param["model_name"] chat_param["chat_mode"] = ChatScene.ChatExcel self.chat_param = chat_param diff --git a/dbgpt/app/scene/chat_data/chat_excel/excel_reader.py b/dbgpt/app/scene/chat_data/chat_excel/excel_reader.py index a6c543840..909007e2e 100644 --- a/dbgpt/app/scene/chat_data/chat_excel/excel_reader.py +++ b/dbgpt/app/scene/chat_data/chat_excel/excel_reader.py @@ -230,7 +230,7 @@ def is_chinese(text): class ExcelReader: - def __init__(self, conv_uid, file_param): + def __init__(self, conv_uid: str, file_param: str): self.conv_uid = conv_uid self.file_param = file_param if isinstance(file_param, str) and os.path.isabs(file_param): diff --git a/dbgpt/datasource/manages/connect_config_db.py b/dbgpt/datasource/manages/connect_config_db.py index 591b7b4cd..499ed0afa 100644 --- a/dbgpt/datasource/manages/connect_config_db.py +++ b/dbgpt/datasource/manages/connect_config_db.py @@ -214,10 +214,15 @@ def get_db_config(self, db_name): def get_db_list(self, db_name: Optional[str] = None, user_id: Optional[str] = None): """Get db list.""" session = self.get_raw_session() - if db_name: + if db_name and user_id: sql = f"SELECT * FROM connect_config where (user_id='{user_id}' or user_id='' or user_id IS NULL) and db_name='{db_name}'" # noqa - else: + elif user_id: sql = f"SELECT * FROM connect_config where user_id='{user_id}' or user_id='' or user_id IS NULL" # noqa + elif db_name: + sql = f"SELECT * FROM connect_config where db_name='{db_name}'" # noqa + else: + sql = f"SELECT * FROM connect_config" # noqa + result = session.execute(text(sql)) fields = [field[0] for field in result.cursor.description] # type: ignore data = [] diff --git a/dbgpt/serve/agent/agents/expand/actions/app_start_action.py b/dbgpt/serve/agent/agents/expand/actions/app_start_action.py index 1910a6f6f..d72cbed71 100644 --- a/dbgpt/serve/agent/agents/expand/actions/app_start_action.py +++ b/dbgpt/serve/agent/agents/expand/actions/app_start_action.py @@ -50,6 +50,7 @@ async def run( **kwargs, ) -> ActionOutput: conv_id = kwargs.get("conv_id") + user_input = kwargs.get("user_input") paren_agent = kwargs.get("paren_agent") init_message_rounds = kwargs.get("init_message_rounds") @@ -83,7 +84,7 @@ async def run( from dbgpt.serve.agent.agents.controller import multi_agents await multi_agents.agent_team_chat_new( - new_user_input, + new_user_input if new_user_input else user_input, conv_id, gpts_app, paren_agent.memory, diff --git a/dbgpt/serve/agent/agents/expand/actions/intent_recognition_action.py b/dbgpt/serve/agent/agents/expand/actions/intent_recognition_action.py index 49daca542..9bf5f6ca5 100644 --- a/dbgpt/serve/agent/agents/expand/actions/intent_recognition_action.py +++ b/dbgpt/serve/agent/agents/expand/actions/intent_recognition_action.py @@ -54,18 +54,28 @@ def out_model_type(self): @property def ai_out_schema(self) -> Optional[str]: - out_put_schema = { - "intent": "[The recognized intent is placed here]", - "app_code": "[App code in selected intent]", - "slots": {"意图定义中槽位属性1": "具体值", "意图定义中槽位属性2": "具体值"}, - "ask_user": "If you want the user to supplement slot data, ask the user a question", - "user_input": "[Complete instructions generated based on intent and slot]", - } if self.language == "en": + out_put_schema = { + "intent": "[The recognized intent is placed here]", + "app_code": "[App code in selected intent]", + "slots": { + "Slot attribute 1 in intent definition": "value", + "Slot attribute 2 in intent definition": "value", + }, + "ask_user": "[If you want the user to supplement slot data, ask the user a question]", + "user_input": "[Complete instructions generated based on intent and slot]", + } return f"""Please reply in the following json format: {json.dumps(out_put_schema, indent=2, ensure_ascii=False)} Make sure the output is only json and can be parsed by Python json.loads.""" # noqa: E501 else: + out_put_schema = { + "intent": "选择的意图放在这里", + "app_code": "选择意图对应的Appcode值", + "slots": {"意图定义中槽位属性1": "具体值", "意图定义中槽位属性2": "具体值"}, + "ask_user": "如果需要用户补充槽位属性的具体值,请向用户进行提问", + "user_input": "根据意图和槽位生成完整指令问题", + } return f"""请按如下JSON格式输出: {json.dumps(out_put_schema, indent=2, ensure_ascii=False)} 确保输出只有json,且可以被python json.loads加载.""" diff --git a/dbgpt/storage/knowledge_graph/community/community_store.py b/dbgpt/storage/knowledge_graph/community/community_store.py index f800848d5..41bb494a3 100644 --- a/dbgpt/storage/knowledge_graph/community/community_store.py +++ b/dbgpt/storage/knowledge_graph/community/community_store.py @@ -32,21 +32,17 @@ def __init__( async def build_communities(self): """Discover communities.""" - community_ids = await (self._community_store_adapter.discover_communities()) + community_ids = await self._community_store_adapter.discover_communities() # summarize communities communities = [] for community_id in community_ids: - community = await ( - self._community_store_adapter.get_community(community_id) - ) + community = await self._community_store_adapter.get_community(community_id) graph = community.data.format() if not graph: break - community.summary = await ( - self._community_summarizer.summarize(graph=graph) - ) + community.summary = await self._community_summarizer.summarize(graph=graph) communities.append(community) logger.info( f"Summarize community {community_id}: " f"{community.summary[:50]}..."