some fixes and changes
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"kurumibot/database/red"
|
||||
"kurumibot/utils"
|
||||
"kurumibot/utils/ai"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -322,53 +323,40 @@ func rpSettingList(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
|
||||
func chatStat(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
redisRpRep := red.NewRPRepository(db)
|
||||
waifuId := redisRpRep.GetSelectedWaifu(ctx.FromID)
|
||||
if waifuId == 0 {
|
||||
ctx.Answer("Не выбрана вайфу")
|
||||
chat, err := redisRpRep.GetChat(ctx.FromID)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
chatId := redisRpRep.GetChatId(ctx.FromID, waifuId)
|
||||
if chatId == "" {
|
||||
ctx.Answer("Нет активного чата")
|
||||
return
|
||||
}
|
||||
messageCount, err := mdb.GetRPChatHistorySize(db, chatId)
|
||||
messageCount, err := mdb.GetRPChatHistorySize(db, chat.ID.String())
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
tokens := redisRpRep.GetChatTokens(ctx.FromID, waifuId)
|
||||
kb := laniakea.NewInlineKeyboard(1)
|
||||
kb.AddCallbackButtonStyle("Сжать чат", laniakea.ButtonStyleSuccess, "rp.compress_chat")
|
||||
kb.AddCallbackButtonStyle("На главную", laniakea.ButtonStyleDanger, "rp.info")
|
||||
out := []string{
|
||||
"Статистика чата",
|
||||
fmt.Sprintf("*ID*: `%s`", chatId),
|
||||
fmt.Sprintf("*ID*: `%s`", chat.ID.String()),
|
||||
fmt.Sprintf("*Кол-во сообщений*: %d", messageCount),
|
||||
fmt.Sprintf("*Кол-во токенов*: %d", tokens),
|
||||
fmt.Sprintf("*Кол-во токенов*: %d", chat.ChatTokens),
|
||||
}
|
||||
psqlRpRep := psql.NewRPRepository(db)
|
||||
scenarioIds := redisRpRep.GetChatScenariosIDs(ctx.FromID, waifuId)
|
||||
if len(scenarioIds) > 0 && scenarioIds[0] > 0 {
|
||||
scenarioNames := make([]string, len(scenarioIds))
|
||||
for i, id := range scenarioIds {
|
||||
scenario, err := psqlRpRep.GetScenario(id)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
|
||||
if len(chat.Scenarios) > 0 && chat.Scenarios[0].ID > 0 {
|
||||
scenarioNames := make([]string, len(chat.Scenarios))
|
||||
for i, scenario := range chat.Scenarios {
|
||||
if scenario.ID == 0 {
|
||||
continue
|
||||
}
|
||||
scenarioNames[i] = fmt.Sprintf("%s (ID: %d)", scenario.Name, scenario.ID)
|
||||
}
|
||||
out = append(out, fmt.Sprintf("*Выбранные сценарии*: %s", strings.Join(scenarioNames, ", ")))
|
||||
}
|
||||
|
||||
settingId := redisRpRep.GetChatSettingID(ctx.FromID, waifuId)
|
||||
if settingId > 0 {
|
||||
setting, err := psqlRpRep.GetSetting(settingId)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
}
|
||||
out = append(out, fmt.Sprintf("*Выбранный сеттинг*: %s (ID: %d)", setting.Name, setting.ID))
|
||||
if chat.Setting != nil {
|
||||
out = append(out, fmt.Sprintf("*Выбранный сеттинг*: %s (ID: %d)", chat.Setting.Name, chat.Setting.ID))
|
||||
}
|
||||
ctx.EditCallback(strings.Join(out, "\n"), kb)
|
||||
ctx.AnswerCbQuery()
|
||||
@@ -449,7 +437,7 @@ func newChatStage2(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
if len(scenariosIds) == 0 {
|
||||
scenariosIds = append(scenariosIds, 0)
|
||||
}
|
||||
kb.AddCallbackButton("Продолжить", "rp.new_chat", utils.AppendToInt(settingId, selectedScenariosIds)...)
|
||||
kb.AddCallbackButtonStyle("Создать", laniakea.ButtonStyleSuccess, "rp.new_chat", utils.AppendToInt(settingId, selectedScenariosIds)...)
|
||||
kb.AddLine()
|
||||
kb.AddCallbackButtonStyle("Назад", laniakea.ButtonStyleDanger, "rp.new_chat_s1")
|
||||
ctx.EditCallback(strings.Join(out, "\n"), kb)
|
||||
@@ -476,6 +464,13 @@ func newChat(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
return
|
||||
}
|
||||
|
||||
chat, err := redisRpRep.GetChat(ctx.FromID)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println(ctx.Args)
|
||||
chatPrompt := ""
|
||||
settingId, err := strconv.Atoi(ctx.Args[0])
|
||||
if err != nil {
|
||||
@@ -483,7 +478,7 @@ func newChat(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
return
|
||||
}
|
||||
if settingId > 0 {
|
||||
scenario, err := psqlRpRep.GetSetting(settingId)
|
||||
setting, err := psqlRpRep.GetSetting(settingId)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
ctx.Answerf("Сеттинг №%d не найден", settingId)
|
||||
@@ -492,10 +487,13 @@ func newChat(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
}
|
||||
return
|
||||
}
|
||||
chatPrompt = "Вот краткое описание мира(сеттинг): " + scenario.Prompt + "."
|
||||
chat.Setting = &setting
|
||||
chat.SettingID = settingId
|
||||
chatPrompt = "Вот краткое описание мира(сеттинг): " + setting.Prompt + "."
|
||||
}
|
||||
|
||||
scenariosIds := utils.Map(ctx.Args[1:], utils.StringToInt)
|
||||
chat.Scenarios = make([]psql.RPScenario, 0)
|
||||
if len(scenariosIds) > 0 && scenariosIds[0] > 0 {
|
||||
rep := psql.NewRPRepository(db)
|
||||
var scenariosPrompt []string
|
||||
@@ -510,39 +508,23 @@ func newChat(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
}
|
||||
return
|
||||
}
|
||||
chat.Scenarios = append(chat.Scenarios, scenario)
|
||||
scenariosPrompt = append(scenariosPrompt, scenario.Prompt)
|
||||
}
|
||||
|
||||
chatPrompt += "Вот дополнительная информация: " + strings.Join(scenariosPrompt, ".")
|
||||
}
|
||||
|
||||
if chatPrompt != "" {
|
||||
err = redisRpRep.SetChatPrompt(ctx.FromID, waifuId, chatPrompt)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
err = redisRpRep.SetChatSettingID(ctx.FromID, waifuId, settingId)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
}
|
||||
err = redisRpRep.SetChatScenariosIDs(ctx.FromID, waifuId, strings.Join(utils.Map(scenariosIds, utils.AnyToString), ","))
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
}
|
||||
err = redisRpRep.SetCounter(ctx.FromID, waifuId, 0)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
}
|
||||
err = redisRpRep.SetChatTokens(ctx.FromID, waifuId, 0)
|
||||
chat.Prompt = chatPrompt
|
||||
chat.Counter = 0
|
||||
chat.ChatTokens = 0
|
||||
err = redisRpRep.SaveChat(chat)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
kb := laniakea.NewInlineKeyboard(2)
|
||||
kb.AddCallbackButton("На главную", "rp.info")
|
||||
kb.AddCallbackButtonStyle("На главную", laniakea.ButtonStyleSuccess, "rp.info")
|
||||
kb.AddCallbackButtonStyle("Закрыть", laniakea.ButtonStyleDanger, "general.close")
|
||||
ctx.EditCallback("Был создан новый чат. Для общения используй `/г промпт`.", kb)
|
||||
ctx.AnswerCbQuery()
|
||||
@@ -666,7 +648,7 @@ func generate(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
ctx.SendAction(tgapi.ChatActionTyping)
|
||||
api := ai.NewOpenAIAPI(ai.GPTBaseUrl, "", rpUser.Model.Key)
|
||||
defer api.Close()
|
||||
res, err := api.CreateCompletion(messages, userMessage, 1.0)
|
||||
res, err := api.CreateCompletion(messages, userMessage, 0.5)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
@@ -784,7 +766,7 @@ func regenerateResponse(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext)
|
||||
messages = messages.Pop(count - 2)
|
||||
|
||||
ctx.EditCallback("Генерация запущена...", nil)
|
||||
res, err := api.CreateCompletion(messages, userReq.Message, 1.0)
|
||||
res, err := api.CreateCompletion(messages, userReq.Message, 0.5)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user