OpenAI's images edit takes `markup`, a transparent PNG laid over the
first image to steer the edit (JSON image_url, or a multipart file part;
never beside a mask). magpie read only the mask: a JSON markup stopped in
drawing.Extra, a multipart one was dropped, and neither the ChatGPT
backend's JSON edit nor the plain multipart edit carried it, so an edit
with a markup came back 200 with the layer left out (#5364, kongjianguan,
with the probe and the three places).
drawing has a Markup beside Mask, read from JSON (markup.image_url) and
from a multipart `markup` part, and sent where the mask is: as
{"image_url": data URL} on the ChatGPT/SenseNova JSON edit and as its own
part on the multipart edit. A markup that can't be read, or one sent with
a mask, is a 400 before anything is asked. OpenRouter, Volcengine Ark and
DashScope, which turn a mask away, turn a markup away by name too.
Patch from ggbdpq (mbox sha256 b591c6ad499789c5, matches the issue).
Changed in review:
- "markup" joins drawingRead, so it no longer also rides Extra; on Ark
the raw field went out as an Ark field.
- Ark and DashScope refuse a markup as they refuse a mask; the patch
refused it on OpenRouter only.
- A mask with a markup is a 400 (OpenAI's docs: "Cannot be combined
with mask").
- Tests rewritten: the patch's broken-markup test posted to a server
with no "art" provider, so it would have been a 400 for the unknown
model with or without the fix; the Codex test kept its upstream in a
package global. They now check the bytes that reach the vendor, the
multipart-in case, the mask clash and the three refusals.
Run on mote (-tags nogui, the package's TestMain sandbox):
- TestCodexEditSendsTheMarkup, TestEditSendsTheMarkup/{json,multipart},
TestEditWithAMarkupItCantSendIsRefused/{not base64,beside a mask},
TestMarkupRefusedWhereNoLayerIsTaken: ok.
- Broken by hand: draw.go, draw_dashscope.go, draw_volcengine.go back to
cff530079 (Markup field kept so the tests build): all four FAIL ("the
ChatGPT edit's markup is \"\"", "kept as an extra field too", "the
markup layer never reached the vendor whole", "not base64: 200, want
400", "beside a mask: 200", "Ark: <nil>", "DashScope: <nil>",
"OpenRouter: 502 ... want 400").
- -race -run 'Draw|Image|Edit|Ark|DashScope|OpenRouter|Markup|Mask|Volc|
Seedream|ModelScope|SenseNova' ./internal/gateway: ok 108s.
- go vet -tags nogui, GOOS=linux go vet -tags nogui, GOOS=windows go vet
of ./internal/gateway: clean. gofmt -l: clean.
Not tried: a real ChatGPT account's gpt-image edit with a markup (none
here), so whether that backend applies the field, or refuses it, is
unknown; OpenAI's public docs list markup for GPT Image 2.5. Not
changed: routes that drop a mask today (Grok, ModelScope, chat-model and
Gemini draws, WorkBuddy) drop a markup the same way. The full suite is
run after the push.
Reported-by: kongjianguan
Co-authored-by: ggbdpq <ggbdpq@gmail.com>
178 lines
6.0 KiB
Go
178 lines
6.0 KiB
Go
package gateway
|
|
|
|
// DashScope (Alibaba Cloud Model Studio, Bailian, the Qwen AI platform and
|
|
// Qwen Cloud, pay as you go and Token Plan alike) draws its Qwen-Image and
|
|
// Wan 2.7 image models on its own API, not the OpenAI images API: a POST to
|
|
// <host>/api/v1/services/aigc/multimodal-generation/generation, Bearer the
|
|
// same key, of
|
|
//
|
|
// {"model": …, "input": {"messages": [{"role": "user", "content":
|
|
// [{"image": <URL or data URL>}…, {"text": <prompt>}]}]},
|
|
// "parameters": {"n": …, "size": "W*H", "watermark": false}}
|
|
//
|
|
// answered with the images in output.choices[*].message.content[*].image,
|
|
// each a URL kept for 24 hours (docs.qwencloud.com, "Qwen-Image text-to-
|
|
// image" and the Token Plan's "Integrate multimodal generation"), the only
|
|
// API documented for them. magpie sent them to the OpenAI images API at
|
|
// /compatible-mode/v1, and on its 404 to chat completions, whose answer has
|
|
// no image magpie reads: "qwen-image-3.0-pro drew nothing" (judi0713 on
|
|
// Discord; which of the two answered is not seen from here).
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// dashScopeDraws is whether model draws on DashScope's multimodal-
|
|
// generation API: the Qwen-Image models (qwen-image-3.0-pro, qwen-image-
|
|
// edit-plus…) and Wan 2.7's image ones. Older Wan text-to-image models
|
|
// draw on another, asynchronous API and are left as they were.
|
|
func dashScopeDraws(p provider.Provider, model string) bool {
|
|
m := strings.ToLower(model)
|
|
return dashScope(p) && (strings.HasPrefix(m, "qwen-image") || strings.HasPrefix(m, "wan2.7-image"))
|
|
}
|
|
|
|
// dashScopeRoot is the host's root the native API is under: the base given
|
|
// with its compatible-mode, Anthropic or native path cut off.
|
|
func dashScopeRoot(p provider.Provider) string {
|
|
base := strings.TrimRight(p.Base(provider.Chat), "/")
|
|
if base == "" {
|
|
base = strings.TrimRight(p.Base(provider.Anthropic), "/")
|
|
}
|
|
for _, cut := range []string{"/compatible-mode/", "/apps/", "/api/v1"} {
|
|
if i := strings.Index(base, cut); i >= 0 {
|
|
return base[:i]
|
|
}
|
|
}
|
|
return base
|
|
}
|
|
|
|
// dashScopeSize is size as DashScope takes it, W*H, or "" for the model's
|
|
// own pick. A ratio is an error saying what to send instead.
|
|
func dashScopeSize(size string) (string, error) {
|
|
size = strings.TrimSpace(size)
|
|
switch {
|
|
case size == "" || strings.EqualFold(size, "auto"):
|
|
return "", nil
|
|
case strings.Contains(size, ":"):
|
|
return "", fmt.Errorf("DashScope's image models take no aspect ratio as size: give WIDTHxHEIGHT (e.g. 1328x1328, at most 2048x2048 in pixels), or leave size out for the model to pick one from the prompt")
|
|
}
|
|
return strings.ReplaceAll(strings.ToLower(size), "x", "*"), nil
|
|
}
|
|
|
|
// dashScopeBody is d as the multimodal-generation API takes it.
|
|
func dashScopeBody(model string, d drawing) ([]byte, error) {
|
|
if d.Mask != nil {
|
|
return nil, errors.New("DashScope's image models take no mask: say in the prompt which part of the image to change")
|
|
}
|
|
if d.Markup != nil {
|
|
return nil, errors.New("DashScope's image models take no markup layer: say in the prompt what to change")
|
|
}
|
|
size, err := dashScopeSize(d.Size)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var content []map[string]string
|
|
for _, pic := range d.Images {
|
|
content = append(content, map[string]string{"image": pic.dataURL()})
|
|
}
|
|
content = append(content, map[string]string{"text": d.Prompt})
|
|
params := map[string]any{"watermark": false}
|
|
if d.N > 1 {
|
|
params["n"] = d.N
|
|
}
|
|
if size != "" {
|
|
params["size"] = size
|
|
}
|
|
return json.Marshal(map[string]any{
|
|
"model": model,
|
|
"input": map[string]any{"messages": []map[string]any{{"role": "user", "content": content}}},
|
|
"parameters": params,
|
|
})
|
|
}
|
|
|
|
func (s *Server) drawDashScope(ctx context.Context, p provider.Provider, model string, d drawing) (drawn, int, error) {
|
|
body, err := dashScopeBody(model, d)
|
|
if err != nil {
|
|
return drawn{}, http.StatusBadRequest, err
|
|
}
|
|
url := dashScopeRoot(p) + "/api/v1/services/aigc/multimodal-generation/generation"
|
|
b, code, err := s.send(ctx, p, url, "application/json", body, true)
|
|
if err != nil {
|
|
if said := dashScopeSaid(b); said != "" {
|
|
err = fmt.Errorf("%s: %d %s: %s", provider.HostOf(url), code, http.StatusText(code), said)
|
|
}
|
|
return drawn{}, code, err
|
|
}
|
|
return readDashScopeAnswer(url, b, code)
|
|
}
|
|
|
|
// dashScopeSaid is a DashScope error's words, {"code", "message"}, or "".
|
|
func dashScopeSaid(b []byte) string {
|
|
var e struct {
|
|
Code string `json:"code"`
|
|
Message string `json:"message"`
|
|
}
|
|
if json.Unmarshal(b, &e) != nil || e.Code == "" && e.Message == "" {
|
|
return ""
|
|
}
|
|
if e.Code == "" {
|
|
return e.Message
|
|
}
|
|
return e.Message + " (" + e.Code + ")"
|
|
}
|
|
|
|
// readDashScopeAnswer takes every image of every choice; an answer with
|
|
// none says what DashScope said, or how it ended.
|
|
func readDashScopeAnswer(url string, b []byte, code int) (drawn, int, error) {
|
|
var res struct {
|
|
Output struct {
|
|
Choices []struct {
|
|
FinishReason string `json:"finish_reason"`
|
|
Message struct {
|
|
Content []struct {
|
|
Image string `json:"image"`
|
|
Text string `json:"text"`
|
|
} `json:"content"`
|
|
} `json:"message"`
|
|
} `json:"choices"`
|
|
} `json:"output"`
|
|
}
|
|
if err := json.Unmarshal(b, &res); err != nil {
|
|
return drawn{}, 502, fmt.Errorf("%s's answer isn't DashScope's: %v", provider.HostOf(url), err)
|
|
}
|
|
var out drawn
|
|
var texts, ends []string
|
|
for _, c := range res.Output.Choices {
|
|
for _, part := range c.Message.Content {
|
|
switch {
|
|
case part.Image != "":
|
|
out.Images = append(out.Images, picture{URL: part.Image})
|
|
case strings.TrimSpace(part.Text) != "":
|
|
texts = append(texts, part.Text)
|
|
}
|
|
}
|
|
if c.FinishReason != "" && c.FinishReason != "stop" {
|
|
ends = append(ends, c.FinishReason)
|
|
}
|
|
}
|
|
out.Text = strings.Join(texts, "\n")
|
|
if len(out.Images) == 0 {
|
|
said := dashScopeSaid(b)
|
|
if said == "" {
|
|
said = out.Text
|
|
}
|
|
if said == "" && len(ends) > 0 {
|
|
said = "it ended with " + strings.Join(ends, ", ")
|
|
}
|
|
return out, saidStatus(said), fmt.Errorf("%s answered with no image%s", provider.HostOf(url), vendorSaid(said))
|
|
}
|
|
return out, code, nil
|
|
}
|