Skip to content

Commit

Permalink
Merge pull request #504 from innogames/dalle_timeout
Browse files Browse the repository at this point in the history
DALL-E longer timeout to upload
  • Loading branch information
brainexe authored Nov 21, 2023
2 parents ea24d0f + 3378217 commit f0d87d9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
5 changes: 4 additions & 1 deletion command/openai/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package openai
import (
"bytes"
"net/http"
"time"

"github.com/pkg/errors"
)
Expand All @@ -20,7 +21,9 @@ const (
)

// we don't use our default clients.HttpClient as we need longer timeouts...
var client http.Client
var client = http.Client{
Timeout: 25 * time.Second,
}

func doRequest(cfg Config, apiEndpoint string, data []byte) (*http.Response, error) {
req, err := http.NewRequest("POST", cfg.APIHost+apiEndpoint, bytes.NewBuffer(data))
Expand Down
1 change: 1 addition & 0 deletions command/openai/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ func (c *openaiCommand) GetHelp() []bot.Help {
Category: category,
Examples: []string{
"dalle high resolution image of a sunset, painted by a robot",
"dall-e high resolution image of a sunset, painted by a robot",
},
},
}
Expand Down
9 changes: 7 additions & 2 deletions command/openai/dalle.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ func (c *openaiCommand) dalleGenerateImage(match matcher.Result, message msg.Mes
c.AddReaction(":coffee:", message)

go func() {
defer c.RemoveReaction(":coffee:", message)

prompt := match.GetString(util.FullMatch)
images, err := generateImages(c.cfg, prompt)
c.RemoveReaction(":coffee:", message)
if err != nil {
c.ReplyError(message, err)
return
}

// add 📤 emoji to indicate that the image is being uploaded which can take some time via slack
c.AddReaction(":outbox_tray:", message)
defer c.RemoveReaction(":outbox_tray:", message)

startTime := time.Now()
for _, image := range images {
err := c.sendImageInSlack(image, message)
if err != nil {
Expand All @@ -37,6 +41,7 @@ func (c *openaiCommand) dalleGenerateImage(match matcher.Result, message msg.Mes
)
}
}
log.Infof("Uploading %d images took %s", len(images), time.Since(startTime))
}()
}

Expand Down
2 changes: 2 additions & 0 deletions command/openai/dalle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ func TestDalle(t *testing.T) {

mocks.AssertReaction(slackClient, ":coffee:", message)
mocks.AssertRemoveReaction(slackClient, ":coffee:", message)
mocks.AssertReaction(slackClient, ":outbox_tray:", message)
mocks.AssertRemoveReaction(slackClient, ":outbox_tray:", message)

slackClient.On(
"UploadFile",
Expand Down

0 comments on commit f0d87d9

Please sign in to comment.