Skip to content

Commit

Permalink
feat: 对话隔离
Browse files Browse the repository at this point in the history
  • Loading branch information
xjd committed Feb 6, 2024
1 parent 3ad0304 commit 46e4ff9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
36 changes: 27 additions & 9 deletions controller/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

}
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()))
Expand Down

0 comments on commit 46e4ff9

Please sign in to comment.