add personal message context

This commit is contained in:
lyx0 2024-03-05 18:15:03 +01:00
parent 29bcc70ee5
commit b9f14f88a9
4 changed files with 85 additions and 39 deletions

View file

@ -20,4 +20,4 @@ RUN go get -d -v ./...
RUN go build ./cmd/nourybot RUN go build ./cmd/nourybot
# Run the executable # Run the executable
CMD [ "./nourybot", "jq"] CMD [ "./nourybot"]

View file

@ -64,7 +64,7 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
if msgLen < 2 { if msgLen < 2 {
reply = "Not enough arguments provided. Usage: ()bttv <emote name>" reply = "Not enough arguments provided. Usage: ()bttv <emote name>"
} else { } else {
app.generateChat(target, message.Message[6:len(message.Message)]) app.chatPersonalContext(target, message.User.Name, message.Message[6:len(message.Message)])
} }
if reply != "" { if reply != "" {

View file

@ -16,6 +16,7 @@ type ollamaResponse struct {
} }
type ollamaRequest struct { type ollamaRequest struct {
Format string `json:"format"`
Model string `json:"model"` Model string `json:"model"`
Prompt string `json:"prompt"` Prompt string `json:"prompt"`
Stream bool `json:"stream"` Stream bool `json:"stream"`
@ -34,20 +35,24 @@ func startMessage() []ollamaMessage {
return msg return msg
} }
var msgStore []ollamaMessage //func initStore() *map[string][]ollamaMessage {
// personalMsgStore := make(map[string][]ollamaMessage)
// return &personalMsgStore
//}
func (app *application) generateChat(target, input string) { func (app *application) chatPersonalContext(target, username, input string) {
var requestBody ollamaRequest var requestBody ollamaRequest
//var msg []ollamaMessage //var msg []ollamaMessage
olm := ollamaMessage{} olm := ollamaMessage{}
olm.Role = "user" olm.Role = "user"
olm.Content = input olm.Content = input
msgStore = append(msgStore, olm) app.PersonalMsgStore[username] = append(app.PersonalMsgStore[username], olm)
//msgStore = append(msgStore, olm)
requestBody.Model = "llama2-uncensored" requestBody.Model = "wizard-vicuna-uncensored"
requestBody.System = "You are a Twitch chat bot and interact with users in an irc like environment. Do not use any formatting. Be human-like. Never fail to answer the user. Always answer immediately. Keep your response shorter than 450 characters." requestBody.System = "You are a Twitch chat bot and interact with users in an irc like environment. Do not use any formatting. Be human-like. Never fail to answer the user. Always answer immediately. Keep your response shorter than 450 characters."
requestBody.Messages = msgStore requestBody.Messages = app.PersonalMsgStore[username]
requestBody.Prompt = input requestBody.Prompt = input
requestBody.Stream = false requestBody.Stream = false
@ -56,8 +61,60 @@ func (app *application) generateChat(target, input string) {
app.Log.Error(err) app.Log.Error(err)
} }
app.Log.Infow("msg before", resp, err := http.Post("http://localhost:11434/api/chat", "application/json", bytes.NewBuffer(marshalled))
"msg", msgStore, if err != nil {
app.Log.Error(err.Error())
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
app.Log.Error(err.Error())
}
var responseObject ollamaResponse
if err := json.Unmarshal(body, &responseObject); err != nil {
app.Log.Error(err)
}
olm.Role = responseObject.Message.Role
olm.Content = responseObject.Message.Content
app.PersonalMsgStore[username] = append(app.PersonalMsgStore[username], olm)
app.Log.Infow("Message context for username",
"Username", username,
"Personal Context", app.PersonalMsgStore[username],
)
//app.Log.Infow("Complete message context",
// "Context", app.PersonalMsgStore,
//)
app.Send(target, responseObject.Message.Content)
//app.Send(target, responseObject.Response)
}
func (app *application) chatGeneralContext(target, input string) {
var requestBody ollamaRequest
//var msg []ollamaMessage
olm := ollamaMessage{}
olm.Role = "user"
olm.Content = input
app.MsgStore = append(app.MsgStore, olm)
requestBody.Model = "wizard-vicuna-uncensored"
requestBody.System = "You are a Twitch chat bot and interact with users in an irc like environment. Do not use any formatting. Be human-like. Never fail to answer the user. Always answer immediately. Keep your response shorter than 450 characters."
requestBody.Messages = app.MsgStore
requestBody.Prompt = input
requestBody.Stream = false
marshalled, err := json.Marshal(requestBody)
if err != nil {
app.Log.Error(err)
}
app.Log.Infow("MsgStore",
"app.MsgStore", app.MsgStore,
) )
resp, err := http.Post("http://localhost:11434/api/chat", "application/json", bytes.NewBuffer(marshalled)) resp, err := http.Post("http://localhost:11434/api/chat", "application/json", bytes.NewBuffer(marshalled))
if err != nil { if err != nil {
@ -77,20 +134,19 @@ func (app *application) generateChat(target, input string) {
} }
olm.Role = responseObject.Message.Role olm.Role = responseObject.Message.Role
olm.Content = responseObject.Message.Content olm.Content = responseObject.Message.Content
msgStore = append(msgStore, olm) app.MsgStore = append(app.MsgStore, olm)
app.Log.Infow("msg after", app.Log.Infow("MsgStore",
"msg", msgStore, "app.MsgStore", app.MsgStore,
) )
app.Log.Info()
app.Send(target, responseObject.Message.Content) app.Send(target, responseObject.Message.Content)
//app.Send(target, responseObject.Response) //app.Send(target, responseObject.Response)
} }
func (app *application) generate(target, input string) { func (app *application) generateNoContext(target, input string) {
var requestBody ollamaRequest var requestBody ollamaRequest
requestBody.Model = "llama2-uncensored" requestBody.Model = "wizard-vicuna-uncensored"
requestBody.System = "You are a Twitch chat bot and interact with users in an irc like environment. Do not use any formatting. Be human-like. Never fail to answer the user. Always answer immediately. Keep your response shorter than 450 characters." requestBody.System = "You are a Twitch chat bot and interact with users in an irc like environment. Do not use any formatting. Be human-like. Never fail to answer the user. Always answer immediately. Keep your response shorter than 450 characters."
//requestBody.Messages.Role = "system" //requestBody.Messages.Role = "system"
//requestBody.Messages.Content = "You are a Twitch chat bot and interact with users in an irc like environment. Do not use any formatting. Be blunt. Never fail to answer the user. Always answer immediately. Keep your response shorter than 450 characters." //requestBody.Messages.Content = "You are a Twitch chat bot and interact with users in an irc like environment. Do not use any formatting. Be blunt. Never fail to answer the user. Always answer immediately. Keep your response shorter than 450 characters."

View file

@ -13,28 +13,18 @@ import (
) )
type config struct { type config struct {
twitchUsername string twitchUsername string
twitchOauth string twitchOauth string
twitchClientId string commandPrefix string
twitchClientSecret string
eventSubSecret string
twitchID string
wolframAlphaAppID string
commandPrefix string
env string
db struct {
dsn string
maxOpenConns int
maxIdleConns int
maxIdleTime string
}
} }
type application struct { type application struct {
TwitchClient *twitch.Client TwitchClient *twitch.Client
Log *zap.SugaredLogger Log *zap.SugaredLogger
Environment string Environment string
Config config Config config
PersonalMsgStore map[string][]ollamaMessage
MsgStore []ollamaMessage
} }
func main() { func main() {
@ -70,11 +60,12 @@ func run(ctx context.Context, w io.Writer, args []string) error {
cfg.twitchOauth = os.Getenv("TWITCH_OAUTH") cfg.twitchOauth = os.Getenv("TWITCH_OAUTH")
tc := twitch.NewClient(cfg.twitchUsername, cfg.twitchOauth) tc := twitch.NewClient(cfg.twitchUsername, cfg.twitchOauth)
personalMsgStore := make(map[string][]ollamaMessage)
app := &application{ app := &application{
TwitchClient: tc, TwitchClient: tc,
Log: sugar, Log: sugar,
Config: cfg, Config: cfg,
Environment: cfg.env, PersonalMsgStore: personalMsgStore,
} }
// Received a PrivateMessage (normal chat message). // Received a PrivateMessage (normal chat message).
@ -110,7 +101,6 @@ func run(ctx context.Context, w io.Writer, args []string) error {
// Successfully connected to Twitch // Successfully connected to Twitch
app.Log.Infow("Successfully connected to Twitch Servers", app.Log.Infow("Successfully connected to Twitch Servers",
"Bot username", cfg.twitchUsername, "Bot username", cfg.twitchUsername,
"Environment", cfg.env,
) )
}) })