From 3136a9fd7c2ef41c0c6dde631cb6d445ef98735f Mon Sep 17 00:00:00 2001 From: tangxingyu Date: Sat, 30 Mar 2024 18:25:39 +0800 Subject: [PATCH 1/3] claude sys msg adapt --- adapter/claude/chat.go | 9 ++++++++- adapter/claude/types.go | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/adapter/claude/chat.go b/adapter/claude/chat.go index 46fd31f..d4a79f2 100644 --- a/adapter/claude/chat.go +++ b/adapter/claude/chat.go @@ -72,7 +72,7 @@ func (c *ChatInstance) ConvertMessages(props *adaptercommon.ChatProps) []globals // if is system message, set it to user message if message.Role == globals.System { - message.Role = globals.User + continue } // anthropic api does not allow multi-same role messages @@ -128,10 +128,17 @@ func (c *ChatInstance) GetMessages(props *adaptercommon.ChatProps) []Message { func (c *ChatInstance) GetChatBody(props *adaptercommon.ChatProps, stream bool) *ChatBody { messages := c.GetMessages(props) + var systemStr string + for _, message := range messages { + if message.Role == globals.System { + systemStr += message.Content + } + } return &ChatBody{ Messages: messages, MaxTokens: c.GetTokens(props), Model: props.Model, + System: systemStr Stream: stream, Temperature: props.Temperature, TopP: props.TopP, diff --git a/adapter/claude/types.go b/adapter/claude/types.go index a39820b..3d1e832 100644 --- a/adapter/claude/types.go +++ b/adapter/claude/types.go @@ -23,6 +23,7 @@ type ChatBody struct { Messages []Message `json:"messages"` MaxTokens int `json:"max_tokens"` Model string `json:"model"` + System string `json:"system"` Stream bool `json:"stream"` Temperature *float32 `json:"temperature,omitempty"` TopP *float32 `json:"top_p,omitempty"` From 86cae7a46dd7a978532cc230a85784b17225dc07 Mon Sep 17 00:00:00 2001 From: tangxingyu Date: Sat, 30 Mar 2024 18:49:44 +0800 Subject: [PATCH 2/3] grammer fixed --- adapter/claude/chat.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/adapter/claude/chat.go b/adapter/claude/chat.go index d4a79f2..61ed6a0 100644 --- a/adapter/claude/chat.go +++ b/adapter/claude/chat.go @@ -131,14 +131,17 @@ func (c *ChatInstance) GetChatBody(props *adaptercommon.ChatProps, stream bool) var systemStr string for _, message := range messages { if message.Role == globals.System { - systemStr += message.Content + content, ok := message.Content.(string) + if ok { + systemStr += content + } } } return &ChatBody{ Messages: messages, MaxTokens: c.GetTokens(props), Model: props.Model, - System: systemStr + System: systemStr, Stream: stream, Temperature: props.Temperature, TopP: props.TopP, From aac760378f2dc1460dc0042fd9db687a40f3fc7a Mon Sep 17 00:00:00 2001 From: Zhang Minghan Date: Sat, 30 Mar 2024 18:57:08 +0800 Subject: [PATCH 3/3] fix: fix claude text merging and update code style --- adapter/claude/chat.go | 18 +++++++++--------- adapter/claude/types.go | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/adapter/claude/chat.go b/adapter/claude/chat.go index 61ed6a0..93f1df6 100644 --- a/adapter/claude/chat.go +++ b/adapter/claude/chat.go @@ -126,22 +126,22 @@ func (c *ChatInstance) GetMessages(props *adaptercommon.ChatProps) []Message { }) } -func (c *ChatInstance) GetChatBody(props *adaptercommon.ChatProps, stream bool) *ChatBody { - messages := c.GetMessages(props) - var systemStr string - for _, message := range messages { +func (c *ChatInstance) GetSystemPrompt(props *adaptercommon.ChatProps) (prompt string) { + for _, message := range props.Message { if message.Role == globals.System { - content, ok := message.Content.(string) - if ok { - systemStr += content - } + prompt += message.Content } } + return +} + +func (c *ChatInstance) GetChatBody(props *adaptercommon.ChatProps, stream bool) *ChatBody { + messages := c.GetMessages(props) return &ChatBody{ Messages: messages, MaxTokens: c.GetTokens(props), Model: props.Model, - System: systemStr, + System: c.GetSystemPrompt(props), Stream: stream, Temperature: props.Temperature, TopP: props.TopP, diff --git a/adapter/claude/types.go b/adapter/claude/types.go index 3d1e832..ceac4b3 100644 --- a/adapter/claude/types.go +++ b/adapter/claude/types.go @@ -23,7 +23,7 @@ type ChatBody struct { Messages []Message `json:"messages"` MaxTokens int `json:"max_tokens"` Model string `json:"model"` - System string `json:"system"` + System string `json:"system"` Stream bool `json:"stream"` Temperature *float32 `json:"temperature,omitempty"` TopP *float32 `json:"top_p,omitempty"`