添加豆包会话文件内容添加,目前图片已完成,文件内容未测试
This commit is contained in:
parent
7850126c50
commit
d148d453b8
|
|
@ -206,13 +206,13 @@ func doReceiveGenChatMessage(userId int, sessionID *uint, data *proto.WSMessageR
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
sessionName := ""
|
sessionName := ""
|
||||||
if data.IsImage {
|
if data.IsImage || data.IsFile {
|
||||||
var imgMsg proto.UserImageMsg
|
var fileMsg proto.UserFileMessage
|
||||||
err = json.Unmarshal([]byte(data.Msg), &imgMsg)
|
err = json.Unmarshal([]byte(data.Msg), &fileMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error parsing JSON:", err)
|
log.Println("Error parsing JSON:", err)
|
||||||
}
|
}
|
||||||
sessionName = imgMsg.Text
|
sessionName = fileMsg.Text
|
||||||
} else {
|
} else {
|
||||||
sessionName = data.Msg
|
sessionName = data.Msg
|
||||||
}
|
}
|
||||||
|
|
@ -258,7 +258,7 @@ func doReceiveGenChatMessage(userId int, sessionID *uint, data *proto.WSMessageR
|
||||||
|
|
||||||
var userMsgID uint
|
var userMsgID uint
|
||||||
status := proto.MsgHasRead
|
status := proto.MsgHasRead
|
||||||
if data.IsImage {
|
if data.IsImage || data.IsFile {
|
||||||
status = proto.UserToModelImageMsgType //图片类型
|
status = proto.UserToModelImageMsgType //图片类型
|
||||||
}
|
}
|
||||||
//将消息存入数据库
|
//将消息存入数据库
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@ type WSMessageReq struct {
|
||||||
ToID int `json:"to_id"` //接收者id
|
ToID int `json:"to_id"` //接收者id
|
||||||
ModelID uint `json:"model_id"` //模型id
|
ModelID uint `json:"model_id"` //模型id
|
||||||
IsImage bool `json:"is_image"` //是否为图片(图片消息),如果是图片消息,则msg为图片消息结构为{"img_url":"","text":""}
|
IsImage bool `json:"is_image"` //是否为图片(图片消息),如果是图片消息,则msg为图片消息结构为{"img_url":"","text":""}
|
||||||
|
IsFile bool `json:"is_file"` //是否为文件(文件消息),如果是文件消息,则msg为文件消息结构为{"file_url":"","text":""}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ModelParam struct {
|
type ModelParam struct {
|
||||||
|
|
|
||||||
|
|
@ -167,7 +167,7 @@ const (
|
||||||
|
|
||||||
// 文件类型(文本、图片)
|
// 文件类型(文本、图片)
|
||||||
const (
|
const (
|
||||||
DownloadFileBaseURL = "https://pm.ljsea.top/tool/"
|
DownloadFileBaseURL = "https://pm.ljsea.top/tool/file/"
|
||||||
FileTypeText = "text_file"
|
FileTypeText = "text_file"
|
||||||
FileTypeImage = "image_file"
|
FileTypeImage = "image_file"
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,7 @@ func DouBaoV2(modelParam proto.ModelParam, imCtx *proto.IMParamContext) {
|
||||||
)
|
)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
messages := make([]*model.ChatCompletionMessage, 0)
|
messages := make([]*model.ChatCompletionMessage, 0)
|
||||||
err := GetDouBaoSessionHistoryMsg(imCtx.SessionID, modelParam.System, &messages, imCtx.Question)
|
err := GetDouBaoSessionHistoryMsgV2(imCtx.SessionID, modelParam.System, &messages, imCtx.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("get doubao session history message error:", err)
|
log.Println("get doubao session history message error:", err)
|
||||||
}
|
}
|
||||||
|
|
@ -273,12 +273,13 @@ func GetDouBaoSessionHistoryMsgV2(sessionID int, systemPrompt string, messages *
|
||||||
//只取最后的 SparkContextLength 条消息
|
//只取最后的 SparkContextLength 条消息
|
||||||
msgs = msgs[len(msgs)-proto.DouBaoContextLength-1:]
|
msgs = msgs[len(msgs)-proto.DouBaoContextLength-1:]
|
||||||
}
|
}
|
||||||
for _, v := range msgs {
|
//log.Println("get doubao session history message:", msgs)
|
||||||
|
for i, v := range msgs {
|
||||||
var message model.ChatCompletionMessage
|
var message model.ChatCompletionMessage
|
||||||
if v.Type == proto.UserToModelMsgType {
|
if v.Type == proto.UserToModelMsgType {
|
||||||
message.Role = model.ChatMessageRoleUser
|
message.Role = model.ChatMessageRoleUser
|
||||||
//用户消息为有图片类型
|
//用户消息为有图片类型且是最后一条消息
|
||||||
if v.Status == proto.UserToModelFileMsgType {
|
if v.Status == proto.UserToModelFileMsgType && i == len(msgs)-1 {
|
||||||
listValue, err2 := decodeFileMessageToDouBaoMessage(v.Msg, userID)
|
listValue, err2 := decodeFileMessageToDouBaoMessage(v.Msg, userID)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
log.Println("unmarshal user image message error:", err2)
|
log.Println("unmarshal user image message error:", err2)
|
||||||
|
|
@ -315,14 +316,15 @@ func decodeFileMessageToDouBaoMessage(msg string, userID int) ([]*model.ChatComp
|
||||||
log.Println("DouBao unmarshal file message error:", err)
|
log.Println("DouBao unmarshal file message error:", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
log.Println("doubaov2,decode file message:", fileMsgContent)
|
||||||
var listValue []*model.ChatCompletionMessageContentPart
|
var listValue []*model.ChatCompletionMessageContentPart
|
||||||
for _, file := range fileMsgContent.FileContent {
|
for _, file := range fileMsgContent.FileContent {
|
||||||
//文件消息为文本类型,则获取文本内容,若为图片则添加图片链接
|
//文件消息为文本类型,则获取文本内容,若为图片则添加图片链接
|
||||||
if file.FileType == proto.FileTypeText {
|
if file.FileType == proto.FileTypeText {
|
||||||
fileContents, err2 := FindFileContentV2(file.FileContent.FileID, userID)
|
fileContents, err2 := FindFileContentV2(file.FileContent.FileID, userID)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
log.Println("doubao find file content error:", err2)
|
log.Println("doubao find file content error:", err2, "\t,file id:", file.FileContent.FileID, "\t,file name:", file.FileContent.UserFileName)
|
||||||
return nil, err2
|
continue
|
||||||
}
|
}
|
||||||
fileContent := fileContents[0].FileContent
|
fileContent := fileContents[0].FileContent
|
||||||
//文件文本内容
|
//文件文本内容
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue