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了

相关推荐
计算机毕设定制辅导-无忧学长1 小时前
西门子 PLC 与 Modbus 集成:S7-1500 RTU/TCP 配置指南(一)
服务器·数据库·tcp/ip
小赖同学啊1 小时前
物联网数据安全区块链服务
开发语言·python·区块链
程序员柳1 小时前
基于微信小程序的校园二手交易平台、微信小程序校园二手商城源代码+数据库+使用说明,layui+微信小程序+Spring Boot
数据库·微信小程序·layui
码荼2 小时前
学习开发之hashmap
java·python·学习·哈希算法·个人开发·小白学开发·不花钱不花时间crud
梦在深巷、2 小时前
MySQL/MariaDB数据库主从复制之基于二进制日志的方式
linux·数据库·mysql·mariadb
IT乌鸦坐飞机2 小时前
ansible部署数据库服务随机启动并创建用户和设置用户有完全权限
数据库·ansible·centos7
IT_10242 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
猫头虎2 小时前
猫头虎 AI工具分享:一个网页抓取、结构化数据提取、网页爬取、浏览器自动化操作工具:Hyperbrowser MCP
运维·人工智能·gpt·开源·自动化·文心一言·ai编程
小陈phd3 小时前
李宏毅机器学习笔记——梯度下降法
人工智能·python·机器学习
kk爱闹3 小时前
【挑战14天学完python和pytorch】- day01
android·pytorch·python