From f66d59531d392a876bbcac58da4ca3e2a6ddb58d Mon Sep 17 00:00:00 2001 From: RockYang Date: Fri, 26 Jan 2024 16:54:00 +0800 Subject: [PATCH] feat: add model field for chat_item and and chat_history data table --- api/handler/chatimpl/azure_handler.go | 3 +++ api/handler/chatimpl/baidu_handler.go | 3 +++ api/handler/chatimpl/chatglm_handler.go | 3 +++ api/handler/chatimpl/openai_handler.go | 3 +++ api/handler/chatimpl/qwen_handler.go | 3 +++ api/handler/chatimpl/xunfei_handler.go | 3 +++ api/store/model/chat_history.go | 1 + api/store/model/chat_item.go | 3 ++- api/store/vo/chat_history.go | 1 + api/store/vo/chat_item.go | 1 + database/update-v3.2.7.sql | 11 ++++++++++- 11 files changed, 33 insertions(+), 2 deletions(-) diff --git a/api/handler/chatimpl/azure_handler.go b/api/handler/chatimpl/azure_handler.go index f4f75e6b..302ae539 100644 --- a/api/handler/chatimpl/azure_handler.go +++ b/api/handler/chatimpl/azure_handler.go @@ -135,6 +135,7 @@ func (h *ChatHandler) sendAzureMessage( Content: template.HTMLEscapeString(prompt), Tokens: promptToken, UseContext: true, + Model: req.Model, } historyUserMsg.CreatedAt = promptCreatedAt historyUserMsg.UpdatedAt = promptCreatedAt @@ -156,6 +157,7 @@ func (h *ChatHandler) sendAzureMessage( Content: message.Content, Tokens: totalTokens, UseContext: true, + Model: req.Model, } historyReplyMsg.CreatedAt = replyCreatedAt historyReplyMsg.UpdatedAt = replyCreatedAt @@ -181,6 +183,7 @@ func (h *ChatHandler) sendAzureMessage( } else { chatItem.Title = prompt } + chatItem.Model = req.Model h.db.Create(&chatItem) } } diff --git a/api/handler/chatimpl/baidu_handler.go b/api/handler/chatimpl/baidu_handler.go index 62fcbf1a..7771d2c3 100644 --- a/api/handler/chatimpl/baidu_handler.go +++ b/api/handler/chatimpl/baidu_handler.go @@ -160,6 +160,7 @@ func (h *ChatHandler) sendBaiduMessage( Content: template.HTMLEscapeString(prompt), Tokens: promptToken, UseContext: true, + Model: req.Model, } historyUserMsg.CreatedAt = promptCreatedAt historyUserMsg.UpdatedAt = promptCreatedAt @@ -181,6 +182,7 @@ func (h *ChatHandler) sendBaiduMessage( Content: message.Content, Tokens: totalTokens, UseContext: true, + Model: req.Model, } historyReplyMsg.CreatedAt = replyCreatedAt historyReplyMsg.UpdatedAt = replyCreatedAt @@ -205,6 +207,7 @@ func (h *ChatHandler) sendBaiduMessage( } else { chatItem.Title = prompt } + chatItem.Model = req.Model h.db.Create(&chatItem) } } diff --git a/api/handler/chatimpl/chatglm_handler.go b/api/handler/chatimpl/chatglm_handler.go index 5fbacad7..00b88325 100644 --- a/api/handler/chatimpl/chatglm_handler.go +++ b/api/handler/chatimpl/chatglm_handler.go @@ -139,6 +139,7 @@ func (h *ChatHandler) sendChatGLMMessage( Content: template.HTMLEscapeString(prompt), Tokens: promptToken, UseContext: true, + Model: req.Model, } historyUserMsg.CreatedAt = promptCreatedAt historyUserMsg.UpdatedAt = promptCreatedAt @@ -160,6 +161,7 @@ func (h *ChatHandler) sendChatGLMMessage( Content: message.Content, Tokens: totalTokens, UseContext: true, + Model: req.Model, } historyReplyMsg.CreatedAt = replyCreatedAt historyReplyMsg.UpdatedAt = replyCreatedAt @@ -184,6 +186,7 @@ func (h *ChatHandler) sendChatGLMMessage( } else { chatItem.Title = prompt } + chatItem.Model = req.Model h.db.Create(&chatItem) } } diff --git a/api/handler/chatimpl/openai_handler.go b/api/handler/chatimpl/openai_handler.go index c4f54176..ff2fd20b 100644 --- a/api/handler/chatimpl/openai_handler.go +++ b/api/handler/chatimpl/openai_handler.go @@ -206,6 +206,7 @@ func (h *ChatHandler) sendOpenAiMessage( Content: template.HTMLEscapeString(prompt), Tokens: promptToken, UseContext: useContext, + Model: req.Model, } historyUserMsg.CreatedAt = promptCreatedAt historyUserMsg.UpdatedAt = promptCreatedAt @@ -235,6 +236,7 @@ func (h *ChatHandler) sendOpenAiMessage( Content: message.Content, Tokens: totalTokens, UseContext: useContext, + Model: req.Model, } historyReplyMsg.CreatedAt = replyCreatedAt historyReplyMsg.UpdatedAt = replyCreatedAt @@ -260,6 +262,7 @@ func (h *ChatHandler) sendOpenAiMessage( } else { chatItem.Title = prompt } + chatItem.Model = req.Model h.db.Create(&chatItem) } } diff --git a/api/handler/chatimpl/qwen_handler.go b/api/handler/chatimpl/qwen_handler.go index 0b3e148d..f7805304 100644 --- a/api/handler/chatimpl/qwen_handler.go +++ b/api/handler/chatimpl/qwen_handler.go @@ -160,6 +160,7 @@ func (h *ChatHandler) sendQWenMessage( Content: template.HTMLEscapeString(prompt), Tokens: promptToken, UseContext: true, + Model: req.Model, } historyUserMsg.CreatedAt = promptCreatedAt historyUserMsg.UpdatedAt = promptCreatedAt @@ -181,6 +182,7 @@ func (h *ChatHandler) sendQWenMessage( Content: message.Content, Tokens: totalTokens, UseContext: true, + Model: req.Model, } historyReplyMsg.CreatedAt = replyCreatedAt historyReplyMsg.UpdatedAt = replyCreatedAt @@ -205,6 +207,7 @@ func (h *ChatHandler) sendQWenMessage( } else { chatItem.Title = prompt } + chatItem.Model = req.Model h.db.Create(&chatItem) } } diff --git a/api/handler/chatimpl/xunfei_handler.go b/api/handler/chatimpl/xunfei_handler.go index b1b33d16..f0890403 100644 --- a/api/handler/chatimpl/xunfei_handler.go +++ b/api/handler/chatimpl/xunfei_handler.go @@ -198,6 +198,7 @@ func (h *ChatHandler) sendXunFeiMessage( Content: template.HTMLEscapeString(prompt), Tokens: promptToken, UseContext: true, + Model: req.Model, } historyUserMsg.CreatedAt = promptCreatedAt historyUserMsg.UpdatedAt = promptCreatedAt @@ -219,6 +220,7 @@ func (h *ChatHandler) sendXunFeiMessage( Content: message.Content, Tokens: totalTokens, UseContext: true, + Model: req.Model, } historyReplyMsg.CreatedAt = replyCreatedAt historyReplyMsg.UpdatedAt = replyCreatedAt @@ -243,6 +245,7 @@ func (h *ChatHandler) sendXunFeiMessage( } else { chatItem.Title = prompt } + chatItem.Model = req.Model h.db.Create(&chatItem) } } diff --git a/api/store/model/chat_history.go b/api/store/model/chat_history.go index 8fa09683..395f2b3d 100644 --- a/api/store/model/chat_history.go +++ b/api/store/model/chat_history.go @@ -7,6 +7,7 @@ type HistoryMessage struct { ChatId string // 会话 ID UserId uint // 用户 ID RoleId uint // 角色 ID + Model string // AI模型 Type string Icon string Tokens int diff --git a/api/store/model/chat_item.go b/api/store/model/chat_item.go index f0b653d1..80b45901 100644 --- a/api/store/model/chat_item.go +++ b/api/store/model/chat_item.go @@ -7,7 +7,8 @@ type ChatItem struct { ChatId string `gorm:"column:chat_id;unique"` // 会话 ID UserId uint // 用户 ID RoleId uint // 角色 ID - ModelId uint // 会话模型 + ModelId uint // 模型 ID + Model string // 模型 Title string // 会话标题 DeletedAt gorm.DeletedAt } diff --git a/api/store/vo/chat_history.go b/api/store/vo/chat_history.go index 1f4cbd4e..3f534f39 100644 --- a/api/store/vo/chat_history.go +++ b/api/store/vo/chat_history.go @@ -5,6 +5,7 @@ type HistoryMessage struct { ChatId string `json:"chat_id"` UserId uint `json:"user_id"` RoleId uint `json:"role_id"` + Model string `json:"model"` Type string `json:"type"` Icon string `json:"icon"` Tokens int `json:"tokens"` diff --git a/api/store/vo/chat_item.go b/api/store/vo/chat_item.go index eeb34031..ec43c320 100644 --- a/api/store/vo/chat_item.go +++ b/api/store/vo/chat_item.go @@ -7,5 +7,6 @@ type ChatItem struct { RoleId uint `json:"role_id"` ChatId string `json:"chat_id"` ModelId uint `json:"model_id"` + Model string `json:"model"` Title string `json:"title"` } diff --git a/database/update-v3.2.7.sql b/database/update-v3.2.7.sql index 1339fa0f..2f0e8f80 100644 --- a/database/update-v3.2.7.sql +++ b/database/update-v3.2.7.sql @@ -1,4 +1,13 @@ ALTER TABLE `chatgpt_mj_jobs` ADD `err_msg` VARCHAR(255) DEFAULT NULL COMMENT '错误信息' AFTER `publish`; ALTER TABLE `chatgpt_sd_jobs` ADD `err_msg` VARCHAR(255) DEFAULT NULL COMMENT '错误信息' AFTER `publish`; -ALTER TABLE `chatgpt_chat_items` ADD `model` VARCHAR(30) NULL COMMENT '模型名称' AFTER `model_id`; \ No newline at end of file +ALTER TABLE `chatgpt_chat_items` ADD `model` VARCHAR(30) NULL COMMENT '模型名称' AFTER `model_id`; +ALTER TABLE `chatgpt_chat_history` ADD `model` VARCHAR(30) NULL COMMENT '模型名称' AFTER `role_id`; + +-- 初始化对话数据 +UPDATE chatgpt_chat_items s SET model=(SELECT value FROM chatgpt_chat_models WHERE id = s.model_id); +-- 初始化聊天记录数据 +UPDATE chatgpt_chat_history s SET model=(SELECT model FROM chatgpt_chat_items WHERE chat_id = s.chat_id); + +-- 清理对话已删除的聊天记录(可选) +-- DELETE FROM `chatgpt_chat_history` WHERE model is NULL; \ No newline at end of file