Explorar el Código

add: 增加kimi模型的相关映射

zqbao hace 10 meses
padre
commit
9a25d206b3
Se han modificado 5 ficheros con 36 adiciones y 14 borrados
  1. 7 3
      controllers/ai/ai.go
  2. 13 7
      controllers/ai/ai_file.go
  3. 9 1
      models/aimod/ai.go
  4. 1 0
      models/aimod/ai_file.go
  5. 6 3
      services/aiser/ai.go

+ 7 - 3
controllers/ai/ai.go

@@ -102,7 +102,7 @@ func (this *AiController) List() {
 			historyChatList = append(historyChatList, *historyChat)
 		}
 
-		answer, err = aiser.ChatAutoMsg(req.Ask, historyChatList)
+		answer, err = aiser.ChatAutoMsg(req.Ask, historyChatList, req.Model)
 		if err != nil {
 			br.Msg = "获取数据失败!"
 			br.ErrMsg = "获取数据失败,ChatAutoMsg,Err:" + err.Error()
@@ -111,6 +111,7 @@ func (this *AiController) List() {
 	}
 	resp.Ask = req.Ask
 	resp.Answer = answer
+	resp.Model = req.Model
 
 	if req.AiChatTopicId <= 0 { //新增
 		topic := new(aimod.AiChatTopic)
@@ -131,7 +132,7 @@ func (this *AiController) List() {
 		chatItem.Ask = req.Ask
 		chatItem.AskUuid = utils.MD5(req.Ask)
 		chatItem.Answer = answer
-		chatItem.Model = "gpt-4-1106-preview"
+		chatItem.Model = EnabledModelsForMap[req.Model]
 		chatItem.SysUserId = this.SysUser.AdminId
 		chatItem.SysUserRealName = this.SysUser.RealName
 		chatItem.CreateTime = time.Now()
@@ -149,7 +150,7 @@ func (this *AiController) List() {
 		chatItem.Ask = req.Ask
 		chatItem.AskUuid = utils.MD5(req.Ask)
 		chatItem.Answer = answer
-		chatItem.Model = "gpt-4-1106-preview"
+		chatItem.Model = EnabledModelsForMap[req.Model]
 		chatItem.SysUserId = this.SysUser.AdminId
 		chatItem.SysUserRealName = this.SysUser.RealName
 		chatItem.CreateTime = time.Now()
@@ -226,6 +227,9 @@ func (this *AiController) TopicDetail() {
 		br.ErrMsg = "获取主题记录信息失败,Err:" + err.Error()
 		return
 	}
+	for index := range list {
+		list[index].Model = aimod.ModelViewMap[list[index].Model]
+	}
 	resp := new(aimod.AiChatDetailResp)
 	resp.List = list
 	br.Ret = 200

+ 13 - 7
controllers/ai/ai_file.go

@@ -39,6 +39,7 @@ func (this *AiFileController) FileUpload() {
 		br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
 		return
 	}
+	model := this.GetString("Model")
 
 	aiChatTopicId, _ := this.GetInt("AiChatTopicId")
 
@@ -96,7 +97,7 @@ func (this *AiFileController) FileUpload() {
 		ResourceUrl:  resourceUrl,
 		ResourceName: uploadFileName,
 	}
-	uploadResult, err := aiser.OpenAiFileUpload(resourceUrl, uploadFileName)
+	uploadResult, err := aiser.OpenAiFileUpload(resourceUrl, uploadFileName, model)
 	if err != nil {
 		br.Msg = "文件上传失败"
 		br.ErrMsg = "文件上传失败,Err:" + err.Error()
@@ -148,7 +149,7 @@ func (this *AiFileController) FileUpload() {
 		chatItem.AiChatTopicId = aiChatTopicId
 		chatItem.Ask = uploadFileName
 		chatItem.AskUuid = utils.MD5(uploadFileName)
-		chatItem.Model = "gpt-4-1106-preview"
+		chatItem.Model = EnabledModelsForMap[model]
 		chatItem.SysUserId = this.SysUser.AdminId
 		chatItem.SysUserRealName = this.SysUser.RealName
 		if uploadResult != nil && uploadResult.Data != nil {
@@ -169,7 +170,7 @@ func (this *AiFileController) FileUpload() {
 		chatItem.AiChatTopicId = aiChatTopicId
 		chatItem.Ask = uploadFileName
 		chatItem.AskUuid = utils.MD5(fileName)
-		chatItem.Model = "gpt-4-1106-preview"
+		chatItem.Model = EnabledModelsForMap[model]
 		chatItem.SysUserId = this.SysUser.AdminId
 		chatItem.SysUserRealName = this.SysUser.RealName
 		if uploadResult != nil && uploadResult.Data != nil {
@@ -331,7 +332,7 @@ func (this *AiFileController) FileRetrieve() {
 		frList = append(frList, *frItem)
 	}
 
-	fileRetrieveResp, err := aiser.FileRetrieve(assistantId, threadId, frList, req.OpenaiFileId)
+	fileRetrieveResp, err := aiser.FileRetrieve(assistantId, threadId, req.Model, frList, req.OpenaiFileId)
 	if err != nil {
 		br.Msg = "获取数据失败!"
 		br.ErrMsg = "获取数据失败,FileRetrieve,Err:" + err.Error()
@@ -370,7 +371,7 @@ func (this *AiFileController) FileRetrieve() {
 		chatItem.Ask = req.Ask
 		chatItem.AskUuid = utils.MD5(req.Ask)
 		chatItem.Answer = answer
-		chatItem.Model = "gpt-4-1106-preview"
+		chatItem.Model = EnabledModelsForMap[req.Model]
 		chatItem.SysUserId = this.SysUser.AdminId
 		chatItem.SysUserRealName = this.SysUser.RealName
 		chatItem.CreateTime = time.Now()
@@ -388,7 +389,7 @@ func (this *AiFileController) FileRetrieve() {
 		chatItem.Ask = req.Ask
 		chatItem.AskUuid = askUuid
 		chatItem.Answer = answer
-		chatItem.Model = "gpt-4-1106-preview"
+		chatItem.Model = EnabledModelsForMap[req.Model]
 		chatItem.SysUserId = this.SysUser.AdminId
 		chatItem.SysUserRealName = this.SysUser.RealName
 		chatItem.CreateTime = time.Now()
@@ -417,7 +418,7 @@ func (this *AiFileController) FileRetrieve() {
 			return
 		}
 	}
-
+	resp.Model = aimod.ModelViewMap[req.Model]
 	resp.AiChatTopicId = req.AiChatTopicId
 	resp.Ask = req.Ask
 	resp.Answer = answer
@@ -427,3 +428,8 @@ func (this *AiFileController) FileRetrieve() {
 	br.Data = resp
 	return
 }
+
+var EnabledModelsForMap = map[string]string{
+	"Kimi":        "moonshot-v1-32k",
+	"GPT-4 Turbo": "gpt-4-1106-preview",
+}

+ 9 - 1
models/aimod/ai.go

@@ -1,8 +1,9 @@
 package aimod
 
 import (
-	"github.com/beego/beego/v2/client/orm"
 	"time"
+
+	"github.com/beego/beego/v2/client/orm"
 )
 
 type AiChatTopic struct {
@@ -35,6 +36,7 @@ type AiChat struct {
 type ChatReq struct {
 	AiChatTopicId int    `description:"主题id"`
 	Ask           string `description:"提问"`
+	Model         string `description:"模型"`
 }
 
 func GetAiChatByAsk(askUuid string) (item *AiChat, err error) {
@@ -98,6 +100,12 @@ type AiChatView struct {
 	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")

+ 1 - 0
models/aimod/ai_file.go

@@ -4,4 +4,5 @@ type FileRetrieveReq struct {
 	AiChatTopicId int      `description:"主题id"`
 	Ask           string   `description:"提问问题,如果是上传文件,则填入文件名称"`
 	OpenaiFileId  []string `description:"openai返回的文件id"`
+	Model         string   `description:"模型名称"`
 }

+ 6 - 3
services/aiser/ai.go

@@ -11,12 +11,13 @@ import (
 	"time"
 )
 
-func ChatAutoMsg(prompt string, historyChatList []aimod.HistoryChat) (result string, err error) {
+func ChatAutoMsg(prompt string, historyChatList []aimod.HistoryChat, model string) (result string, err error) {
 	chatUrl := utils.EtaAiUrl + `chat/auto_msg`
 
 	param := make(map[string]interface{})
 	param["Prompt"] = prompt
 	param["HistoryChatList"] = historyChatList
+	param["model"] = model
 
 	postData, err := json.Marshal(param)
 
@@ -66,12 +67,13 @@ func ChatAutoMsg(prompt string, historyChatList []aimod.HistoryChat) (result str
 	return result, nil
 }
 
-func OpenAiFileUpload(fileUrl, fileName string) (result *OpenAiFileUploadResp, err error) {
+func OpenAiFileUpload(fileUrl, fileName, model string) (result *OpenAiFileUploadResp, err error) {
 	chatUrl := utils.EtaAiUrl + `chat/file/upload`
 
 	param := make(map[string]interface{})
 	param["FileUrl"] = fileUrl
 	param["FileName"] = fileName
+	param["Model"] = model
 
 	postData, err := json.Marshal(param)
 	if err != nil {
@@ -116,7 +118,7 @@ func OpenAiFileUpload(fileUrl, fileName string) (result *OpenAiFileUploadResp, e
 	return resp, nil
 }
 
-func FileRetrieve(assistantId, threadId string, historyChatList []aimod.HistoryChat, OpenaiFileId []string) (result *OpenAiFileRetrieveResp, err error) {
+func FileRetrieve(assistantId, threadId, model string, historyChatList []aimod.HistoryChat, OpenaiFileId []string) (result *OpenAiFileRetrieveResp, err error) {
 	chatUrl := utils.EtaAiUrl + `chat/file/retrieve`
 
 	param := make(map[string]interface{})
@@ -124,6 +126,7 @@ func FileRetrieve(assistantId, threadId string, historyChatList []aimod.HistoryC
 	param["OpenaiFileId"] = OpenaiFileId
 	param["AssistantId"] = assistantId
 	param["ThreadId"] = threadId
+	param["Model"] = model
 
 	postData, err := json.Marshal(param)
 	if err != nil {