Skip to content

Commit

Permalink
fix: fix anthropic multiple same rule issue (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Mar 26, 2024
1 parent 944fa28 commit 98d9ba2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
43 changes: 28 additions & 15 deletions adapter/claude/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 98d9ba2

Please sign in to comment.