【AI Agent系列】【阿里AgentScope框架】1. 深入源码:详细解读AgentScope中的智能体定义以及模型配置的流程

简介: 【AI Agent系列】【阿里AgentScope框架】1. 深入源码:详细解读AgentScope中的智能体定义以及模型配置的流程
  • 大家好,我是 同学小张,日常分享AI知识和实战案例
  • 欢迎 点赞 + 关注 👏,持续学习持续干货输出
  • + 一起交流💬,一起进步💪。
  • 微信公众号也可搜【同学小张】 🙏

本站文章一览:


书接上篇文章上篇文章我们运行了第一个AgentScope程序,里面我们创建了两个智能体:DialogAgent对话智能体 和 UserAgent用户智能体。本文我们来深入源码,看AgentScope框架内,这些智能体是如何实现的。

0. 智能体基类 - AgentBase

class AgentBase(Operator, metaclass=_RecordInitSettingMeta):
    """Base class for all agents.
    All agents should inherit from this class and implement the `reply`
    function.
    """

这是AgentScope中智能体的基类,所有智能体的定义都需要继承它,并且重写其 reply 函数。

0.1 初始化过程

0.1.1 总过程

初始化实现的功能:

(1)初始化的过程,name是必须参数。其余都可选。

(2)load_model_by_config_name 来加载模型配置,后面细说。

(3)use_memory 默认为True,使用memory时,memoryTemporaryMemory类型,并接收一个可选的 memory_config 参数。

def __init__(
    self,
    name: str,
    sys_prompt: Optional[str] = None,
    model_config_name: str = None,
    use_memory: bool = True,
    memory_config: Optional[dict] = None,
) -> None:
    r"""Initialize an agent from the given arguments.
    Args:
        name (`str`):
            The name of the agent.
        sys_prompt (`Optional[str]`):
            The system prompt of the agent, which can be passed by args
            or hard-coded in the agent.
        model_config_name (`str`, defaults to None):
            The name of the model config, which is used to load model from
            configuration.
        use_memory (`bool`, defaults to `True`):
            Whether the agent has memory.
        memory_config (`Optional[dict]`):
            The config of memory.
    """
    self.name = name
    self.memory_config = memory_config
    if sys_prompt is not None:
        self.sys_prompt = sys_prompt
    # TODO: support to receive a ModelWrapper instance
    if model_config_name is not None:
        self.model = load_model_by_config_name(model_config_name)
    if use_memory:
        self.memory = TemporaryMemory(memory_config)
    else:
        self.memory = None
    # The global unique id of this agent
    self._agent_id = self.__class__.generate_agent_id()
    # The audience of this agent, which means if this agent generates a
    # response, it will be passed to all agents in the audience.
    self._audience = None

0.1.2 加载模型配置 - load_model_by_config_name

def _get_model_wrapper(model_type: str) -> Type[ModelWrapperBase]:
    ......
    if model_type in ModelWrapperBase.type_registry:
        return ModelWrapperBase.type_registry[  # type: ignore[return-value]
            model_type
        ]
    elif model_type in ModelWrapperBase.registry:
        return ModelWrapperBase.registry[  # type: ignore[return-value]
            model_type
        ]
    elif model_type in ModelWrapperBase.deprecated_type_registry:
        cls = ModelWrapperBase.deprecated_type_registry[model_type]
        ......
        return cls  # type: ignore[return-value]
    else:
        ......
        return PostAPIModelWrapperBase
def load_model_by_config_name(config_name: str) -> ModelWrapperBase:
    """Load the model by config name."""
    ......
    return _get_model_wrapper(model_type=model_type)(**kwargs)

从源码看,这个函数的作用是根据你配置中的模型名称来加载模型的APIWrapper,例如我们上篇文章中的"model_type": "openai"。理论上,这里的"model_type"的值,只要是下面图中的任意一个即可:

0.1.3 模型配置的一些细节

上篇文章中的配置部分我有一些疑问:

今天通过源码知道了其中的门道,看下面关于OpenAI接口的封装:

model_name是在外面赋值的,所以必须要有。

api_key是通过OpenAI接口传递进去的参数,根据我们先前的使用经验,这里可以不传,只要环境变量中存在 OPENAI_API_KEYOPENAI_BASE_URL 即可使用。

1. 对话智能体 - DialogAgent

定义如下:通过 sys_prompt 参数指定其承担的角色,例如 “你是一个Python专家” 让其充当Python专家给你解决问题。

class DialogAgent(AgentBase):
    """A simple agent used to perform a dialogue. Your can set its role by
    `sys_prompt`."""

1.1 初始化过程

