memory leak

This commit is contained in:
2026-02-05 15:14:47 +03:00
parent b894cbd9c3
commit 0fded455b8
22 changed files with 420 additions and 170 deletions

60
plugins/ai.go Normal file
View 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)
}