chat compress and some features
This commit is contained in:
@@ -27,7 +27,7 @@ func RegisterRP(bot *laniakea.Bot) {
|
||||
rp.Payload(rpModelSet, "rp.model_set")
|
||||
rp.Payload(rpScenarioList, "rp.scenario_list")
|
||||
rp.Payload(rpSettingList, "rp.setting_list")
|
||||
rp.Payload(chatStat, "rp.tokens")
|
||||
rp.Payload(chatStat, "rp.chat_stat")
|
||||
|
||||
rp.Payload(newChatStage1, "rp.new_chat_s1")
|
||||
rp.Payload(newChatStage2, "rp.new_chat_s2")
|
||||
@@ -98,7 +98,7 @@ func rpInfo(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
}
|
||||
|
||||
kb := laniakea.NewInlineKeyboard(2)
|
||||
kb.AddCallbackButton("Статистика чата", "rp.tokens")
|
||||
kb.AddCallbackButton("Статистика чата", "rp.chat_stat")
|
||||
kb.AddCallbackButton("Сменить вайфу", "rp.waifu_list")
|
||||
kb.AddCallbackButton("Сменить пресет", "rp.preset_list")
|
||||
kb.AddCallbackButton("Сменить модель", "rp.model_list")
|
||||
@@ -167,7 +167,7 @@ func rpPresetsList(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
kb := laniakea.NewInlineKeyboard(2)
|
||||
for i, preset := range presets {
|
||||
out[i] = fmt.Sprintf(
|
||||
"*%s*\n%s",
|
||||
"*%s* - %s",
|
||||
laniakea.EscapeMarkdown(preset.Name), preset.Description,
|
||||
)
|
||||
kb.AddCallbackButton(preset.Name, "rp.preset_set", preset.ID)
|
||||
@@ -212,7 +212,7 @@ func rpModelList(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
out := make([]string, len(models))
|
||||
kb := laniakea.NewInlineKeyboard(2)
|
||||
for i, model := range models {
|
||||
out[i] = fmt.Sprintf("*%s* - размер контекста %dK", model.Name, model.ContextSize)
|
||||
out[i] = fmt.Sprintf("*%s* - размер контекста _%dK_", model.Name, model.ContextSize)
|
||||
kb.AddCallbackButton(model.Name, "rp.model_set", model.ID)
|
||||
}
|
||||
kb.AddLine()
|
||||
@@ -266,7 +266,7 @@ func rpSettingList(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
|
||||
out := make([]string, len(settings))
|
||||
for i, setting := range settings {
|
||||
out[i] = fmt.Sprintf("*%s* - _%s_\n", setting.Name, setting.Description)
|
||||
out[i] = fmt.Sprintf("*%s* - %s\n", setting.Name, setting.Description)
|
||||
}
|
||||
kb := laniakea.NewInlineKeyboard(1)
|
||||
kb.AddCallbackButton("На главную", "rp.info")
|
||||
@@ -296,12 +296,14 @@ func chatStat(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
}
|
||||
|
||||
tokens := redisRpRep.GetChatTokens(ctx.FromID, waifuId)
|
||||
kb := laniakea.NewInlineKeyboard(1).AddCallbackButton("На главную", "rp.info")
|
||||
kb := laniakea.NewInlineKeyboard(1)
|
||||
kb = kb.AddCallbackButton("Сжать чат", "rp.compress_chat")
|
||||
kb = kb.AddCallbackButton("На главную", "rp.info")
|
||||
out := []string{
|
||||
"Статистика чата",
|
||||
fmt.Sprintf("ID: `%s`", chatId),
|
||||
fmt.Sprintf("Кол-во сообщений: %d", messageCount),
|
||||
fmt.Sprintf("Кол-во токенов: %d", tokens),
|
||||
fmt.Sprintf("*ID*: `%s`", chatId),
|
||||
fmt.Sprintf("*Кол-во сообщений*: %d", messageCount),
|
||||
fmt.Sprintf("*Кол-во токенов*: %d", tokens),
|
||||
}
|
||||
ctx.EditCallback(strings.Join(out, "\n"), kb)
|
||||
}
|
||||
@@ -352,7 +354,7 @@ func newChatStage2(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
}
|
||||
kb := laniakea.NewInlineKeyboard(2)
|
||||
for _, scenario := range scenarios {
|
||||
out = append(out, fmt.Sprintf("*%s* - _%s_", scenario.Name, scenario.Description))
|
||||
out = append(out, fmt.Sprintf("*%s* - %s", scenario.Name, scenario.Description))
|
||||
kb.AddCallbackButton(scenario.Name, "rp.new_chat", settingId, scenario.ID)
|
||||
}
|
||||
kb.AddCallbackButton("Без сценария", "rp.new_chat", 0)
|
||||
@@ -535,18 +537,12 @@ func generate(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
}
|
||||
|
||||
userMessage := strings.TrimSpace(strings.Join(ctx.Args, " "))
|
||||
messages = append(messages, afterHistory, ai.Message{
|
||||
Role: "user",
|
||||
Content: userMessage,
|
||||
})
|
||||
messages = append(messages, afterHistory)
|
||||
|
||||
kb := laniakea.NewInlineKeyboard(1).AddCallbackButton("Отменить", "rp.cancel")
|
||||
m := ctx.Keyboard("Генерация запущена...", kb)
|
||||
api := ai.NewOpenAIAPI(ai.GPTBaseUrl, "", rpUser.Model.Key)
|
||||
res, err := api.CreateCompletion(ai.CreateCompletionReq{
|
||||
Messages: messages,
|
||||
Temperature: 1.0,
|
||||
})
|
||||
res, err := api.CreateCompletion(messages, userMessage, 1.0)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
@@ -568,9 +564,9 @@ func generate(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
ctx.Error(err)
|
||||
}
|
||||
|
||||
rpUser.UsedTokens = rpUser.UsedTokens + res.Usage.TotalTokens
|
||||
rpUser.UsedTokens = rpUser.UsedTokens + int64(len(userMessage)) + int64(len(answerContent))
|
||||
tokens := redisRpRep.GetChatTokens(ctx.FromID, waifuId)
|
||||
tokens += int(res.Usage.CompletionTokens)
|
||||
tokens += len(userMessage) + len(answerContent)
|
||||
err = redisRpRep.SetChatTokens(ctx.FromID, waifuId, tokens)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
@@ -581,12 +577,11 @@ func generate(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
}
|
||||
|
||||
m.Delete()
|
||||
kb = laniakea.NewInlineKeyboard(1).AddCallbackButton("Сжать чат", "rp.compress_chat")
|
||||
ctx.Keyboard(laniakea.EscapeMarkdown(answerContent), kb)
|
||||
ctx.Answer(laniakea.EscapeMarkdown(answerContent))
|
||||
}
|
||||
|
||||
func compress(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
m := ctx.Answer("Запущено сжатие чата.")
|
||||
m := ctx.EditCallback("Запущено сжатие чата…", nil)
|
||||
redisRpRep := red.NewRPRepository(db)
|
||||
waifuId := redisRpRep.GetSelectedWaifu(ctx.FromID)
|
||||
if waifuId == 0 {
|
||||
@@ -615,17 +610,21 @@ func compress(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
Content: m.Message,
|
||||
})
|
||||
}
|
||||
api := ai.NewOpenAIAPI(ai.GPTBaseUrl, "", "deepseek-ai/deepseek-v3.1-terminus")
|
||||
|
||||
//compressModel := "anthropic/claude-sonnet-4"
|
||||
compressModel := "gpt-5.1"
|
||||
//compressModel := "deepseek-ai/deepseek-v3.2"
|
||||
api := ai.NewOpenAIAPI(ai.GPTBaseUrl, "", compressModel)
|
||||
res, err := api.CompressChat(messages)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
}
|
||||
if len(res.Choices) == 0 {
|
||||
m.Edit("Не удалось сжать диалог")
|
||||
m.Edit("Не удалось сжать чат")
|
||||
return
|
||||
}
|
||||
|
||||
compressedHistory := res.Choices[0].Message.Content
|
||||
compressedHistory := strings.TrimSpace(res.Choices[0].Message.Content)
|
||||
compressedHistory = strings.ReplaceAll(compressedHistory, "*", "")
|
||||
|
||||
chatId = uuid.New().String()
|
||||
err = redisRpRep.SetChatId(ctx.FromID, waifuId, chatId)
|
||||
@@ -637,9 +636,21 @@ func compress(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) {
|
||||
err = mdb.UpdateChatHistory(db, chatId, "assistant", compressedHistory)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
return
|
||||
}
|
||||
m.Edit("Сжатие завершено")
|
||||
offset := 20
|
||||
if len(history) < 20 {
|
||||
offset = len(history)
|
||||
}
|
||||
for _, m := range history[len(history)-offset:] {
|
||||
err = mdb.UpdateChatHistory(db, chatId, m.Role, m.Message)
|
||||
if err != nil {
|
||||
ctx.Error(err)
|
||||
}
|
||||
}
|
||||
kb := laniakea.NewInlineKeyboard(1)
|
||||
kb = kb.AddCallbackButton("Назад", "rp.chat_stat")
|
||||
kb = kb.AddCallbackButton("На главную", "rp.info")
|
||||
ctx.EditCallback("Сжатие завершено", kb)
|
||||
}
|
||||
|
||||
func generalClose(ctx *laniakea.MsgContext, _ *laniakea.DatabaseContext) {
|
||||
|
||||
Reference in New Issue
Block a user