feat(imagegen): optional per-request generation settings
Add Steps, CFGScale, NegativePrompt, Sampler, Seed to imagegen.Request (pointer/empty = leave the backend's per-model default), with mirror options, and forward them in the llamaswap wire payload as the stable-diffusion.cpp fields (steps/cfg_scale/negative_prompt/ sample_method/seed). Unset fields are omitted so sd-server keeps its baked defaults. Lets callers (e.g. mort drawbots) override only what they explicitly set.
This commit is contained in:
@@ -42,3 +42,13 @@ asked for "a new ai image interface as opposed to llm".
|
|||||||
callers (additive fields/options).
|
callers (additive fields/options).
|
||||||
- No health/failover for image models yet; if needed it can be added as a
|
- No health/failover for image models yet; if needed it can be added as a
|
||||||
separate chain type rather than retrofitting the chat chain.
|
separate chain type rather than retrofitting the chat chain.
|
||||||
|
|
||||||
|
## Update — optional per-request settings
|
||||||
|
|
||||||
|
`Request` gained additive optional overrides — `Steps *int`, `CFGScale *float64`,
|
||||||
|
`NegativePrompt string`, `Sampler string`, `Seed *int64` — with mirror options
|
||||||
|
(`WithSteps`, …). nil/"" means "leave the backend's per-model default", so the v1
|
||||||
|
contract is unchanged for callers that don't set them. `provider/llamaswap`
|
||||||
|
forwards them to sd-server as `steps`/`cfg_scale`/`negative_prompt`/`sample_method`/
|
||||||
|
`seed` (omitempty). This realizes the "seeds/steps … additive fields" note above;
|
||||||
|
img2img/masks/streaming remain deferred.
|
||||||
|
|||||||
@@ -38,6 +38,29 @@ type Request struct {
|
|||||||
// Size is the requested resolution, e.g. "512x512" or "1024x1024";
|
// Size is the requested resolution, e.g. "512x512" or "1024x1024";
|
||||||
// "" = provider default.
|
// "" = provider default.
|
||||||
Size string
|
Size string
|
||||||
|
|
||||||
|
// The fields below are optional per-request overrides. Their zero value
|
||||||
|
// (nil pointer or empty string) means "leave the backend's own default" —
|
||||||
|
// for stable-diffusion.cpp that is the per-model default baked into the
|
||||||
|
// llama-swap launch flags. A caller overrides only what it explicitly sets.
|
||||||
|
|
||||||
|
// Steps is the number of diffusion steps; nil = backend default.
|
||||||
|
Steps *int
|
||||||
|
|
||||||
|
// CFGScale is the classifier-free-guidance scale; nil = backend default.
|
||||||
|
// Architecture-sensitive (SDXL likes ~7, Flux wants 1), so prefer leaving
|
||||||
|
// it nil unless the caller knows the target model.
|
||||||
|
CFGScale *float64
|
||||||
|
|
||||||
|
// NegativePrompt steers generation away from concepts; "" = none.
|
||||||
|
NegativePrompt string
|
||||||
|
|
||||||
|
// Sampler selects the sampling method (e.g. "euler", "euler_a");
|
||||||
|
// "" = backend default.
|
||||||
|
Sampler string
|
||||||
|
|
||||||
|
// Seed fixes the RNG seed for reproducible output; nil = random.
|
||||||
|
Seed *int64
|
||||||
}
|
}
|
||||||
|
|
||||||
// Result is the canonical image-generation result.
|
// Result is the canonical image-generation result.
|
||||||
@@ -60,6 +83,21 @@ func WithN(n int) Option { return func(r *Request) { r.N = n } }
|
|||||||
// WithSize sets the requested resolution (e.g. "1024x1024").
|
// WithSize sets the requested resolution (e.g. "1024x1024").
|
||||||
func WithSize(size string) Option { return func(r *Request) { r.Size = size } }
|
func WithSize(size string) Option { return func(r *Request) { r.Size = size } }
|
||||||
|
|
||||||
|
// WithSteps overrides the number of diffusion steps.
|
||||||
|
func WithSteps(n int) Option { return func(r *Request) { r.Steps = &n } }
|
||||||
|
|
||||||
|
// WithCFGScale overrides the classifier-free-guidance scale.
|
||||||
|
func WithCFGScale(s float64) Option { return func(r *Request) { r.CFGScale = &s } }
|
||||||
|
|
||||||
|
// WithNegativePrompt sets a negative prompt.
|
||||||
|
func WithNegativePrompt(s string) Option { return func(r *Request) { r.NegativePrompt = s } }
|
||||||
|
|
||||||
|
// WithSampler overrides the sampling method (e.g. "euler", "euler_a").
|
||||||
|
func WithSampler(s string) Option { return func(r *Request) { r.Sampler = s } }
|
||||||
|
|
||||||
|
// WithSeed fixes the RNG seed for reproducible output.
|
||||||
|
func WithSeed(seed int64) Option { return func(r *Request) { r.Seed = &seed } }
|
||||||
|
|
||||||
// Apply returns a copy of the request with all options applied. Providers call
|
// Apply returns a copy of the request with all options applied. Providers call
|
||||||
// this once at the top of Generate.
|
// this once at the top of Generate.
|
||||||
func (r Request) Apply(opts ...Option) Request {
|
func (r Request) Apply(opts ...Option) Request {
|
||||||
|
|||||||
@@ -27,14 +27,23 @@ type imageModel struct {
|
|||||||
id string
|
id string
|
||||||
}
|
}
|
||||||
|
|
||||||
// imageRequest is the OpenAI /v1/images/generations request shape. We always
|
// imageRequest is the OpenAI /v1/images/generations request shape, plus the
|
||||||
// request b64_json so the bytes come back inline (no second fetch).
|
// stable-diffusion.cpp extras llama-swap forwards to sd-server. We always
|
||||||
|
// request b64_json so the bytes come back inline (no second fetch). The
|
||||||
|
// optional fields are pointers/omitempty so an unset value is omitted entirely
|
||||||
|
// and sd-server falls back to the model's own default (a field name a given
|
||||||
|
// sd-server build doesn't recognize is simply ignored — harmless).
|
||||||
type imageRequest struct {
|
type imageRequest struct {
|
||||||
Model string `json:"model"`
|
Model string `json:"model"`
|
||||||
Prompt string `json:"prompt"`
|
Prompt string `json:"prompt"`
|
||||||
N int `json:"n,omitempty"`
|
N int `json:"n,omitempty"`
|
||||||
Size string `json:"size,omitempty"`
|
Size string `json:"size,omitempty"`
|
||||||
ResponseFormat string `json:"response_format"`
|
ResponseFormat string `json:"response_format"`
|
||||||
|
Steps *int `json:"steps,omitempty"`
|
||||||
|
CFGScale *float64 `json:"cfg_scale,omitempty"`
|
||||||
|
NegativePrompt string `json:"negative_prompt,omitempty"`
|
||||||
|
SampleMethod string `json:"sample_method,omitempty"`
|
||||||
|
Seed *int64 `json:"seed,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type imageResponse struct {
|
type imageResponse struct {
|
||||||
@@ -61,6 +70,11 @@ func (m *imageModel) Generate(ctx context.Context, req imagegen.Request, opts ..
|
|||||||
N: req.N,
|
N: req.N,
|
||||||
Size: req.Size,
|
Size: req.Size,
|
||||||
ResponseFormat: "b64_json",
|
ResponseFormat: "b64_json",
|
||||||
|
Steps: req.Steps,
|
||||||
|
CFGScale: req.CFGScale,
|
||||||
|
NegativePrompt: req.NegativePrompt,
|
||||||
|
SampleMethod: req.Sampler,
|
||||||
|
Seed: req.Seed,
|
||||||
}
|
}
|
||||||
|
|
||||||
var resp imageResponse
|
var resp imageResponse
|
||||||
|
|||||||
@@ -201,6 +201,48 @@ func TestImageGenerate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestImageGenerateSettings(t *testing.T) {
|
||||||
|
var gotBody map[string]any
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_ = json.NewDecoder(r.Body).Decode(&gotBody)
|
||||||
|
_, _ = w.Write([]byte(`{"created":1,"data":[{"b64_json":"` + onePixelPNG + `"}]}`))
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
p := New(WithBaseURL(srv.URL), WithHTTPClient(srv.Client()))
|
||||||
|
im, _ := p.ImageModel("sd")
|
||||||
|
|
||||||
|
// Unset overrides must be omitted entirely so sd-server keeps its own
|
||||||
|
// per-model defaults.
|
||||||
|
if _, err := im.Generate(context.Background(), imagegen.Request{Prompt: "x"}); err != nil {
|
||||||
|
t.Fatalf("Generate: %v", err)
|
||||||
|
}
|
||||||
|
for _, k := range []string{"steps", "cfg_scale", "negative_prompt", "sample_method", "seed"} {
|
||||||
|
if v, ok := gotBody[k]; ok {
|
||||||
|
t.Errorf("unset request sent %q = %v, want omitted", k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set overrides are forwarded with the sd-server-friendly field names.
|
||||||
|
gotBody = nil
|
||||||
|
_, err := im.Generate(context.Background(), imagegen.Request{Prompt: "x"},
|
||||||
|
imagegen.WithSteps(8),
|
||||||
|
imagegen.WithCFGScale(3.5),
|
||||||
|
imagegen.WithNegativePrompt("blurry"),
|
||||||
|
imagegen.WithSampler("euler"),
|
||||||
|
imagegen.WithSeed(42),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Generate: %v", err)
|
||||||
|
}
|
||||||
|
want := map[string]any{"steps": float64(8), "cfg_scale": 3.5, "negative_prompt": "blurry", "sample_method": "euler", "seed": float64(42)}
|
||||||
|
for k, w := range want {
|
||||||
|
if gotBody[k] != w {
|
||||||
|
t.Errorf("%s = %v, want %v", k, gotBody[k], w)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestImageGenerateEmptyPrompt(t *testing.T) {
|
func TestImageGenerateEmptyPrompt(t *testing.T) {
|
||||||
p := New(WithBaseURL("http://example.invalid"))
|
p := New(WithBaseURL("http://example.invalid"))
|
||||||
im, _ := p.ImageModel("sd")
|
im, _ := p.ImageModel("sd")
|
||||||
|
|||||||
Reference in New Issue
Block a user