DB-GPT扩展自定义Agent配置说明

简介

文章主要介绍了如何扩展一个自定义Agent,这里是用官方提供的总结摘要的Agent做了个示例,先给大家看下显示效果

代码目录

博主将代码放在core目录了,后续经过对源码的解读感觉放在dbgpt_serve.agent.agents.expand目录下可能更合适,大家自行把控即可

代码详情

summarizer_action.py

from typing import Optional

from pydantic import BaseModel, Field

from dbgpt.vis import Vis

from dbgpt.agent import Action, ActionOutput, AgentResource, ResourceType

from dbgpt.agent.util import cmp_string_equal

NOT_RELATED_MESSAGE = "Did not find the information you want."

The parameter object that the Action that the current Agent needs to execute needs to output.

class SummaryActionInput(BaseModel):

summary: str = Field(

...,

description="The summary content",

)

class SummaryAction(ActionSummaryActionInput):

def init(self, **kwargs):

super().init(**kwargs)

@property

def resource_need(self) -> OptionalResourceType:

The resource type that the current Agent needs to use

here we do not need to use resources, just return None

return None

@property

def render_protocol(self) -> OptionalVis:

The visualization rendering protocol that the current Agent needs to use

here we do not need to use visualization rendering, just return None

return None

@property

def out_model_type(self):

return SummaryActionInput

async def run(

self,

ai_message: str,

resource: OptionalAgentResource = None,

rely_action_out: OptionalActionOutput = None,

need_vis_render: bool = True,

**kwargs,

) -> ActionOutput:

"""Perform the action.

The entry point for actual execution of Action. Action execution will be

automatically initiated after model inference.

"""

try:

Parse the input message

param: SummaryActionInput = self._input_convert(ai_message, SummaryActionInput)

except Exception:

return ActionOutput(

is_exe_success=False,

content="The requested correctly structured answer could not be found, "

f"ai message: {ai_message}",

)

Check if the summary content is not related to user questions

if param.summary and cmp_string_equal(

param.summary,

NOT_RELATED_MESSAGE,

ignore_case=True,

ignore_punctuation=True,

ignore_whitespace=True,

):

return ActionOutput(

is_exe_success=False,

content="the provided text content is not related to user questions at all."

f"ai message: {ai_message}",

)

else:

return ActionOutput(

is_exe_success=True,

content=param.summary,

)

summarizer_agent.py

from typing import Optional

from pydantic import BaseModel, Field

from dbgpt.vis import Vis

from dbgpt.agent import Action, ActionOutput, AgentResource, ResourceType

from dbgpt.agent.util import cmp_string_equal

NOT_RELATED_MESSAGE = "Did not find the information you want."

The parameter object that the Action that the current Agent needs to execute needs to output.

class SummaryActionInput(BaseModel):

summary: str = Field(

...,

description="The summary content",

)

class SummaryAction(ActionSummaryActionInput):

def init(self, **kwargs):

super().init(**kwargs)

@property

def resource_need(self) -> OptionalResourceType:

The resource type that the current Agent needs to use

here we do not need to use resources, just return None

return None

@property

def render_protocol(self) -> OptionalVis:

The visualization rendering protocol that the current Agent needs to use

here we do not need to use visualization rendering, just return None

return None

@property

def out_model_type(self):

return SummaryActionInput

async def run(

self,

ai_message: str,

resource: OptionalAgentResource = None,

rely_action_out: OptionalActionOutput = None,

need_vis_render: bool = True,

**kwargs,

) -> ActionOutput:

"""Perform the action.

The entry point for actual execution of Action. Action execution will be

automatically initiated after model inference.

"""

try:

Parse the input message

param: SummaryActionInput = self._input_convert(ai_message, SummaryActionInput)

except Exception:

return ActionOutput(

is_exe_success=False,

content="The requested correctly structured answer could not be found, "

f"ai message: {ai_message}",

)

Check if the summary content is not related to user questions

if param.summary and cmp_string_equal(

param.summary,

NOT_RELATED_MESSAGE,

ignore_case=True,

ignore_punctuation=True,

ignore_whitespace=True,

):

return ActionOutput(

is_exe_success=False,

content="the provided text content is not related to user questions at all."

f"ai message: {ai_message}",

)

else:

return ActionOutput(

is_exe_success=True,

content=param.summary,

)

这样重启项目就能看到自定义的agent了

相关推荐
用户033212666367几秒前
使用 Python 将 HTML 内容添加到 PowerPoint 演示文稿中
python·html
看浪的路人2 分钟前
第4讲:MCP Client 开发——连接、发现、调用
windows·python·ai
Swift社区9 分钟前
Python 开发环境怎么选?PyCharm、VS Code、Trae 谁更适合 AI 开发?
人工智能·python·pycharm
卷无止境13 分钟前
聊聊Web开发里的流式数据 从原理到FastAPI实战
后端·python·fastapi
SMF191914 分钟前
【PyCharm】让 PyCharm 使用 .venv 虚拟环境
ide·python·pycharm
Lysander.Jovian18 分钟前
Nginx服务2
运维·数据库·nginx
修远客19 分钟前
感知模块:Agent的眼睛和耳朵 — 三层降级策略让Agent永不"失明"
python·agent
小张同学a.21 分钟前
zabbix企业级监控平台4——分布式监控与grafana数据可视化
linux·运维·数据库·分布式·信息可视化·zabbix·grafana
渣渣盟23 分钟前
当 Redis 写入不再是瓶颈后,Flink 任务的反压可能来自哪里?如何系统性地定位和解决 Flink 反压问题?
数据库·redis·flink
七牛开发者25 分钟前
Codex 实践系列 Vol.04:用 Goal 和 Plan 管住一个长任务
java·数据库·人工智能·github·copilot