Skip to content

Commit

Permalink
Support new fields for /v1/images/generation API (sashabaranov#530)
Browse files Browse the repository at this point in the history
* add support for new image/generation api

* fix one lint

* add revised_prompt to response

* fix lints

* add CreateImageQualityStandard
  • Loading branch information
mikecutalo authored Nov 8, 2023
1 parent a20eb08 commit a0159ad
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
26 changes: 24 additions & 2 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,39 @@ const (
CreateImageSize256x256 = "256x256"
CreateImageSize512x512 = "512x512"
CreateImageSize1024x1024 = "1024x1024"
// dall-e-3 supported only.
CreateImageSize1792x1024 = "1792x1024"
CreateImageSize1024x1792 = "1024x1792"
)

const (
CreateImageResponseFormatURL = "url"
CreateImageResponseFormatB64JSON = "b64_json"
)

const (
CreateImageModelDallE2 = "dall-e-2"
CreateImageModelDallE3 = "dall-e-3"
)

const (
CreateImageQualityHD = "hd"
CreateImageQualityStandard = "standard"
)

const (
CreateImageStyleVivid = "vivid"
CreateImageStyleNatural = "natural"
)

// ImageRequest represents the request structure for the image API.
type ImageRequest struct {
Prompt string `json:"prompt,omitempty"`
Model string `json:"model,omitempty"`
N int `json:"n,omitempty"`
Quality string `json:"quality,omitempty"`
Size string `json:"size,omitempty"`
Style string `json:"style,omitempty"`
ResponseFormat string `json:"response_format,omitempty"`
User string `json:"user,omitempty"`
}
Expand All @@ -39,8 +60,9 @@ type ImageResponse struct {

// ImageResponseDataInner represents a response data structure for image API.
type ImageResponseDataInner struct {
URL string `json:"url,omitempty"`
B64JSON string `json:"b64_json,omitempty"`
URL string `json:"url,omitempty"`
B64JSON string `json:"b64_json,omitempty"`
RevisedPrompt string `json:"revised_prompt,omitempty"`
}

// CreateImage - API call to create an image. This is the main endpoint of the DALL-E API.
Expand Down
9 changes: 8 additions & 1 deletion image_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ func TestImages(t *testing.T) {
defer teardown()
server.RegisterHandler("/v1/images/generations", handleImageEndpoint)
_, err := client.CreateImage(context.Background(), openai.ImageRequest{
Prompt: "Lorem ipsum",
Prompt: "Lorem ipsum",
Model: openai.CreateImageModelDallE3,
N: 1,
Quality: openai.CreateImageQualityHD,
Size: openai.CreateImageSize1024x1024,
Style: openai.CreateImageStyleVivid,
ResponseFormat: openai.CreateImageResponseFormatURL,
User: "user",
})
checks.NoError(t, err, "CreateImage error")
}
Expand Down

0 comments on commit a0159ad

Please sign in to comment.