mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-09 22:00:01 +08:00
♻️ refactor(core): 重构 AI 编排框架与记忆及 RAG 子系统 (#2149)
* ♻️ refactor(core): 重构 AI 编排框架与记忆及 RAG 子系统 - 【重构】重构 `BaseRunnable` 并引入统一的 `RunIntent` 意图载体,规范 Agent、Team 和 Workflow 的执行流 - 【解耦】将中期记忆槽和长期向量记忆从 `MemoryConfig` 中解耦,转为独立的能力组件与工具箱进行管理 - 【记忆】移除 `MemoryReader` 和 `MemoryWriter`,统一封装为 `SessionMemoryContext` 会话记忆门面 - 【RAG】重构检索器与存储后端接口,统一采用 `QueryRequest` 进行多维度联合检索,并引入 `InMemoryScorer` 提升打分性能 - 【事件】优化 `EventBus` 异步事件分发机制,引入队列机制确保事件按序处理,避免并发竞态问题 - 【依赖注入】移除 `memory` 注入项,优化 `DependencyInjector` 的签名解析缓存以提升性能 * 🚨 auto fix by pre-commit hooks --------- Co-authored-by: webjoin111 <455457521@qq.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
webjoin111
pre-commit-ci[bot]
parent
922d092650
commit
52f7dbdedf
@@ -95,7 +95,9 @@ class DBMessageSerializer:
|
||||
return content_parts
|
||||
|
||||
@staticmethod
|
||||
def serialize_content(content_payload: Any) -> list[dict[str, Any]]:
|
||||
def serialize_content(
|
||||
content_payload: list[LLMContentPart] | str,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""将 LLMMessage 消息内容序列化为可存储于数据库的 JSON 格式"""
|
||||
if isinstance(content_payload, str):
|
||||
return [{"type": "text", "text": content_payload}]
|
||||
@@ -134,7 +136,7 @@ class MemoryScope:
|
||||
):
|
||||
"""初始化长期记忆作用域与 RAG 客户端"""
|
||||
self.rag_client = rag_client
|
||||
self._background_tasks: set[Any] = set()
|
||||
self._background_tasks: set[asyncio.Task[Any]] = set()
|
||||
|
||||
async def remember(
|
||||
self,
|
||||
@@ -492,3 +494,13 @@ def get_orm_slot_context(model_class: type[AbstractSlotRecord]) -> TortoiseSlotC
|
||||
[工厂方法] 供第三方开发者调用,将 Tortoise ORM 表直接包装为记忆槽存储系统。
|
||||
"""
|
||||
return TortoiseSlotContext(model_class=model_class)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"AbstractMemoryRecord",
|
||||
"AbstractSlotRecord",
|
||||
"InMemoryChatContext",
|
||||
"MemoryScope",
|
||||
"TortoiseChatContext",
|
||||
"TortoiseSlotContext",
|
||||
]
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from collections.abc import Sequence
|
||||
from typing import Protocol, runtime_checkable
|
||||
|
||||
from zhenxun.services.ai.context.memory.types import (
|
||||
MemorySlot,
|
||||
@@ -10,7 +11,14 @@ from zhenxun.services.ai.run.context import RunContext
|
||||
from zhenxun.services.ai.utils.scope import ScopeSelector
|
||||
|
||||
|
||||
class BaseChatContext(ABC):
|
||||
@runtime_checkable
|
||||
class IClearableBackend(Protocol):
|
||||
"""支持声明式清理的作用域后端协议"""
|
||||
|
||||
async def clear_by_query(self, query: ScopeSelector) -> int | None: ...
|
||||
|
||||
|
||||
class BaseChatContext(IClearableBackend, ABC):
|
||||
"""短期对话历史记忆接口"""
|
||||
|
||||
@abstractmethod
|
||||
@@ -44,13 +52,8 @@ class BaseChatContext(ABC):
|
||||
"""清空当前会话的历史消息。"""
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def clear_by_query(self, query: ScopeSelector) -> None:
|
||||
"""根据条件领域查询对象清理对话历史。"""
|
||||
...
|
||||
|
||||
|
||||
class BaseSlotContext(ABC):
|
||||
class BaseSlotContext(IClearableBackend, ABC):
|
||||
"""中期记忆槽持久化接口"""
|
||||
|
||||
@abstractmethod
|
||||
@@ -80,11 +83,6 @@ class BaseSlotContext(ABC):
|
||||
"""列出当前会话的所有记忆槽(包括未置顶的)。"""
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def clear_by_query(self, query: ScopeSelector) -> None:
|
||||
"""根据条件领域查询对象清理记忆槽。"""
|
||||
...
|
||||
|
||||
|
||||
class BaseMemoryReducer(ABC):
|
||||
"""记忆压缩器基类"""
|
||||
@@ -97,7 +95,19 @@ class BaseMemoryReducer(ABC):
|
||||
model_name: str,
|
||||
base_overhead: int = 0,
|
||||
) -> tuple[list[LLMMessage], bool, int]:
|
||||
"""对消息列表进行压缩处理。"""
|
||||
"""
|
||||
执行记忆压缩处理,精简或提炼对话上下文以降低 Token 消耗。
|
||||
|
||||
参数:
|
||||
messages: 需要进行压缩的原始 LLM 消息历史列表。
|
||||
current_tokens: 压缩前消息列表的当前 Token 总数。
|
||||
model_name: 用于判定压缩阈值或计算 Token 的底层大模型名称。
|
||||
base_overhead: 基础系统提示词等静态开销的 Token 计数。
|
||||
|
||||
返回:
|
||||
tuple[list[LLMMessage], bool, int]: 包含压缩后的新消息历史列表、
|
||||
本次是否实际触发了压缩的布尔标记、以及压缩后的新 Token 总数。
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user