feat: 用 Ragflow 搭建鲜境方案客服
咨询台接 Ragflow 检索与示例知识库,支持追问、判定、引用和留资。 Co-authored-by: haijd <haijd@163.com>
This commit is contained in:
+37
-51
@@ -2,93 +2,79 @@ from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
|
||||
from .config_loader import business_config
|
||||
from .ragflow_client import RagflowClient, RagflowError
|
||||
from .settings import settings
|
||||
|
||||
|
||||
class RetrievalResult:
|
||||
def __init__(self, chunks: list[dict[str, Any]]):
|
||||
def __init__(self, chunks: list[dict[str, Any]], backend: str, warning: str = ""):
|
||||
self.chunks = chunks
|
||||
self.backend = backend
|
||||
self.warning = warning
|
||||
|
||||
|
||||
def retrieve_for_scheme(slots: dict[str, str], bundle: list[str]) -> RetrievalResult:
|
||||
backend = (settings.retrieval_backend or "mock").lower()
|
||||
if backend == "ragflow":
|
||||
backend = (settings.retrieval_backend or "ragflow").lower()
|
||||
if backend == "mock":
|
||||
return _retrieve_mock(slots, bundle)
|
||||
try:
|
||||
return _retrieve_ragflow(slots, bundle)
|
||||
return _retrieve_mock(slots, bundle)
|
||||
except RagflowError as exc:
|
||||
return RetrievalResult([], "ragflow", warning=str(exc))
|
||||
|
||||
|
||||
def _retrieve_mock(slots: dict[str, str], bundle: list[str]) -> RetrievalResult:
|
||||
chunks = business_config.chunks
|
||||
picked: list[dict[str, Any]] = []
|
||||
bundle_set = set(bundle)
|
||||
for c in chunks:
|
||||
if c.get("product") in bundle_set:
|
||||
picked.append(c)
|
||||
# tag overlap with slot values
|
||||
for chunk in chunks:
|
||||
if chunk.get("product") in bundle_set:
|
||||
picked.append(chunk)
|
||||
blob = " ".join(slots.values())
|
||||
for c in chunks:
|
||||
if c in picked:
|
||||
for chunk in chunks:
|
||||
if chunk in picked:
|
||||
continue
|
||||
tags = c.get("tags") or []
|
||||
if any(str(t) in blob for t in tags):
|
||||
picked.append(c)
|
||||
tags = chunk.get("tags") or []
|
||||
if any(str(tag) in blob for tag in tags):
|
||||
picked.append(chunk)
|
||||
if len(picked) >= 4:
|
||||
break
|
||||
if not picked:
|
||||
picked = chunks[:2]
|
||||
return RetrievalResult(picked[:4])
|
||||
return RetrievalResult(picked[:4], "mock")
|
||||
|
||||
|
||||
def _retrieve_ragflow(slots: dict[str, str], bundle: list[str]) -> RetrievalResult:
|
||||
"""Minimal Ragflow retrieval hook. Falls back to mock if not configured/unavailable."""
|
||||
if not settings.ragflow_api_key:
|
||||
return _retrieve_mock(slots, bundle)
|
||||
|
||||
client = RagflowClient()
|
||||
query = " ".join(
|
||||
[
|
||||
slots.get("tech_category", ""),
|
||||
slots.get("scenario", ""),
|
||||
slots.get("key_metric", ""),
|
||||
slots.get("constraints", ""),
|
||||
" ".join(bundle),
|
||||
"产品组合 适用场景 注意事项",
|
||||
]
|
||||
).strip() or "产品方案"
|
||||
dataset_ids = [x for x in settings.ragflow_dataset_ids.split(",") if x.strip()]
|
||||
url = settings.ragflow_base_url.rstrip("/") + "/api/v1/retrieval"
|
||||
headers = {"Authorization": f"Bearer {settings.ragflow_api_key}"}
|
||||
payload: dict[str, Any] = {"question": query, "top_k": 4}
|
||||
if dataset_ids:
|
||||
payload["dataset_ids"] = dataset_ids
|
||||
|
||||
try:
|
||||
with httpx.Client(timeout=20.0) as client:
|
||||
resp = client.post(url, headers=headers, json=payload)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
except Exception:
|
||||
return _retrieve_mock(slots, bundle)
|
||||
|
||||
# Ragflow response shapes vary by version; normalize best-effort
|
||||
records = []
|
||||
if isinstance(data, dict):
|
||||
records = data.get("data") or data.get("chunks") or data.get("records") or []
|
||||
if isinstance(records, dict):
|
||||
records = records.get("chunks") or records.get("records") or []
|
||||
).strip()
|
||||
dataset_ids = [item.strip() for item in settings.ragflow_dataset_ids.split(",") if item.strip()]
|
||||
raw = client.retrieve(query, dataset_ids, page_size=4)
|
||||
normalized = []
|
||||
for i, r in enumerate(records[:4]):
|
||||
if not isinstance(r, dict):
|
||||
for index, item in enumerate(raw[:4]):
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
normalized.append(
|
||||
{
|
||||
"id": r.get("id") or f"rf-{i}",
|
||||
"title": r.get("document_keyword") or r.get("title") or "Ragflow片段",
|
||||
"product": r.get("document_id") or "",
|
||||
"content": r.get("content") or r.get("text") or "",
|
||||
"id": item.get("id") or f"rf-{index}",
|
||||
"title": item.get("document_keyword")
|
||||
or item.get("document_name")
|
||||
or item.get("docnm_kwd")
|
||||
or "知识片段",
|
||||
"product": item.get("document_id") or "",
|
||||
"content": item.get("content") or item.get("content_with_weight") or "",
|
||||
"tags": [],
|
||||
"score": item.get("similarity"),
|
||||
}
|
||||
)
|
||||
if not normalized:
|
||||
return _retrieve_mock(slots, bundle)
|
||||
return RetrievalResult(normalized)
|
||||
warning = "" if normalized else "知识库没有召回到可用片段,方案仅供草稿,建议转人工。"
|
||||
return RetrievalResult(normalized, "ragflow", warning=warning)
|
||||
|
||||
Reference in New Issue
Block a user