first commit
This commit is contained in:
0
services/compliance-backend/app/core/__init__.py
Normal file
0
services/compliance-backend/app/core/__init__.py
Normal file
37
services/compliance-backend/app/core/config.py
Normal file
37
services/compliance-backend/app/core/config.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(env_file=".env", extra="ignore")
|
||||
|
||||
# 应用
|
||||
app_env: str = "development"
|
||||
log_level: str = "INFO"
|
||||
api_secret_key: str = "change_this_key"
|
||||
|
||||
# 数据库
|
||||
database_url: str = "postgresql+asyncpg://compliance:compliance123@postgres:5432/compliance_db"
|
||||
redis_url: str = "redis://:redis123@redis:6379/0"
|
||||
|
||||
# Milvus
|
||||
milvus_host: str = "milvus"
|
||||
milvus_port: int = 19530
|
||||
|
||||
# Neo4j
|
||||
neo4j_uri: str = "bolt://neo4j:7687"
|
||||
neo4j_user: str = "neo4j"
|
||||
neo4j_password: str = "neo4j123"
|
||||
|
||||
# AI 服务
|
||||
embedding_service_url: str = "http://embedding-service:8010"
|
||||
mcp_server_url: str = "http://mcp-server:8011"
|
||||
|
||||
# LLM
|
||||
llm_provider: str = "deepseek" # deepseek / qwen
|
||||
deepseek_api_key: str = ""
|
||||
deepseek_model: str = "deepseek-chat"
|
||||
dashscope_api_key: str = ""
|
||||
qwen_model: str = "qwen-plus"
|
||||
|
||||
|
||||
settings = Settings()
|
||||
54
services/compliance-backend/app/core/deps.py
Normal file
54
services/compliance-backend/app/core/deps.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from functools import lru_cache
|
||||
from typing import AsyncGenerator
|
||||
|
||||
import httpx
|
||||
from neo4j import AsyncGraphDatabase
|
||||
from pymilvus import connections, Collection
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
|
||||
|
||||
from .config import settings
|
||||
|
||||
# ── PostgreSQL ──────────────────────────────────
|
||||
engine = create_async_engine(settings.database_url, pool_size=10, max_overflow=20)
|
||||
AsyncSessionLocal = async_sessionmaker(engine, expire_on_commit=False)
|
||||
|
||||
|
||||
async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
||||
async with AsyncSessionLocal() as session:
|
||||
try:
|
||||
yield session
|
||||
await session.commit()
|
||||
except Exception:
|
||||
await session.rollback()
|
||||
raise
|
||||
|
||||
|
||||
# ── Milvus ──────────────────────────────────────
|
||||
def get_milvus_collection(name: str) -> Collection:
|
||||
connections.connect(host=settings.milvus_host, port=settings.milvus_port)
|
||||
return Collection(name)
|
||||
|
||||
|
||||
# ── Neo4j ───────────────────────────────────────
|
||||
_neo4j_driver = None
|
||||
|
||||
|
||||
def get_neo4j():
|
||||
global _neo4j_driver
|
||||
if _neo4j_driver is None:
|
||||
_neo4j_driver = AsyncGraphDatabase.driver(
|
||||
settings.neo4j_uri,
|
||||
auth=(settings.neo4j_user, settings.neo4j_password),
|
||||
)
|
||||
return _neo4j_driver
|
||||
|
||||
|
||||
# ── HTTP 客户端(复用连接池)────────────────────
|
||||
_http_client = None
|
||||
|
||||
|
||||
def get_http_client() -> httpx.AsyncClient:
|
||||
global _http_client
|
||||
if _http_client is None:
|
||||
_http_client = httpx.AsyncClient(timeout=120.0)
|
||||
return _http_client
|
||||
56
services/compliance-backend/app/core/llm.py
Normal file
56
services/compliance-backend/app/core/llm.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from langchain_openai import ChatOpenAI
|
||||
from tenacity import retry, stop_after_attempt, wait_exponential
|
||||
from .config import settings
|
||||
|
||||
|
||||
def get_llm(temperature: float = 0.1) -> ChatOpenAI:
|
||||
"""获取 LLM 客户端(DeepSeek 或 Qwen,均兼容 OpenAI API)"""
|
||||
if settings.llm_provider == "deepseek":
|
||||
return ChatOpenAI(
|
||||
model=settings.deepseek_model,
|
||||
api_key=settings.deepseek_api_key,
|
||||
base_url="https://api.deepseek.com/v1",
|
||||
temperature=temperature,
|
||||
max_retries=3,
|
||||
timeout=120,
|
||||
)
|
||||
elif settings.llm_provider == "qwen":
|
||||
return ChatOpenAI(
|
||||
model=settings.qwen_model,
|
||||
api_key=settings.dashscope_api_key,
|
||||
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||||
temperature=temperature,
|
||||
max_retries=3,
|
||||
timeout=120,
|
||||
)
|
||||
raise ValueError(f"不支持的 LLM 提供商:{settings.llm_provider}")
|
||||
|
||||
|
||||
RAG_SYSTEM_PROMPT = """你是一位专业的汽车行业合规专家,具备深厚的法规知识(GB标准、UN-ECE、ISO 45001、IATF 16949等)。
|
||||
|
||||
回答规则:
|
||||
1. 仅基于提供的参考文献回答,不添加不在文献中的信息
|
||||
2. 每个关键陈述必须标注来源(格式:[来源:文件名,第X页])
|
||||
3. 如果参考文献不足以回答问题,明确说明
|
||||
4. 使用专业但清晰的语言,适合工程师和法务人员阅读
|
||||
5. 对于数值要求(如绝缘电阻值、时间限制等),精确引用原文"""
|
||||
|
||||
|
||||
COMPLIANCE_CHECK_PROMPT = """你是一位专业的汽车合规审查专家。
|
||||
|
||||
请对以下内容进行合规性评估:
|
||||
|
||||
【待审查内容】
|
||||
{content}
|
||||
|
||||
【相关法规要求】
|
||||
{regulations}
|
||||
|
||||
请按以下格式输出:
|
||||
1. 整体风险等级:[low/medium/high/critical]
|
||||
2. 风险分数:[0-100]
|
||||
3. 发现的合规问题(逐条列出):
|
||||
- 问题描述
|
||||
- 违反的具体法规条款
|
||||
- 严重程度
|
||||
4. 整改建议(具体可操作)"""
|
||||
Reference in New Issue
Block a user