PMFlow/backend/app/services/adaptive_rag_service/shared_imports.py

81 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""全自适应 RAG 服务。
能力:
1. 意图识别(question / statement / mixed)
2. 多域路由(结构化 DB + 向量检索)
3. 回答生成与来源引用
4. QA 记录、反馈、JSONL 导出
5. 知识库文档/URL/飞书消息入库向量化
"""
from __future__ import annotations
import ast
import asyncio
from difflib import SequenceMatcher
import hashlib
import json
import math
import os
from pathlib import Path
import re
import time
from urllib.parse import urlparse
from dataclasses import dataclass
from datetime import datetime, timezone
from typing import Any, Optional
import httpx
from sqlalchemy import and_, bindparam, delete, func, or_, select, text
from sqlalchemy.ext.asyncio import AsyncSession
from ...config.ai_prompts import (
PROMPT_AI_PMO_QA,
PROMPT_AI_PMO_REQUIREMENT_PLAN_QA,
PROMPT_DECISION_QA,
PROMPT_EXTERNAL_TREND_QA,
PROMPT_INDUSTRY_TREND_QA,
PROMPT_FEISHU_DIRECT_QA,
PROMPT_FEISHU_MIXED_QA,
PROMPT_MEMBER_WORKLOAD_QA,
PROMPT_RAG_ANSWER,
PROMPT_RAG_DB_PROJECT_QA,
PROMPT_RAG_FOLLOWUP_ANCHOR_SEMANTICS,
PROMPT_RAG_QUERY_SEMANTICS,
PROMPT_RAG_ANSWER_SCENE_SEMANTICS,
PROMPT_INTERNAL_TARGET_RESOLUTION,
)
from ...core.database import AsyncSessionLocal
from ...models import (
KnowledgeBaseChunk,
KnowledgeBaseDoc,
KGEvidence,
KGEntity,
KGRelation,
AICallLog,
FeishuReplyOutbox,
IngestEvent,
Project,
ProjectSignalLink,
QAFeedback,
QARecord,
QueryRouteMemory,
QASession,
QASessionState,
SemanticFact,
Signal,
Task,
TruthNode,
User,
UserProject,
UserWorkload,
)
from ..context_cache_service import context_cache_service
from ..llm_service import LLMService
from ..signal.signal_source_orchestration import SignalSourceOrchestrationService
from ..signal.symbol_index import SymbolIndexService
from ...utils.timezone import CN_TZ, now_cn_aware, now_cn_naive
from ...utils.thread_context import build_feishu_conversation_scope_key, build_feishu_thread_key