From 46e4ff94ee4bd4183c5b13317a56620dcac781ec Mon Sep 17 00:00:00 2001 From: xjd Date: Tue, 6 Feb 2024 11:12:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AF=B9=E8=AF=9D=E9=9A=94=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/chat.go | 36 +++++++++++++++++++++++++++--------- discord/discord.go | 15 +++++++++++---- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/controller/chat.go b/controller/chat.go index 35568d54..a49f5d1f 100644 --- a/controller/chat.go +++ b/controller/chat.go @@ -64,7 +64,7 @@ func Chat(c *gin.Context) { discord.RepliesChans[sentMsg.ID] = replyChan defer delete(discord.RepliesChans, sentMsg.ID) - stopChan := make(chan string) + stopChan := make(chan model.ChannelResp) discord.ReplyStopChans[sentMsg.ID] = stopChan defer delete(discord.ReplyStopChans, sentMsg.ID) @@ -157,7 +157,16 @@ func ChatForOpenAI(c *gin.Context) { if message.Role == "user" { switch contentObj := message.Content.(type) { case string: - content = contentObj + jsonData, err := json.Marshal(messages) + if err != nil { + c.JSON(http.StatusOK, gin.H{ + "success": false, + "message": err.Error(), + }) + return + } + content = string(jsonData) + //content = contentObj case []interface{}: content, err = buildOpenAIGPT4VForImageContent(contentObj) if err != nil { @@ -211,7 +220,7 @@ func ChatForOpenAI(c *gin.Context) { discord.RepliesOpenAIChans[sentMsg.ID] = replyChan defer delete(discord.RepliesOpenAIChans, sentMsg.ID) - stopChan := make(chan string) + stopChan := make(chan model.ChannelResp) discord.ReplyStopChans[sentMsg.ID] = stopChan defer delete(discord.ReplyStopChans, sentMsg.ID) @@ -319,9 +328,9 @@ func buildOpenAIGPT4VForImageContent(objs []interface{}) (string, error) { return "", fmt.Errorf("消息格式错误") } } - if runeCount := len([]rune(content)); runeCount > 2000 { - return "", fmt.Errorf("prompt最大为2000字符 [%v]", runeCount) - } + //if runeCount := len([]rune(content)); runeCount > 2000 { + // return "", fmt.Errorf("prompt最大为2000字符 [%v]", runeCount) + //} return content, nil } @@ -390,7 +399,7 @@ func ImagesForOpenAI(c *gin.Context) { discord.RepliesOpenAIImageChans[sentMsg.ID] = replyChan defer delete(discord.RepliesOpenAIImageChans, sentMsg.ID) - stopChan := make(chan string) + stopChan := make(chan model.ChannelResp) discord.ReplyStopChans[sentMsg.ID] = stopChan defer delete(discord.ReplyStopChans, sentMsg.ID) @@ -464,14 +473,23 @@ func getSendChannelIdAndCozeBotId(c *gin.Context, isOpenAIAPI bool, request mode if err != nil { return "", "", err } - return botConfig.ChannelId, botConfig.CozeBotId, nil + var sendChannelId string + if channelId := botConfig.ChannelId; channelId != "" { + sendChannelId = channelId + } else { + // 创建新频道 + sendChannelId, _ = discord.ChannelCreate(discord.GuildId, fmt.Sprintf("对话%s", c.Request.Context().Value(common.RequestIdKey)), 0) + } + return sendChannelId, botConfig.CozeBotId, nil } // 没有值抛出异常 return "", "", fmt.Errorf("secret和channelId匹配不到有效bot") } else { channelId := request.GetChannelId() if channelId == nil || *channelId == "" { - channelId = &discord.ChannelId + //channelId = &discord.ChannelId + channelCreateId, _ := discord.ChannelCreate(discord.GuildId, fmt.Sprintf("对话%s", c.Request.Context().Value(common.RequestIdKey)), 0) + channelId = &channelCreateId } // botConfigs为空 return *channelId, discord.CozeBotId, nil diff --git a/discord/discord.go b/discord/discord.go index aeffcd55..4405bdb7 100644 --- a/discord/discord.go +++ b/discord/discord.go @@ -35,7 +35,7 @@ var RepliesChans = make(map[string]chan model.ReplyResp) var RepliesOpenAIChans = make(map[string]chan model.OpenAIChatCompletionResponse) var RepliesOpenAIImageChans = make(map[string]chan model.OpenAIImagesGenerationResponse) -var ReplyStopChans = make(map[string]chan string) +var ReplyStopChans = make(map[string]chan model.ChannelResp) var Session *discordgo.Session func StartBot(ctx context.Context, token string) { @@ -154,7 +154,10 @@ func messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdate) { // 如果作者为 nil 或消息来自 bot 本身,则发送停止信号 if m.Author == nil || m.Author.ID == s.State.User.ID { - stopChan <- m.ReferencedMessage.ID + ChannelDel(m.ChannelID) + stopChan <- model.ChannelResp{ + Id: m.ChannelID, + } return } @@ -192,7 +195,11 @@ func messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdate) { reply.Choices[0].FinishReason = &stopStr replyOpenAIChan <- reply } - stopChan <- m.ReferencedMessage.ID + // 删除该频道 + ChannelDel(m.ChannelID) + stopChan <- model.ChannelResp{ + Id: m.ChannelID, + } } return @@ -335,7 +342,7 @@ func ChannelCreate(guildID, channelName string, channelType int) (string, error) } func ChannelDel(channelId string) (string, error) { - // 创建新的频道 + // 删除频道 st, err := Session.ChannelDelete(channelId) if err != nil { common.LogError(context.Background(), fmt.Sprintf("删除频道时异常 %s", err.Error()))