package aimod import ( "eta_gn/eta_api/global" "time" ) type AiChatTopic struct { AiChatTopicId int `gorm:"primaryKey;column:ai_chat_topic_id;type:int(11);not null"` TopicName string `gorm:"column:topic_name;type:varchar(255);default:''"` // 话题名称 SysUserId int `gorm:"column:sys_user_id;type:int(11);default:0"` // 创建人id SysUserRealName string `gorm:"column:sys_user_real_name;type:varchar(100);default:''"` // 创建人姓名 CreateTime time.Time `gorm:"column:create_time;type:datetime"` // 创建时间 ModifyTime time.Time `gorm:"column:modify_time;type:datetime"` // 修改时间 AssistantId string `gorm:"column:assistant_id;type:varchar(255);default:''"` // 助手id ThreadId string `gorm:"column:thread_id;type:varchar(255);default:''"` // 进程id } type AiChat struct { AiChatId int `gorm:"primaryKey;column:ai_chat_id;type:int(11);not null"` AiChatTopicId int `gorm:"column:ai_chat_topic_id;type:int(11);default:0"` Ask string `gorm:"column:ask;type:text"` // 提问 AskUuid string `gorm:"index:idx_ask_uuid;column:ask_uuid;type:varchar(50);default:''"` // ask,md5值 Answer string `gorm:"column:answer;type:text"` // 答案 SysUserId int `gorm:"column:sys_user_id;type:int(11);default:0"` // 提问人id SysUserRealName string `gorm:"column:sys_user_real_name;type:varchar(100);default:''"` // 提问人名称 Model string `gorm:"column:model;type:varchar(20);default:''"` // 模型 CreateTime time.Time `gorm:"column:create_time;type:datetime"` ModifyTime time.Time `gorm:"column:modify_time;type:datetime"` OpenaiFileId string `gorm:"column:openai_file_id;type:varchar(255);default:''"` // openai返回的文件id OpenaiFileName string `gorm:"column:openai_file_name;type:varchar(255);default:''"` // 文件名称 OpenaiFilePath string `gorm:"column:openai_file_path;type:varchar(255);default:''"` // 文件路径 } type ChatReq struct { AiChatTopicId int `description:"主题id"` Ask string `description:"提问"` Model string `description:"模型"` } func GetAiChatByAsk(askUuid string) (item *AiChat, err error) { sql := `SELECT * FROM ai_chat WHERE ask_uuid=?` err = global.DmSQL["ai"].Raw(sql, askUuid).First(&item).Error return } type ChatResp struct { AiChatTopicId int `description:"主题id"` Ask string `description:"提问"` Answer string `description:"回答"` Model string } // AddAiChatTopic 新增主题 func AddAiChatTopic(item *AiChatTopic) (lastId int64, err error) { err = global.DmSQL["ai"].Create(item).Error lastId = int64(item.AiChatTopicId) return } // AddAiChat 新增聊天 func AddAiChat(item *AiChat) (lastId int64, err error) { err = global.DmSQL["ai"].Create(item).Error lastId = int64(item.AiChatId) return } type AiChatTopicView struct { AiChatTopicId int `gorm:"primaryKey;column:ai_chat_topic_id;type:int(11);not null"` TopicName string `gorm:"column:topic_name;type:varchar(255);default:''"` // 话题名称 CreateTime *global.LocalTime `gorm:"column:create_time;type:datetime"` // 创建时间 ModifyTime *global.LocalTime `gorm:"column:modify_time;type:datetime"` // 修改时间 AssistantId string `gorm:"column:assistant_id;type:varchar(255);default:''"` // 助手id ThreadId string `gorm:"column:thread_id;type:varchar(255);default:''"` // 进程id } func GetAiChatTopicList(sysUserId int) (item []*AiChatTopicView, err error) { sql := ` SELECT * FROM ai_chat_topic WHERE sys_user_id=? ORDER BY create_time DESC ` err = global.DmSQL["ai"].Raw(sql, sysUserId).Find(&item).Error return } type AiChatTopicListResp struct { List []*AiChatTopicView } type AiChatView struct { AiChatId int `gorm:"primaryKey;column:ai_chat_id;type:int(11);not null"` AiChatTopicId int `gorm:"column:ai_chat_topic_id;type:int(11);default:0"` Ask string `gorm:"column:ask;type:text"` // 提问 Answer string `gorm:"column:answer;type:text"` // 答案 Model string `gorm:"column:model;type:varchar(20);default:''"` // 模型 CreateTime string `gorm:"column:create_time;type:datetime"` ModifyTime string `gorm:"column:modify_time;type:datetime"` OpenaiFileId string `gorm:"column:openai_file_id;type:varchar(255);default:''"` // openai返回的文件id OpenaiFileName string `gorm:"column:openai_file_name;type:varchar(255);default:''"` // 文件名称 OpenaiFilePath string `gorm:"column:openai_file_path;type:varchar(255);default:''"` // 文件路径 } var ModelViewMap = map[string]string{ "gpt-4-1106-preview": "GPT-4 Turbo", "moonshot-v1-32k": "Kimi", } func GetAiChatList(aiChatTopicId int) (item []*AiChatView, err error) { sql := ` SELECT * FROM ai_chat WHERE ai_chat_topic_id=? ORDER BY create_time ASC ` err = global.DmSQL["ai"].Raw(sql, aiChatTopicId).Find(&item).Error return } type AiChatDetailResp struct { List []*AiChatView } type TopicDeleteReq struct { AiChatTopicId int `description:"主题id"` } func DeleteTopic(topicId int) (err error) { tx := global.DmSQL["ai"].Begin() defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() sql := ` DELETE FROM ai_chat_topic WHERE ai_chat_topic_id=? ` err = tx.Exec(sql, topicId).Error if err != nil { return } sql = ` DELETE FROM ai_chat WHERE ai_chat_topic_id=? ` err = tx.Exec(sql, topicId).Error if err != nil { return } return } type TopicEditReq struct { AiChatTopicId int `description:"主题id"` TopicName string `description:"主题名称"` } func GetAiChatTopicByTopicName(topicName string) (item *AiChatTopicView, err error) { sql := ` SELECT * FROM ai_chat_topic WHERE topic_name=? ` err = global.DmSQL["ai"].Raw(sql, topicName).First(&item).Error return } func EditTopic(topicId int, topicName string) (err error) { sql := ` UPDATE ai_chat_topic SET topic_name=? WHERE ai_chat_topic_id=? ` err = global.DmSQL["ai"].Exec(sql, topicName, topicId).Error return } func (obj *AiChatTopic) GetAiChatTopicById() (item *AiChatTopicView, err error) { sql := ` SELECT * FROM ai_chat_topic WHERE ai_chat_topic_id=? ` err = global.DmSQL["ai"].Raw(sql, obj.AiChatTopicId).Find(&item).Error return } type HistoryChat struct { Ask string Answer string OpenaiFileId []string `description:"文件ID"` } func (obj *AiChatTopic) UpdateAssistantAndThreadId(assistantId, threadId string, topicId int) (err error) { sql := `UPDATE ai_chat_topic SET assistant_id = ?, thread_id = ?, modify_time = NOW() WHERE ai_chat_topic_id = ?` err = global.DmSQL["ai"].Exec(sql, assistantId, threadId, topicId).Error return }