Skip to content
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

dall-e: add "regenerate" button #556

Merged
merged 1 commit into from
Apr 29, 2024
Merged
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
4 changes: 2 additions & 2 deletions command/openai/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
)

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

Expand All @@ -34,7 +34,7 @@ func doRequest(cfg Config, apiEndpoint string, data []byte) (*http.Response, err
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+cfg.APIKey)

return client.Do(req)
return httpClient.Do(req)
}

// https://platform.openai.com/docs/api-reference/chat
Expand Down
5 changes: 4 additions & 1 deletion command/openai/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (c *openaiCommand) startConversation(message msg.Ref, text string) bool {

var storageIdentifier string
if message.GetThread() != "" {
// "openai" was triggerd within a existing thread. -> fetch the whole thread history as context
// "openai" was triggered within a existing thread. -> fetch the whole thread history as context
threadMessages, err := c.SlackClient.GetThreadMessages(message)
if err != nil {
c.ReplyError(message, fmt.Errorf("can't load thread messages: %w", err))
Expand Down Expand Up @@ -236,9 +236,12 @@ func (c *openaiCommand) callAndStore(messages []ChatMessage, storageIdentifier s
Role: roleAssistant,
Content: responseText.String(),
})

// truncate the history if needed to the last X messages
if len(messages) > c.cfg.HistorySize {
messages = messages[len(messages)-c.cfg.HistorySize:]
}

err = storage.Write(storageKey, storageIdentifier, messages)
if err != nil {
log.Warnf("Error while storing openai history: %s", err)
Expand Down
16 changes: 14 additions & 2 deletions command/openai/dalle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/innogames/slack-bot/v2/bot/msg"
"github.com/innogames/slack-bot/v2/bot/stats"
"github.com/innogames/slack-bot/v2/bot/util"
"github.com/innogames/slack-bot/v2/client"
log "github.com/sirupsen/logrus"
"github.com/slack-go/slack"
)
Expand Down Expand Up @@ -51,7 +52,7 @@ func (c *openaiCommand) sendImageInSlack(image DalleResponseImage, message msg.M
if err != nil {
return err
}
resp, err := client.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
return err
}
Expand All @@ -63,9 +64,20 @@ func (c *openaiCommand) sendImageInSlack(image DalleResponseImage, message msg.M
Channels: []string{message.Channel},
ThreadTimestamp: message.Timestamp,
Reader: resp.Body,
InitialComment: image.RevisedPrompt,
InitialComment: fmt.Sprintf("Dall-e prompt: %s", image.RevisedPrompt),
})

c.SlackClient.SendBlockMessage(
message,
[]slack.Block{
slack.NewActionBlock(
"",
client.GetInteractionButton("dalle", "Regenerate", fmt.Sprintf("dall-e %s", image.RevisedPrompt)),
),
},
slack.MsgOptionTS(message.Timestamp),
)

return err
}

Expand Down
6 changes: 6 additions & 0 deletions command/openai/dalle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ func TestDalle(t *testing.T) {
mocks.AssertRemoveReaction(slackClient, ":coffee:", message)
mocks.AssertReaction(slackClient, ":outbox_tray:", message)
mocks.AssertRemoveReaction(slackClient, ":outbox_tray:", message)
mocks.AssertSlackBlocks(
t,
slackClient,
message,
`[{"type":"actions","elements":[{"type":"button","text":{"type":"plain_text","text":"Regenerate","emoji":true},"action_id":"dalle","value":"dall-e revised prompt 1234"}]}]`,
)

slackClient.On(
"UploadFile",
Expand Down
2 changes: 1 addition & 1 deletion mocks/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func AssertSlackBlocks(t *testing.T, slackClient *SlackClient, message msg.Ref,
}

return expectedJSON == string(givenJSON)
})).Once().Return("")
}), mock.Anything).Once().Return("")
}

// AssertContainsSlackBlocks is a small test helper to check for certain slack.Block
Expand Down
Loading