当前位置: 首页 > news >正文

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了

http://www.xdnf.cn/news/606349.html

相关文章:

  • 微信小程序调用蓝牙API “wx.writeBLECharacteristicValue()“ 报 errCode: 10008 的解决方案
  • GMP模型入门
  • Lyra学习笔记1地图角色加载流程
  • 树莓派WiringPi库
  • 大模型「瘦身」指南:从LLaMA到MobileBERT的轻量化部署实战
  • php 根据另一个数组中 create_time 的时间顺序,对原始数组进行排序。
  • Neo4j入门第一期(Cypher入门)
  • RabbitMQ ⑥-集群 || Raft || 仲裁队列
  • CentOS 7.6 升级 Openssl 及 Openssh 方法文档
  • Unity EventCenter 消息中心的设计与实现
  • EasyExcel使用
  • GD32 IIC(I2C)通信(使用示例为SD2068)
  • 2.4g芯片引脚功能
  • 56 在standby待机打通uart调试的方法
  • 5.23本日总结
  • SDL2常用函数SDL事件处理:SDL_Event|SDL_PollEvent
  • Vue+css实现扫描动画效果(使用@keyframes scan)
  • RequestBody注解中Map
  • 为什么信号经过线束会有衰减?
  • AG32VH 系列应用指南
  • 嵌入式鸿蒙openharmony应用开发环境搭建与工程创建实现
  • Postgresql 数据库实例管理命令
  • Spring IoC容器初始化过程
  • 设计模式-结构型模式(详解)
  • el-dialog 组件 多层嵌套 被遮罩问题
  • Redis 缓存使用的BigKey问题
  • SAP在金属行业的数字化转型:无锡哲讯科技的智能解决方案
  • A10服务器使用vllm推理框架成功运行Qwen3大模型
  • 机器学习第二十四讲:scikit-learn → 机器学习界的瑞士军刀
  • Rancher 部署与使用指南