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:
@@ -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