mirror of https://github.com/velour/catbase.git
Compare commits
No commits in common. "c09dfd46e29307acd1561207aeb06e2b6b946e76" and "0a12e796d0bae4001fb8cf83ff815f9c6844c771" have entirely different histories.
c09dfd46e2
...
0a12e796d0
|
@ -179,10 +179,6 @@ func (c *Config) Set(key, value string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) SetBool(key string, value bool) error {
|
|
||||||
return c.Set(key, fmt.Sprintf("%v", value))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Config) RefreshSecrets() error {
|
func (c *Config) RefreshSecrets() error {
|
||||||
q := `select key, value from secrets`
|
q := `select key, value from secrets`
|
||||||
var secrets []Secret
|
var secrets []Secret
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/velour/catbase/plugins/talklikeapirate"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -34,8 +33,6 @@ type Discord struct {
|
||||||
cmdHandlers map[string]CmdHandler
|
cmdHandlers map[string]CmdHandler
|
||||||
|
|
||||||
guildID string
|
guildID string
|
||||||
|
|
||||||
Pirate *talklikeapirate.TalkLikeAPirateFilter
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(config *config.Config) *Discord {
|
func New(config *config.Config) *Discord {
|
||||||
|
@ -115,14 +112,6 @@ func (d Discord) Send(kind bot.Kind, args ...any) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Discord) sendMessage(channel, message string, meMessage bool, args ...any) (string, error) {
|
func (d *Discord) sendMessage(channel, message string, meMessage bool, args ...any) (string, error) {
|
||||||
var err error
|
|
||||||
if d.Pirate != nil {
|
|
||||||
message, err = d.Pirate.Filter(message)
|
|
||||||
if err != nil {
|
|
||||||
log.Error().Err(err).Msg("could not pirate message")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if meMessage && !strings.HasPrefix(message, "_") && !strings.HasSuffix(message, "_") {
|
if meMessage && !strings.HasPrefix(message, "_") && !strings.HasSuffix(message, "_") {
|
||||||
message = "_" + message + "_"
|
message = "_" + message + "_"
|
||||||
}
|
}
|
||||||
|
@ -178,6 +167,7 @@ func (d *Discord) sendMessage(channel, message string, meMessage bool, args ...a
|
||||||
maxLen := 2000
|
maxLen := 2000
|
||||||
chunkSize := maxLen - 100
|
chunkSize := maxLen - 100
|
||||||
var st *discordgo.Message
|
var st *discordgo.Message
|
||||||
|
var err error
|
||||||
if len(data.Content) > maxLen {
|
if len(data.Content) > maxLen {
|
||||||
tmp := data.Content
|
tmp := data.Content
|
||||||
data.Content = tmp[:chunkSize]
|
data.Content = tmp[:chunkSize]
|
||||||
|
|
5
main.go
5
main.go
|
@ -7,7 +7,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"github.com/velour/catbase/plugins"
|
"github.com/velour/catbase/plugins"
|
||||||
"github.com/velour/catbase/plugins/talklikeapirate"
|
|
||||||
"io"
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
|
@ -72,9 +71,7 @@ func main() {
|
||||||
case "slackapp":
|
case "slackapp":
|
||||||
client = slackapp.New(c)
|
client = slackapp.New(c)
|
||||||
case "discord":
|
case "discord":
|
||||||
d := discord.New(c)
|
client = discord.New(c)
|
||||||
d.Pirate = talklikeapirate.NewFilter(c)
|
|
||||||
client = d
|
|
||||||
default:
|
default:
|
||||||
log.Fatal().Msgf("Unknown connection type: %s", c.Get("type", "UNSET"))
|
log.Fatal().Msgf("Unknown connection type: %s", c.Get("type", "UNSET"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultMessage = "I don't know how to respond to that. If you'd like to ask an LLM, use the `llm` command (or prefix your message with &)."
|
const defaultMessage = "I don't know how to respond to that. If you'd like to ask an LLM, use the `llm` command."
|
||||||
|
|
||||||
type DeadEndPlugin struct {
|
type DeadEndPlugin struct {
|
||||||
b bot.Bot
|
b bot.Bot
|
||||||
|
|
|
@ -25,7 +25,7 @@ func (p *LLMPlugin) geminiConnect() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *LLMPlugin) gemini(msg string) (chatEntry, error) {
|
func (p *LLMPlugin) gemini(msg string) (chatEntry, error) {
|
||||||
model := p.geminiClient.GenerativeModel(p.c.Get("gemini.model", "gemini-1.5-flash"))
|
model := p.geminiClient.GenerativeModel("gemini-1.5-flash")
|
||||||
model.SetMaxOutputTokens(int32(p.c.GetInt("gemini.maxtokens", 100)))
|
model.SetMaxOutputTokens(int32(p.c.GetInt("gemini.maxtokens", 100)))
|
||||||
model.SetTopP(float32(p.c.GetFloat64("gemini.topp", 0.95)))
|
model.SetTopP(float32(p.c.GetFloat64("gemini.topp", 0.95)))
|
||||||
model.SetTopK(int32(p.c.GetInt("gemini.topk", 20)))
|
model.SetTopK(int32(p.c.GetInt("gemini.topk", 20)))
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"github.com/velour/catbase/bot"
|
"github.com/velour/catbase/bot"
|
||||||
"github.com/velour/catbase/config"
|
"github.com/velour/catbase/config"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -49,12 +48,6 @@ func (p *LLMPlugin) register() {
|
||||||
HelpText: "set the ChatGPT prompt",
|
HelpText: "set the ChatGPT prompt",
|
||||||
Handler: p.setPromptMessage,
|
Handler: p.setPromptMessage,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
Kind: bot.Message, IsCmd: false,
|
|
||||||
Regex: regexp.MustCompile(`(?is)^&(?P<text>.*)`),
|
|
||||||
HelpText: "chat completion using first-available AI",
|
|
||||||
Handler: p.geminiChatMessage,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
Kind: bot.Message, IsCmd: true,
|
Kind: bot.Message, IsCmd: true,
|
||||||
Regex: regexp.MustCompile(`(?is)^llm (?P<text>.*)`),
|
Regex: regexp.MustCompile(`(?is)^llm (?P<text>.*)`),
|
||||||
|
@ -62,7 +55,7 @@ func (p *LLMPlugin) register() {
|
||||||
Handler: p.geminiChatMessage,
|
Handler: p.geminiChatMessage,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Kind: bot.Message, IsCmd: false,
|
Kind: bot.Message, IsCmd: true,
|
||||||
Regex: regexp.MustCompile(`(?is)^gpt4 (?P<text>.*)`),
|
Regex: regexp.MustCompile(`(?is)^gpt4 (?P<text>.*)`),
|
||||||
HelpText: "chat completion using OpenAI",
|
HelpText: "chat completion using OpenAI",
|
||||||
Handler: p.gptMessage,
|
Handler: p.gptMessage,
|
||||||
|
@ -73,11 +66,6 @@ func (p *LLMPlugin) register() {
|
||||||
HelpText: "clear chat history",
|
HelpText: "clear chat history",
|
||||||
Handler: p.puke,
|
Handler: p.puke,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
Kind: bot.Help, IsCmd: false,
|
|
||||||
Regex: regexp.MustCompile(`.*`),
|
|
||||||
Handler: p.help,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
p.b.RegisterTable(p, p.h)
|
p.b.RegisterTable(p, p.h)
|
||||||
}
|
}
|
||||||
|
@ -176,16 +164,3 @@ func (p *LLMPlugin) puke(r bot.Request) bool {
|
||||||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, resp)
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, resp)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *LLMPlugin) help(r bot.Request) bool {
|
|
||||||
out := "Talk like a pirate commands:\n"
|
|
||||||
for _, h := range p.h {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -45,7 +45,6 @@ import (
|
||||||
"github.com/velour/catbase/plugins/sms"
|
"github.com/velour/catbase/plugins/sms"
|
||||||
"github.com/velour/catbase/plugins/stock"
|
"github.com/velour/catbase/plugins/stock"
|
||||||
"github.com/velour/catbase/plugins/talker"
|
"github.com/velour/catbase/plugins/talker"
|
||||||
"github.com/velour/catbase/plugins/talklikeapirate"
|
|
||||||
"github.com/velour/catbase/plugins/tappd"
|
"github.com/velour/catbase/plugins/tappd"
|
||||||
"github.com/velour/catbase/plugins/tell"
|
"github.com/velour/catbase/plugins/tell"
|
||||||
"github.com/velour/catbase/plugins/tldr"
|
"github.com/velour/catbase/plugins/tldr"
|
||||||
|
@ -103,7 +102,6 @@ func Register(b bot.Bot) {
|
||||||
b.AddPlugin(talker.New(b))
|
b.AddPlugin(talker.New(b))
|
||||||
b.AddPlugin(fact.New(b))
|
b.AddPlugin(fact.New(b))
|
||||||
b.AddPlugin(llm.New(b))
|
b.AddPlugin(llm.New(b))
|
||||||
b.AddPlugin(talklikeapirate.New(b))
|
|
||||||
// catches anything left, will always return true
|
// catches anything left, will always return true
|
||||||
b.AddPlugin(deadend.New(b))
|
b.AddPlugin(deadend.New(b))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,108 +0,0 @@
|
||||||
package talklikeapirate
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"github.com/google/generative-ai-go/genai"
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"github.com/velour/catbase/bot"
|
|
||||||
"github.com/velour/catbase/config"
|
|
||||||
"google.golang.org/api/option"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TalkLikeAPirateFilter reimplements the send function
|
|
||||||
// with an AI intermediate.
|
|
||||||
type TalkLikeAPirateFilter struct {
|
|
||||||
client *genai.Client
|
|
||||||
prompt string
|
|
||||||
|
|
||||||
b bot.Bot
|
|
||||||
c *config.Config
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewFilter(c *config.Config) *TalkLikeAPirateFilter {
|
|
||||||
p := &TalkLikeAPirateFilter{
|
|
||||||
c: c,
|
|
||||||
}
|
|
||||||
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *TalkLikeAPirateFilter) Filter(input string) (string, error) {
|
|
||||||
if !p.c.GetBool("talklikeapirate.enabled", false) {
|
|
||||||
return input, nil
|
|
||||||
}
|
|
||||||
if p.client == nil {
|
|
||||||
var err error
|
|
||||||
p.client, err = p.getClient()
|
|
||||||
if err != nil {
|
|
||||||
return input, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
model, err := p.GetModel()
|
|
||||||
if err != nil {
|
|
||||||
log.Error().Err(err).Send()
|
|
||||||
return input, err
|
|
||||||
}
|
|
||||||
|
|
||||||
res, err := model.GenerateContent(context.Background(), genai.Text(input))
|
|
||||||
if err != nil {
|
|
||||||
log.Error().Err(err).Send()
|
|
||||||
return input, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(res.Candidates) == 0 {
|
|
||||||
err := errors.New("no candidates found")
|
|
||||||
log.Error().Err(err).Send()
|
|
||||||
return input, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Need to check here that we got an actual completion, not a
|
|
||||||
// warning about bad content. FinishReason exists on Completion.
|
|
||||||
|
|
||||||
completion := ""
|
|
||||||
for _, p := range res.Candidates[0].Content.Parts {
|
|
||||||
completion += fmt.Sprintf("%s", p)
|
|
||||||
}
|
|
||||||
|
|
||||||
return completion, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
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)))
|
|
||||||
model.SetTopK(int32(p.c.GetInt("gemini.topk", 20)))
|
|
||||||
model.SetTemperature(float32(p.c.GetFloat64("gemini.temp", 0.9)))
|
|
||||||
|
|
||||||
model.SafetySettings = []*genai.SafetySetting{
|
|
||||||
{genai.HarmCategoryHarassment, genai.HarmBlockNone},
|
|
||||||
{genai.HarmCategoryHateSpeech, genai.HarmBlockNone},
|
|
||||||
{genai.HarmCategorySexuallyExplicit, genai.HarmBlockNone},
|
|
||||||
{genai.HarmCategoryDangerousContent, genai.HarmBlockNone},
|
|
||||||
}
|
|
||||||
|
|
||||||
if prompt := p.c.Get("talklikeapirate.systemprompt", ""); prompt != "" {
|
|
||||||
model.SystemInstruction = &genai.Content{
|
|
||||||
Parts: []genai.Part{genai.Text(prompt)},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return nil, errors.New("no system prompt selected")
|
|
||||||
}
|
|
||||||
|
|
||||||
return model, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *TalkLikeAPirateFilter) getClient() (*genai.Client, error) {
|
|
||||||
ctx := context.Background()
|
|
||||||
key := p.c.Get("GEMINI_API_KEY", "")
|
|
||||||
if key == "" {
|
|
||||||
return nil, errors.New("missing GEMINI_API_KEY")
|
|
||||||
}
|
|
||||||
client, err := genai.NewClient(ctx, option.WithAPIKey(key))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return client, nil
|
|
||||||
}
|
|
|
@ -1,84 +0,0 @@
|
||||||
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
|
|
||||||
}
|
|
|
@ -129,7 +129,7 @@ func (p *TLDRPlugin) betterTLDR(r bot.Request) bool {
|
||||||
ch := r.Msg.Channel
|
ch := r.Msg.Channel
|
||||||
c, err := p.getClient()
|
c, err := p.getClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Couldn't fetch an AI client")
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Couldn't fetch an OpenAI client")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
promptConfig := p.c.Get(templateKey, defaultTemplate)
|
promptConfig := p.c.Get(templateKey, defaultTemplate)
|
||||||
|
@ -148,7 +148,7 @@ func (p *TLDRPlugin) betterTLDR(r bot.Request) bool {
|
||||||
backlog = str + backlog
|
backlog = str + backlog
|
||||||
}
|
}
|
||||||
|
|
||||||
model := c.GenerativeModel(p.c.Get("gemini.model", "gemini-1.5-flash"))
|
model := c.GenerativeModel("gemini-1.5-flash")
|
||||||
model.SystemInstruction = &genai.Content{
|
model.SystemInstruction = &genai.Content{
|
||||||
Parts: []genai.Part{genai.Text(prompt.String())},
|
Parts: []genai.Part{genai.Text(prompt.String())},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue