feat: update cms case api
This commit is contained in:
@@ -72,6 +72,54 @@ def parse_pdf(files, case_type: str, user) -> dict:
|
||||
}
|
||||
|
||||
|
||||
def generate_from_prompt(prompt: str, case_type: str, user) -> dict:
|
||||
"""CMS-CASE-AI-1: 一段长 prompt → DeepSeek 按病例模板生成结构化病例内容。
|
||||
|
||||
产出与 parse_pdf 同构(不落库、不含评分规则),缓存 parse_id 供后续 full-create 复用。
|
||||
"""
|
||||
if case_type not in ('traditional', 'teaching'):
|
||||
raise AppError('CASE_TYPE_NOT_SUPPORTED', f'case_type 不支持: {case_type}', status_code=400)
|
||||
if not (prompt or '').strip():
|
||||
raise AppError('CASE_VALIDATION_ERROR', 'prompt 必填', status_code=400)
|
||||
|
||||
t0 = time.time()
|
||||
|
||||
prompt_name = f'case_{case_type}_full'
|
||||
system_prompt, prompt_version = load_prompt(prompt_name)
|
||||
|
||||
result = deepseek_client.call_deepseek(system_prompt, prompt)
|
||||
data = result['data']
|
||||
|
||||
data.pop('scoring_rules', None)
|
||||
data.pop('stages', None)
|
||||
|
||||
data['case_type'] = case_type
|
||||
if 'exam_items' in data:
|
||||
data['exam_items'] = normalize_exam_items(data.get('exam_items') or [])
|
||||
else:
|
||||
data['exam_items'] = []
|
||||
|
||||
_strip_unknown_fields(data)
|
||||
_validate_schema(data)
|
||||
|
||||
parse_id = uuid.uuid4().hex[:12]
|
||||
cache.set(f'parse_result:{parse_id}', json.dumps(data, ensure_ascii=False), PARSE_RESULT_TTL)
|
||||
|
||||
audit.info(
|
||||
'CASE_AI_GENERATE user=%s parse_id=%s tokens=%s prompt_version=%s',
|
||||
user.id, parse_id, result.get('usage', {}), prompt_version,
|
||||
)
|
||||
|
||||
return {
|
||||
'parse_id': parse_id,
|
||||
'case_type': case_type,
|
||||
'ai_usage': result.get('usage', {}),
|
||||
'prompt_version': prompt_version,
|
||||
'generating_seconds': round(time.time() - t0, 1),
|
||||
'data': data,
|
||||
}
|
||||
|
||||
|
||||
_SCHEMA_ALLOWED_KEYS = {
|
||||
'title', 'case_type', 'difficulty', 'chief_complaint', 'description',
|
||||
'patient_age', 'patient_gender', 'tags', 'symptom_tags', 'disease_tags',
|
||||
|
||||
Reference in New Issue
Block a user