JL (#help): magpie_image through a Volcengine Ark provider came back
watermarked, edits went as a multipart form, and n and a ratio size
were sent as for OpenAI. Checked against Ark's own doc, 图片生成 API
(https://www.volcengine.com/docs/82379/1541523, read 2026-10-15):
- watermark is a boolean, default true ("AI 生成" in the corner);
- image is a string or string[], a URL or data:image/<fmt>;base64,...;
there is no edits endpoint, it goes to /images/generations;
- there is no n field (one image, or a group the model sizes itself
under sequential_image_generation, which 5.0 pro/flash refuse);
- size is a tier (1K/1.5K/2K/3K/4K by model) or WIDTHxHEIGHT; a ratio
is to be described in the prompt. The doc lists each ratio's 2K WxH;
4.0/4.5/5.0 lite's 2K table is inside every model's pixel range (5.0
pro's max 2048x2048x1.1025 too), so it is the one used;
- no quality, no mask; output_format png/jpeg and background
transparent exist (5.0 models) and are passed when asked.
drawArk (draw_volcengine.go), reached from drawImages for any
ark.*.volces.com base (the preset's plans or a pay-as-you-go provider
added by hand): JSON body with watermark false and b64_json, images in
image, one request per image through eachDrawing, a listed ratio sent
as its 2K size and any other ratio a 400 saying to put it in the
prompt or give WxH, before Ark is asked. A mask is a 400.
Not tried against the real Ark API (no Ark key here); the fake server
follows the doc's request and answer shapes.
Run (worktree at origin/main dd25997a4, go1.26.8, -tags nogui -p 1):
- go test -run 'TestArk|TestIsArk' ./internal/gateway: ok. With the
isArk branch in drawImages removed by hand: TestArkDrawsUnmarkedOncePerImage
(one image, no watermark field), TestArkEditSendsImagesAsJSON (404 at
/images/edits) and TestArkSizes (ratio sent, 200) red; restored, green.
- go test -run 'Image|Draw|Ark|ModelScope|Drawing|WorkBuddyDraw|Video'
./internal/gateway: ok (74.5s).
- go test -run TestLoopGuardLeavesOrdinaryRepliesAlone (reads the
docs): ok.
- gofmt -l internal/gateway: clean. go vet -tags nogui ./internal/gateway
for GOOS=darwin, linux, windows: clean.
- Not run: the full suite.
Full suite: SUITE_LINE
126 lines
4.4 KiB
Go
126 lines
4.4 KiB
Go
package gateway
|
|
|
|
// Volcengine Ark's images API (火山方舟 图片生成 API, POST
|
|
// <base>/images/generations, https://www.volcengine.com/docs/82379/1541523)
|
|
// is OpenAI-shaped in its answer but not in what it takes:
|
|
//
|
|
// - watermark defaults to true: every image gets an "AI 生成" mark in its
|
|
// corner unless the request says false;
|
|
// - reference images go in the same JSON body, as image (a string, or an
|
|
// array for several), each a URL or a data:image/<fmt>;base64 URL; there
|
|
// is no edits endpoint and no multipart;
|
|
// - there is no n: a request draws one image (or a group the model sizes
|
|
// itself, which magpie doesn't ask for);
|
|
// - size is a tier ("1K", "2K", "4K"…) or WIDTHxHEIGHT, never a ratio:
|
|
// the doc says to give a ratio in the prompt, and lists the WxH each
|
|
// ratio maps to at 2K;
|
|
// - there is no quality and no mask.
|
|
//
|
|
// So magpie draws there as the doc says: watermark false, the images as
|
|
// JSON, one request per image asked for, and a ratio sent as the doc's 2K
|
|
// size for it, or turned away with what to do instead (JL on Discord).
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// isArk is a provider at Volcengine Ark (ark.<region>.volces.com): the
|
|
// preset's plans or a provider added by hand on pay-as-you-go.
|
|
var isArk = func(p provider.Provider) bool {
|
|
for _, base := range []string{p.Base(provider.Chat), p.Base(provider.Responses)} {
|
|
if h := provider.HostOf(base); strings.HasPrefix(h, "ark.") && strings.HasSuffix(h, ".volces.com") {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// arkRatioSizes are the 2K sizes Ark's doc lists for each ratio (Seedream
|
|
// 4.0, 4.5 and 5.0 lite's table). Each is in every Seedream model's pixel
|
|
// range, 5.0 pro's [1280x720, 2048x2048x1.1025] included.
|
|
var arkRatioSizes = map[string]string{
|
|
"1:1": "2048x2048", "4:3": "2304x1728", "3:4": "1728x2304",
|
|
"16:9": "2848x1600", "9:16": "1600x2848", "3:2": "2496x1664",
|
|
"2:3": "1664x2496", "21:9": "3136x1344",
|
|
}
|
|
|
|
// arkSize is size as Ark takes it: a tier or WIDTHxHEIGHT as given, a
|
|
// ratio its doc lists as that ratio's 2K size, "" for the model's own.
|
|
// Another ratio is an error that says what to send instead.
|
|
func arkSize(size string) (string, error) {
|
|
size = strings.TrimSpace(size)
|
|
switch {
|
|
case size == "" || strings.EqualFold(size, "auto"):
|
|
return "", nil
|
|
case strings.Contains(size, ":"):
|
|
if s, ok := arkRatioSizes[strings.ReplaceAll(size, " ", "")]; ok {
|
|
return s, nil
|
|
}
|
|
return "", fmt.Errorf("Volcengine's images API takes no aspect ratio as size, and %q isn't one magpie can turn into a size (it can: 1:1, 4:3, 3:4, 16:9, 9:16, 3:2, 2:3, 21:9): describe the ratio in the prompt and leave size out, or give WIDTHxHEIGHT (e.g. 2048x2048)", size)
|
|
}
|
|
return size, nil
|
|
}
|
|
|
|
// arkBody is d as Ark's images API takes it, for one image.
|
|
func arkBody(model string, d drawing) ([]byte, error) {
|
|
if d.Mask != nil {
|
|
return nil, errors.New("Volcengine's images API takes no mask: say in the prompt which part of the image to change")
|
|
}
|
|
size, err := arkSize(d.Size)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req := map[string]any{"model": model, "prompt": d.Prompt, "watermark": false, "response_format": "b64_json"}
|
|
if size != "" {
|
|
req["size"] = size
|
|
}
|
|
switch f := strings.ToLower(d.Format); f {
|
|
case "png", "jpeg":
|
|
req["output_format"] = f
|
|
case "jpg":
|
|
req["output_format"] = "jpeg"
|
|
}
|
|
if strings.EqualFold(d.Background, "transparent") {
|
|
req["background"] = "transparent"
|
|
}
|
|
switch len(d.Images) {
|
|
case 0:
|
|
case 1:
|
|
req["image"] = d.Images[0].dataURL()
|
|
default:
|
|
var urls []string
|
|
for _, pic := range d.Images {
|
|
urls = append(urls, pic.dataURL())
|
|
}
|
|
req["image"] = urls
|
|
}
|
|
return json.Marshal(req)
|
|
}
|
|
|
|
// drawArk asks Ark's images API once for each of d's images.
|
|
func (s *Server) drawArk(ctx context.Context, p provider.Provider, model string, d drawing) (drawn, int, error) {
|
|
body, err := arkBody(model, d)
|
|
if err != nil {
|
|
return drawn{}, http.StatusBadRequest, err
|
|
}
|
|
url := strings.TrimRight(p.Base(provider.Chat), "/")
|
|
if url == "" {
|
|
url = strings.TrimRight(p.Base(provider.Responses), "/")
|
|
}
|
|
url += "/images/generations"
|
|
return s.eachDrawing(max(d.N, 1), func() (drawn, int, error) {
|
|
b, code, err := s.send(ctx, p, url, "application/json", body, true)
|
|
if err != nil {
|
|
return drawn{}, code, err
|
|
}
|
|
return readImagesAnswer(p, url, b, code)
|
|
})
|
|
}
|