From de5ff38c60f6c65de554787dc2e24d301fdee225 Mon Sep 17 00:00:00 2001 From: awen Date: Thu, 3 Apr 2025 14:25:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=AD=97=E6=AE=B5=E4=B8=8E?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E5=AF=B9=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rules/new/ExtractionSettings.tsx | 16 +- app/routes/rules.new.tsx | 427 +++++++++++++++++- 2 files changed, 428 insertions(+), 15 deletions(-) diff --git a/app/components/rules/new/ExtractionSettings.tsx b/app/components/rules/new/ExtractionSettings.tsx index aee74af..5c93ac4 100644 --- a/app/components/rules/new/ExtractionSettings.tsx +++ b/app/components/rules/new/ExtractionSettings.tsx @@ -442,8 +442,9 @@ export function ExtractionSettings({ onChange }: ExtractionSettingsProps) { onChange({ extractionMethod: currentTab, promptSettings: { - ...promptType, - [type]: value + type: value, + template: selectedTemplate[type], + content: promptContent[type] } }); } @@ -503,6 +504,17 @@ export function ExtractionSettings({ onChange }: ExtractionSettingsProps) { ...promptContent, [type]: '' }); + + if (onChange) { + onChange({ + extractionMethod: currentTab, + promptSettings: { + type: promptType[type], + template: '', + content: '' + } + }); + } } }; diff --git a/app/routes/rules.new.tsx b/app/routes/rules.new.tsx index db76e8c..7421320 100644 --- a/app/routes/rules.new.tsx +++ b/app/routes/rules.new.tsx @@ -26,10 +26,73 @@ export const handle = { breadcrumb: "新增评查点" }; +// 定义类型 +interface RegexField { + fieldName: string; + regex: string; +} + +interface PromptSetting { + type: string; + template: string; +} + +interface ExtactionConfigType { + llm_ocr: { + fields: string[]; + prompt_setting: PromptSetting; + }; + llm_vl: { + fields: string[]; + prompt_setting: PromptSetting; + }; + ocr_regex: { + fields: RegexField[]; + }; +} + +interface RuleConfigType { + [key: string]: any; +} + +interface Rule { + id: string; + type: string; + config: RuleConfigType; +} + +interface EvaluationConfigType { + logicType: string; + customLogic: string; + rules: Rule[]; +} + +interface FormDataType { + name: string; + code: string; + risk: string; + is_enabled: boolean; + description: string; + references_laws: { + name: string; + articles: string[]; + content: string; + }; + evaluation_point_groups_id: number | null; + extraction_config: ExtactionConfigType; + evaluation_config: EvaluationConfigType; + pass_message: string; + fail_message: string; + suggestion_message: string; + suggestion_message_type: string; + post_action: string; + action_config: string; +} + export default function RuleNew() { const navigate = useNavigate(); const [extractionFields, setExtractionFields] = useState([]); - const [formData, setFormData] = useState({ + const [formData, setFormData] = useState({ // 基本信息字段 name: '', code: '', @@ -97,23 +160,240 @@ export default function RuleNew() { // 处理ExtractionSettings组件数据变更 const handleExtractionSettingsChange = (data: Record) => { - setFormData(prevData => ({ - ...prevData, - ...data - })); + setFormData(prevData => { + // 获取数据 + const extractionMethod = data.extractionMethod as string; + const regexFields = data.regexFields as Array<{ id: string; fieldName: string; regex: string }>; + const fields = data.fields as Record; + + // 根据抽取方法更新对应字段 + const updatedExtractionConfig = { ...prevData.extraction_config }; + + // 更新正则抽取字段 + if (regexFields) { + updatedExtractionConfig.ocr_regex.fields = regexFields + .filter(field => field.fieldName && field.regex) + .map(field => ({ + fieldName: field.fieldName, + regex: field.regex + })); + } + + // 更新LLM字段 + if (fields) { + if (fields.llm_ocr) { + updatedExtractionConfig.llm_ocr.fields = fields.llm_ocr; + } + if (fields.llm) { + updatedExtractionConfig.llm_vl.fields = fields.llm; + } + } + + // 更新提示词设置 + if (data.promptSettings) { + const promptSettings = data.promptSettings as Record; + + // 确定当前是处理哪种类型的提示词 + if (extractionMethod === 'llm_ocr') { + updatedExtractionConfig.llm_ocr.prompt_setting.type = promptSettings.type || 'system'; + updatedExtractionConfig.llm_ocr.prompt_setting.template = promptSettings.content || ''; + } else if (extractionMethod === 'llm') { + updatedExtractionConfig.llm_vl.prompt_setting.type = promptSettings.type || 'system'; + updatedExtractionConfig.llm_vl.prompt_setting.template = promptSettings.content || ''; + } + } else { + // 兼容旧的API + if (data.promptType) { + const promptType = data.promptType as Record; + if (promptType.llm_ocr) { + updatedExtractionConfig.llm_ocr.prompt_setting.type = promptType.llm_ocr; + } + if (promptType.llm) { + updatedExtractionConfig.llm_vl.prompt_setting.type = promptType.llm; + } + } + + if (data.promptContent) { + const promptContent = data.promptContent as Record; + if (promptContent.llm_ocr) { + updatedExtractionConfig.llm_ocr.prompt_setting.template = promptContent.llm_ocr; + } + if (promptContent.llm) { + updatedExtractionConfig.llm_vl.prompt_setting.template = promptContent.llm; + } + } + } + + console.log('更新的抽取设置:', updatedExtractionConfig); + + return { + ...prevData, + extraction_config: updatedExtractionConfig + }; + }); }; // 处理ReviewSettings组件数据变更 const handleReviewSettingsChange = (data: Record) => { - setFormData(prevData => ({ - ...prevData, - ...data - })); + setFormData(prevData => { + const updatedData = { ...prevData }; + + // 更新规则 + if (data.rules) { + updatedData.evaluation_config.rules = data.rules as Rule[]; + } + + // 更新组合逻辑 + if (data.combinationLogic !== undefined) { + updatedData.evaluation_config.logicType = data.combinationLogic as string; + } + + // 更新自定义逻辑 + if (data.customLogic !== undefined) { + updatedData.evaluation_config.customLogic = data.customLogic as string; + } + + // 更新通过/不通过/建议消息 + if (data.pass_message !== undefined) { + updatedData.pass_message = data.pass_message as string; + } + + if (data.fail_message !== undefined) { + updatedData.fail_message = data.fail_message as string; + } + + if (data.suggestion_message !== undefined) { + updatedData.suggestion_message = data.suggestion_message as string; + } + + if (data.suggestion_message_type !== undefined) { + updatedData.suggestion_message_type = data.suggestion_message_type as string; + } + + // 更新评查后动作 + if (data.post_action !== undefined) { + updatedData.post_action = data.post_action as string; + } + + if (data.action_config !== undefined) { + updatedData.action_config = data.action_config as string; + } + + return updatedData; + }); }; // 保存评查点 const handleSave = async () => { try { + // 转换提取配置为符合数据库格式的JSON + const extractionConfig = { + llm: { + fields: formData.extraction_config.llm_ocr.fields || [], + prompt_setting: { + type: formData.extraction_config.llm_ocr.prompt_setting.type || 'system', + template: formData.extraction_config.llm_ocr.prompt_setting.template || '' + } + }, + vlm: { + fields: (formData.extraction_config.llm_vl.fields || []).map((field: any) => { + // 处理带有类型后缀的字段名 (如 "字段名_类型") + if (typeof field === 'string' && field.includes('_')) { + const [name, type] = field.split('_'); + return { + name, + type: type || 'default' + }; + } + return { name: field, type: 'default' }; + }), + prompt_setting: { + type: formData.extraction_config.llm_vl.prompt_setting.type || 'system', + template: formData.extraction_config.llm_vl.prompt_setting.template || '' + } + }, + regex: { + fields: (formData.extraction_config.ocr_regex.fields || []).map((field: any) => { + if (typeof field === 'object') { + return { + field: field.fieldName || '', + pattern: field.regex || '' + }; + } + return { field: '', pattern: '' }; + }) + } + }; + + console.log('提交的提取配置:', extractionConfig); + console.log('原始提取配置:', formData.extraction_config); + + // 转换评查配置为符合数据库格式的JSON + const evaluationConfig = { + logicType: formData.evaluation_config.logicType || 'and', + customLogic: formData.evaluation_config.customLogic || '', + rules: (formData.evaluation_config.rules || []).map((rule: any) => { + let config = {}; + + // 根据不同的规则类型生成对应的配置 + switch (rule.type) { + case 'exists': + config = { + fields: rule.config?.selectedFields || [], + logic: rule.config?.logicRelation || 'and' + }; + break; + case 'consistency': + config = { + pairs: rule.config?.pairs || [], + logic: rule.config?.logicRelation || 'and' + }; + break; + case 'format': + config = { + field: rule.config?.field || '', + formatType: rule.config?.formatType || '', + parameters: rule.config?.parameters || '' + }; + break; + case 'logic': + config = { + conditions: rule.config?.conditions || [], + logic: rule.config?.logicRelation || 'and' + }; + break; + case 'regex': + config = { + field: rule.config?.field || '', + pattern: rule.config?.pattern || '', + matchType: rule.config?.matchType || 'match' + }; + break; + case 'ai': + config = { + model: rule.config?.model || 'qwen14b', + temperature: rule.config?.temperature || 0.1, + prompt: rule.config?.prompt || '' + }; + break; + case 'code': + config = { + language: rule.config?.language || 'javascript', + code: rule.config?.code || '' + }; + break; + default: + config = rule.config || {}; + } + + return { + id: rule.id, + type: rule.type, + config + }; + }) + }; + // 构建完整的评查点数据 const evaluationPointData = { code: formData.code, @@ -123,8 +403,8 @@ export default function RuleNew() { description: formData.description, is_enabled: formData.is_enabled, references_laws: formData.references_laws, - extraction_config: formData.extraction_config, - evaluation_config: formData.evaluation_config, + extraction_config: extractionConfig, + evaluation_config: evaluationConfig, pass_message: formData.pass_message, fail_message: formData.fail_message, suggestion_message: formData.suggestion_message, @@ -160,11 +440,132 @@ export default function RuleNew() { // 保存为草稿 const handleSaveDraft = async () => { try { + // 转换提取配置为符合数据库格式的JSON + const extractionConfig = { + llm: { + fields: formData.extraction_config.llm_ocr.fields || [], + prompt_setting: { + type: formData.extraction_config.llm_ocr.prompt_setting.type || 'system', + template: formData.extraction_config.llm_ocr.prompt_setting.template || '' + } + }, + vlm: { + fields: (formData.extraction_config.llm_vl.fields || []).map((field: any) => { + // 处理带有类型后缀的字段名 (如 "字段名_类型") + if (typeof field === 'string' && field.includes('_')) { + const [name, type] = field.split('_'); + return { + name, + type: type || 'default' + }; + } + return { name: field, type: 'default' }; + }), + prompt_setting: { + type: formData.extraction_config.llm_vl.prompt_setting.type || 'system', + template: formData.extraction_config.llm_vl.prompt_setting.template || '' + } + }, + regex: { + fields: (formData.extraction_config.ocr_regex.fields || []).map((field: any) => { + if (typeof field === 'object') { + return { + field: field.fieldName || '', + pattern: field.regex || '' + }; + } + return { field: '', pattern: '' }; + }) + } + }; + + console.log('草稿提交的提取配置:', extractionConfig); + console.log('草稿原始提取配置:', formData.extraction_config); + + // 转换评查配置为符合数据库格式的JSON + const evaluationConfig = { + logicType: formData.evaluation_config.logicType || 'and', + customLogic: formData.evaluation_config.customLogic || '', + rules: (formData.evaluation_config.rules || []).map((rule: any) => { + let config = {}; + + // 根据不同的规则类型生成对应的配置 + switch (rule.type) { + case 'exists': + config = { + fields: rule.config?.selectedFields || [], + logic: rule.config?.logicRelation || 'and' + }; + break; + case 'consistency': + config = { + pairs: rule.config?.pairs || [], + logic: rule.config?.logicRelation || 'and' + }; + break; + case 'format': + config = { + field: rule.config?.field || '', + formatType: rule.config?.formatType || '', + parameters: rule.config?.parameters || '' + }; + break; + case 'logic': + config = { + conditions: rule.config?.conditions || [], + logic: rule.config?.logicRelation || 'and' + }; + break; + case 'regex': + config = { + field: rule.config?.field || '', + pattern: rule.config?.pattern || '', + matchType: rule.config?.matchType || 'match' + }; + break; + case 'ai': + config = { + model: rule.config?.model || 'qwen14b', + temperature: rule.config?.temperature || 0.1, + prompt: rule.config?.prompt || '' + }; + break; + case 'code': + config = { + language: rule.config?.language || 'javascript', + code: rule.config?.code || '' + }; + break; + default: + config = rule.config || {}; + } + + return { + id: rule.id, + type: rule.type, + config + }; + }) + }; + // 构建带草稿标记的评查点数据 const draftData = { - ...formData, + code: formData.code, + name: formData.name, + evaluation_point_groups_id: formData.evaluation_point_groups_id, + risk: formData.risk, + description: formData.description, is_enabled: false, // 草稿默认不启用 - is_draft: true // 标记为草稿 + is_draft: true, // 标记为草稿 + references_laws: formData.references_laws, + extraction_config: extractionConfig, + evaluation_config: evaluationConfig, + pass_message: formData.pass_message, + fail_message: formData.fail_message, + suggestion_message: formData.suggestion_message, + suggestion_message_type: formData.suggestion_message_type, + post_action: formData.post_action, + action_config: formData.action_config }; // 发送数据到API