123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- 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
- }
|