Skip to content

Commit 58f4327

Browse files
committed
fix(mcp-chat): prevent bigint/float data from displaying in scientific notation
Add zero-width space (\u200b) prefix to all numeric values in Markdown tables to maintain formatting.
1 parent bd5015c commit 58f4327

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

backend/apps/chat/task/llm.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,14 +1131,19 @@ def run_task(self, in_chat: bool = True, stream: bool = True,
11311131
yield 'data:' + orjson.dumps({'type': 'finish'}).decode() + '\n\n'
11321132
else:
11331133
# todo generate picture
1134-
if chart['type'] != 'table':
1135-
yield '### generated chart picture\n\n'
1136-
image_url = request_picture(self.record.chat_id, self.record.id, chart, format_json_data(result))
1137-
SQLBotLogUtil.info(image_url)
1134+
try:
1135+
if chart['type'] != 'table':
1136+
yield '### generated chart picture\n\n'
1137+
image_url = request_picture(self.record.chat_id, self.record.id, chart,
1138+
format_json_data(result))
1139+
SQLBotLogUtil.info(image_url)
1140+
if stream:
1141+
yield f'![{chart["type"]}]({image_url})'
1142+
else:
1143+
json_result['image_url'] = image_url
1144+
except Exception as e:
11381145
if stream:
1139-
yield f'![{chart["type"]}]({image_url})'
1140-
else:
1141-
json_result['image_url'] = image_url
1146+
raise e
11421147

11431148
if not stream:
11441149
yield json_result
@@ -1173,20 +1178,16 @@ def run_task(self, in_chat: bool = True, stream: bool = True,
11731178

11741179
@staticmethod
11751180
def safe_convert_to_string(df):
1176-
"""
1177-
安全地将数值列转换为字符串,避免科学记数法
1178-
"""
11791181
df_copy = df.copy()
11801182

1183+
def format_value(x):
1184+
if pd.isna(x):
1185+
return ""
1186+
1187+
return "\u200b" + str(x)
1188+
11811189
for col in df_copy.columns:
1182-
# 只处理数值类型的列
1183-
if pd.api.types.is_numeric_dtype(df_copy[col]):
1184-
try:
1185-
df_copy[col] = df_copy[col].astype(str)
1186-
except Exception as e:
1187-
print(f"列 {col} 转换失败: {e}")
1188-
# 如果转换失败,保持原样
1189-
continue
1190+
df_copy[col] = df_copy[col].apply(format_value)
11901191

11911192
return df_copy
11921193

0 commit comments

Comments
 (0)