Skip to content

images/edits API support azure endpoint #1008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ var azureDeploymentsEndpoints = []string{
"/audio/translations",
"/audio/speech",
"/images/generations",
"/images/edits",
"/images/variations",
}

// fullURL returns full URL for request.
Expand Down
54 changes: 38 additions & 16 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strconv"
Expand Down Expand Up @@ -69,6 +70,10 @@
CreateImageOutputFormatWEBP = "webp"
)

const (
minFileTypeLength = 6 // "image/"
)

// ImageRequest represents the request structure for the image API.
type ImageRequest struct {
Prompt string `json:"prompt,omitempty"`
Expand Down Expand Up @@ -134,26 +139,41 @@

// ImageEditRequest represents the request structure for the image API.
type ImageEditRequest struct {
Image io.Reader `json:"image,omitempty"`
Mask io.Reader `json:"mask,omitempty"`
Prompt string `json:"prompt,omitempty"`
Model string `json:"model,omitempty"`
N int `json:"n,omitempty"`
Size string `json:"size,omitempty"`
ResponseFormat string `json:"response_format,omitempty"`
Quality string `json:"quality,omitempty"`
User string `json:"user,omitempty"`
Images []io.Reader `json:"images,omitempty"`
Mask io.Reader `json:"mask,omitempty"`
Prompt string `json:"prompt,omitempty"`
Model string `json:"model,omitempty"`
N int `json:"n,omitempty"`
Size string `json:"size,omitempty"`
ResponseFormat string `json:"response_format,omitempty"`
Quality string `json:"quality,omitempty"`
User string `json:"user,omitempty"`
}

// CreateEditImage - API call to create an image. This is the main endpoint of the DALL-E API.
func (c *Client) CreateEditImage(ctx context.Context, request ImageEditRequest) (response ImageResponse, err error) {
body := &bytes.Buffer{}
builder := c.createFormBuilder(body)

// image, filename is not required
err = builder.CreateFormFileReader("image", request.Image, "")
if err != nil {
return
for i, img := range request.Images {
// judge file type
var data []byte
data, err = io.ReadAll(img)
if err != nil {
return
}

Check warning on line 164 in image.go

View check run for this annotation

Codecov / codecov/patch

image.go#L163-L164

Added lines #L163 - L164 were not covered by tests
fileType := http.DetectContentType(data)
if len(fileType) < minFileTypeLength {
err = fmt.Errorf("invalid file type: %s", fileType)
return
}

Check warning on line 169 in image.go

View check run for this annotation

Codecov / codecov/patch

image.go#L167-L169

Added lines #L167 - L169 were not covered by tests
// get file extension
ext := fileType[minFileTypeLength:]
filename := fmt.Sprintf("%d.%s", i, ext)
err = builder.CreateFormFileReader("image", img, filename)
if err != nil {
return
}

Check warning on line 176 in image.go

View check run for this annotation

Codecov / codecov/patch

image.go#L175-L176

Added lines #L175 - L176 were not covered by tests
}

// mask, it is optional
Expand All @@ -180,9 +200,11 @@
return
}

err = builder.WriteField("response_format", request.ResponseFormat)
if err != nil {
return
if request.ResponseFormat != "" {
err = builder.WriteField("response_format", request.ResponseFormat)
if err != nil {
return
}

Check warning on line 207 in image.go

View check run for this annotation

Codecov / codecov/patch

image.go#L206-L207

Added lines #L206 - L207 were not covered by tests
}

err = builder.Close()
Expand Down
4 changes: 2 additions & 2 deletions image_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestImageEdit(t *testing.T) {
defer mask.Close()

_, err = client.CreateEditImage(context.Background(), openai.ImageEditRequest{
Image: origin,
Images: []io.Reader{origin},
Mask: mask,
Prompt: "There is a turtle in the pool",
N: 3,
Expand All @@ -122,7 +122,7 @@ func TestImageEditWithoutMask(t *testing.T) {
defer origin.Close()

_, err = client.CreateEditImage(context.Background(), openai.ImageEditRequest{
Image: origin,
Images: []io.Reader{origin},
Prompt: "There is a turtle in the pool",
N: 3,
Size: openai.CreateImageSize1024x1024,
Expand Down
Loading