opt: optimize the styles of chat page; caculate all tokens of context as chat history's token

This commit is contained in:
RockYang
2023-09-11 13:34:20 +08:00
parent e2c18c4e1e
commit 3cc8c3284a
14 changed files with 95 additions and 60 deletions

View File

@@ -291,6 +291,19 @@ func (h *ChatHandler) Tokens(c *gin.Context) {
return
}
// 如果没有传入 text 字段,则说明是获取当前 reply 总的 token 消耗(带上下文)
if data.Text == "" {
var item model.HistoryMessage
userId, _ := c.Get(types.LoginUserID)
res := h.db.Where("user_id = ?", userId).Last(&item)
if res.Error != nil {
resp.ERROR(c, res.Error.Error())
return
}
resp.SUCCESS(c, item.Tokens)
return
}
tokens, err := utils.CalcTokens(data.Text, data.Model)
if err != nil {
resp.ERROR(c, err.Error())