mirror of https://github.com/velour/catbase.git
gpt: reset chat prompt every N messages
This commit is contained in:
parent
21ce66fc15
commit
b63b317dfc
|
@ -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
|
||||
}
|
||||
|
|
|
@ -23,6 +23,8 @@ type GPTPlugin struct {
|
|||
b bot.Bot
|
||||
c *config.Config
|
||||
h bot.HandlerTable
|
||||
|
||||
chatCount int
|
||||
}
|
||||
|
||||
func New(b bot.Bot) *GPTPlugin {
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
package stats
|
Loading…
Reference in New Issue