123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- package aimod
- import (
- "eta/eta_api/utils"
- "time"
- "github.com/beego/beego/v2/client/orm"
- )
- type AiChatTopic struct {
- AiChatTopicId int `orm:"column(ai_chat_topic_id);pk;auto"`
- TopicName string
- SysUserId int
- SysUserRealName string
- AssistantId string
- ThreadId string
- CreateTime time.Time
- ModifyTime time.Time
- }
- func (obj *AiChatTopic) AiChatTopicView() *AiChatTopicView {
- return &AiChatTopicView{
- AiChatTopicId: obj.AiChatTopicId,
- TopicName: obj.TopicName,
- CreateTime: obj.CreateTime.Format(utils.FormatDateTime),
- ModifyTime: obj.ModifyTime.Format(utils.FormatDateTime),
- AssistantId: obj.AssistantId,
- ThreadId: obj.ThreadId,
- }
- }
- func AiChatTopicViewList(topics []*AiChatTopic) (itemList []*AiChatTopicView) {
- for _, item := range topics {
- itemList = append(itemList, item.AiChatTopicView())
- }
- return
- }
- type AiChat struct {
- AiChatId int `orm:"column(ai_chat_id);pk;auto"`
- AiChatTopicId int
- Ask string
- AskUuid string
- Answer string
- Model string
- SysUserId int
- SysUserRealName string `description:"提问人名称"`
- OpenaiFileId string `description:"openai返回的文件id"`
- OpenaiFileName string `description:"文件名称"`
- OpenaiFilePath string `description:"文件路径"`
- CreateTime time.Time
- ModifyTime time.Time
- }
- func (obj *AiChat) AiChatView() *AiChatView {
- return &AiChatView{
- AiChatId: obj.AiChatId,
- AiChatTopicId: obj.AiChatTopicId,
- Ask: obj.Ask,
- Answer: obj.Answer,
- Model: obj.Model,
- OpenaiFileId: obj.OpenaiFileId,
- OpenaiFileName: obj.OpenaiFileName,
- OpenaiFilePath: obj.OpenaiFilePath,
- CreateTime: obj.CreateTime.Format(utils.FormatDateTime),
- ModifyTime: obj.ModifyTime.Format(utils.FormatDateTime),
- }
- }
- func AiChatViewList(chats []*AiChat) (itemList []*AiChatView) {
- for _, item := range chats {
- itemList = append(itemList, item.AiChatView())
- }
- return
- }
- 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=?`
- o := orm.NewOrmUsingDB("ai")
- err = o.Raw(sql, askUuid).QueryRow(&item)
- 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) {
- o := orm.NewOrmUsingDB("ai")
- lastId, err = o.Insert(item)
- return
- }
- // AddAiChat 新增聊天
- func AddAiChat(item *AiChat) (lastId int64, err error) {
- o := orm.NewOrmUsingDB("ai")
- lastId, err = o.Insert(item)
- return
- }
- type AiChatTopicView struct {
- AiChatTopicId int `description:"主题id"`
- TopicName string `description:"主题名称"`
- CreateTime string `description:"创建时间"`
- ModifyTime string `description:"修改时间"`
- AssistantId string
- ThreadId string
- }
- // [2025-zsh-时间类型修复-chenhan]
- func GetAiChatTopicList(sysUserId int) (item []*AiChatTopicView, err error) {
- var dbItemList []*AiChatTopic
- sql := ` SELECT * FROM ai_chat_topic WHERE sys_user_id=? ORDER BY create_time DESC `
- o := orm.NewOrmUsingDB("ai")
- _, err = o.Raw(sql, sysUserId).QueryRows(&dbItemList)
- if err != nil {
- return
- }
- item = AiChatTopicViewList(dbItemList)
- return
- }
- type AiChatTopicListResp struct {
- List []*AiChatTopicView
- }
- type AiChatView struct {
- AiChatId int `description:"记录id"`
- AiChatTopicId int `description:"主题id"`
- Ask string `description:"提问"`
- Answer string `description:"答案"`
- Model string
- OpenaiFileId string `description:"文件ID"`
- OpenaiFileName string `description:"文件名称"`
- OpenaiFilePath string `description:"文件路径"`
- CreateTime string `description:"创建时间"`
- ModifyTime string `description:"修改时间"`
- }
- var ModelViewMap = map[string]string{
- "gpt-4-1106-preview": "GPT-4 Turbo",
- "moonshot-v1-32k": "Kimi",
- // "moonshot-v1-8k": "Kimi",
- }
- // [2025-zsh-时间类型修复-chenhan]
- func GetAiChatList(aiChatTopicId int) (item []*AiChatView, err error) {
- var dbItemList []*AiChat
- sql := ` SELECT * FROM ai_chat WHERE ai_chat_topic_id=? ORDER BY create_time ASC `
- o := orm.NewOrmUsingDB("ai")
- _, err = o.Raw(sql, aiChatTopicId).QueryRows(&dbItemList)
- if err != nil {
- return
- }
- item = AiChatViewList(dbItemList)
- return
- }
- type AiChatDetailResp struct {
- List []*AiChatView
- }
- type TopicDeleteReq struct {
- AiChatTopicId int `description:"主题id"`
- }
- func DeleteTopic(topicId int) (err error) {
- o := orm.NewOrmUsingDB("ai")
- tx, err := o.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- sql := ` DELETE FROM ai_chat_topic WHERE ai_chat_topic_id=? `
- _, err = tx.Raw(sql, topicId).Exec()
- if err != nil {
- return err
- }
- sql = ` DELETE FROM ai_chat WHERE ai_chat_topic_id=? `
- _, err = tx.Raw(sql, topicId).Exec()
- if err != nil {
- return err
- }
- return err
- }
- type TopicEditReq struct {
- AiChatTopicId int `description:"主题id"`
- TopicName string `description:"主题名称"`
- }
- // [2025-zsh-时间类型修复-chenhan]
- func GetAiChatTopicByTopicName(topicName string) (item *AiChatTopicView, err error) {
- var dbItem *AiChatTopic
- sql := ` SELECT * FROM ai_chat_topic WHERE topic_name=? `
- o := orm.NewOrmUsingDB("ai")
- err = o.Raw(sql, topicName).QueryRow(&dbItem)
- if err != nil {
- return
- }
- item = dbItem.AiChatTopicView()
- return
- }
- func EditTopic(topicId int, topicName string) (err error) {
- o := orm.NewOrmUsingDB("ai")
- sql := ` UPDATE ai_chat_topic SET topic_name=? WHERE ai_chat_topic_id=? `
- _, err = o.Raw(sql, topicName, topicId).Exec()
- return err
- }
- // [2025-zsh-时间类型修复-chenhan]
- func (obj *AiChatTopic) GetAiChatTopicById() (item *AiChatTopicView, err error) {
- var dbItem *AiChatTopic
- o := orm.NewOrmUsingDB("ai")
- sql := ` SELECT * FROM ai_chat_topic WHERE ai_chat_topic_id=? `
- err = o.Raw(sql, obj.AiChatTopicId).QueryRow(&dbItem)
- if err != nil {
- return
- }
- item = dbItem.AiChatTopicView()
- return
- }
- type HistoryChat struct {
- Ask string
- Answer string
- OpenaiFileId []string `description:"文件ID"`
- }
- // 修改
- func (obj *AiChatTopic) Update(updateParams, whereParam map[string]interface{}) (err error) {
- to := orm.NewOrmUsingDB("ai")
- ptrStructOrTableName := "ai_chat_topic"
- qs := to.QueryTable(ptrStructOrTableName)
- for expr, exprV := range whereParam {
- qs = qs.Filter(expr, exprV)
- }
- _, err = qs.Update(updateParams)
- return
- }
|