def __init__(
    self,
    name: str,
    sys_prompt: str,
    model_config_name: str,
    use_memory: bool = True,
    memory_config: Optional[dict] = None,
    prompt_type: Optional[PromptType] = None,
) -> None:
    """Initialize the dialog agent.
    Arguments:
        name (`str`):
            The name of the agent.
        sys_prompt (`Optional[str]`):
            The system prompt of the agent, which can be passed by args
            or hard-coded in the agent.
        model_config_name (`str`):
            The name of the model config, which is used to load model from
            configuration.
        use_memory (`bool`, defaults to `True`):
            Whether the agent has memory.
        memory_config (`Optional[dict]`):
            The config of memory.
        prompt_type (`Optional[PromptType]`, defaults to
        `PromptType.LIST`):
            The type of the prompt organization, chosen from
            `PromptType.LIST` or `PromptType.STRING`.
    """

初始化参数namesys_promptmodel_config_name是必须设置的。

1.2 重写 reply 函数

这个reply函数是必须重写的。下面来看源码:

def reply(self, x: dict = None) -> dict:
    """Reply function of the agent. Processes the input data,
    generates a prompt using the current dialogue memory and system
    prompt, and invokes the language model to produce a response. The
    response is then formatted and added to the dialogue memory.
    Args:
        x (`dict`, defaults to `None`):
            A dictionary representing the user's input to the agent. This
            input is added to the dialogue memory if provided. Defaults to
            None.
    Returns:
        A dictionary representing the message generated by the agent in
        response to the user's input.
    """
    # record the input if needed
    if self.memory:
        self.memory.add(x)
    # prepare prompt
    prompt = self.model.format(
        Msg("system", self.sys_prompt, role="system"),
        self.memory and self.memory.get_memory(),  # type: ignore[arg-type]
    )
    # call llm and generate response
    response = self.model(prompt).text
    msg = Msg(self.name, response, role="assistant")
    # Print/speak the message in this agent's voice
    self.speak(msg)
    # Record the message in memory
    if self.memory:
        self.memory.add(msg)
    return msg

(1)首先是 memory 的操作,开始时将输入信息添加到 memory 中,结束前将大模型的回复写入到 memory 中:

if self.memory:
    self.memory.add(x)
......
if self.memory:
    self.memory.add(msg)

(2)prompt组装部分:将 system_promptmemory的内容组装成OpenAI需要的message列表。

def format(
    self,
    *args: Union[Msg, Sequence[Msg]],
) -> List[dict]:
......
    messages = []
    for arg in args:
        if arg is None:
            continue
        if isinstance(arg, Msg):
            messages.append(
                {
                    "role": arg.role,
                    "name": arg.name,
                    "content": _convert_to_str(arg.content),
                },
            )
        elif isinstance(arg, list):
            messages.extend(self.format(*arg))
        ......
    return messages
prompt = self.model.format(
    Msg("system", self.sys_prompt, role="system"),
    self.memory and self.memory.get_memory(),  # type: ignore[arg-type]
)

(3)调用大模型获取结果

response = self.model(prompt).text

1.3 小结

所以,对话智能体的实现就是将 system_prompt 和 对话的 memory 组装成prompt,然后让大模型回复。

2. 用户智能体 - UserAgent

2.1 初始化过程

所有参数都是可选的,默认名称是 “User” :

class UserAgent(AgentBase):
    """User agent class"""
    def __init__(self, name: str = "User", require_url: bool = False) -> None:
        """Initialize a UserAgent object.
        Arguments:
            name (`str`, defaults to `"User"`):
                The name of the agent. Defaults to "User".
            require_url (`bool`, defaults to `False`):
                Whether the agent requires user to input a URL. Defaults to
                False. The URL can lead to a website, a file,
                or a directory. It will be added into the generated message
                in field `url`.
        """
        super().__init__(name=name)
        self.name = name
        self.require_url = require_url

2.2 重写 reply 函数

实现的功能就是接收用户输入:content = user_input(timeout=timeout)

def user_input(
    prefix: str = "User input: ",
    timeout: Optional[int] = None,
) -> str:
    """get user input"""
    if hasattr(thread_local_data, "uid"):
        content = get_player_input(
            timeout=timeout,
            uid=thread_local_data.uid,
        )
    else:
        if timeout:
            from inputimeout import inputimeout, TimeoutOccurred
            try:
                content = inputimeout(prefix, timeout=timeout)
            except TimeoutOccurred as exc:
                raise TimeoutError("timed out") from exc
        else:
            content = input(prefix)
    return content
def reply(
    self,
    x: dict = None,
    required_keys: Optional[Union[list[str], str]] = None,
    timeout: Optional[int] = None,
) -> dict:
    ......
    if self.memory:
        self.memory.add(x)
    ......
    content = user_input(timeout=timeout)
    ......
    # Add to memory
    if self.memory:
        self.memory.add(msg)
    return msg

