2024-05-11 17:56:29 +00:00
|
|
|
package llm
|
2024-05-11 14:37:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/config"
|
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
|
|
|
const gpt3URL = "https://api.openai.com/v1/engines/%s/completions"
|
|
|
|
const gpt3ModURL = "https://api.openai.com/v1/moderations"
|
|
|
|
|
2024-05-11 17:56:29 +00:00
|
|
|
type LLMPlugin struct {
|
2024-05-11 14:37:57 +00:00
|
|
|
b bot.Bot
|
|
|
|
c *config.Config
|
|
|
|
h bot.HandlerTable
|
|
|
|
|
|
|
|
chatCount int
|
|
|
|
chatHistory []chatEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
type chatEntry struct {
|
|
|
|
Role string `json:"role"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
}
|
|
|
|
|
2024-05-11 17:56:29 +00:00
|
|
|
func New(b bot.Bot) *LLMPlugin {
|
|
|
|
p := &LLMPlugin{
|
2024-05-11 14:37:57 +00:00
|
|
|
b: b,
|
|
|
|
c: b.Config(),
|
|
|
|
}
|
|
|
|
p.register()
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2024-05-11 17:56:29 +00:00
|
|
|
func (p *LLMPlugin) register() {
|
2024-05-11 14:37:57 +00:00
|
|
|
p.h = bot.HandlerTable{
|
2024-05-11 18:29:43 +00:00
|
|
|
{
|
|
|
|
Kind: bot.Message, IsCmd: true,
|
|
|
|
Regex: regexp.MustCompile(`(?is)^gpt-prompt: (?P<text>.*)`),
|
|
|
|
HelpText: "set the ChatGPT prompt",
|
|
|
|
Handler: p.setPromptMessage,
|
|
|
|
},
|
2024-05-11 18:12:04 +00:00
|
|
|
{
|
|
|
|
Kind: bot.Message, IsCmd: true,
|
|
|
|
Regex: regexp.MustCompile(`(?is)^llm (?P<text>.*)`),
|
2024-05-24 14:32:52 +00:00
|
|
|
HelpText: "chat completion using first-available AI",
|
|
|
|
Handler: p.chatMessage,
|
2024-05-11 18:12:04 +00:00
|
|
|
},
|
2024-05-11 14:37:57 +00:00
|
|
|
{
|
|
|
|
Kind: bot.Message, IsCmd: true,
|
2024-05-24 14:32:52 +00:00
|
|
|
Regex: regexp.MustCompile(`(?is)^gpt4 (?P<text>.*)`),
|
|
|
|
HelpText: "chat completion using OpenAI",
|
|
|
|
Handler: p.gptMessage,
|
2024-05-11 14:37:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Kind: bot.Message, IsCmd: true,
|
|
|
|
Regex: regexp.MustCompile(`(?is)^got (?P<text>.*)`),
|
|
|
|
HelpText: "chat completion",
|
2024-05-24 14:32:52 +00:00
|
|
|
Handler: p.chatMessage,
|
2024-05-11 14:37:57 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
p.b.RegisterTable(p, p.h)
|
|
|
|
}
|
|
|
|
|
2024-05-11 17:56:29 +00:00
|
|
|
func (p *LLMPlugin) setPromptMessage(r bot.Request) bool {
|
2024-05-11 14:37:57 +00:00
|
|
|
prompt := r.Values["text"]
|
|
|
|
if err := p.setPrompt(prompt); err != nil {
|
|
|
|
resp := fmt.Sprintf("Error: %s", err)
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, resp)
|
|
|
|
}
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf(`Okay. I set the prompt to: "%s"`, prompt))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-05-11 17:56:29 +00:00
|
|
|
func (p *LLMPlugin) chatMessage(r bot.Request) bool {
|
2024-05-11 14:37:57 +00:00
|
|
|
p.chatHistory = append(p.chatHistory, chatEntry{
|
|
|
|
Role: "user",
|
|
|
|
Content: r.Values["text"],
|
|
|
|
})
|
2024-05-11 18:29:43 +00:00
|
|
|
maxHist := p.c.GetInt("gpt.maxhist", 10)
|
|
|
|
if len(p.chatHistory) > maxHist {
|
|
|
|
p.chatHistory = p.chatHistory[len(p.chatHistory)-maxHist:]
|
|
|
|
}
|
2024-05-11 14:37:57 +00:00
|
|
|
chatResp, err := p.llama()
|
|
|
|
if err == nil {
|
|
|
|
p.chatHistory = append(p.chatHistory, chatResp)
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, chatResp.Content)
|
|
|
|
return true
|
|
|
|
} else if !errors.Is(err, InstanceNotFoundError) {
|
|
|
|
log.Error().Err(err).Msgf("error contacting llama")
|
|
|
|
} else {
|
|
|
|
log.Info().Msgf("Llama is currently down")
|
|
|
|
}
|
2024-05-24 14:32:52 +00:00
|
|
|
return p.gptMessage(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LLMPlugin) gptMessage(r bot.Request) bool {
|
2024-05-11 14:37:57 +00:00
|
|
|
resp, err := p.chatGPT(r.Values["text"])
|
|
|
|
if err != nil {
|
|
|
|
resp = fmt.Sprintf("Error: %s", err)
|
|
|
|
}
|
|
|
|
p.chatHistory = append(p.chatHistory, chatEntry{
|
|
|
|
Role: "assistant",
|
|
|
|
Content: resp,
|
|
|
|
})
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, resp)
|
|
|
|
return true
|
|
|
|
}
|