63 lines
2.1 KiB
Python
63 lines
2.1 KiB
Python
"""Default application wiring for the客服 Agent runtime."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from config.database.milvus import client as milvus_client
|
|
from config.database.mysql import get_session_factory
|
|
from config.database.redis import client as redis_client
|
|
from service.customer_agent.config import DatabaseConfigProvider
|
|
from service.customer_agent.runtime import build_anonymous_runtime
|
|
from service.knowledge_base.upload import KnowledgeUploadService
|
|
from rag.embedding import embed_texts
|
|
from rag.milvus_collections import KNOWLEDGE_COLLECTIONS
|
|
from rag.milvus_delete import _escape_filter_value
|
|
from tool.llm import llm as llm_client
|
|
|
|
|
|
def build_default_runtime():
|
|
provider = DatabaseConfigProvider(session_factory=get_session_factory())
|
|
return build_anonymous_runtime(
|
|
redis=redis_client(),
|
|
milvus_client=milvus_client(),
|
|
llm_client=llm_client,
|
|
config_getter=provider.get,
|
|
audit_writer=provider.write_audit,
|
|
)
|
|
|
|
|
|
async def document_exists_in_milvus(milvus, doc_id: str) -> bool:
|
|
"""Check duplicate document IDs across all project knowledge collections."""
|
|
escaped_doc_id = _escape_filter_value(doc_id)
|
|
for collection_name in KNOWLEDGE_COLLECTIONS:
|
|
rows = await milvus.query(
|
|
collection_name=collection_name,
|
|
filter=f'doc_id == "{escaped_doc_id}"',
|
|
output_fields=["doc_id"],
|
|
limit=1,
|
|
)
|
|
if rows:
|
|
return True
|
|
return False
|
|
|
|
|
|
def build_default_knowledge_upload_service():
|
|
provider = DatabaseConfigProvider(session_factory=get_session_factory())
|
|
redis = redis_client()
|
|
milvus = milvus_client()
|
|
|
|
async def publish_event(event_name, payload):
|
|
await redis.publish(
|
|
event_name,
|
|
json.dumps(payload, ensure_ascii=False),
|
|
)
|
|
|
|
return KnowledgeUploadService(
|
|
storage_dir=Path("data/files"),
|
|
milvus_client=milvus,
|
|
embedder=lambda texts: embed_texts(texts, client=llm_client),
|
|
publisher=publish_event,
|
|
document_exists=lambda doc_id: document_exists_in_milvus(milvus, doc_id),
|
|
)
|