some changes
This commit is contained in:
122
plugins/ai.go
122
plugins/ai.go
@@ -1,10 +1,10 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
"ymgb/database"
|
||||
"ymgb/database/mdb"
|
||||
"ymgb/database/red"
|
||||
"ymgb/openai"
|
||||
"ymgb/utils/ai"
|
||||
|
||||
"git.nix13.pw/scuroneko/laniakea"
|
||||
@@ -12,50 +12,96 @@ import (
|
||||
|
||||
func RegisterAi() *laniakea.Plugin[database.Context] {
|
||||
p := laniakea.NewPlugin[database.Context]("AI")
|
||||
p.AddCommand(p.NewCommand(gpt, "gpt").SkipCommandAutoGen())
|
||||
p.AddCommand(p.NewCommand(gptTest, "gpt").SkipCommandAutoGen())
|
||||
//p.AddCommand(p.NewCommand(gptTest, "gpt2").SkipCommandAutoGen())
|
||||
return p
|
||||
}
|
||||
|
||||
func gpt(ctx *laniakea.MsgContext, db *database.Context) {
|
||||
func gptTest(ctx *laniakea.MsgContext, _ *database.Context) {
|
||||
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)
|
||||
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
|
||||
}
|
||||
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)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
ctx.Answer(answer)
|
||||
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)
|
||||
//}
|
||||
|
||||
Reference in New Issue
Block a user