From b9f14f88a914feffd7bfc4f3e672558b48692a3d Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Tue, 5 Mar 2024 18:15:03 +0100 Subject: [PATCH] add personal message context --- Dockerfile | 2 +- cmd/nourybot/commands.go | 2 +- cmd/nourybot/generate.go | 82 +++++++++++++++++++++++++++++++++------- cmd/nourybot/main.go | 38 +++++++------------ 4 files changed, 85 insertions(+), 39 deletions(-) diff --git a/Dockerfile b/Dockerfile index d65be95..5454126 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,4 +20,4 @@ RUN go get -d -v ./... RUN go build ./cmd/nourybot # Run the executable -CMD [ "./nourybot", "jq"] +CMD [ "./nourybot"] diff --git a/cmd/nourybot/commands.go b/cmd/nourybot/commands.go index b1003f7..1e7d741 100644 --- a/cmd/nourybot/commands.go +++ b/cmd/nourybot/commands.go @@ -64,7 +64,7 @@ func (app *application) handleCommand(message twitch.PrivateMessage) { if msgLen < 2 { reply = "Not enough arguments provided. Usage: ()bttv " } else { - app.generateChat(target, message.Message[6:len(message.Message)]) + app.chatPersonalContext(target, message.User.Name, message.Message[6:len(message.Message)]) } if reply != "" { diff --git a/cmd/nourybot/generate.go b/cmd/nourybot/generate.go index 2bcc41a..8bf18fe 100644 --- a/cmd/nourybot/generate.go +++ b/cmd/nourybot/generate.go @@ -16,6 +16,7 @@ type ollamaResponse struct { } type ollamaRequest struct { + Format string `json:"format"` Model string `json:"model"` Prompt string `json:"prompt"` Stream bool `json:"stream"` @@ -34,20 +35,24 @@ func startMessage() []ollamaMessage { 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 msg []ollamaMessage olm := ollamaMessage{} olm.Role = "user" 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.Messages = msgStore + requestBody.Messages = app.PersonalMsgStore[username] requestBody.Prompt = input requestBody.Stream = false @@ -56,8 +61,60 @@ func (app *application) generateChat(target, input string) { app.Log.Error(err) } - app.Log.Infow("msg before", - "msg", msgStore, + resp, err := http.Post("http://localhost:11434/api/chat", "application/json", bytes.NewBuffer(marshalled)) + 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)) if err != nil { @@ -77,20 +134,19 @@ func (app *application) generateChat(target, input string) { } olm.Role = responseObject.Message.Role olm.Content = responseObject.Message.Content - msgStore = append(msgStore, olm) + app.MsgStore = append(app.MsgStore, olm) - app.Log.Infow("msg after", - "msg", msgStore, + app.Log.Infow("MsgStore", + "app.MsgStore", app.MsgStore, ) - app.Log.Info() app.Send(target, responseObject.Message.Content) //app.Send(target, responseObject.Response) } -func (app *application) generate(target, input string) { +func (app *application) generateNoContext(target, input string) { 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.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." diff --git a/cmd/nourybot/main.go b/cmd/nourybot/main.go index 7fa8bc9..b9d3c7a 100644 --- a/cmd/nourybot/main.go +++ b/cmd/nourybot/main.go @@ -13,28 +13,18 @@ import ( ) type config struct { - twitchUsername string - twitchOauth string - twitchClientId string - twitchClientSecret string - eventSubSecret string - twitchID string - wolframAlphaAppID string - commandPrefix string - env string - db struct { - dsn string - maxOpenConns int - maxIdleConns int - maxIdleTime string - } + twitchUsername string + twitchOauth string + commandPrefix string } type application struct { - TwitchClient *twitch.Client - Log *zap.SugaredLogger - Environment string - Config config + TwitchClient *twitch.Client + Log *zap.SugaredLogger + Environment string + Config config + PersonalMsgStore map[string][]ollamaMessage + MsgStore []ollamaMessage } func main() { @@ -70,11 +60,12 @@ func run(ctx context.Context, w io.Writer, args []string) error { cfg.twitchOauth = os.Getenv("TWITCH_OAUTH") tc := twitch.NewClient(cfg.twitchUsername, cfg.twitchOauth) + personalMsgStore := make(map[string][]ollamaMessage) app := &application{ - TwitchClient: tc, - Log: sugar, - Config: cfg, - Environment: cfg.env, + TwitchClient: tc, + Log: sugar, + Config: cfg, + PersonalMsgStore: personalMsgStore, } // 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 app.Log.Infow("Successfully connected to Twitch Servers", "Bot username", cfg.twitchUsername, - "Environment", cfg.env, ) })