Skip to content

Commit

Permalink
add handling for timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
mazzz1y committed Aug 28, 2023
1 parent d02c544 commit 4ddb4dd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
8 changes: 4 additions & 4 deletions cmd/matrix-gpt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func main() {
Name: "history-limit",
Usage: "Maximum number of history entries",
EnvVars: []string{"HISTORY_LIMIT"},
Value: 0,
Value: 5,
},
&cli.IntFlag{
Name: "history-expire",
Expand All @@ -69,13 +69,13 @@ func main() {
Name: "gpt-timeout",
Usage: "Time to wait for a GPT response (in seconds)",
EnvVars: []string{"GPT_TIMEOUT"},
Value: 180,
Value: 35,
},
&cli.IntFlag{
Name: "max-attempts",
Usage: "Maximum number of retry attempts for GPT",
Usage: "Maximum number of attempts for GPT requests",
EnvVars: []string{"MAX_ATTEMPTS"},
Value: 3,
Value: 1,
},
&cli.StringSliceFlag{
Name: "user-ids",
Expand Down
3 changes: 2 additions & 1 deletion internal/bot/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (b *Bot) completionResponse(ctx context.Context, u *user, evt *event.Event,

// helpResponse responds with help message.
func (b *Bot) helpResponse(ctx context.Context, u *user, evt *event.Event, msg string) error {
return b.markdownResponse(evt, false, helpMessage)
return b.markdownResponse(evt, false, helpMsg)
}

// imageResponse responds to the user message with a DALL-E created image.
Expand Down Expand Up @@ -123,6 +123,7 @@ func (b *Bot) markdownResponse(evt *event.Event, reply bool, msg string) error {
if reply {
formattedMsg.SetReply(evt)
}

_, err := b.client.SendMessageEvent(evt.RoomID, event.EventMessage, &formattedMsg)
return err
}
Expand Down
7 changes: 6 additions & 1 deletion internal/bot/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bot

import (
"context"
"errors"
"time"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -115,8 +116,12 @@ func (b *Bot) sendResponse(ctx context.Context, u *user, e *event.Event) (err er
func (b *Bot) err(evt *event.Event, err error) {
switch t := err.(type) {
case *unknownCommandError:
b.markdownResponse(evt, true, errorMessage)
b.markdownResponse(evt, true, unknownCommandMsg)
case *openai.APIError:
b.markdownResponse(evt, true, t.Message)
default:
if errors.Is(err, context.DeadlineExceeded) {
b.markdownResponse(evt, true, timeoutMsg)
}
}
}
6 changes: 3 additions & 3 deletions internal/bot/strings.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package bot

const (
errorMessage = "Unknown command. Please use the `!help` command to access the available commands"
helpMessage = `**Commands**
helpMsg = `**Commands**
- *!image [text]*: Creates an image based on the provided text.
- *!reset [text]*: Resets the user history. If a text is provided after the reset command, it will generate a GPT response based on this text.
- *[text]*: If only text is provided, the bot will generate a GPT-based response related to that text.
**Notes**
- You can use the first letter of a command as an alias. For example, "!i" for "!image".
- If you wish to terminate the current processing, simply delete your message from the chat.
- The bot responds with ❌ reaction if there are any errors. Contact the administrator if you see this.
`
timeoutMsg = "Timeout error. Please try again. If issue persists, contact the administrator."
unknownCommandMsg = "Unknown command. Please use the `!help` command to access the available commands"
)

0 comments on commit 4ddb4dd

Please sign in to comment.