Files
YaeMikoBot/plugins/ai.go
2026-03-02 00:58:43 +03:00

108 lines
2.3 KiB
Go

package plugins
import (
"io"
"strings"
"ymgb/database"
"ymgb/openai"
"ymgb/utils/ai"
"git.nix13.pw/scuroneko/laniakea"
)
func RegisterAi() *laniakea.Plugin[database.Context] {
p := laniakea.NewPlugin[database.Context]("AI")
p.AddCommand(p.NewCommand(gptTest, "gpt").SkipCommandAutoGen())
//p.AddCommand(p.NewCommand(gptTest, "gpt2").SkipCommandAutoGen())
return p
}
func gptTest(ctx *laniakea.MsgContext, _ *database.Context) {
q := strings.Join(ctx.Args, " ")
m := ctx.Answer("Генерация запущена")
api := openai.NewOpenAIAPI(ai.GPTBaseUrl, "", "anthropic/claude-sonnet-4")
resp, err := api.CreateCompletionStream([]openai.Message{}, q, 1.0)
if err != nil {
m.Delete()
ctx.Error(err)
return
}
draft := ctx.NewDraft()
for r, err := range resp {
if err == io.EOF {
break
}
if m != nil {
m.Delete()
m = nil
}
if err != nil {
ctx.Error(err)
return
}
if len(r.Choices) == 0 {
continue
}
content := r.Choices[0].Delta.Content
if content == "" {
continue
}
err = draft.Push(content)
if err != nil {
ctx.Error(err)
//draft.Flush()
break
}
}
err = draft.Flush()
if err != nil {
ctx.Error(err)
}
}
//func gpt(ctx *laniakea.MsgContext, db *database.Context) {
// q := strings.Join(ctx.Args, " ")
// api := openai.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([]openai.Message, len(history))
// for _, m := range history {
// aiHistory = append(aiHistory, openai.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)
//}