From 98d9ba21bda9682f82e144b64fd9b602bb394cf9 Mon Sep 17 00:00:00 2001 From: Zhang Minghan Date: Tue, 26 Mar 2024 19:07:33 +0800 Subject: [PATCH] fix: fix anthropic multiple same rule issue (#138) --- README.md | 22 ++++++++++----------- adapter/claude/chat.go | 43 +++++++++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 408e37a4..ecdc1f39 100644 --- a/README.md +++ b/README.md @@ -169,7 +169,7 @@ English | [简体中文](https://github.com/Deeptrain-Community/chatnio/blob/mas > - *-v ~/config:/config* mount host machine directory of configuration file, *-v ~/logs:/logs* mount host machine directory of log file,*-v ~/storage:/storage* mount generated files of additional functions > - You need to configure MySQL and Redis services, please refer to the information above to modify the environment variables yourself. - Version Update (_With Watchtower auto-updating enabled, there is no need for manual updates, Just run through the steps above again after execution._): + Version Update (_With Watchtower auto-updating enabled, there is no need for manual updates, Just run through the steps above again after execution._): ```shell docker stop chatnio docker rm chatnio @@ -287,16 +287,16 @@ English | [简体中文](https://github.com/Deeptrain-Community/chatnio/blob/mas ## ✨ Some EXCELLENT Open-source Projects > **Frontend projects here refer to projects that focus on user chat interfaces, backend projects refer to projects that focus on API transfer and management, and one-stop projects refer to projects that include user chat interfaces and API transfer and management** -- [Next Chat @yidadaa](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web) (Front-end Oriented Projects) -- [Lobe Chat @arvinxx](https://github.com/lobehub/lobe-chat) (Front-end Oriented Projects) -- [Chat Box @bin-huang](https://github.com/Bin-Huang/chatbox) (Front-end Oriented Projects) -- [OpenAI Forward @kenyony](https://github.com/KenyonY/openai-forward) (Back-end Oriented Projects) -- [One API @justsong](https://github.com/songquanpeng/one-api) (Back-end Oriented Projects) -- [New API @calon](https://github.com/Calcium-Ion/new-api) (Back-end Oriented Projects) -- [FastGPT @labring](https://github.com/labring/FastGPT) (Knowledge Base) -- [Quivr @quivrhq](https://github.com/StanGirard/quivr) (Knowledge Base) -- [Bingo @weaigc](https://github.com/weaigc/bingo) (Knowledge Base) -- [Midjourney Proxy @novicezk](https://github.com/novicezk/midjourney-proxy) (Model Library) +- [Next Chat @yidadaa](https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web) (Front-end Oriented Projects) +- [Lobe Chat @arvinxx](https://github.com/lobehub/lobe-chat) (Front-end Oriented Projects) +- [Chat Box @bin-huang](https://github.com/Bin-Huang/chatbox) (Front-end Oriented Projects) +- [OpenAI Forward @kenyony](https://github.com/KenyonY/openai-forward) (Back-end Oriented Projects) +- [One API @justsong](https://github.com/songquanpeng/one-api) (Back-end Oriented Projects) +- [New API @calon](https://github.com/Calcium-Ion/new-api) (Back-end Oriented Projects) +- [FastGPT @labring](https://github.com/labring/FastGPT) (Knowledge Base) +- [Quivr @quivrhq](https://github.com/StanGirard/quivr) (Knowledge Base) +- [Bingo @weaigc](https://github.com/weaigc/bingo) (Knowledge Base) +- [Midjourney Proxy @novicezk](https://github.com/novicezk/midjourney-proxy) (Model Library) ## 📄 Open Source License diff --git a/adapter/claude/chat.go b/adapter/claude/chat.go index 0b5100c0..46fd31fb 100644 --- a/adapter/claude/chat.go +++ b/adapter/claude/chat.go @@ -53,30 +53,43 @@ func (c *ChatInstance) GetTokens(props *adaptercommon.ChatProps) int { return *props.MaxTokens } -func (c *ChatInstance) GetMessages(props *adaptercommon.ChatProps) []Message { +func (c *ChatInstance) ConvertMessages(props *adaptercommon.ChatProps) []globals.Message { + // anthropic api: top message must be user message, system message is not allowed start := false - return utils.Each(props.Message, func(message globals.Message) Message { - // anthropic api: top message must be user message, system message is not allowed + result := make([]globals.Message, 0) + + for _, message := range props.Message { + // if is first message, set it to user message if !start { start = true - // set first message to user message - if message.Role != globals.User { - return Message{ - Role: globals.User, - Content: message.Content, - } - } + result = append(result, globals.Message{ + Role: globals.User, + Content: message.Content, + }) + continue } + // if is system message, set it to user message if message.Role == globals.System { - // set system message to user message - return Message{ - Role: message.Role, - Content: message.Content, - } + message.Role = globals.User } + // anthropic api does not allow multi-same role messages + if len(result) > 0 && result[len(result)-1].Role == message.Role { + result[len(result)-1].Content += "\n" + message.Content + continue + } + + result = append(result, message) + } + + return result +} + +func (c *ChatInstance) GetMessages(props *adaptercommon.ChatProps) []Message { + converted := c.ConvertMessages(props) + return utils.Each(converted, func(message globals.Message) Message { if !globals.IsVisionModel(props.Model) || message.Role != globals.User { return Message{ Role: message.Role,