merge: integrate ZSY customer service and profile capabilities
This commit is contained in:
@@ -113,16 +113,25 @@ class KnowledgeRetrievalService:
|
||||
def __init__(
|
||||
self,
|
||||
client: Any,
|
||||
*,
|
||||
*legacy_args: Any,
|
||||
embedder: Any = None,
|
||||
session_factory: Callable[[], Any] | None = None,
|
||||
config: KnowledgeRuntimeConfig | None = None,
|
||||
vector_dim: int = VECTOR_DIM,
|
||||
) -> None:
|
||||
self.client = client
|
||||
self.embedder = embedder
|
||||
# 兼容早期客服工具的 positional 构造:embedder, vector_store, config, authority。
|
||||
self._legacy = len(legacy_args) == 3
|
||||
if self._legacy:
|
||||
self.embedder = client
|
||||
self.client = legacy_args[0]
|
||||
self.config = legacy_args[1]
|
||||
self._authority = legacy_args[2]
|
||||
else:
|
||||
self.client = client
|
||||
self.embedder = embedder
|
||||
self.config = config or KnowledgeRuntimeConfig()
|
||||
self._authority = None
|
||||
self._session_factory: Callable[[], Any] = session_factory or SessionFactory
|
||||
self.config = config or KnowledgeRuntimeConfig()
|
||||
self.vector_dim = int(vector_dim)
|
||||
|
||||
# --- 入口 -----------------------------------------------------------------
|
||||
@@ -130,11 +139,14 @@ class KnowledgeRetrievalService:
|
||||
async def search(
|
||||
self,
|
||||
query: KnowledgeQuery,
|
||||
legacy_context: Any = None,
|
||||
*,
|
||||
embedding_endpoints: Sequence[Any] | None = None,
|
||||
embedder: Any = None,
|
||||
) -> KnowledgeSearchResult:
|
||||
"""执行检索。集合由意图映射,调用方无法指定集合名。"""
|
||||
if self._legacy:
|
||||
return await self._legacy_search(query)
|
||||
targets = self._assert_collections_allowed(query.intents)
|
||||
top_k = min(int(query.top_k), self.config.result_limit)
|
||||
searched = tuple(sorted({collection for collection, _ in targets}))
|
||||
@@ -150,6 +162,49 @@ class KnowledgeRetrievalService:
|
||||
hits = self._to_hits(verified)
|
||||
return KnowledgeSearchResult(hits=hits, degraded=False, searched_collections=searched)
|
||||
|
||||
async def _legacy_search(self, query: KnowledgeQuery) -> KnowledgeSearchResult:
|
||||
"""兼容旧工具调用,仍复用同一意图路由和 MySQL 权威回查。"""
|
||||
targets = self._assert_collections_allowed(query.intents)
|
||||
collections = tuple(sorted({collection for collection, _ in targets}))
|
||||
top_k = min(int(query.top_k), int(getattr(self.config, "result_limit", 20)))
|
||||
try:
|
||||
raw_vector = await self.embedder.embed(query.query)
|
||||
vector = self._vector_of(raw_vector)
|
||||
self.assert_vector_dim(vector)
|
||||
rows: list[dict[str, Any]] = []
|
||||
for collection, route_top_k in targets:
|
||||
rows.extend(await self.client.search(
|
||||
collection, vector, min(top_k, route_top_k)
|
||||
))
|
||||
if self._authority is not None:
|
||||
verified = await self._authority.filter_published(tuple(
|
||||
KnowledgeHit(
|
||||
knowledge_id=str(row.get("knowledge_id")),
|
||||
collection=collection,
|
||||
title=row.get("title"),
|
||||
snippet=str(row.get("snippet") or ""),
|
||||
score=self._score(row),
|
||||
)
|
||||
for row in rows
|
||||
for collection, _ in targets
|
||||
if str(row.get("collection") or collection) == collection
|
||||
))
|
||||
return KnowledgeSearchResult(
|
||||
hits=tuple(verified), searched_collections=collections
|
||||
)
|
||||
return KnowledgeSearchResult(hits=self._to_hits(rows), searched_collections=collections)
|
||||
except Exception:
|
||||
if self._authority is None:
|
||||
return KnowledgeSearchResult(
|
||||
hits=(), degraded=True, degradation_reason="milvus_unavailable",
|
||||
searched_collections=collections,
|
||||
)
|
||||
fallback = await self._authority.search_keyword(query, collections, top_k)
|
||||
return KnowledgeSearchResult(
|
||||
hits=tuple(fallback), degraded=True,
|
||||
degradation_reason="milvus_unavailable", searched_collections=collections,
|
||||
)
|
||||
|
||||
# --- 路由与白名单 ---------------------------------------------------------
|
||||
|
||||
def _assert_collections_allowed(
|
||||
@@ -202,6 +257,8 @@ class KnowledgeRetrievalService:
|
||||
|
||||
@staticmethod
|
||||
def _vector_of(execution: Any) -> list[float]:
|
||||
if isinstance(execution, list | tuple):
|
||||
return [float(item) for item in execution]
|
||||
raw = getattr(execution, "vector", None)
|
||||
if raw is None and isinstance(execution, Mapping):
|
||||
raw = execution.get("vector")
|
||||
|
||||
Reference in New Issue
Block a user