Skip to content

fix: app-chat generation images add prefix prompt#396

Open
keung67 wants to merge 2 commits intochenyme:mainfrom
keung67:fix-generation-images-add-prefix-prompt
Open

fix: app-chat generation images add prefix prompt#396
keung67 wants to merge 2 commits intochenyme:mainfrom
keung67:fix-generation-images-add-prefix-prompt

Conversation

@keung67
Copy link
Contributor

@keung67 keung67 commented Mar 24, 2026

Summary

為圖片生成模型 (grok-imagine-1.0grok-imagine-1.0-fast) 自動加上 "generation image: " 的提示詞前綴。

這修復了當使用者透過 app-chat 的 REST 方式 (/chat/completions) 或是透過前端 /v1/function/imagine 呼叫生圖模型時,因為缺乏明確的圖像生成指令,導致 Grok 模型誤判為一般聊天對話,最終「只生成文字而不生成圖片」的問題。

同樣在grok.com官網測試對話,若不加類似"生成圖片"的提示詞,則被當成一般對話生成文字而非生圖。

Changes

  • 功能新增
  • Bug 修复
  • 重构/清理
  • 文档更新
  • 其他(请说明)

具體修改:

  • 在 app/api/v1/chat.py 的 REST /chat/completions API 中,針對圖片模型攔截並自動在 prompt 開頭加上 "generation image: " 前綴。
  • 在 app/api/v1/function/imagine.py 的 WebSocket (_run) 和 SSE (event_stream) 路徑中同步加上相同的邏輯補丁。

Related Issues

Verification

  • 本地运行验证
  • 单元/集成测试
  • Docker 构建通过
  • 未验证(请说明原因)

验证说明:

- 啟動後端服務。
- 使用支援 REST API 的 Chat 客戶端 (如 Cherry Studio/NextChat/LobeChat) 選擇 `grok-imagine-1.0-fast` 或 `grok-imagine-1.0` 發送隨意文字(例:「一隻小狗」)。
- 確認終端機能正常發送包含 "generation image:" 的 prompt,並且對話能成功返回 base64 或 URL 圖片而不再是一般文字回覆。
- 測試前端 Imagine 的 WebSocket/SSE 畫廊功能,確認生圖功能正常。

JinchengGao-Infty and others added 2 commits March 24, 2026 22:55
## Problem

Grok silently updated their web API:
1. Image generation via `app-chat` with explicit `modelName`/`modelMode`
   no longer triggers image generation - Grok returns text instead
2. Image results moved from `modelResponse.generatedImageUrls` to
   `cardAttachment.jsonData.image_chunk` format
3. Image editing uses a different card type (`render_edited_image`)
   than generation (`render_generated_image`)

This broke both `/v1/images/generations` and `/v1/images/edits`.

## Root Cause

Captured browser network requests and compared with grok2api payloads:
- Browser uses `modeId: "auto"` without `modelName`/`modelMode`
- Browser sends explicit `toolOverrides` disabling search tools
- Browser sends `disableMemory: false, temporary: false` for edits

## Fix

**app_chat.py**: `build_payload()` supports `model=None` — omits
`modelName`/`modelMode` fields, allowing `modeId`-based routing.

**image.py**: `_stream_app_chat()` and `_collect_app_chat()` now use
`model=None, mode=None` with `modeId: "auto"` in request_overrides.

**image_edit.py**:
- `ImageStreamProcessor` and `ImageCollectProcessor` parse new
  `cardAttachment` format with `image_chunk.imageUrl` (progress >= 100)
- Match both `render_generated_image` and `render_edited_image` types
- Image URL: `https://assets.grok.com/{image_chunk.imageUrl}`
- Edit path uses `disableMemory=false, temporary=false` and
  browser-matching `toolOverrides` to prevent search fallback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants