You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
4.7 KiB
4.7 KiB
图片生成性能优化 - 使用 Base64 格式
日期: 2026-01-31
类型: 性能优化
影响范围: AI 图片生成功能
背景
在测试图片生成功能时,发现性能瓶颈:
- 总耗时:96 秒
- 其中文件下载耗时:83 秒(从 Azure Blob Storage 下载)
问题分析
原有流程
1. 调用 DALL-E 3 API(10 秒)
2. 从 Azure Blob Storage 下载图片(83 秒)← 瓶颈
3. 上传到 MinIO(1 秒)
瓶颈原因
- Azure Blob Storage 服务器在国外
- 网络延迟高
- 图片文件较大(3.16 MB)
优化方案
使用 response_format="b64_json" 参数,直接在 API 响应中获取 Base64 编码的图片数据。
优化后流程
1. 调用 DALL-E 3 API(返回 Base64)(10 秒)
2. 直接上传到 MinIO(1 秒)
实现细节
1. 修改 AIHubMixProvider
文件: server/app/services/ai_providers/aihubmix_provider.py
async def generate_image(
self,
prompt: str,
width: int = 1024,
height: int = 1024,
**kwargs
) -> Dict[str, Any]:
response_format = kwargs.get('response_format', 'b64_json') # 默认使用 b64_json
response = await self.client.images.generate(
model=self.model_name,
prompt=prompt,
size=size,
quality=quality,
style=style,
response_format=response_format, # 指定响应格式
n=1
)
# 根据 response_format 处理响应
if response_format == 'b64_json' and hasattr(image_data, 'b64_json'):
b64_json = image_data.b64_json
url = None
elif hasattr(image_data, 'url'):
url = image_data.url
b64_json = None
return {
'url': url,
'b64_json': b64_json,
'metadata': {...}
}
2. 添加 Base64 上传支持
文件: server/app/tasks/ai_tasks.py
新增函数
async def _upload_file_from_bytes(
file_data: bytes,
filename: str,
content_type: str,
category: str,
user_id: str
) -> Dict[str, Any]:
"""直接从字节数据上传文件到自有 OSS(用于 Base64 数据)"""
async with async_session_maker_temp() as session:
file_storage = FileStorageService(session)
metadata = await file_storage.upload_file(
file_content=file_data,
filename=filename,
content_type=content_type,
category=category,
user_id=UUID(user_id)
)
await session.commit()
return {
'file_url': metadata.file_url,
'file_size': metadata.file_size,
'checksum': metadata.checksum,
...
}
修改图片生成任务
# 5. 处理文件(URL 或 Base64)
if result.get('b64_json'):
# 使用 Base64 数据直接上传
import base64
file_data = base64.b64decode(result['b64_json'])
file_metadata = await _upload_file_from_bytes(
file_data=file_data,
filename=f"image_{job_id}.png",
content_type='image/png',
category='ai-generated/images',
user_id=user_id
)
result.update(file_metadata)
elif result.get('url'):
# 从 URL 下载并上传(备用方案)
file_metadata = await _download_and_upload_file(...)
result.update(file_metadata)
性能对比
| 阶段 | 优化前 | 优化后 | 提升 |
|---|---|---|---|
| 图片生成 | 10 秒 | 10 秒 | - |
| 文件下载 | 83 秒 | 0 秒 | ✅ 消除 |
| 文件上传 | 1 秒 | 1 秒 | - |
| 总耗时 | 96 秒 | 16 秒 | 83% ↓ |
测试结果
docker exec jointo-server-app python scripts/test_ai_image_generation_e2e.py
结果:
- ✅ 任务创建成功
- ✅ 图片生成完成(16 秒)
- ✅ 文件上传到 MinIO 成功
- ✅ 积分正确扣除
修改的文件
-
✅
server/app/services/ai_providers/aihubmix_provider.py- 添加
response_format参数支持 - 默认使用
b64_json格式
- 添加
-
✅
server/app/tasks/ai_tasks.py- 新增
_upload_file_from_bytes()函数 - 修改
generate_image_task()支持 Base64 数据
- 新增
影响
- ✅ 图片生成速度提升 83%
- ✅ 降低网络依赖(不再依赖 Azure Blob Storage 下载速度)
- ✅ 用户体验显著提升
- ✅ 保留 URL 下载作为备用方案(兼容性)
后续优化
可以考虑对其他 AI 功能应用类似优化:
- 视频生成:如果支持 Base64,可以应用相同优化
- 语音生成:TTS 通常返回较小的音频文件,Base64 更合适
- 字幕生成:文本数据,Base64 非常合适