♻️ 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:
Rumio
2026-07-14 16:48:33 +08:00
committed by GitHub
co-authored by webjoin111 pre-commit-ci[bot]
parent 922d092650
commit 52f7dbdedf
66 changed files with 2131 additions and 2353 deletions
+15 -19
View File
@@ -2,11 +2,13 @@
LLM 服务的高级 API 接口 - 便捷函数入口 (无状态)
"""
import json
from pathlib import Path
from typing import Any, Literal, TypeVar, overload
from pydantic import BaseModel
from zhenxun.services.ai.config import get_llm_config
from zhenxun.services.ai.core.exceptions import (
ControlFlowExit,
LLMException,
@@ -27,14 +29,18 @@ from zhenxun.services.ai.core.messages import (
RerankRequest,
RerankResult,
SpeechRequest,
UsageInfo,
)
from zhenxun.services.ai.core.models import ModelName
from zhenxun.services.ai.core.options import (
GenerationConfig,
LLMEmbeddingConfig,
OutputFormatConfig,
ResponseFormat,
StructuredOutputStrategy,
TTSConfig,
)
from zhenxun.services.ai.guardrails import GuardrailSource
from zhenxun.services.ai.guardrails import GuardrailSource, parse_guardrails
from zhenxun.services.ai.utils.logger import log_llm as logger
from .builder import IntentBuilder
@@ -109,7 +115,7 @@ async def embed(
@overload
async def embed(
input_batch: list[Any],
input_batch: list[PromptInput],
*,
model: ModelName = None,
task: Literal[
@@ -122,7 +128,7 @@ async def embed(
async def embed(
input_batch: PromptInput | list[Any],
input_batch: PromptInput | list[PromptInput],
*,
model: ModelName = None,
task: Literal[
@@ -158,8 +164,6 @@ async def embed(
)
if not batch.payloads:
from zhenxun.services.ai.core.messages import UsageInfo
return EmbeddingResponse(
embeddings=[], usage=UsageInfo(), model_name=str(model)
)
@@ -256,21 +260,13 @@ async def generate_structured(
T: 解析验证通过后的 Pydantic 模型实例。
""" # noqa: E501
try:
from zhenxun.services.ai.config import get_llm_config
from zhenxun.services.ai.core.engine.structured_parser import (
BaseOutputProcessor,
)
from zhenxun.services.ai.core.options import (
OutputFormatConfig,
ResponseFormat,
StructuredOutputStrategy,
)
if max_retries is None:
max_retries = get_llm_config().client_settings.structured_retries
from zhenxun.services.ai.guardrails import parse_guardrails
parsed_guardrails = parse_guardrails(guardrails)
output_processor = BaseOutputProcessor(
@@ -291,8 +287,6 @@ async def generate_structured(
if instruction:
prompt_parts.append(instruction)
import json
schema_str = json.dumps(json_schema, ensure_ascii=False, indent=2)
prompt_parts.append(
"### ⚠️ [结构化输出要求]\n"
@@ -393,7 +387,9 @@ async def generate(
llm_context = LLMContext(request=request)
combined_cap = CombinedCapability(sys_caps)
async def inner_handler(ctx: LLMContext[Any, Any]) -> ChatResponse:
async def inner_handler(
ctx: LLMContext[ChatRequest, ChatResponse],
) -> ChatResponse:
return await LLMOrchestrator.invoke(
ctx.request,
model_name=model,
@@ -420,7 +416,7 @@ async def generate(
@overload
async def create_image(
prompt: str | Any,
prompt: PromptInput,
*,
images: None = None,
model: ModelName = None,
@@ -432,7 +428,7 @@ async def create_image(
@overload
async def create_image(
prompt: str | Any,
prompt: PromptInput,
*,
images: list[Path | bytes | str] | Path | bytes | str,
model: ModelName = None,
@@ -443,7 +439,7 @@ async def create_image(
async def create_image(
prompt: str | Any,
prompt: PromptInput,
*,
images: list[Path | bytes | str] | Path | bytes | str | None = None,
model: ModelName = None,
+6 -11
View File
@@ -2,13 +2,18 @@
LLM 生成配置相关类和函数
"""
from typing import Any, Literal
import inspect
from typing import Any, Literal, cast
from typing_extensions import Self
from pydantic import BaseModel
from zhenxun.services.ai.config import get_gemini_safety_threshold
from zhenxun.services.ai.core.exceptions import ConfigurationException
from zhenxun.services.ai.core.options import (
GenerationConfig,
ResponseFormat,
StructuredOutputStrategy,
)
from zhenxun.services.ai.utils.logger import log_llm as logger
from zhenxun.utils.pydantic_compat import model_json_schema, model_validate
@@ -94,20 +99,12 @@ class IntentBuilder:
强制要求结构化输出意图。
支持自动处理 Pydantic 模型并转换为厂商所需的 JSON Schema。
"""
import inspect
from pydantic import BaseModel
from zhenxun.services.ai.core.options import StructuredOutputStrategy
self._config.output.response_format = ResponseFormat.JSON
self._config.output.response_mime_type = "application/json"
if schema:
if inspect.isclass(schema) and issubclass(schema, BaseModel):
self._config.output.response_schema = model_json_schema(schema)
else:
from typing import cast
self._config.output.response_schema = cast(dict[str, Any], schema)
if strict:
self._config.output.structured_output_strategy = (
@@ -137,8 +134,6 @@ class IntentBuilder:
安全合规意图。
level 取值: 'strict' (最严格), 'moderate' (中等), 'none' (完全无限制)。
"""
from zhenxun.services.ai.config import get_gemini_safety_threshold
if level == "strict":
self.gemini.set_safety_threshold("BLOCK_LOW_AND_ABOVE")
elif level == "none":
+1 -7
View File
@@ -17,6 +17,7 @@ from zhenxun.services.ai.utils.logger import log_llm as logger
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
from zhenxun.utils.pydantic_compat import model_dump
from .system.cache import clear_model_cache, get_or_create_model
from .system.capabilities import get_model_capabilities
from .system.network import health_manager
@@ -277,8 +278,6 @@ async def get_model_instance(
provider_config_found, model_detail_found = config_tuple_found
from .system.cache import get_or_create_model
return await get_or_create_model(
provider_config_found, model_detail_found, override_config
)
@@ -288,8 +287,6 @@ def clear_all_cache() -> None:
"""
清空模型实例缓存与路由组解析缓存。
"""
from .system.cache import clear_model_cache
clear_model_cache()
clear_resolved_group_cache()
logger.debug("已清空全局模型实例与路由组缓存")
@@ -300,11 +297,8 @@ async def _init_llm_config_on_startup():
"""启动时初始化 LLM 配置、密钥状态并预热工具提供者管理器。"""
logger.info("正在初始化 LLM 配置并加载遥测状态...")
try:
from zhenxun.services.ai.config import get_llm_config
from zhenxun.services.ai.tools.engine.registry import tool_provider_manager
from .system.network import health_manager
get_llm_config()
await health_manager.initialize()
await tool_provider_manager.initialize()