Autogen_core 测试代码:test_base_agent.py

目录

    • 测试代码
    • 测试代码讲解
      • 详细解释
        • 第一部分:定义`NoopAgent`类
        • 第二部分:测试函数`test_base_agent_create_notebook`
      • 完成的功能

测试代码

代码运行在notebook上

from autogen_core import (
    BaseAgent,
    MessageContext,
)
from typing import Any

class NoopAgent(BaseAgent):
    def __init__(self) -> None:
        super().__init__("A no op agent")

    async def on_message_impl(self, message: Any, ctx: MessageContext) -> Any:
        raise NotImplementedError


import asyncio
from unittest.mock import Mock
from autogen_core import AgentId, AgentInstantiationContext, AgentRuntime

async def test_base_agent_create_notebook():
    runtime = Mock(spec=AgentRuntime)   
    
    with AgentInstantiationContext.populate_context((runtime, AgentId("name", "namespace"))):
        agent = NoopAgent()
        assert agent.runtime == runtime
        assert agent.id == AgentId("name", "namespace")
        print("Test Passed: Agent runtime and ID are correctly assigned.")

# 直接运行异步函数
await test_base_agent_create_notebook()

Test Passed: Agent runtime and ID are correctly assigned.

测试代码讲解

这段代码涉及两个主要部分:

  1. 定义了一个NoopAgent,它继承自BaseAgent。这个类目前没有实现任何实际的功能,其on_message_impl方法直接抛出了一个NotImplementedError异常,表明这个方法还没有具体的实现。

  2. 定义了一个异步的测试函数test_base_agent_create_notebook,用于测试BaseAgent类的实例化和环境上下文的设置。

详细解释

第一部分:定义NoopAgent
from autogen_core import (
    BaseAgent,
    MessageContext,
)
from typing import Any

class NoopAgent(BaseAgent):
    def __init__(self) -> None:
        super().__init__("A no op agent")

    async def on_message_impl(self, message: Any, ctx: MessageContext) -> Any:
        raise NotImplementedError
  • NoopAgent类继承自BaseAgent: 这意味着NoopAgent具备BaseAgent的所有属性和方法。
  • __init__方法: 初始化方法中,调用了父类的初始化方法super().__init__("A no op agent"),为这个代理设置了一个名称。
  • on_message_impl方法: 这是一个异步方法,用于处理接收到的消息。当前实现中,它直接抛出NotImplementedError,表示这个方法还没有被具体实现。
第二部分:测试函数test_base_agent_create_notebook
import asyncio
from unittest.mock import Mock
from autogen_core import AgentId, AgentInstantiationContext, AgentRuntime

async def test_base_agent_create_notebook():
    runtime = Mock(spec=AgentRuntime)   
    with AgentInstantiationContext.populate_context((runtime, AgentId("name", "namespace"))):
        agent = NoopAgent()
        assert agent.runtime == runtime
        assert agent.id == AgentId("name", "namespace")
        print("Test Passed: Agent runtime and ID are correctly assigned.")

# 直接运行异步函数
await test_base_agent_create_notebook()

  • Mock(spec=AgentRuntime): 创建了一个AgentRuntime的模拟对象runtime,用于测试。
  • AgentInstantiationContext.populate_context: 这个上下文管理器用于设置当前环境中的运行时和代理ID。
  • 创建NoopAgent实例: 在给定的上下文中创建了一个NoopAgent实例。
  • 断言检查: 通过两个断言语句检查:
    • agent.runtime == runtime:确保代理的运行时环境与测试中设置的模拟运行时一致。
    • agent.id == AgentId("name", "namespace"):确保代理的ID与上下文中设置的ID一致。

如果两个断言都通过,则打印“Test Passed: Agent runtime and ID are correctly assigned.”。

完成的功能

这段代码主要完成了一个单元测试,验证了以下功能:

  • BaseAgent的子类能够正确地实例化。
  • 在实例化过程中,能够正确地设置代理的运行时环境和ID。
  • 测试框架能够捕获并确认这些设置是否按预期工作。

测试通过了,这表明BaseAgent类及其相关组件(如AgentInstantiationContext)能够正确地设置和管理代理的运行时环境和ID。

你可能感兴趣的:(autogen,人工智能,agent)