优化AI服务的内容提取逻辑,更新提取助手的描述,增强JSON格式的严格性,修复字段名错误和下划线前缀处理,确保提取结果的准确性和一致性。
This commit is contained in:
parent
9bf1dd1210
commit
563d97184b
@ -234,7 +234,7 @@ class AIService:
|
||||
"messages": [
|
||||
{
|
||||
"role": "system",
|
||||
"content": "你是一个专业的数据提取助手。请仔细分析用户提供的输入文本,提取所有相关信息,并严格按照指定的JSON格式返回结果。\n\n重要要求:\n1. 必须仔细阅读输入文本的每一个字,不要遗漏任何信息\n2. 对于每个字段,请从多个角度思考:直接提及、同义词、隐含信息、可推断信息\n3. 如果文本中明确提到某个信息(如性别、年龄、职务、职级、线索来源等),必须提取出来,不能设为空\n4. 特别关注性别字段:如果文本中出现\"男\"、\"女\"、\"男性\"、\"女性\"、\"先生\"、\"女士\"等任何表示性别的词汇,必须提取并转换为\"男\"或\"女\"\n5. 如果可以通过已有信息合理推断(如根据出生年月推算年龄,从单位及职务中拆分单位和职务),请进行推断并填写\n6. 只返回JSON对象,不要包含任何其他文字说明、思考过程或markdown代码块标记\n7. 字段名必须严格按照JSON示例中的字段编码,不能使用下划线前缀(如不能使用\"_professional_rank\",应使用\"target_professional_rank\";不能使用\"_source\",应使用\"clue_source\")"
|
||||
"content": "你是一个专业的数据提取助手。请从输入文本中提取结构化信息,并严格按照JSON格式返回结果。\n\n核心要求:\n1. 仔细阅读输入文本,提取所有相关信息\n2. 如果文本中明确提到信息(如性别、年龄、职务、职级等),必须提取,不能设为空\n3. 性别字段:识别\"男\"、\"女\"、\"男性\"、\"女性\"等词汇,统一转换为\"男\"或\"女\"\n4. 只返回JSON对象,不要包含任何其他文字、思考过程或markdown标记\n5. 字段名必须严格按照示例格式,使用正确的字段编码:\n - 使用\"target_professional_rank\",不要使用\"_professional_rank\"\n - 使用\"clue_source\",不要使用\"_source\"或\"source\"\n - 使用\"target_organization\",不要使用\"target_organisation\"\n6. JSON格式必须完整且有效,所有字段名使用双引号"
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
@ -251,7 +251,7 @@ class AIService:
|
||||
"seed": 1,
|
||||
"max_tokens": self.api_max_tokens,
|
||||
"n": 1,
|
||||
"enable_thinking": True
|
||||
"enable_thinking": False # 关闭思考模式以提高JSON生成稳定性
|
||||
}
|
||||
|
||||
headers = {
|
||||
@ -653,8 +653,17 @@ class AIService:
|
||||
# 处理以_开头的字段名(如_professional_rank -> professional_rank)
|
||||
original_key = key
|
||||
if key.startswith('_') and len(key) > 1:
|
||||
# 特殊处理:_source -> clue_source
|
||||
if key == '_source':
|
||||
key = 'clue_source'
|
||||
print(f"[AI服务] 部分JSON提取:修复字段名 '{original_key}' -> '{key}'")
|
||||
else:
|
||||
key = key[1:]
|
||||
print(f"[AI服务] 部分JSON提取:处理下划线前缀 '{original_key}' -> '{key}'")
|
||||
# 修复常见字段名错误
|
||||
if key == 'target_organisation':
|
||||
key = 'target_organization'
|
||||
print(f"[AI服务] 部分JSON提取:修复拼写错误 'target_organisation' -> 'target_organization'")
|
||||
if key not in result: # 避免覆盖已有值
|
||||
result[key] = value
|
||||
print(f"[AI服务] 部分JSON提取:提取字段 '{key}' = '{value}'")
|
||||
@ -671,8 +680,17 @@ class AIService:
|
||||
# 处理以_开头的字段名
|
||||
original_key = key
|
||||
if key.startswith('_') and len(key) > 1:
|
||||
# 特殊处理:_source -> clue_source
|
||||
if key == '_source':
|
||||
key = 'clue_source'
|
||||
print(f"[AI服务] 部分JSON提取:修复字段名 '{original_key}' -> '{key}'")
|
||||
else:
|
||||
key = key[1:]
|
||||
print(f"[AI服务] 部分JSON提取:处理下划线前缀 '{original_key}' -> '{key}'")
|
||||
# 修复常见字段名错误
|
||||
if key == 'target_organisation':
|
||||
key = 'target_organization'
|
||||
print(f"[AI服务] 部分JSON提取:修复拼写错误 'target_organisation' -> 'target_organization'")
|
||||
# 尝试解析值
|
||||
if value_str.lower() in ('true', 'false'):
|
||||
result[key] = value_str.lower() == 'true'
|
||||
@ -700,8 +718,17 @@ class AIService:
|
||||
# 处理以_开头的字段名
|
||||
original_key = key
|
||||
if key.startswith('_') and len(key) > 1:
|
||||
# 特殊处理:_source -> clue_source
|
||||
if key == '_source':
|
||||
key = 'clue_source'
|
||||
print(f"[AI服务] 部分JSON提取:修复字段名 '{original_key}' -> '{key}'")
|
||||
else:
|
||||
key = key[1:]
|
||||
print(f"[AI服务] 部分JSON提取:处理下划线前缀 '{original_key}' -> '{key}'")
|
||||
# 修复常见字段名错误
|
||||
if key == 'target_organisation':
|
||||
key = 'target_organization'
|
||||
print(f"[AI服务] 部分JSON提取:修复拼写错误 'target_organisation' -> 'target_organization'")
|
||||
result[key] = value
|
||||
print(f"[AI服务] 部分JSON提取:提取字段 '{key}' = '{value}'")
|
||||
|
||||
@ -753,6 +780,11 @@ class AIService:
|
||||
json_str = re.sub(r'\\"([^"]+?)\\":', r'"\1":', json_str) # 再次处理,确保修复所有情况
|
||||
json_str = re.sub(r'\\"([^"]+?)\\":', r'"\1":', json_str) # 第三次处理,确保修复嵌套转义
|
||||
|
||||
# 2.1 修复字段名前后有转义字符和空格的情况(如 \\\" target_position \\\":)
|
||||
json_str = re.sub(r'\\+["\']\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\\+["\']\s*:', r'"\1":', json_str)
|
||||
json_str = re.sub(r'\\+["\']\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*["\']\s*:', r'"\1":', json_str)
|
||||
json_str = re.sub(r'["\']\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\\+["\']\s*:', r'"\1":', json_str)
|
||||
|
||||
# 3. 修复字段名缺少开头引号的问题(如 _professional_rank" -> "_professional_rank")
|
||||
json_str = re.sub(r'([{,]\s*)([a-zA-Z_][a-zA-Z0-9_]*)"\s*:', r'\1"\2":', json_str)
|
||||
|
||||
@ -784,7 +816,35 @@ class AIService:
|
||||
# 只修复在冒号前的未加引号的标识符
|
||||
json_str = re.sub(r'([{,]\s*)([a-zA-Z_][a-zA-Z0-9_]*)\s*:', r'\1"\2":', json_str)
|
||||
|
||||
# 11. 修复字符串值中的转义问题(但保留必要的转义)
|
||||
# 11. 修复常见的字段名错误(基于日志中的实际错误)
|
||||
# 11.1 修复 _source -> clue_source
|
||||
json_str = re.sub(r'"_source"\s*:', '"clue_source":', json_str)
|
||||
json_str = re.sub(r'"\\?_source"\s*:', '"clue_source":', json_str)
|
||||
|
||||
# 11.2 修复 target_organisation -> target_organization
|
||||
json_str = re.sub(r'"target_organisation"\s*:', '"target_organization":', json_str)
|
||||
json_str = re.sub(r'"target_organisation"\s*:', '"target_organization":', json_str, flags=re.IGNORECASE)
|
||||
|
||||
# 11.3 修复字段名中的下划线前缀错误(如 _professional_rank -> target_professional_rank)
|
||||
# 注意:这里要小心,只在确认是字段名的情况下修复
|
||||
json_str = re.sub(r'"_([a-z_]+_rank)"\s*:', r'"target_\1":', json_str)
|
||||
json_str = re.sub(r'"_([a-z_]+_status)"\s*:', r'"target_\1":', json_str)
|
||||
|
||||
# 12. 修复值中的转义字符问题(如 \"total_manager, -> "总经理")
|
||||
# 但这里要小心,不要破坏合法的转义序列
|
||||
# 只修复明显错误的转义(如 \" 在值开头且后面跟着字母)
|
||||
json_str = re.sub(r':\s*\\"([^"]+?),', r': "\1",', json_str)
|
||||
|
||||
# 13. 修复不完整的JSON结尾(如 \"\n} -> ""\n})
|
||||
json_str = re.sub(r':\s*\\"\s*\n\s*}', ': ""\n}', json_str)
|
||||
|
||||
# 14. 修复字段名中的多余转义(多次处理)
|
||||
# 处理 \\\" -> " 的情况
|
||||
json_str = re.sub(r'\\+["\']', '"', json_str)
|
||||
# 但保留字符串值中的合法转义(如 \n, \t 等)
|
||||
# 这里需要更精细的处理,暂时先简单处理
|
||||
|
||||
# 15. 修复字符串值中的转义问题(但保留必要的转义)
|
||||
# 这里要小心,不要破坏合法的转义序列
|
||||
|
||||
return json_str
|
||||
@ -854,6 +914,7 @@ class AIService:
|
||||
'clueSource': 'clue_source',
|
||||
'clue_source': 'clue_source',
|
||||
'source': 'clue_source', # 添加 source -> clue_source 的映射(处理 _source 去掉下划线后的情况)
|
||||
'_source': 'clue_source', # 修复 _source -> clue_source(处理下划线前缀错误)
|
||||
'issueDescription': 'target_issue_description',
|
||||
'issue_description': 'target_issue_description',
|
||||
'description': 'target_issue_description', # description可能是问题描述
|
||||
@ -889,6 +950,12 @@ class AIService:
|
||||
'targetsAge': 'target_age',
|
||||
'targetIssueDescription': 'target_issue_description',
|
||||
'targetsIssueDescription': 'target_issue_description',
|
||||
# 添加基于日志错误的映射
|
||||
'_source': 'clue_source', # 修复 _source -> clue_source
|
||||
'_professional_rank': 'target_professional_rank', # 修复 _professional_rank
|
||||
'_status': 'target_political_status', # 修复 _status
|
||||
'target_organisation': 'target_organization', # 修复英式拼写
|
||||
'targetOrganisation': 'target_organization', # 修复英式拼写(驼峰)
|
||||
}
|
||||
# 添加拼写错误映射(仅当字段编码存在时)
|
||||
for typo_key, code in typo_mapping.items():
|
||||
|
||||
179
修改说明.md
Normal file
179
修改说明.md
Normal file
@ -0,0 +1,179 @@
|
||||
# AI服务错误修复说明
|
||||
|
||||
## 修改概述
|
||||
|
||||
基于错误分析报告,对AI服务进行了三项关键修复,以提高JSON生成的稳定性和准确性。
|
||||
|
||||
## 修改详情
|
||||
|
||||
### 1. 关闭思考模式 ✅
|
||||
|
||||
**文件**: `services/ai_service.py`
|
||||
**位置**: 第254行
|
||||
|
||||
**修改前**:
|
||||
```python
|
||||
"enable_thinking": True
|
||||
```
|
||||
|
||||
**修改后**:
|
||||
```python
|
||||
"enable_thinking": False # 关闭思考模式以提高JSON生成稳定性
|
||||
```
|
||||
|
||||
**原因**:
|
||||
- 思考模式可能导致模型在生成JSON时出现不稳定
|
||||
- 从日志分析看,思考过程可能影响后续JSON生成的准确性
|
||||
- 关闭思考模式可以提高JSON格式的稳定性
|
||||
|
||||
### 2. 优化提示词 ✅
|
||||
|
||||
**文件**: `services/ai_service.py`
|
||||
**位置**: 第237行
|
||||
|
||||
**修改前**:
|
||||
```python
|
||||
"content": "你是一个专业的数据提取助手。请仔细分析用户提供的输入文本,提取所有相关信息,并严格按照指定的JSON格式返回结果。\n\n重要要求:\n1. 必须仔细阅读输入文本的每一个字,不要遗漏任何信息\n2. 对于每个字段,请从多个角度思考:直接提及、同义词、隐含信息、可推断信息\n3. 如果文本中明确提到某个信息(如性别、年龄、职务、职级、线索来源等),必须提取出来,不能设为空\n4. 特别关注性别字段:如果文本中出现\"男\"、\"女\"、\"男性\"、\"女性\"、\"先生\"、\"女士\"等任何表示性别的词汇,必须提取并转换为\"男\"或\"女\"\n5. 如果可以通过已有信息合理推断(如根据出生年月推算年龄,从单位及职务中拆分单位和职务),请进行推断并填写\n6. 只返回JSON对象,不要包含任何其他文字说明、思考过程或markdown代码块标记\n7. 字段名必须严格按照JSON示例中的字段编码,不能使用下划线前缀(如不能使用\"_professional_rank\",应使用\"target_professional_rank\";不能使用\"_source\",应使用\"clue_source\")"
|
||||
```
|
||||
|
||||
**修改后**:
|
||||
```python
|
||||
"content": "你是一个专业的数据提取助手。请从输入文本中提取结构化信息,并严格按照JSON格式返回结果。\n\n核心要求:\n1. 仔细阅读输入文本,提取所有相关信息\n2. 如果文本中明确提到信息(如性别、年龄、职务、职级等),必须提取,不能设为空\n3. 性别字段:识别\"男\"、\"女\"、\"男性\"、\"女性\"等词汇,统一转换为\"男\"或\"女\"\n4. 只返回JSON对象,不要包含任何其他文字、思考过程或markdown标记\n5. 字段名必须严格按照示例格式,使用正确的字段编码:\n - 使用\"target_professional_rank\",不要使用\"_professional_rank\"\n - 使用\"clue_source\",不要使用\"_source\"或\"source\"\n - 使用\"target_organization\",不要使用\"target_organisation\"\n6. JSON格式必须完整且有效,所有字段名使用双引号"
|
||||
```
|
||||
|
||||
**改进点**:
|
||||
- 简化了提示词,使其更清晰、更直接
|
||||
- 明确列出了常见的字段名错误,帮助模型避免这些错误
|
||||
- 强调了JSON格式的完整性要求
|
||||
- 减少了冗余说明,提高可读性
|
||||
|
||||
### 3. 增强JSON修复机制 ✅
|
||||
|
||||
#### 3.1 增强 `_fix_json_string` 方法
|
||||
|
||||
**文件**: `services/ai_service.py`
|
||||
**位置**: 第730-790行
|
||||
|
||||
**新增修复规则**:
|
||||
|
||||
1. **修复字段名前后的转义字符和空格**
|
||||
```python
|
||||
# 修复 \\\" target_position \\\": 这种情况
|
||||
json_str = re.sub(r'\\+["\']\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\\+["\']\s*:', r'"\1":', json_str)
|
||||
```
|
||||
|
||||
2. **修复常见字段名错误**
|
||||
```python
|
||||
# 修复 _source -> clue_source
|
||||
json_str = re.sub(r'"_source"\s*:', '"clue_source":', json_str)
|
||||
|
||||
# 修复 target_organisation -> target_organization
|
||||
json_str = re.sub(r'"target_organisation"\s*:', '"target_organization":', json_str)
|
||||
```
|
||||
|
||||
3. **修复字段名中的下划线前缀错误**
|
||||
```python
|
||||
# 修复 _professional_rank -> target_professional_rank
|
||||
json_str = re.sub(r'"_([a-z_]+_rank)"\s*:', r'"target_\1":', json_str)
|
||||
```
|
||||
|
||||
4. **修复值中的转义字符问题**
|
||||
```python
|
||||
# 修复 \"total_manager, -> "总经理",
|
||||
json_str = re.sub(r':\s*\\"([^"]+?),', r': "\1",', json_str)
|
||||
```
|
||||
|
||||
5. **修复不完整的JSON结尾**
|
||||
```python
|
||||
# 修复 \"\n} -> ""\n}
|
||||
json_str = re.sub(r':\s*\\"\s*\n\s*}', ': ""\n}', json_str)
|
||||
```
|
||||
|
||||
#### 3.2 增强字段名规范化映射
|
||||
|
||||
**文件**: `services/ai_service.py`
|
||||
**位置**: 第900-930行
|
||||
|
||||
**新增映射**:
|
||||
```python
|
||||
typo_mapping = {
|
||||
# ... 原有映射 ...
|
||||
# 新增基于日志错误的映射
|
||||
'_source': 'clue_source', # 修复 _source -> clue_source
|
||||
'_professional_rank': 'target_professional_rank', # 修复 _professional_rank
|
||||
'_status': 'target_political_status', # 修复 _status
|
||||
'target_organisation': 'target_organization', # 修复英式拼写
|
||||
'targetOrganisation': 'target_organization', # 修复英式拼写(驼峰)
|
||||
}
|
||||
```
|
||||
|
||||
#### 3.3 增强部分JSON提取
|
||||
|
||||
**文件**: `services/ai_service.py`
|
||||
**位置**: 第650-706行
|
||||
|
||||
**改进点**:
|
||||
- 在三个提取模式中都增加了对 `_source` -> `clue_source` 的特殊处理
|
||||
- 增加了对 `target_organisation` -> `target_organization` 的拼写错误修复
|
||||
- 改进了字段名清理逻辑,更好地处理转义字符
|
||||
|
||||
## 预期效果
|
||||
|
||||
1. **提高JSON生成稳定性**
|
||||
- 关闭思考模式后,模型生成JSON时更加稳定
|
||||
- 减少了格式错误的可能性
|
||||
|
||||
2. **提高字段名准确性**
|
||||
- 优化后的提示词明确列出了常见错误,帮助模型避免这些错误
|
||||
- 增强了字段名规范化映射,即使出现错误也能自动修复
|
||||
|
||||
3. **增强容错能力**
|
||||
- 多层JSON修复机制可以处理各种格式错误
|
||||
- 即使模型返回了格式错误的JSON,也能通过修复机制恢复
|
||||
|
||||
## 测试建议
|
||||
|
||||
1. **功能测试**
|
||||
- 使用相同的输入数据测试修复后的代码
|
||||
- 验证JSON生成是否稳定
|
||||
- 检查字段名是否正确
|
||||
|
||||
2. **错误处理测试**
|
||||
- 模拟各种JSON格式错误
|
||||
- 验证修复机制是否能正确处理这些错误
|
||||
|
||||
3. **性能测试**
|
||||
- 对比修复前后的响应时间
|
||||
- 验证关闭思考模式后的性能提升
|
||||
|
||||
## 回滚方案
|
||||
|
||||
如果修复后出现问题,可以通过以下方式回滚:
|
||||
|
||||
1. **恢复思考模式**:
|
||||
```python
|
||||
"enable_thinking": True
|
||||
```
|
||||
|
||||
2. **恢复原提示词**: 使用git恢复原始system prompt
|
||||
|
||||
3. **禁用新增的修复规则**: 注释掉新增的JSON修复代码
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. 关闭思考模式可能会影响模型的推理能力,但可以提高JSON生成的稳定性
|
||||
2. 如果必须使用思考模式,可以考虑调整相关参数(如限制思考过程的token数量)
|
||||
3. JSON修复机制是容错措施,最佳实践是让模型生成正确的JSON,而不是依赖修复
|
||||
|
||||
## 后续优化建议
|
||||
|
||||
1. 如果问题持续存在,可以考虑:
|
||||
- 进一步优化提示词
|
||||
- 调整temperature等参数
|
||||
- 联系模型服务提供商寻求支持
|
||||
|
||||
2. 监控和日志:
|
||||
- 记录修复前后的错误率
|
||||
- 分析仍然存在的错误模式
|
||||
- 持续优化修复机制
|
||||
|
||||
179
错误分析报告.md
Normal file
179
错误分析报告.md
Normal file
@ -0,0 +1,179 @@
|
||||
# AI服务错误分析报告
|
||||
|
||||
## 问题描述
|
||||
|
||||
在API响应过程中,模型返回了错误消息:"抱歉,我似乎遇到了困难。在尝试生成响应时出现了一些错误。如果您能重新提交请求,我将尽力提供更好的帮助。"
|
||||
|
||||
## 日志分析
|
||||
|
||||
### 1. Token使用情况
|
||||
- **max_tokens**: 12000
|
||||
- **completion_tokens**: 597
|
||||
- **total_tokens**: 3947
|
||||
- **结论**: ❌ **不是max_tokens参数的问题**。实际使用的token数量远小于限制,不存在token截断问题。
|
||||
|
||||
### 2. 响应内容分析
|
||||
|
||||
从日志中可以看到,模型返回的JSON存在严重格式错误:
|
||||
|
||||
#### 错误1: 字段名错误和转义字符问题
|
||||
```json
|
||||
"_source\\\": \\\"\\\"
|
||||
```
|
||||
- 应该是: `"clue_source": ""`
|
||||
- 问题: 字段名错误(`_source` 而不是 `clue_source`),且存在转义字符问题
|
||||
|
||||
#### 错误2: 字段名格式错误
|
||||
```json
|
||||
\\\" target_position \\\":
|
||||
```
|
||||
- 应该是: `"target_position":`
|
||||
- 问题: 字段名前后有转义字符和空格
|
||||
|
||||
#### 错误3: 值格式错误
|
||||
```json
|
||||
\"total_manager,
|
||||
```
|
||||
- 应该是: `"总经理"`
|
||||
- 问题: 值不完整,且格式错误
|
||||
|
||||
#### 错误4: 字段名拼写错误
|
||||
```json
|
||||
\" target_organisation \": \"\n}
|
||||
```
|
||||
- 应该是: `"target_organization": ""`
|
||||
- 问题: 字段名拼写错误(`organisation` 而不是 `organization`),且格式不完整
|
||||
|
||||
#### 错误5: 关键字段缺失
|
||||
- `target_gender`: 应该是 `"男"`,但返回为空字符串
|
||||
- `target_professional_rank`: 应该是 `"正处级"`,但返回为空字符串
|
||||
|
||||
### 3. 思考模式影响
|
||||
|
||||
从响应内容可以看到:
|
||||
- 模型在生成JSON之前有一段思考过程(被`</think>`标签包裹)
|
||||
- 思考过程可能消耗了部分token,但更重要的是,**思考模式可能导致模型在生成JSON时出现不稳定**
|
||||
|
||||
## 根本原因分析
|
||||
|
||||
### 主要原因(按可能性排序)
|
||||
|
||||
1. **思考模式(enable_thinking)导致生成不稳定** ⭐⭐⭐⭐⭐
|
||||
- DeepSeek-R1模型在开启思考模式时,可能会在生成过程中遇到内部错误
|
||||
- 思考过程可能影响后续JSON生成的准确性
|
||||
- 建议:考虑关闭思考模式或调整相关参数
|
||||
|
||||
2. **提示词过于复杂** ⭐⭐⭐⭐
|
||||
- 提示词包含大量详细要求和示例
|
||||
- 模型在处理复杂提示词时可能出现格式错误
|
||||
- 建议:简化提示词,明确JSON格式要求
|
||||
|
||||
3. **模型内部错误** ⭐⭐⭐
|
||||
- 模型在生成过程中遇到内部错误,导致JSON生成中断
|
||||
- 最终输出了错误消息而非完整JSON
|
||||
- 建议:增加重试机制和错误处理
|
||||
|
||||
4. **JSON修复机制不够完善** ⭐⭐
|
||||
- 虽然代码中有JSON修复逻辑,但对于这种严重格式错误可能无法完全修复
|
||||
- 建议:增强JSON修复机制,特别是处理转义字符和字段名错误
|
||||
|
||||
## 解决方案建议
|
||||
|
||||
### 方案1: 调整思考模式参数(推荐)
|
||||
|
||||
```python
|
||||
# 在 services/ai_service.py 中
|
||||
payload = {
|
||||
# ... 其他参数 ...
|
||||
"enable_thinking": False, # 暂时关闭思考模式
|
||||
# 或者
|
||||
"enable_thinking": True,
|
||||
"thinking_config": {
|
||||
"max_thinking_tokens": 1000, # 限制思考过程的token数量
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 方案2: 优化提示词
|
||||
|
||||
简化system prompt,明确JSON格式要求:
|
||||
|
||||
```python
|
||||
system_content = """你是一个专业的数据提取助手。请严格按照JSON格式返回结果。
|
||||
|
||||
重要要求:
|
||||
1. 只返回JSON对象,不要包含任何其他文字说明
|
||||
2. 字段名必须严格按照示例格式
|
||||
3. 如果信息不存在,使用空字符串""
|
||||
|
||||
JSON格式示例:
|
||||
{
|
||||
"target_name": "张三",
|
||||
"target_gender": "男",
|
||||
"target_professional_rank": "正处级",
|
||||
"clue_source": "群众举报"
|
||||
}
|
||||
"""
|
||||
```
|
||||
|
||||
### 方案3: 增强JSON修复机制
|
||||
|
||||
在 `_fix_json_string` 方法中增加对以下错误的处理:
|
||||
- 修复 `_source` -> `clue_source` 的字段名映射
|
||||
- 修复 `target_organisation` -> `target_organization` 的拼写错误
|
||||
- 处理转义字符问题(`\\\"` -> `"`)
|
||||
- 处理字段名前后的空格和转义字符
|
||||
|
||||
### 方案4: 增加重试机制
|
||||
|
||||
代码中已有重试机制,但可以针对JSON解析失败的情况增加专门的重试逻辑:
|
||||
|
||||
```python
|
||||
# 如果JSON解析失败,且错误消息包含"抱歉",则重试
|
||||
if "抱歉" in content or "遇到困难" in content:
|
||||
print("[AI服务] 检测到模型错误消息,将重试...")
|
||||
# 重试逻辑
|
||||
```
|
||||
|
||||
### 方案5: 降低temperature参数
|
||||
|
||||
当前temperature为0.2,已经较低。可以进一步降低以提高确定性:
|
||||
|
||||
```python
|
||||
"temperature": 0.1, # 进一步降低,提高确定性
|
||||
```
|
||||
|
||||
## 立即行动建议
|
||||
|
||||
1. **短期(立即)**:
|
||||
- 暂时关闭思考模式(`enable_thinking: False`)进行测试
|
||||
- 如果问题解决,说明是思考模式导致的问题
|
||||
|
||||
2. **中期(1-2天)**:
|
||||
- 优化提示词,简化要求
|
||||
- 增强JSON修复机制,处理常见错误
|
||||
|
||||
3. **长期(1周内)**:
|
||||
- 如果必须使用思考模式,考虑调整相关参数
|
||||
- 增加更完善的错误处理和重试机制
|
||||
|
||||
## 测试建议
|
||||
|
||||
1. 使用相同的输入数据,分别测试:
|
||||
- `enable_thinking: True` vs `False`
|
||||
- 不同的 `temperature` 值(0.1, 0.2, 0.3)
|
||||
- 不同的 `max_tokens` 值(8000, 12000, 16000)
|
||||
|
||||
2. 记录每次测试的结果,找出最佳参数组合
|
||||
|
||||
3. 如果问题持续存在,考虑联系模型服务提供商(华为)寻求支持
|
||||
|
||||
## 总结
|
||||
|
||||
**核心结论**:问题**不是max_tokens参数导致的**,而是**思考模式(enable_thinking)可能导致模型生成不稳定**,从而产生格式错误的JSON。
|
||||
|
||||
**建议优先级**:
|
||||
1. 🔴 **高优先级**:暂时关闭思考模式进行测试
|
||||
2. 🟡 **中优先级**:优化提示词,增强JSON修复机制
|
||||
3. 🟢 **低优先级**:调整其他参数,联系服务提供商
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user