From 300b03fdde6138c43b02e9a9c040083b578c5244 Mon Sep 17 00:00:00 2001 From: itismygo <167516635+itismygo@users.noreply.github.com> Date: Fri, 12 Dec 2025 02:26:29 +0000 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20grok-imagine-0.?= =?UTF-8?q?9=20=E7=BA=AF=E6=96=87=E6=9C=AC=E7=94=9F=E6=88=90=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 问题描述 当使用 grok-imagine-0.9 模型进行纯文本到图片生成时,会抛出 "list index out of range" 错误。 ## 根本原因 1. **模型配置错误**: grok-imagine-0.9 被错误地标记为视频模型 (`is_video_model: True`) 2. **数组访问bug**: 在没有图片附件时,代码尝试访问空列表的第一个元素 `payload.get("fileAttachments", [""])[0]` ## 修复内容 ### app/models/grok_models.py - 将 `is_video_model` 从 `True` 改为 `False` - 更新 description 为 "Image generation model powered by Grok" ### app/services/grok/client.py - 修复空列表访问bug,使用安全的访问方式: ```python file_attachments = payload.get("fileAttachments", []) ref_id = post_id or (file_attachments[0] if file_attachments else "") ``` ## 测试结果 ✅ 纯文本生成图片:成功 ✅ 文本对话:正常 ✅ 其他模型:不受影响 ## 相关信息 根据 xAI 官方文档,Grok Imagine 主要是图像生成模型(使用 Aurora 引擎),支持 text-to-image 功能。 --- app/models/grok_models.py | 4 ++-- app/services/grok/client.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/models/grok_models.py b/app/models/grok_models.py index 6fab827..fe15fc7 100644 --- a/app/models/grok_models.py +++ b/app/models/grok_models.py @@ -103,13 +103,13 @@ _MODEL_CONFIG: Dict[str, Dict[str, Any]] = { "cost": {"type": "low_cost", "multiplier": 1, "description": "计1次调用"}, "requires_super": False, "display_name": "Grok Imagine 0.9", - "description": "Video generation model powered by Grok", + "description": "Image generation model powered by Grok", "raw_model_path": "xai/grok-imagine-0.9", "default_temperature": 1.0, "default_max_output_tokens": 8192, "supported_max_output_tokens": 131072, "default_top_p": 0.95, - "is_video_model": True + "is_video_model": False } } diff --git a/app/services/grok/client.py b/app/services/grok/client.py index 456bdfb..277d640 100644 --- a/app/services/grok/client.py +++ b/app/services/grok/client.py @@ -188,7 +188,8 @@ class GrokClient: # 构建请求 headers = GrokClient._build_headers(token) if model == "grok-imagine-0.9": - ref_id = post_id or payload.get("fileAttachments", [""])[0] + file_attachments = payload.get("fileAttachments", []) + ref_id = post_id or (file_attachments[0] if file_attachments else "") if ref_id: headers["Referer"] = f"https://grok.com/imagine/{ref_id}" From eff701c485c484134ce7907f0ecff996f6ff5c2c Mon Sep 17 00:00:00 2001 From: itismygo <167516635+itismygo@users.noreply.github.com> Date: Fri, 12 Dec 2025 04:14:01 +0000 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20grok-imagine-0.?= =?UTF-8?q?9=20=E5=90=8C=E6=97=B6=E6=94=AF=E6=8C=81=E6=96=87=E7=94=9F?= =?UTF-8?q?=E5=9B=BE=E5=92=8C=E5=9B=BE=E7=94=9F=E8=A7=86=E9=A2=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 恢复 is_video_model: True 以支持视频生成 - 更新模型描述说明支持两种模式 - 保留空列表安全访问修复 根据 xAI 官方文档,grok-imagine-0.9 支持: 1. 文本 → 图片 (text-to-image) 2. 图片 + 文本 → 视频 (image-to-video) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- app/models/grok_models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/grok_models.py b/app/models/grok_models.py index fe15fc7..b067c54 100644 --- a/app/models/grok_models.py +++ b/app/models/grok_models.py @@ -103,13 +103,13 @@ _MODEL_CONFIG: Dict[str, Dict[str, Any]] = { "cost": {"type": "low_cost", "multiplier": 1, "description": "计1次调用"}, "requires_super": False, "display_name": "Grok Imagine 0.9", - "description": "Image generation model powered by Grok", + "description": "Image and video generation model. Supports text-to-image and image-to-video generation.", "raw_model_path": "xai/grok-imagine-0.9", "default_temperature": 1.0, "default_max_output_tokens": 8192, "supported_max_output_tokens": 131072, "default_top_p": 0.95, - "is_video_model": False + "is_video_model": True } }