Skip to content

Andrew-peng/go-dalle2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-dalle2

GitHub go.mod Go version GoDoc GitHub Workflow Status GitHub Workflow Status codecov GitHub

Unofficial Dalle-2 API golang client library

Setup

go-dalle2 is a simple client library, so with Go installed:

go get github.com/Andrew-peng/go-dalle2/dalle2

or

import "github.com/Andrew-peng/go-dalle2/dalle2"

Usage

For details about DALLE-2 (and api) visit the OpenAI documentation.

Instantiate client

Instantiate the client with a valid OpenAI API key

client, err := dalle2.MakeNewClientV1(apiKey)
if err != nil {
    log.Fatalf("Error initializing client: %s", err)
}

Creating images

resp, err := client.Create(
    context.Background(),
    "A skyline view of New York during the sunset, watercolor",
    dalle2.WithNumImages(1),
    dalle2.WithSize(dalle2.SMALL),
    dalle2.WithFormat(dalle2.URL),
)
if err != nil {
    log.Fatal(err)
}
for _, img := range resp.Data {
    fmt.Println("%s", img.Url)
}
Prompt Output
A skyline view of New York during the sunset, watercolor A skyline view of New York during the sunset, watercolor

Editing images

imgBytes := ...
maskBytes := ...
resp, err := client.Edit(
    context.Background(),
    imgBytes,
    maskBytes,
    "A cute baby sea otter wearing a large sombrero",
    dalle2.WithNumImages(1),
    dalle2.WithSize(dalle2.SMALL),
    dalle2.WithFormat(dalle2.URL),
)
if err != nil {
    log.Fatal(err)
}
for _, img := range resp.Data {
    fmt.Println("%s", img.Url)
}
Prompt Image Mask Output
A cute baby sea otter wearing a large sombrero otter mask A cute baby sea otter wearing a large sombrero

Creating variations

imgBytes := ...
resp, err := client.Edit(
    context.Background(),
    imgBytes,
    dalle2.WithNumImages(1),
    dalle2.WithSize(dalle2.SMALL),
    dalle2.WithFormat(dalle2.URL),
)
if err != nil {
    log.Fatal(err)
}
for _, img := range resp.Data {
    fmt.Println("%s", img.Url)
}
Image Output
otter output variation

License

go-dalle2 is distributed under the MIT-style license found in the LICENSE file.