2.3 小结

所以,UserAgent的作用就是接收用户输入,让人参与到多智能体的交互中。

3. 总结

本文主要看了下AgentScope中智能体agent的定义源码,深入学习了agent初始化的过程,配置的加载等流程。挑选了两个简单的agent - DialogAgent 和 UserAgent进行了详细学习。个性化Agent的定义,需要继承AgentBase基类,重写其reply函数,在reply函数中,定义个性化Agent的个性化动作。

AgentScope目前还有一些其它的个性化Agent,例如react_agent、rpc_agent等,咱们后面用到再学。

如果觉得本文对你有帮助,麻烦点个赞和关注呗 ~~~


  • 大家好,我是 同学小张,日常分享AI知识和实战案例
  • 欢迎 点赞 + 关注 👏,持续学习持续干货输出
  • 一起交流💬,一起进步💪。
  • 微信公众号也可搜【同学小张】 🙏

本站文章一览:

相关文章
|
9天前
|
人工智能 Ubuntu 机器人
AI电销机器人系统源码部署之:freeswitch安装Linux
在Linux服务器上安装FreeSWITCH的简要步骤:更新软件包,安装依赖(如build-essential,libssl-dev等),下载v1.10.7源代码,解压并配置,编译,然后运行`./bootstrap.sh -j`,`./configure`,`make`,`make install`。启动FreeSWITCH服务,配置SIP用户和路由,测试连接与通话,并确保防火墙打开SIP(5060)和RTP端口。注意,实际部署可能需按需求调整。
|
10天前
|
存储 人工智能 测试技术
【AI智能体】SuperAGI-开源AI Agent 管理平台
【4月更文挑战第9天】智能体管理平台SuperAGI简介及实践
|
11天前
|
存储 搜索推荐 前端开发
变革来袭!多Agent框架MuAgent带你解锁代码开发新姿势
在这个信息技术爆炸的时代,我们都知道大型语言模型(LLM)拥有处理复杂问题的能力,但当遇到编程难题这种更高级的挑战时,单独的LLM Agent可能就不够看了。社区里动起了脑筋,玩出了新花样——组合多个Agent来应对高难度挑战!正如Multi Agent的构建过程所示,与其说我们是在设计Agents,不如说是对当前需求的深入理解后去构建出一条专属于某个场景的SOP。
36 1
|
12天前
|
人工智能 API 决策智能
【AI Agent系列】【阿里AgentScope框架】实战1:利用AgentScope实现动态创建Agent和自由组织讨论
【AI Agent系列】【阿里AgentScope框架】实战1:利用AgentScope实现动态创建Agent和自由组织讨论
56 2
|
12天前
|
人工智能 决策智能 C++
【AI Agent系列】【阿里AgentScope框架】5. Pipeline模块的组合使用及Pipeline模块总结
【AI Agent系列】【阿里AgentScope框架】5. Pipeline模块的组合使用及Pipeline模块总结
32 1
|
12天前
|
人工智能 决策智能
【AI Agent系列】【阿里AgentScope框架】4. 深入源码:Pipeline模块如何组织多智能体间的数据流?- 循环结构
【AI Agent系列】【阿里AgentScope框架】4. 深入源码:Pipeline模块如何组织多智能体间的数据流?- 循环结构
24 0
|
12天前
|
人工智能 决策智能
【AI Agent系列】【阿里AgentScope框架】3. 深入源码:Pipeline模块如何组织多智能体间的数据流?- 顺序结构与条件分支
【AI Agent系列】【阿里AgentScope框架】3. 深入源码:Pipeline模块如何组织多智能体间的数据流?- 顺序结构与条件分支
31 2
|
12天前
|
人工智能 决策智能 C++
【AI Agent教程】【MetaGPT】案例拆解:使用MetaGPT实现“狼人杀“游戏(1)- 整体框架解析
【AI Agent教程】【MetaGPT】案例拆解:使用MetaGPT实现“狼人杀“游戏(1)- 整体框架解析
109 1
|
12天前
|
人工智能 决策智能
【AI Agent系列】【阿里AgentScope框架】2. Pipeline模块入门:使用Pipeline模块实现最简单的多智能体交互
【AI Agent系列】【阿里AgentScope框架】2. Pipeline模块入门:使用Pipeline模块实现最简单的多智能体交互
29 0
|
12天前
|
存储 人工智能 开发框架
【AI Agent系列】【阿里AgentScope框架】0. 快速上手:AgentScope框架简介与你的第一个AgentScope程序
【AI Agent系列】【阿里AgentScope框架】0. 快速上手:AgentScope框架简介与你的第一个AgentScope程序
118 0

热门文章

最新文章