fix: 修复f-string花括号转义问题导致的API错误

This commit is contained in:
2026-04-27 18:44:37 +08:00
parent 5433605fec
commit 9525d56ffc

25
app.py
View File

@@ -140,26 +140,29 @@ def parse_with_llm(text, category_type, images=None):
}
fields = field_templates.get(category_type, field_templates['dynamic'])
fields_json = json.dumps(fields, ensure_ascii=False, indent=2)
# 构建消息内容
content_parts = []
# 如果有图片,添加图片内容
if images and len(images) > 0:
content_parts.append({
"type": "text",
"text": f"""请分析图片中的产品参数信息,提取结构化数据。
prompt_text = """请分析图片中的产品参数信息,提取结构化数据。
需要提取的字段:
{json.dumps(fields, ensure_ascii=False, indent=2)}
""" + fields_json + """
重要要求:
1. 图片中可能包含1个或多个产品请识别所有产品
2. 如果是多张图片,请综合分析所有图片内容
3. 数字字段只返回数字,不带单位
4. 如果某字段没有提及返回null
5. 返回格式:如果识别到多个产品,返回数组 [{"name": ...}, {"name": ...}]; 如果只有一个产品,返回单个对象 {"name": ...}
5. 返回格式:如果识别到多个产品,返回数组 [对象列表]; 如果只有一个产品,返回单个对象
6. 只返回JSON数据不要其他内容"""
content_parts.append({
"type": "text",
"text": prompt_text
})
# 添加每张图片支持URL或base64
@@ -194,15 +197,13 @@ def parse_with_llm(text, category_type, images=None):
print(f"读取图片失败: {e}")
else:
# 纯文本解析
content_parts.append({
"type": "text",
"text": f"""请解析以下文本,提取结构化数据。
prompt_text = """请解析以下文本,提取结构化数据。
文本内容:
{text}
""" + str(text) + """
需要提取的字段:
{json.dumps(fields, ensure_ascii=False, indent=2)}
""" + fields_json + """
要求:
1. 根据文本内容智能提取各个字段的值
@@ -211,6 +212,10 @@ def parse_with_llm(text, category_type, images=None):
4. 返回JSON格式不要包含任何其他内容
请直接返回JSON数据"""
content_parts.append({
"type": "text",
"text": prompt_text
})
try: