prepare for multiple bots
This commit is contained in:
parent
ae6d0532bd
commit
988951970b
18
bot/bot.go
18
bot/bot.go
|
@ -3,10 +3,7 @@ package bot
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/chzyer/readline"
|
|
||||||
"github.com/rs/zerolog"
|
|
||||||
"github.com/sashabaranov/go-openai"
|
"github.com/sashabaranov/go-openai"
|
||||||
"golang.org/x/exp/slog"
|
"golang.org/x/exp/slog"
|
||||||
"maunium.net/go/mautrix"
|
"maunium.net/go/mautrix"
|
||||||
|
@ -25,11 +22,11 @@ type Config struct {
|
||||||
DBPath string
|
DBPath string
|
||||||
Pickle string
|
Pickle string
|
||||||
OpenAIKey string
|
OpenAIKey string
|
||||||
|
SystemPrompt string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Bot struct {
|
type Bot struct {
|
||||||
config Config
|
config Config
|
||||||
readline *readline.Instance
|
|
||||||
client *mautrix.Client
|
client *mautrix.Client
|
||||||
cryptoHelper *cryptohelper.CryptoHelper
|
cryptoHelper *cryptohelper.CryptoHelper
|
||||||
characters []Character
|
characters []Character
|
||||||
|
@ -53,10 +50,6 @@ func (m *Bot) Init() error {
|
||||||
var oei mautrix.OldEventIgnorer
|
var oei mautrix.OldEventIgnorer
|
||||||
oei.Register(client.Syncer.(mautrix.ExtensibleSyncer))
|
oei.Register(client.Syncer.(mautrix.ExtensibleSyncer))
|
||||||
m.client = client
|
m.client = client
|
||||||
m.client.Log = zerolog.New(zerolog.NewConsoleWriter(func(w *zerolog.ConsoleWriter) {
|
|
||||||
w.TimeFormat = time.Stamp
|
|
||||||
})).With().Timestamp().Logger().Level(zerolog.InfoLevel)
|
|
||||||
|
|
||||||
m.cryptoHelper, err = cryptohelper.NewCryptoHelper(client, []byte(m.config.Pickle), m.config.DBPath)
|
m.cryptoHelper, err = cryptohelper.NewCryptoHelper(client, []byte(m.config.Pickle), m.config.DBPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -70,11 +63,8 @@ func (m *Bot) Init() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
m.client.Crypto = m.cryptoHelper
|
m.client.Crypto = m.cryptoHelper
|
||||||
|
|
||||||
m.gptClient = NewGPT(m.config.OpenAIKey)
|
m.gptClient = NewGPT(m.config.OpenAIKey)
|
||||||
|
|
||||||
m.conversations = make(Conversations, 0)
|
m.conversations = make(Conversations, 0)
|
||||||
|
|
||||||
m.AddEventHandler(m.InviteHandler())
|
m.AddEventHandler(m.InviteHandler())
|
||||||
m.AddEventHandler(m.ResponseHandler())
|
m.AddEventHandler(m.ResponseHandler())
|
||||||
|
|
||||||
|
@ -114,7 +104,7 @@ func (m *Bot) InviteHandler() (event.Type, mautrix.EventHandler) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
m.logger.Info("Joined room after invite", slog.String("room_id", evt.RoomID.String()), slog.String("inviter", evt.Sender.String()))
|
m.logger.Info("joined room after invite", slog.String("room_id", evt.RoomID.String()), slog.String("inviter", evt.Sender.String()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -156,11 +146,11 @@ func (m *Bot) ResponseHandler() (event.Type, mautrix.EventHandler) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// find out if message is addressed to the bot
|
// find out if message is a new question addressed to the bot
|
||||||
m.logger.Info(content.Body)
|
m.logger.Info(content.Body)
|
||||||
if conv == nil && strings.HasPrefix(strings.ToLower(content.Body), strings.ToLower(fmt.Sprintf("%s: ", m.config.UserDisplayName))) {
|
if conv == nil && strings.HasPrefix(strings.ToLower(content.Body), strings.ToLower(fmt.Sprintf("%s: ", m.config.UserDisplayName))) {
|
||||||
m.logger.Info("message is addressed to bot", slog.String("event_id", eventID.String()))
|
m.logger.Info("message is addressed to bot", slog.String("event_id", eventID.String()))
|
||||||
conv = NewConversation(eventID, content.Body)
|
conv = NewConversation(eventID, m.config.SystemPrompt, content.Body)
|
||||||
m.conversations = append(m.conversations, conv)
|
m.conversations = append(m.conversations, conv)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,8 +6,6 @@ import (
|
||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
const systemPrompt = "You are a chatbot that helps people by responding to their questions with short messages."
|
|
||||||
|
|
||||||
type Character struct {
|
type Character struct {
|
||||||
UserID string
|
UserID string
|
||||||
Password string
|
Password string
|
||||||
|
@ -27,7 +25,7 @@ type Conversation struct {
|
||||||
Messages []Message
|
Messages []Message
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConversation(id id.EventID, question string) *Conversation {
|
func NewConversation(id id.EventID, systemPrompt, question string) *Conversation {
|
||||||
return &Conversation{
|
return &Conversation{
|
||||||
Messages: []Message{
|
Messages: []Message{
|
||||||
{
|
{
|
||||||
|
|
16
main.go
16
main.go
|
@ -13,7 +13,7 @@ func main() {
|
||||||
|
|
||||||
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
||||||
|
|
||||||
matrixClient := bot.New(bot.Config{
|
botConfigs := []bot.Config{{
|
||||||
Homeserver: getParam("MATRIX_HOMESERVER", "http://localhost"),
|
Homeserver: getParam("MATRIX_HOMESERVER", "http://localhost"),
|
||||||
UserID: getParam("MATRIX_USER_ID", "@bot:localhost"),
|
UserID: getParam("MATRIX_USER_ID", "@bot:localhost"),
|
||||||
UserPassword: getParam("MATRIX_PASSWORD", "secret"),
|
UserPassword: getParam("MATRIX_PASSWORD", "secret"),
|
||||||
|
@ -22,13 +22,17 @@ func main() {
|
||||||
DBPath: getParam("BOT_DB_PATH", "bot.db"),
|
DBPath: getParam("BOT_DB_PATH", "bot.db"),
|
||||||
Pickle: getParam("BOT_PICKLE", "scrambled"),
|
Pickle: getParam("BOT_PICKLE", "scrambled"),
|
||||||
OpenAIKey: getParam("OPENAI_API_KEY", "no key"),
|
OpenAIKey: getParam("OPENAI_API_KEY", "no key"),
|
||||||
}, logger)
|
SystemPrompt: "You are a chatbot that helps people by responding to their questions with short messages.",
|
||||||
|
}}
|
||||||
|
|
||||||
if err := matrixClient.Init(); err != nil {
|
for _, bc := range botConfigs {
|
||||||
logger.Error(err.Error())
|
b := bot.New(bc, logger)
|
||||||
os.Exit(1)
|
if err := b.Init(); err != nil {
|
||||||
|
logger.Error(err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
go b.Run()
|
||||||
}
|
}
|
||||||
go matrixClient.Run()
|
|
||||||
|
|
||||||
done := make(chan os.Signal)
|
done := make(chan os.Signal)
|
||||||
signal.Notify(done, os.Interrupt)
|
signal.Notify(done, os.Interrupt)
|
||||||
|
|
Loading…
Reference in New Issue