-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #498 from innogames/dalle
WIP: DALL-E integration
- Loading branch information
Showing
9 changed files
with
285 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package openai | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/innogames/slack-bot/v2/bot/matcher" | ||
"github.com/innogames/slack-bot/v2/bot/msg" | ||
"github.com/innogames/slack-bot/v2/bot/util" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// bot function to generate images with Dall-E | ||
func (c *chatGPTCommand) dalleGenerateImage(match matcher.Result, message msg.Message) { | ||
prompt := match.GetString(util.FullMatch) | ||
|
||
go func() { | ||
c.AddReaction(":coffee:", message) | ||
defer c.RemoveReaction(":coffee:", message) | ||
|
||
images, err := generateImage(c.cfg, prompt) | ||
if err != nil { | ||
c.ReplyError(message, err) | ||
return | ||
} | ||
|
||
text := "" | ||
for _, image := range images { | ||
text += fmt.Sprintf( | ||
" - %s: <%s|open image>\n", | ||
image.RevisedPrompt, | ||
image.URL, | ||
) | ||
} | ||
c.SendMessage(message, text) | ||
}() | ||
} | ||
|
||
func generateImage(cfg Config, prompt string) ([]DalleResponseImage, error) { | ||
jsonData, _ := json.Marshal(DalleRequest{ | ||
Model: cfg.DalleModel, | ||
Size: cfg.DalleImageSize, | ||
N: cfg.DalleNumberOfImages, | ||
Prompt: prompt, | ||
}) | ||
|
||
start := time.Now() | ||
resp, err := doRequest(cfg, apiDalleGenerateImageURL, jsonData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
var response DalleResponse | ||
err = json.NewDecoder(resp.Body).Decode(&response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if response.Error.Message != "" { | ||
return nil, fmt.Errorf(response.Error.Message) | ||
} | ||
|
||
log.WithField("model", cfg.DalleModel). | ||
Infof("Dall-E image generation took %s", time.Since(start)) | ||
|
||
return response.Data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package openai | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/innogames/slack-bot/v2/bot" | ||
"github.com/innogames/slack-bot/v2/bot/config" | ||
"github.com/innogames/slack-bot/v2/bot/msg" | ||
"github.com/innogames/slack-bot/v2/bot/storage" | ||
"github.com/innogames/slack-bot/v2/mocks" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDalle(t *testing.T) { | ||
// init memory based storage | ||
storage.InitStorage("") | ||
|
||
slackClient := &mocks.SlackClient{} | ||
base := bot.BaseCommand{SlackClient: slackClient} | ||
|
||
t.Run("test http error", func(t *testing.T) { | ||
ts := startTestServer( | ||
t, | ||
apiDalleGenerateImageURL, | ||
[]testRequest{ | ||
{ | ||
`{"model":"dall-e-3","prompt":"a nice cat","n":1,"size":"1024x1024"}`, | ||
`{ | ||
"error": { | ||
"code": "invalid_api_key", | ||
"message": "Incorrect API key provided: sk-1234**************************************567.", | ||
"type": "invalid_request_error" | ||
} | ||
}`, | ||
http.StatusUnauthorized, | ||
}, | ||
}, | ||
) | ||
openaiCfg := defaultConfig | ||
openaiCfg.APIHost = ts.URL | ||
openaiCfg.APIKey = "0815pass" | ||
|
||
cfg := &config.Config{} | ||
cfg.Set("openai", openaiCfg) | ||
|
||
defer ts.Close() | ||
|
||
commands := GetCommands(base, cfg) | ||
|
||
message := msg.Message{} | ||
message.Text = "dalle a nice cat" | ||
|
||
mocks.AssertReaction(slackClient, ":coffee:", message) | ||
mocks.AssertRemoveReaction(slackClient, ":coffee:", message) | ||
mocks.AssertError(slackClient, message, "Incorrect API key provided: sk-1234**************************************567.") | ||
|
||
actual := commands.Run(message) | ||
time.Sleep(time.Millisecond * 100) | ||
assert.True(t, actual) | ||
}) | ||
|
||
t.Run("test generate image", func(t *testing.T) { | ||
ts := startTestServer( | ||
t, | ||
apiDalleGenerateImageURL, | ||
[]testRequest{ | ||
{ | ||
`{"model":"dall-e-3","prompt":"a nice cat","n":1,"size":"1024x1024"}`, | ||
` { | ||
"created": 1700233554, | ||
"data": [ | ||
{ | ||
"url": "https://example.com/image123", | ||
"revised_prompt": "revised prompt 1234" | ||
} | ||
] | ||
}`, | ||
http.StatusUnauthorized, | ||
}, | ||
}, | ||
) | ||
openaiCfg := defaultConfig | ||
openaiCfg.APIHost = ts.URL | ||
openaiCfg.APIKey = "0815pass" | ||
|
||
cfg := &config.Config{} | ||
cfg.Set("openai", openaiCfg) | ||
|
||
defer ts.Close() | ||
|
||
commands := GetCommands(base, cfg) | ||
|
||
message := msg.Message{} | ||
message.Text = "dalle a nice cat" | ||
|
||
mocks.AssertReaction(slackClient, ":coffee:", message) | ||
mocks.AssertRemoveReaction(slackClient, ":coffee:", message) | ||
mocks.AssertSlackMessage(slackClient, message, " - revised prompt 1234: <https://example.com/image123|open image>\n") | ||
|
||
actual := commands.Run(message) | ||
time.Sleep(time.Millisecond * 100) | ||
assert.True(t, actual) | ||
}) | ||
} |
Oops, something went wrong.