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了

相关推荐
xingpanvip21 小时前
使用 Webwright 在 CSDN 自动发文:Python 浏览器自动化实践
开发语言·python·自动化
薛定猫AI21 小时前
【深度解析】从无状态 ChatBot 到有状态 AI Companion:大模型记忆系统原理与工程落地
大数据·人工智能·gpt
armwind21 小时前
openISP学习7-CCM — Color Correction Matrix(色彩校正矩阵)
python·学习·矩阵
C137的本贾尼21 小时前
MySQL 整体架构与存储引擎对比
数据库·mysql·架构
艺杯羹21 小时前
零成本!3步设置Windows动态壁纸,免费无广告
python
C137的本贾尼21 小时前
【实战】分析一张真实业务表的 InnoDB 存储结构
java·大数据·数据库
huangdong_21 小时前
京东整店商品图片视频批量下载技术:从商品列表到自动分类
开发语言·python·音视频
超梦dasgg21 小时前
亿级数据 不停服务平滑迁移(生产环境实战方案)
java·数据库
j_xxx404_21 小时前
MySQL数据库基础硬核解析:从 C/S 网络服务到磁盘文件与存储引擎
linux·运维·服务器·开发语言·数据库·mysql·ai
糖果店的幽灵21 小时前
Spring AI 从入门到精通-ChatClient你与 AI 对话的终极武器
人工智能·python·spring