talklikeapirate: add controls

This commit is contained in:
Chris Sexton 2024-09-28 10:52:49 -04:00
parent 624258a794
commit c20df2d659
6 changed files with 99 additions and 9 deletions

View File

@ -179,6 +179,10 @@ func (c *Config) Set(key, value string) error {
return nil
}
func (c *Config) SetBool(key string, value bool) error {
return c.Set(key, fmt.Sprintf("%v", value))
}
func (c *Config) RefreshSecrets() error {
q := `select key, value from secrets`
var secrets []Secret

View File

@ -35,7 +35,7 @@ type Discord struct {
guildID string
Pirate *talklikeapirate.TalkLikeAPiratePlugin
Pirate *talklikeapirate.TalkLikeAPirateFilter
}
func New(config *config.Config) *Discord {

View File

@ -73,7 +73,7 @@ func main() {
client = slackapp.New(c)
case "discord":
d := discord.New(c)
d.Pirate = talklikeapirate.New(c)
d.Pirate = talklikeapirate.NewFilter(c)
client = d
default:
log.Fatal().Msgf("Unknown connection type: %s", c.Get("type", "UNSET"))

View File

@ -45,6 +45,7 @@ import (
"github.com/velour/catbase/plugins/sms"
"github.com/velour/catbase/plugins/stock"
"github.com/velour/catbase/plugins/talker"
"github.com/velour/catbase/plugins/talklikeapirate"
"github.com/velour/catbase/plugins/tappd"
"github.com/velour/catbase/plugins/tell"
"github.com/velour/catbase/plugins/tldr"
@ -102,6 +103,7 @@ func Register(b bot.Bot) {
b.AddPlugin(talker.New(b))
b.AddPlugin(fact.New(b))
b.AddPlugin(llm.New(b))
b.AddPlugin(talklikeapirate.New(b))
// catches anything left, will always return true
b.AddPlugin(deadend.New(b))
}

View File

@ -11,9 +11,9 @@ import (
"google.golang.org/api/option"
)
// TalkLikeAPiratePlugin reimplements the send function
// TalkLikeAPirateFilter reimplements the send function
// with an AI intermediate.
type TalkLikeAPiratePlugin struct {
type TalkLikeAPirateFilter struct {
client *genai.Client
prompt string
@ -21,15 +21,15 @@ type TalkLikeAPiratePlugin struct {
c *config.Config
}
func New(c *config.Config) *TalkLikeAPiratePlugin {
p := &TalkLikeAPiratePlugin{
func NewFilter(c *config.Config) *TalkLikeAPirateFilter {
p := &TalkLikeAPirateFilter{
c: c,
}
return p
}
func (p *TalkLikeAPiratePlugin) Filter(input string) (string, error) {
func (p *TalkLikeAPirateFilter) Filter(input string) (string, error) {
if !p.c.GetBool("talklikeapirate.enabled", false) {
return input, nil
}
@ -69,7 +69,7 @@ func (p *TalkLikeAPiratePlugin) Filter(input string) (string, error) {
return completion, nil
}
func (p *TalkLikeAPiratePlugin) GetModel() (*genai.GenerativeModel, error) {
func (p *TalkLikeAPirateFilter) GetModel() (*genai.GenerativeModel, error) {
model := p.client.GenerativeModel(p.c.Get("gemini.model", "gemini-1.5-flash"))
model.SetMaxOutputTokens(int32(p.c.GetInt("gemini.maxtokens", 100)))
model.SetTopP(float32(p.c.GetFloat64("gemini.topp", 0.95)))
@ -94,7 +94,7 @@ func (p *TalkLikeAPiratePlugin) GetModel() (*genai.GenerativeModel, error) {
return model, nil
}
func (p *TalkLikeAPiratePlugin) getClient() (*genai.Client, error) {
func (p *TalkLikeAPirateFilter) getClient() (*genai.Client, error) {
ctx := context.Background()
key := p.c.Get("GEMINI_API_KEY", "")
if key == "" {

View File

@ -0,0 +1,84 @@
package talklikeapirate
import (
"fmt"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/config"
"regexp"
"strings"
)
// TalkLikeAPiratePlugin allows admin of the filter
type TalkLikeAPiratePlugin struct {
b bot.Bot
c *config.Config
handlers bot.HandlerTable
}
func New(b bot.Bot) *TalkLikeAPiratePlugin {
p := &TalkLikeAPiratePlugin{
b: b,
c: b.Config(),
}
p.register()
return p
}
func (p *TalkLikeAPiratePlugin) register() {
p.handlers = bot.HandlerTable{
{
Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile(`^enable pirate$`),
HelpText: "Enable message filter",
Handler: p.setEnabled(true),
},
{
Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile(`^disable pirate$`),
HelpText: "Disable message filter",
Handler: p.setEnabled(false),
},
{
Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile(`^pirate-prompt:? (?P<text>.*)$`),
HelpText: "Set message filter prompt",
Handler: p.setPrompt,
},
{
Kind: bot.Help, IsCmd: false,
Regex: regexp.MustCompile(`.*`),
Handler: p.help,
},
}
p.b.RegisterTable(p, p.handlers)
}
func (p *TalkLikeAPiratePlugin) setEnabled(isEnabled bool) bot.ResponseHandler {
return func(r bot.Request) bool {
p.c.SetBool("talklikeapirate.enabled", isEnabled)
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("I just set the message filter status to: %v", isEnabled))
return true
}
}
func (p *TalkLikeAPiratePlugin) setPrompt(r bot.Request) bool {
prompt := r.Values["text"]
p.c.Set("talklikeapirate.systemprompt", prompt)
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("I set the message filter prompt to: %s", prompt))
return true
}
func (p *TalkLikeAPiratePlugin) help(r bot.Request) bool {
out := "Talk like a pirate commands:\n"
for _, h := range p.handlers {
if h.HelpText == "" {
continue
}
out += fmt.Sprintf("```%s```\t%s", h.Regex.String(), h.HelpText)
}
out = strings.TrimSpace(out)
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, out)
return true
}