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(Action[SummaryActionInput]):

def init(self, **kwargs):

super().init(**kwargs)

@property

def resource_need(self) -> Optional[ResourceType]:

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) -> Optional[Vis]:

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: Optional[AgentResource] = None,

rely_action_out: Optional[ActionOutput] = 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(Action[SummaryActionInput]):

def init(self, **kwargs):

super().init(**kwargs)

@property

def resource_need(self) -> Optional[ResourceType]:

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) -> Optional[Vis]:

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: Optional[AgentResource] = None,

rely_action_out: Optional[ActionOutput] = 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了

相关推荐
木鱼show1 小时前
Flask集成pyotp生成动态口令
python·flask·totp动态口令
hqxstudying1 小时前
Redis击穿,穿透和雪崩详解以及解决方案
java·数据库·redis·缓存
lubiii_2 小时前
网络安全十大漏洞
python·安全·web安全·网络安全
之之为知知2 小时前
深度学习能取代机器学习吗?
人工智能·pytorch·python·深度学习·机器学习·数据挖掘·tensorflow
小森77672 小时前
(九)深度学习---自然语言处理基础
人工智能·python·深度学习·机器学习·自然语言处理·keras
hello kitty w2 小时前
Python学习(1) ----- Python的文件读取和写入
java·python·学习
Coding的叶子2 小时前
深入了解PyTorch:起源、优势、发展与安装指南
人工智能·pytorch·python
檀越剑指大厂2 小时前
【数据库系列】bulk_save_objects 与 bulk_insert_mappings 对比
运维·服务器·数据库
hvinsion2 小时前
【开源解析】基于PyQt5+Folium的谷歌地图应用开发:从入门到实战
python·qt·开源·地图·谷歌地图·folium·地图引擎
Z.Virgil3 小时前
【案例94】笛卡尔积导致报“临时表空间不足”
java·运维·服务器·开发语言·数据库·sql