mirror-ollama-twitch-bot/cmd/nourybot/generate.go

183 lines
5.4 KiB
Go
Raw Normal View History

2024-03-03 00:07:33 +01:00
package main
import (
"bytes"
"encoding/json"
"io"
"net/http"
)
type ollamaResponse struct {
Model string `json:"model"`
//CreatedAt string `json:"created_at"`
Response string `json:"response"`
Done bool `json:"done"`
Message ollamaMessage `json:"message"`
}
type ollamaRequest struct {
2024-03-05 18:15:03 +01:00
Format string `json:"format"`
2024-03-03 00:07:33 +01:00
Model string `json:"model"`
Prompt string `json:"prompt"`
Stream bool `json:"stream"`
System string `json:"system"`
Raw bool `json:"raw"`
Messages []ollamaMessage `json:"messages"`
}
type ollamaMessage struct {
Role string `json:"role"`
Content string `json:"content"`
}
func startMessage() []ollamaMessage {
var msg = make([]ollamaMessage, 0)
return msg
}
2024-03-05 18:15:03 +01:00
//func initStore() *map[string][]ollamaMessage {
// personalMsgStore := make(map[string][]ollamaMessage)
// return &personalMsgStore
//}
2024-03-03 00:07:33 +01:00
2024-03-05 18:15:03 +01:00
func (app *application) chatPersonalContext(target, username, input string) {
2024-03-03 00:07:33 +01:00
var requestBody ollamaRequest
//var msg []ollamaMessage
olm := ollamaMessage{}
olm.Role = "user"
olm.Content = input
2024-03-05 18:15:03 +01:00
app.PersonalMsgStore[username] = append(app.PersonalMsgStore[username], olm)
//msgStore = append(msgStore, olm)
2024-03-03 00:07:33 +01:00
2024-03-05 18:15:03 +01:00
requestBody.Model = "wizard-vicuna-uncensored"
2024-03-03 00:07:33 +01:00
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."
2024-03-05 18:15:03 +01:00
requestBody.Messages = app.PersonalMsgStore[username]
2024-03-03 00:07:33 +01:00
requestBody.Prompt = input
requestBody.Stream = false
marshalled, err := json.Marshal(requestBody)
if err != nil {
app.Log.Error(err)
}
2024-03-05 18:15:03 +01:00
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,
2024-03-03 00:07:33 +01:00
)
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
2024-03-05 18:15:03 +01:00
app.MsgStore = append(app.MsgStore, olm)
2024-03-03 00:07:33 +01:00
2024-03-05 18:15:03 +01:00
app.Log.Infow("MsgStore",
"app.MsgStore", app.MsgStore,
2024-03-03 00:07:33 +01:00
)
app.Send(target, responseObject.Message.Content)
//app.Send(target, responseObject.Response)
}
2024-03-05 18:15:03 +01:00
func (app *application) generateNoContext(target, input string) {
2024-03-03 00:07:33 +01:00
var requestBody ollamaRequest
2024-03-05 18:15:03 +01:00
requestBody.Model = "wizard-vicuna-uncensored"
2024-03-03 00:07:33 +01:00
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."
//requestBody.Messages.Role = "user"
//requestBody.Messages.Content = input
requestBody.Prompt = input
requestBody.Stream = false
marshalled, err := json.Marshal(requestBody)
if err != nil {
app.Log.Error(err)
}
resp, err := http.Post("http://localhost:11434/api/generate", "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)
}
//app.Log.Info(responseObject.Message.Content)
//app.Send(target, responseObject.Message.Content)
app.Send(target, responseObject.Response)
}