agentscope.middlewareRAGMiddleware
class RAGMiddleware(MiddlewareBase)
agentscope/middleware/_rag.py:626–1021RAG 中间件,在智能体执行流程中提供横切扩展。位于 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.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
knowledge_bases | list['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. |
agentscope/middleware/_rag.py:800–828方法详情Method Detail
list_tools
async def list_tools() -> list[ToolBase]
列出当前连接可用的工具。
Expose the search tool in "agentic" mode.
agentscope/middleware/_rag.py:834–853on_reply
async def on_reply(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGenerator
拦截并扩展回复流程。
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.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | 'Agent' | 智能体(类型 `'Agent'`)。The executing agent. Unused, but part of the middleware contract. |
input_kwargs | dict | 输入关键字参数(类型 `dict`)。Reply input kwargs (`inputs`, plus any extras); forwarded unchanged. |
next_handler | Callable[..., AsyncGenerator] | 下一个处理器(类型 `Callable[..., AsyncGenerator]`)。The downstream middleware or core reply logic. |
agentscope/middleware/_rag.py:859–927on_reasoning
async def on_reasoning(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGenerator
拦截并扩展推理阶段。
Only active in "static" mode and only when agent.state.cur_iter == 0 — i.e.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | 'Agent' | 智能体(类型 `'Agent'`)。The executing agent whose `state.context` receives the hint. |
input_kwargs | dict | 输入关键字参数(类型 `dict`)。Reasoning input kwargs; forwarded unchanged. |
next_handler | Callable[..., AsyncGenerator] | 下一个处理器(类型 `Callable[..., AsyncGenerator]`)。The downstream middleware or core reasoning logic. |
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().