forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcache_control_test.go
More file actions
58 lines (53 loc) · 1.63 KB
/
Copy pathcache_control_test.go
File metadata and controls
58 lines (53 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package openai_test
import (
"encoding/json"
"strings"
"testing"
openai "github.com/sashabaranov/go-openai"
)
// TestChatMessagePartCacheControlMarshal verifies that a CacheControl breakpoint
// on a MultiContent text part serializes into the OpenAI/LiteLLM wire format:
//
// {"role":"system","content":[{"type":"text","text":"...","cache_control":{"type":"ephemeral"}}]}
func TestChatMessagePartCacheControlMarshal(t *testing.T) {
msg := openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleSystem,
MultiContent: []openai.ChatMessagePart{
{
Type: openai.ChatMessagePartTypeText,
Text: "stable system prompt",
CacheControl: &openai.CacheControl{Type: "ephemeral"},
},
},
}
b, err := json.Marshal(msg)
if err != nil {
t.Fatalf("marshal: %v", err)
}
got := string(b)
for _, want := range []string{
`"content":[`,
`"type":"text"`,
`"text":"stable system prompt"`,
`"cache_control":{"type":"ephemeral"}`,
} {
if !strings.Contains(got, want) {
t.Errorf("marshaled message missing %q\n got: %s", want, got)
}
}
}
// TestChatMessagePartCacheControlOmitted verifies the field is omitted when unset,
// so existing callers are unaffected.
func TestChatMessagePartCacheControlOmitted(t *testing.T) {
msg := openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleSystem,
MultiContent: []openai.ChatMessagePart{{Type: openai.ChatMessagePartTypeText, Text: "hi"}},
}
b, err := json.Marshal(msg)
if err != nil {
t.Fatalf("marshal: %v", err)
}
if strings.Contains(string(b), "cache_control") {
t.Errorf("cache_control should be omitted when unset, got: %s", string(b))
}
}