From b63b317dfc23deb1a057ce6fda47ab527d0eea8e Mon Sep 17 00:00:00 2001 From: Chris Sexton <3216719+chrissexton@users.noreply.github.com> Date: Sat, 8 Apr 2023 21:17:21 -0400 Subject: [PATCH] gpt: reset chat prompt every N messages --- plugins/gpt/chatgpt.go | 15 +++- plugins/gpt/gpt3.go | 2 + plugins/twitch/demo/main.go | 146 ++++++++++++++++++++++++++++++++++++ util/stats/main.go | 1 + 4 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 plugins/twitch/demo/main.go create mode 100644 util/stats/main.go diff --git a/plugins/gpt/chatgpt.go b/plugins/gpt/chatgpt.go index f877ea7..9b0f5f5 100644 --- a/plugins/gpt/chatgpt.go +++ b/plugins/gpt/chatgpt.go @@ -19,15 +19,20 @@ func (p *GPTPlugin) getClient() (*openai.Client, error) { func (p *GPTPlugin) chatGPT(request string) (string, error) { if client == nil { - if err := p.setDefaultPrompt(); err != nil { + if err := p.setPrompt(p.getDefaultPrompt()); err != nil { return "", err } } + if p.chatCount > p.c.GetInt("gpt.maxchats", 10) { + p.setPrompt(p.c.Get("gpt3.lastprompt", p.getDefaultPrompt())) + p.chatCount = 0 + } + p.chatCount++ return session.Complete(context.Background(), request) } -func (p *GPTPlugin) setDefaultPrompt() error { - return p.setPrompt(p.c.Get("gpt.prompt", "")) +func (p *GPTPlugin) getDefaultPrompt() string { + return p.c.Get("gpt.prompt", "") } func (p *GPTPlugin) setPrompt(prompt string) error { @@ -37,5 +42,9 @@ func (p *GPTPlugin) setPrompt(prompt string) error { return err } session = client.NewChatSession(prompt) + err = p.c.Set("gpt3.lastprompt", prompt) + if err != nil { + return err + } return nil } diff --git a/plugins/gpt/gpt3.go b/plugins/gpt/gpt3.go index 4cba3f0..764ca53 100644 --- a/plugins/gpt/gpt3.go +++ b/plugins/gpt/gpt3.go @@ -23,6 +23,8 @@ type GPTPlugin struct { b bot.Bot c *config.Config h bot.HandlerTable + + chatCount int } func New(b bot.Bot) *GPTPlugin { diff --git a/plugins/twitch/demo/main.go b/plugins/twitch/demo/main.go new file mode 100644 index 0000000..e77fdb8 --- /dev/null +++ b/plugins/twitch/demo/main.go @@ -0,0 +1,146 @@ +package main + +import ( + "encoding/json" + "fmt" + "github.com/nicklaw5/helix" + "io" + "log" + "net/http" +) + +func main() { + client, err := helix.NewClient(&helix.Options{ + ClientID: "ptwtiuzl9tcrekpf3d26ey3hb7qsge", + ClientSecret: "rpa0w6qemjqp7sgrmidwi4k0kcah82", + }) + if err != nil { + log.Printf("Login error: %v", err) + return + } + + access, err := client.RequestAppAccessToken([]string{"user:read:email"}) + if err != nil { + log.Printf("Login error: %v", err) + return + } + + fmt.Printf("%+v\n", access) + + // Set the access token on the client + client.SetAppAccessToken(access.Data.AccessToken) + + users, err := client.GetUsers(&helix.UsersParams{ + Logins: []string{"drseabass"}, + }) + if err != nil { + log.Printf("Error getting users: %v", err) + return + } + + if users.Error != "" { + log.Printf("Users error: %s", users.Error) + return + } + + log.Printf("drseabass: %+v", users.Data.Users[0]) + return + + resp, err := client.CreateEventSubSubscription(&helix.EventSubSubscription{ + Type: helix.EventSubTypeStreamOnline, + Version: "1", + Condition: helix.EventSubCondition{ + BroadcasterUserID: users.Data.Users[0].ID, + }, + Transport: helix.EventSubTransport{ + Method: "webhook", + Callback: "https://rathaus.chrissexton.org/live", + Secret: "s3cre7w0rd", + }, + }) + if err != nil { + log.Printf("Eventsub error: %v", err) + return + } + + fmt.Printf("%+v\n", resp) + + resp, err = client.CreateEventSubSubscription(&helix.EventSubSubscription{ + Type: helix.EventSubTypeStreamOffline, + Version: "1", + Condition: helix.EventSubCondition{ + BroadcasterUserID: users.Data.Users[0].ID, + }, + Transport: helix.EventSubTransport{ + Method: "webhook", + Callback: "https://rathaus.chrissexton.org/offline", + Secret: "s3cre7w0rd", + }, + }) + if err != nil { + log.Printf("Eventsub error: %v", err) + return + } + + fmt.Printf("%+v\n", resp) + + http.HandleFunc("/offline", func(w http.ResponseWriter, r *http.Request) { + body, err := io.ReadAll(r.Body) + if err != nil { + log.Println(err) + return + } + defer r.Body.Close() + // verify that the notification came from twitch using the secret. + if !helix.VerifyEventSubNotification("s3cre7w0rd", r.Header, string(body)) { + log.Println("no valid signature on subscription") + return + } else { + log.Println("verified signature for subscription") + } + var vals map[string]any + if err = json.Unmarshal(body, &vals); err != nil { + log.Println(err) + return + } + + if challenge, ok := vals["challenge"]; ok { + w.Write([]byte(challenge.(string))) + return + } + + log.Printf("got offline webhook: %v\n", vals) + w.WriteHeader(200) + w.Write([]byte("ok")) + }) + http.HandleFunc("/live", func(w http.ResponseWriter, r *http.Request) { + body, err := io.ReadAll(r.Body) + if err != nil { + log.Println(err) + return + } + defer r.Body.Close() + // verify that the notification came from twitch using the secret. + if !helix.VerifyEventSubNotification("s3cre7w0rd", r.Header, string(body)) { + log.Println("no valid signature on subscription") + return + } else { + log.Println("verified signature for subscription") + } + var vals map[string]any + if err = json.Unmarshal(body, &vals); err != nil { + log.Println(err) + return + } + + if challenge, ok := vals["challenge"]; ok { + w.Write([]byte(challenge.(string))) + return + } + + log.Printf("got live webhook: %v\n", vals) + w.WriteHeader(200) + w.Write([]byte("ok")) + }) + http.ListenAndServe("0.0.0.0:1337", nil) +} diff --git a/util/stats/main.go b/util/stats/main.go new file mode 100644 index 0000000..43b4fd5 --- /dev/null +++ b/util/stats/main.go @@ -0,0 +1 @@ +package stats