mirror-ollama-twitch-bot/generate.go

163 lines
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 {
2024-03-05 18:35:46 +01:00
Model string `json:"model"`
CreatedAt string `json:"created_at"`
Response string `json:"response"`
Done bool `json:"done"`
Message ollamaMessage `json:"message"`
2024-03-03 00:07:33 +01:00
}
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"`
}
2024-03-05 18:35:46 +01:00
var requestBody ollamaRequest
2024-03-03 00:07:33 +01:00
2024-03-05 18:35:46 +01:00
// chatUserContext provides additional message context from specifically
// the past interactions of user with the AI since last restart.
func (app *application) chatUserContext(target, username, input string) {
2024-03-03 00:07:33 +01:00
olm := ollamaMessage{}
olm.Role = "user"
olm.Content = input
2024-03-05 18:35:46 +01:00
app.UserMsgStore[username] = append(app.UserMsgStore[username], olm)
2024-03-03 00:07:33 +01:00
2024-03-05 19:59:44 +01:00
requestBody.Model = app.OllamaModel
requestBody.System = app.OllamaSystem
2024-03-05 18:35:46 +01:00
requestBody.Messages = app.UserMsgStore[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)
}
2024-03-05 18:35:46 +01:00
2024-03-05 18:15:03 +01:00
olm.Role = responseObject.Message.Role
olm.Content = responseObject.Message.Content
2024-03-05 18:35:46 +01:00
app.UserMsgStore[username] = append(app.UserMsgStore[username], olm)
2024-03-05 18:15:03 +01:00
app.Log.Infow("Message context for username",
"Username", username,
2024-03-05 18:35:46 +01:00
"Personal Context", app.UserMsgStore[username],
2024-03-05 18:15:03 +01:00
)
app.Send(target, responseObject.Message.Content)
}
2024-03-05 18:35:46 +01:00
// chatGeneralContext provides additional message context from every past
// interaction with the AI since last restart.
2024-03-05 18:15:03 +01:00
func (app *application) chatGeneralContext(target, input string) {
olm := ollamaMessage{}
olm.Role = "user"
olm.Content = input
app.MsgStore = append(app.MsgStore, olm)
2024-03-05 19:59:44 +01:00
requestBody.Model = app.OllamaModel
requestBody.System = app.OllamaSystem
2024-03-05 18:15:03 +01:00
requestBody.Messages = app.MsgStore
requestBody.Prompt = input
requestBody.Stream = false
marshalled, err := json.Marshal(requestBody)
if err != nil {
app.Log.Error(err)
}
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)
}
2024-03-05 18:35:46 +01:00
2024-03-03 00:07:33 +01:00
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)
}
2024-03-05 18:35:46 +01:00
// generateNoContext provides no additional message context
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 19:59:44 +01:00
requestBody.Model = app.OllamaModel
requestBody.System = app.OllamaSystem
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)
}
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)
}
2024-03-05 18:35:46 +01:00
2024-03-03 00:07:33 +01:00
app.Send(target, responseObject.Response)
}