123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- package aimod
- import (
- "time"
- "github.com/beego/beego/v2/client/orm"
- )
- type AiChatTopic struct {
- AiChatTopicId int `orm:"column(ai_chat_topic_id);pk"`
- TopicName string
- SysUserId int
- SysUserRealName string
- AssistantId string
- ThreadId string
- CreateTime time.Time
- ModifyTime time.Time
- }
- type AiChat struct {
- AiChatId int `orm:"column(ai_chat_id);pk"`
- 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
- }
- 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
- }
- func GetAiChatTopicList(sysUserId int) (item []*AiChatTopicView, err error) {
- 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(&item)
- 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",
- }
- func GetAiChatList(aiChatTopicId int) (item []*AiChatView, err error) {
- 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(&item)
- 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:"主题名称"`
- }
- func GetAiChatTopicByTopicName(topicName string) (item *AiChatTopicView, err error) {
- sql := ` SELECT * FROM ai_chat_topic WHERE topic_name=? `
- o := orm.NewOrmUsingDB("ai")
- err = o.Raw(sql, topicName).QueryRow(&item)
- 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
- }
- func (obj *AiChatTopic) GetAiChatTopicById() (item *AiChatTopicView, err error) {
- o := orm.NewOrmUsingDB("ai")
- sql := ` SELECT * FROM ai_chat_topic WHERE ai_chat_topic_id=? `
- err = o.Raw(sql, obj.AiChatTopicId).QueryRow(&item)
- 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
- }
|