类class
agentscope.middlewareModelRouterMiddleware
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
方法摘要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_model | ClassifierModelBase | ChatModelBase | 分类器模型(类型 `ClassifierModelBase | ChatModelBase`)。The classifier or chat model used to select a candidate. |
candidates | Sequence[ChatModelCandidate] | candidates(类型 `Sequence[ChatModelCandidate]`)。The uniquely named chat model candidates. |
instructions默认default _DEFAULT_INSTRUCTIONS | str | 路由问题的指令。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_kwargs | dict | 输入关键字参数(类型 `dict`)。The input kwargs. |
next_handler | Callable[..., AsyncGenerator] | 下一个处理器(类型 `Callable[..., AsyncGenerator]`)。The next handler. |
AsyncGenerator
返回AsyncGenerator。Returns async generator.
源码Source
agentscope/middleware/_model_router.py:84–110