feat(adapters): EmbeddingProvider Protocol + local/remote 双后端实现
- app/ports.py: 添加 EmbeddingProvider Protocol(runtime_checkable,dim 属性 + embed 方法) - adapters/embedding.py: 从参考代码迁移,拆分为 LocalEmbeddingProvider 和 RemoteEmbeddingProvider - Local: sentence-transformers 冻结推理,维度校验 - Remote: OpenAI 兼容 API,L2 归一化,按 index 排序 - 两者均提供 embed() 和 embed_tensor() 统一接口 - tests/unit/test_embedding_adapter.py: Protocol 满足性、形状校验、导入测试 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1 +1,31 @@
|
||||
"""应用层 Protocol 端口定义。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING, Protocol, runtime_checkable
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import numpy as np
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class EmbeddingProvider(Protocol):
|
||||
"""文本嵌入端口。
|
||||
|
||||
属性:
|
||||
dim: 嵌入维度 D。
|
||||
"""
|
||||
|
||||
@property
|
||||
def dim(self) -> int: ...
|
||||
|
||||
def embed(self, texts: str | list[str]) -> np.ndarray:
|
||||
"""文本 → 嵌入向量(L2 归一化)。
|
||||
|
||||
参数:
|
||||
texts: 单条文本或文本列表。
|
||||
|
||||
返回:
|
||||
[N, D] ndarray,每行 L2 范数为 1.0。
|
||||
"""
|
||||
...
|
||||
|
||||
Reference in New Issue
Block a user