mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
fix(anthropic): keep client cache_control on translated system messages
The translator joined system and developer messages into one string. This dropped any cache_control marker that the client set on a part. Now, when a marker exists, system becomes a list of text blocks that keep the marker. Without markers, the joined string is unchanged.
This commit is contained in:
+32
-1
@@ -313,6 +313,20 @@ func systemTextFromContentParts(parts []any) (string, error) {
|
|||||||
return strings.Join(texts, "\n"), nil
|
return strings.Join(texts, "\n"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// systemBlockFromPart builds a standard Anthropic text block from a
|
||||||
|
// validated system content part, keeping a client cache_control marker.
|
||||||
|
// The second result reports whether a marker was kept.
|
||||||
|
func systemBlockFromPart(pm map[string]any) (map[string]any, bool) {
|
||||||
|
text, _ := pm["text"].(string)
|
||||||
|
block := map[string]any{"type": "text", "text": text}
|
||||||
|
cc, has := pm["cache_control"]
|
||||||
|
if has && cc != nil {
|
||||||
|
block["cache_control"] = cc
|
||||||
|
return block, true
|
||||||
|
}
|
||||||
|
return block, false
|
||||||
|
}
|
||||||
|
|
||||||
// anthropicToolChoiceFromOpenAI maps OpenAI's "tool_choice" to Anthropic's
|
// anthropicToolChoiceFromOpenAI maps OpenAI's "tool_choice" to Anthropic's
|
||||||
// tool_choice object, and reports whether "tools" should be dropped from
|
// tool_choice object, and reports whether "tools" should be dropped from
|
||||||
// the request. tool_choice:"none" maps to Anthropic's own
|
// the request. tool_choice:"none" maps to Anthropic's own
|
||||||
@@ -395,6 +409,8 @@ func anthropicRequestFromOpenAI(req map[string]any) (map[string]any, error) {
|
|||||||
|
|
||||||
msgsRaw, _ := req["messages"].([]any)
|
msgsRaw, _ := req["messages"].([]any)
|
||||||
var systemParts []string
|
var systemParts []string
|
||||||
|
var systemBlocks []any
|
||||||
|
systemMarked := false
|
||||||
anthMsgs := make([]any, 0, len(msgsRaw))
|
anthMsgs := make([]any, 0, len(msgsRaw))
|
||||||
// pendingToolResults accumulates consecutive role:"tool" messages'
|
// pendingToolResults accumulates consecutive role:"tool" messages'
|
||||||
// blocks. Anthropic requires alternating user/assistant turns and
|
// blocks. Anthropic requires alternating user/assistant turns and
|
||||||
@@ -431,12 +447,25 @@ func anthropicRequestFromOpenAI(req map[string]any) (map[string]any, error) {
|
|||||||
switch c := msg["content"].(type) {
|
switch c := msg["content"].(type) {
|
||||||
case string:
|
case string:
|
||||||
systemParts = append(systemParts, c)
|
systemParts = append(systemParts, c)
|
||||||
|
if c != "" {
|
||||||
|
systemBlocks = append(systemBlocks, map[string]any{"type": "text", "text": c})
|
||||||
|
}
|
||||||
case []any:
|
case []any:
|
||||||
text, err := systemTextFromContentParts(c)
|
text, err := systemTextFromContentParts(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
systemParts = append(systemParts, text)
|
systemParts = append(systemParts, text)
|
||||||
|
for _, part := range c {
|
||||||
|
pm, _ := part.(map[string]any)
|
||||||
|
block, marked := systemBlockFromPart(pm)
|
||||||
|
if marked {
|
||||||
|
systemMarked = true
|
||||||
|
}
|
||||||
|
if block["text"] != "" {
|
||||||
|
systemBlocks = append(systemBlocks, block)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case "user", "assistant":
|
case "user", "assistant":
|
||||||
flushPendingToolResults()
|
flushPendingToolResults()
|
||||||
@@ -456,7 +485,9 @@ func anthropicRequestFromOpenAI(req map[string]any) (map[string]any, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
flushPendingToolResults()
|
flushPendingToolResults()
|
||||||
if len(systemParts) > 0 {
|
if systemMarked {
|
||||||
|
out["system"] = systemBlocks
|
||||||
|
} else if len(systemParts) > 0 {
|
||||||
out["system"] = strings.Join(systemParts, "\n\n")
|
out["system"] = strings.Join(systemParts, "\n\n")
|
||||||
}
|
}
|
||||||
out["messages"] = anthMsgs
|
out["messages"] = anthMsgs
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package traefikllmgateway
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTranslateSystemKeepsCacheControl(t *testing.T) {
|
||||||
|
cc := map[string]any{"type": "ephemeral"}
|
||||||
|
req := map[string]any{
|
||||||
|
"model": "claude-x",
|
||||||
|
"messages": []any{
|
||||||
|
map[string]any{"role": "system", "content": []any{
|
||||||
|
map[string]any{"type": "text", "text": "A"},
|
||||||
|
map[string]any{"type": "text", "text": "B", "cache_control": cc},
|
||||||
|
}},
|
||||||
|
map[string]any{"role": "user", "content": "hi"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
out, err := anthropicRequestFromOpenAI(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
want := []any{
|
||||||
|
map[string]any{"type": "text", "text": "A"},
|
||||||
|
map[string]any{"type": "text", "text": "B", "cache_control": map[string]any{"type": "ephemeral"}},
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(out["system"], want) {
|
||||||
|
t.Fatalf("system = %#v, want %#v", out["system"], want)
|
||||||
|
}
|
||||||
|
if _, ok := req["messages"].([]any)[0].(map[string]any)["content"].([]any)[1].(map[string]any)["cache_control"]; !ok {
|
||||||
|
t.Fatal("input mutated")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTranslateSystemNoMarkersUnchanged(t *testing.T) {
|
||||||
|
req := map[string]any{
|
||||||
|
"model": "claude-x",
|
||||||
|
"messages": []any{
|
||||||
|
map[string]any{"role": "system", "content": "A"},
|
||||||
|
map[string]any{"role": "developer", "content": "B"},
|
||||||
|
map[string]any{"role": "user", "content": "hi"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
out, err := anthropicRequestFromOpenAI(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if out["system"] != "A\n\nB" {
|
||||||
|
t.Fatalf("system = %#v", out["system"])
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user