97 lines
3.5 KiB
Python
97 lines
3.5 KiB
Python
import json
|
|
import aiohttp
|
|
from typing import Dict, List, Optional, Tuple
|
|
from appPublic.jsonConfig import getConfig
|
|
|
|
class AIContractService:
|
|
def __init__(self):
|
|
self.config = getConfig()
|
|
|
|
async def get_ai_config(self, org_id: str) -> Optional[Dict]:
|
|
"""获取组织的AI配置"""
|
|
# 这里需要从数据库查询 contract_ai_config 表
|
|
# 为简化,先假设配置存在
|
|
dbp = await getDBP(org_id)
|
|
sql = "SELECT * FROM contract_ai_config WHERE org_id = %(org_id)s"
|
|
result = await dbp.doQuery(sql, {'org_id': org_id})
|
|
return result[0] if result else None
|
|
|
|
async def _make_ai_request(self, url: str, api_key: str, payload: Dict) -> Dict:
|
|
"""发送AI请求"""
|
|
headers = {
|
|
'Authorization': f'Bearer {api_key}',
|
|
'Content-Type': 'application/json'
|
|
}
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
async with session.post(url, headers=headers, json=payload) as response:
|
|
if response.status == 200:
|
|
return await response.json()
|
|
else:
|
|
raise Exception(f"AI service error: {response.status} - {await response.text()}")
|
|
|
|
async def ai_version_compare(self, file1_content: str, file2_content: str,
|
|
org_id: str) -> Dict:
|
|
"""AI版本差异化对比"""
|
|
ai_config = await self.get_ai_config(org_id)
|
|
if not ai_config:
|
|
raise Exception("AI configuration not found for organization")
|
|
|
|
payload = {
|
|
"task": "version_compare",
|
|
"file1_content": file1_content,
|
|
"file2_content": file2_content
|
|
}
|
|
|
|
return await self._make_ai_request(
|
|
ai_config['ai_service_url'],
|
|
ai_config['api_key'],
|
|
payload
|
|
)
|
|
|
|
async def ai_compliance_check(self, contract_content: str, org_id: str) -> Dict:
|
|
"""AI合同合规检查"""
|
|
ai_config = await self.get_ai_config(org_id)
|
|
if not ai_config:
|
|
raise Exception("AI configuration not found for organization")
|
|
|
|
payload = {
|
|
"task": "compliance_check",
|
|
"contract_content": contract_content
|
|
}
|
|
|
|
return await self._make_ai_request(
|
|
ai_config['ai_service_url'],
|
|
ai_config['api_key'],
|
|
payload
|
|
)
|
|
|
|
async def ai_extract_key_dates(self, contract_content: str, org_id: str) -> Dict:
|
|
"""AI合同关键时点提取"""
|
|
ai_config = await self.get_ai_config(org_id)
|
|
if not ai_config:
|
|
raise Exception("AI configuration not found for organization")
|
|
|
|
payload = {
|
|
"task": "extract_key_dates",
|
|
"contract_content": contract_content
|
|
}
|
|
|
|
return await self._make_ai_request(
|
|
ai_config['ai_service_url'],
|
|
ai_config['api_key'],
|
|
payload
|
|
)
|
|
|
|
# 全局实例
|
|
ai_service = AIContractService()
|
|
|
|
# 导出函数
|
|
async def ai_version_compare(file1_content: str, file2_content: str, org_id: str) -> Dict:
|
|
return await ai_service.ai_version_compare(file1_content, file2_content, org_id)
|
|
|
|
async def ai_compliance_check(contract_content: str, org_id: str) -> Dict:
|
|
return await ai_service.ai_compliance_check(contract_content, org_id)
|
|
|
|
async def ai_extract_key_dates(contract_content: str, org_id: str) -> Dict:
|
|
return await ai_service.ai_extract_key_dates(contract_content, org_id) |