feat: 智能添加弹框显示解析prompt,支持编辑修改
- 后端新增 /api/parse-prompt API 获取 prompt 模板 - parse_with_llm 函数支持 custom_prompt 参数 - 智能添加弹框添加可折叠的 prompt 编辑区域 - 显示字段配置信息,方便用户理解解析逻辑 - 可重置为默认 prompt 或刷新字段配置 - 所有 smart-add API 支持接收自定义 prompt
This commit is contained in:
125
app.py
125
app.py
@@ -101,11 +101,87 @@ def save_data(file_path, data):
|
||||
|
||||
# ============ 大模型智能解析 ============
|
||||
|
||||
def parse_with_llm(text, category_type, images=None, category_id=None, subcategory_id=None):
|
||||
def get_parse_prompt_template(category_type, category_id=None, subcategory_id=None):
|
||||
"""
|
||||
获取解析 prompt 模板(供前端显示和编辑)
|
||||
"""
|
||||
# 从类别配置中获取字段定义
|
||||
categories = load_data(CATEGORIES_FILE)
|
||||
|
||||
# 确定类别ID
|
||||
if category_id:
|
||||
cat = next((c for c in categories if c['id'] == category_id), None)
|
||||
else:
|
||||
type_to_cat_id = {'model': 'ai-models', 'gpu': 'gpus', 'cpu': 'cpus', 'dynamic': None}
|
||||
cat_id = type_to_cat_id.get(category_type)
|
||||
cat = next((c for c in categories if c['id'] == cat_id), None)
|
||||
|
||||
# 构建字段模板
|
||||
fields = {}
|
||||
|
||||
if cat and 'fields' in cat:
|
||||
for field in cat['fields']:
|
||||
field_desc = field['label']
|
||||
if field.get('input_style') == 'long':
|
||||
field_desc += '(长文本)'
|
||||
else:
|
||||
field_desc += '(文本)'
|
||||
if field.get('description'):
|
||||
field_desc += f" - {field['description']}"
|
||||
fields[field['key']] = field_desc
|
||||
|
||||
if subcategory_id:
|
||||
subcat = next((s for s in cat.get('subcategories', []) if s['id'] == subcategory_id), None)
|
||||
if subcat and 'extra_fields' in subcat:
|
||||
for field in subcat['extra_fields']:
|
||||
field_desc = field['label']
|
||||
if field.get('input_style') == 'long':
|
||||
field_desc += '(长文本)'
|
||||
else:
|
||||
field_desc += '(文本)'
|
||||
if field.get('description'):
|
||||
field_desc += f" - {field['description']}"
|
||||
fields[field['key']] = field_desc
|
||||
else:
|
||||
fields = {
|
||||
'name': '名称',
|
||||
'brand': '品牌',
|
||||
'price': '价格(数字)',
|
||||
'year': '年份(数字)',
|
||||
'specs': '规格参数(JSON对象)',
|
||||
'description': '简介描述',
|
||||
}
|
||||
|
||||
fields_json = json.dumps(fields, ensure_ascii=False, indent=2)
|
||||
|
||||
# 图片解析 prompt
|
||||
image_prompt = """请分析图片中的产品参数信息,提取结构化数据。
|
||||
|
||||
需要提取的字段:
|
||||
""" + fields_json + """
|
||||
|
||||
重要要求:
|
||||
1. 图片中可能包含1个或多个产品,请识别所有产品
|
||||
2. 如果是多张图片,请综合分析所有图片内容
|
||||
3. 数字字段只返回数字,不带单位
|
||||
4. 如果某字段没有提及,返回null
|
||||
5. 返回格式:如果识别到多个产品,返回数组 [对象列表]; 如果只有一个产品,返回单个对象
|
||||
6. 只返回JSON数据,不要其他内容"""
|
||||
|
||||
return {
|
||||
'fields': fields,
|
||||
'fields_json': fields_json,
|
||||
'image_prompt': image_prompt,
|
||||
'category_name': cat.get('name', '') if cat else ''
|
||||
}
|
||||
|
||||
|
||||
def parse_with_llm(text, category_type, images=None, category_id=None, subcategory_id=None, custom_prompt=None):
|
||||
"""
|
||||
使用大模型解析文本/图片,提取结构化数据
|
||||
支持多张图片输入,可能解析出多个产品
|
||||
根据类别配置的参数字段进行解析
|
||||
支持自定义 prompt(优先使用自定义)
|
||||
"""
|
||||
|
||||
# 从类别配置中获取字段定义
|
||||
@@ -167,7 +243,11 @@ def parse_with_llm(text, category_type, images=None, category_id=None, subcatego
|
||||
|
||||
# 如果有图片,添加图片内容
|
||||
if images and len(images) > 0:
|
||||
prompt_text = """请分析图片中的产品参数信息,提取结构化数据。
|
||||
# 优先使用自定义 prompt,否则使用默认
|
||||
if custom_prompt and custom_prompt.strip():
|
||||
prompt_text = custom_prompt
|
||||
else:
|
||||
prompt_text = """请分析图片中的产品参数信息,提取结构化数据。
|
||||
|
||||
需要提取的字段:
|
||||
""" + fields_json + """
|
||||
@@ -492,6 +572,25 @@ def api_toggle_model_visible(model_id):
|
||||
|
||||
return jsonify({'success': True, 'visible': model['visible']})
|
||||
|
||||
# ============ 获取解析Prompt模板API ============
|
||||
|
||||
@app.route('/api/parse-prompt', methods=['POST'])
|
||||
def api_get_parse_prompt():
|
||||
"""
|
||||
获取智能解析的 prompt 模板(供前端显示和编辑)
|
||||
"""
|
||||
data = request.get_json()
|
||||
category_type = data.get('category_type', 'dynamic')
|
||||
category_id = data.get('category_id')
|
||||
subcategory_id = data.get('subcategory_id')
|
||||
|
||||
template = get_parse_prompt_template(category_type, category_id, subcategory_id)
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'template': template
|
||||
})
|
||||
|
||||
# ============ 图片解析API(预览) ============
|
||||
|
||||
@app.route('/api/parse-images', methods=['POST'])
|
||||
@@ -500,12 +599,14 @@ def api_parse_images():
|
||||
解析图片中的产品参数(预览模式,不保存)
|
||||
支持多张图片,可能返回多个产品
|
||||
根据类别配置的参数字段进行解析
|
||||
支持自定义 prompt(可选)
|
||||
"""
|
||||
data = request.get_json()
|
||||
text = data.get('text', '')
|
||||
images = data.get('images', [])
|
||||
category_type = data.get('category_type', 'dynamic')
|
||||
subcategory_id = data.get('subcategory_id', '')
|
||||
custom_prompt = data.get('custom_prompt', '') # 自定义 prompt
|
||||
|
||||
if not text and not images:
|
||||
return jsonify({'error': '文本或图片不能都为空'}), 400
|
||||
@@ -517,8 +618,8 @@ def api_parse_images():
|
||||
type_to_cat_id = {'model': 'ai-models', 'gpu': 'gpus', 'cpu': 'cpus', 'dynamic': None}
|
||||
category_id = type_to_cat_id.get(category_type)
|
||||
|
||||
# 调用大模型解析(根据类别字段配置)
|
||||
parsed_list = parse_with_llm(text, category_type, images, category_id=category_id, subcategory_id=subcategory_id)
|
||||
# 调用大模型解析(根据类别字段配置,支持自定义 prompt)
|
||||
parsed_list = parse_with_llm(text, category_type, images, category_id=category_id, subcategory_id=subcategory_id, custom_prompt=custom_prompt)
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
@@ -728,12 +829,13 @@ def api_smart_add_model():
|
||||
text = data.get('text', '')
|
||||
images = data.get('images', [])
|
||||
subcategory_id = data.get('subcategory_id', '') # 子类别ID
|
||||
custom_prompt = data.get('custom_prompt', '') # 自定义 prompt
|
||||
|
||||
if not text and not images:
|
||||
return jsonify({'error': '文本或图片不能都为空'}), 400
|
||||
|
||||
# 大模型解析(根据类别字段配置)
|
||||
parsed_list = parse_with_llm(text, 'model', images, category_id='ai-models', subcategory_id=subcategory_id)
|
||||
# 大模型解析(根据类别字段配置,支持自定义 prompt)
|
||||
parsed_list = parse_with_llm(text, 'model', images, category_id='ai-models', subcategory_id=subcategory_id, custom_prompt=custom_prompt)
|
||||
|
||||
# 处理多个产品
|
||||
results = []
|
||||
@@ -773,11 +875,12 @@ def api_smart_add_gpu():
|
||||
text = data.get('text', '')
|
||||
images = data.get('images', [])
|
||||
subcategory_id = data.get('subcategory_id', '')
|
||||
custom_prompt = data.get('custom_prompt', '') # 自定义 prompt
|
||||
|
||||
if not text and not images:
|
||||
return jsonify({'error': '文本或图片不能都为空'}), 400
|
||||
|
||||
parsed_list = parse_with_llm(text, 'gpu', images, category_id='gpus', subcategory_id=subcategory_id)
|
||||
parsed_list = parse_with_llm(text, 'gpu', images, category_id='gpus', subcategory_id=subcategory_id, custom_prompt=custom_prompt)
|
||||
|
||||
results = []
|
||||
gpus = load_data(GPUS_FILE)
|
||||
@@ -814,11 +917,12 @@ def api_smart_add_cpu():
|
||||
text = data.get('text', '')
|
||||
images = data.get('images', [])
|
||||
subcategory_id = data.get('subcategory_id', '')
|
||||
custom_prompt = data.get('custom_prompt', '') # 自定义 prompt
|
||||
|
||||
if not text and not images:
|
||||
return jsonify({'error': '文本或图片不能都为空'}), 400
|
||||
|
||||
parsed_list = parse_with_llm(text, 'cpu', images, category_id='cpus', subcategory_id=subcategory_id)
|
||||
parsed_list = parse_with_llm(text, 'cpu', images, category_id='cpus', subcategory_id=subcategory_id, custom_prompt=custom_prompt)
|
||||
|
||||
results = []
|
||||
cpus = load_data(CPUS_FILE)
|
||||
@@ -855,12 +959,13 @@ def api_smart_add_item(category_id):
|
||||
text = data.get('text', '')
|
||||
images = data.get('images', [])
|
||||
subcategory_id = data.get('subcategory_id', '')
|
||||
custom_prompt = data.get('custom_prompt', '') # 自定义 prompt
|
||||
|
||||
if not text and not images:
|
||||
return jsonify({'error': '文本或图片不能都为空'}), 400
|
||||
|
||||
# 使用类别配置的字段解析
|
||||
parsed_list = parse_with_llm(text, 'dynamic', images, category_id=category_id, subcategory_id=subcategory_id)
|
||||
# 使用类别配置的字段解析,支持自定义 prompt
|
||||
parsed_list = parse_with_llm(text, 'dynamic', images, category_id=category_id, subcategory_id=subcategory_id, custom_prompt=custom_prompt)
|
||||
|
||||
results = []
|
||||
items_file = DATA_DIR / f'items_{category_id}.json'
|
||||
|
||||
Reference in New Issue
Block a user