memory leak
This commit is contained in:
60
plugins/ai.go
Normal file
60
plugins/ai.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"kurumibot/database/mdb"
|
||||
"kurumibot/database/red"
|
||||
"kurumibot/utils/ai"
|
||||
"strings"
|
||||
|
||||
"git.nix13.pw/scuroneko/laniakea"
|
||||
)
|
||||
|
||||
func RegisterAi(bot *laniakea.Bot) {
|
||||
p := laniakea.NewPlugin("AI")
|
||||
p.Command(gpt, "gpt")
|
||||
bot.AddPlugins(p.Build())
|
||||
}
|
||||
|
||||
func gpt(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
q := strings.Join(ctx.Args, " ")
|
||||
api := ai.NewOpenAIAPI(ai.GPTBaseUrl, "", "anthropic/claude-sonnet-4")
|
||||
defer api.Close()
|
||||
|
||||
aiRedisRep := red.NewAiRepository(db)
|
||||
chatId, err := aiRedisRep.GetOrCreateChatId(ctx.FromID)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
history, err := mdb.GetGptChatHistory(db, chatId)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
aiHistory := make([]ai.Message, len(history))
|
||||
for _, m := range history {
|
||||
aiHistory = append(aiHistory, ai.Message{
|
||||
Role: m.Role,
|
||||
Content: m.Message,
|
||||
})
|
||||
}
|
||||
|
||||
m := ctx.Answer("Генерация запущена...")
|
||||
res, err := api.CreateCompletion(aiHistory, q, 1.0)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
answer := res.Choices[0].Message.Content
|
||||
m.Delete()
|
||||
err = mdb.UpdateGptChatHistory(db, chatId, "user", q)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
}
|
||||
err = mdb.UpdateGptChatHistory(db, chatId, "assistant", answer)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
}
|
||||
|
||||
ctx.Answer(answer)
|
||||
}
|
||||
Reference in New Issue
Block a user