mirror of
https://github.com/lyx0/ollama-twitch-bot.git
synced 2024-11-06 18:52:03 +01:00
add personal message context
This commit is contained in:
parent
29bcc70ee5
commit
b9f14f88a9
4 changed files with 85 additions and 39 deletions
|
@ -20,4 +20,4 @@ RUN go get -d -v ./...
|
|||
RUN go build ./cmd/nourybot
|
||||
|
||||
# Run the executable
|
||||
CMD [ "./nourybot", "jq"]
|
||||
CMD [ "./nourybot"]
|
||||
|
|
|
@ -64,7 +64,7 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
|
|||
if msgLen < 2 {
|
||||
reply = "Not enough arguments provided. Usage: ()bttv <emote name>"
|
||||
} else {
|
||||
app.generateChat(target, message.Message[6:len(message.Message)])
|
||||
app.chatPersonalContext(target, message.User.Name, message.Message[6:len(message.Message)])
|
||||
}
|
||||
|
||||
if reply != "" {
|
||||
|
|
|
@ -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."
|
||||
|
|
|
@ -15,19 +15,7 @@ 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
|
||||
}
|
||||
}
|
||||
|
||||
type application struct {
|
||||
|
@ -35,6 +23,8 @@ type application struct {
|
|||
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,
|
||||
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,
|
||||
)
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in a new issue