AS AgentScopeAPI 手册 / API Reference
类class agentscope.middleware

ModelRouterMiddleware

class ModelRouterMiddleware(MiddlewareBase)
源码Source agentscope/middleware/_model_router.py:37–158

模型路由器中间件,在智能体执行流程中提供横切扩展。位于 agentscope.middleware 包,继承自 MiddlewareBase。核心成员包括 on_reply。

The decision is made once per reply and kept when the reply resumes, e.g. after a user confirmation. When the routing model fails or picks an unknown candidate, the agent keeps its own model.

构造器摘要Constructor Summary

构造器Constructor说明Description
def __init__(classifier_model: ClassifierModelBase | ChatModelBase, candidates: Sequence[ChatModelCandidate], instructi…
初始化 `ModelRouterMiddleware` 实例。
Initialize the model router.

方法摘要Method Summary

方法Method说明Description
async def on_reply(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGeneratorasync
拦截并扩展回复流程。
Swap ``agent.model`` for the selected candidate during the reply.

构造器详情Constructor Detail

ModelRouterMiddleware

def __init__(classifier_model: ClassifierModelBase | ChatModelBase, candidates: Sequence[ChatModelCandidate], instructions: str = _DEFAULT_INSTRUCTIONS) -> None

初始化 ModelRouterMiddleware 实例。

Initialize the model router.

参数Parameters
名称Name类型Type说明Description
classifier_modelClassifierModelBase | ChatModelBase分类器模型(类型 `ClassifierModelBase | ChatModelBase`)。The classifier or chat model used to select a candidate.
candidatesSequence[ChatModelCandidate]candidates(类型 `Sequence[ChatModelCandidate]`)。The uniquely named chat model candidates.
instructions默认default _DEFAULT_INSTRUCTIONSstr路由问题的指令。The instructions of the routing question.
源码Source agentscope/middleware/_model_router.py:45–82

方法详情Method Detail

on_reply

async def on_reply(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGenerator
async

拦截并扩展回复流程。

Swap agent.model for the selected candidate during the reply.

参数Parameters
名称Name类型Type说明Description
agent'Agent'智能体(类型 `'Agent'`)。The agent.
input_kwargsdict输入关键字参数(类型 `dict`)。The input kwargs.
next_handlerCallable[..., AsyncGenerator]下一个处理器(类型 `Callable[..., AsyncGenerator]`)。The next handler.
返回Returns AsyncGenerator
返回AsyncGenerator。Returns async generator.
源码Source agentscope/middleware/_model_router.py:84–110

继承成员Inherited Members

基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.