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

RAGMiddleware

class RAGMiddleware(MiddlewareBase)
源码Source agentscope/middleware/_rag.py:626–1021

RAG 中间件,在智能体执行流程中提供横切扩展。位于 agentscope.middleware 包,继承自 MiddlewareBase。核心成员包括 list_tools、on_reply、on_reasoning。

Constructed from a list of :class:~agentscope.rag.KnowledgeBase handles — each carrying its own embedding model, vector store, and metadata filter. The middleware does not own these resources; it only orchestrates search against them.

示例Example

# Automatic injection (static mode)
middleware = RAGMiddleware(
    knowledge_bases=[kb1, kb2],
    search_config=SearchConfig(mode="static"),
)

# Agent-driven search (agentic mode, the default)
middleware = RAGMiddleware(knowledge_bases=[kb1, kb2])

agent = Agent(..., middlewares=[middleware], ...)

嵌套类摘要Nested Class Summary

类Class类型Kind说明Description
Parameters类class
参数,驱动智能体的推理、工具调用与会话执行。
User-tunable knowledge-base search parameters of :class:`RAGMiddleware`.

构造器摘要Constructor Summary

构造器Constructor说明Description
def __init__(knowledge_bases: list['KnowledgeBase'], parameters: 'RAGMiddleware.Parameters | None' = None, rerank_model…
初始化 `RAGMiddleware` 实例。
Initialize the RAG middleware.

方法摘要Method Summary

方法Method说明Description
async def list_tools() -> list[ToolBase]async
列出当前连接可用的工具。
Expose the search tool in ``"agentic"`` mode.
async def on_reply(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGeneratorasync
拦截并扩展回复流程。
Cache reply inputs for the static-mode search.
async def on_reasoning(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGenerat…async
拦截并扩展推理阶段。
Inject a one-shot RAG hint on the first reasoning step.

构造器详情Constructor Detail

RAGMiddleware

def __init__(knowledge_bases: list['KnowledgeBase'], parameters: 'RAGMiddleware.Parameters | None' = None, rerank_model: 'ChatModelBase | None' = None) -> None

初始化 RAGMiddleware 实例。

Initialize the RAG middleware.

参数Parameters
名称Name类型Type说明Description
knowledge_baseslist['KnowledgeBase']知识库 bases(类型 `list['KnowledgeBase']`)。The knowledge bases this agent searches.
parameters默认default None'RAGMiddleware.Parameters | None'参数(类型 `'RAGMiddleware.Parameters | None'`)。Search-time knobs (mode, top_k, score threshold, hint behaviour). `None` uses the defaults of :class:`SearchConfig`.
rerank_model默认default None'ChatModelBase | None'重排模型(类型 `'ChatModelBase | None'`)。Optional chat model that picks the final `top_k` chunks out of the `rerank_candidate_k` retrieved ones. Best-effort: when it fails, the vector order is kept.
源码Source agentscope/middleware/_rag.py:800–828

方法详情Method Detail

list_tools

async def list_tools() -> list[ToolBase]
async

列出当前连接可用的工具。

Expose the search tool in "agentic" mode.

返回Returns list[ToolBase]
返回ToolBase列表。`list[ToolBase]`:
源码Source agentscope/middleware/_rag.py:834–853

on_reply

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

拦截并扩展回复流程。

on_reasoning runs after the agent has potentially consumed the inputs and started its own reply, so it cannot recover them from agent.state.context reliably.

参数Parameters
名称Name类型Type说明Description
agent'Agent'智能体(类型 `'Agent'`)。The executing agent. Unused, but part of the middleware contract.
input_kwargsdict输入关键字参数(类型 `dict`)。Reply input kwargs (`inputs`, plus any extras); forwarded unchanged.
next_handlerCallable[..., AsyncGenerator]下一个处理器(类型 `Callable[..., AsyncGenerator]`)。The downstream middleware or core reply logic.
返回Returns AsyncGenerator
返回AsyncGenerator。`Any`:
源码Source agentscope/middleware/_rag.py:859–927

on_reasoning

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

拦截并扩展推理阶段。

Only active in "static" mode and only when agent.state.cur_iter == 0 — i.e.

参数Parameters
名称Name类型Type说明Description
agent'Agent'智能体(类型 `'Agent'`)。The executing agent whose `state.context` receives the hint.
input_kwargsdict输入关键字参数(类型 `dict`)。Reasoning input kwargs; forwarded unchanged.
next_handlerCallable[..., AsyncGenerator]下一个处理器(类型 `Callable[..., AsyncGenerator]`)。The downstream middleware or core reasoning logic.
返回Returns AsyncGenerator
返回AsyncGenerator。`Any`:
源码Source agentscope/middleware/_rag.py:929–1021

嵌套类详情Nested Class Detail

Parameters

class Parameters

参数,驱动智能体的推理、工具调用与会话执行。位于 agentscope.middleware 包。

The fields here are exactly the keys the hosted service persists into SessionKnowledgeConfig.parameters and the keys :class:RAGMiddleware accepts as search_config. Every field is annotated with a title and description so the front-end can render them as labels and tooltips via model_json_schema().

继承成员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.