-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_capability_override_test.go
More file actions
165 lines (151 loc) · 4.39 KB
/
example_capability_override_test.go
File metadata and controls
165 lines (151 loc) · 4.39 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package integration
import (
"context"
"errors"
"testing"
"github.com/flexigpt/inference-go"
"github.com/flexigpt/inference-go/internal/sdkutil"
"github.com/flexigpt/inference-go/spec"
)
// overrideResolver is a minimal ModelCapabilityResolver example used in tests.
// In a real app you might:
// - keep a per-model capability table,
// - or derive capabilities from an upstream registry.
type overrideResolver struct {
// key: model name
byModel map[spec.ModelName]*spec.ModelCapabilities
}
func (r overrideResolver) ResolveModelCapabilities(
ctx context.Context,
req spec.ResolveModelCapabilitiesRequest,
) (*spec.ModelCapabilities, error) {
if r.byModel == nil {
return nil, errors.New("invalid model")
}
if c := r.byModel[req.ModelName]; c != nil {
return c, nil
}
return nil, errors.New("model not found")
}
func TestCapabilityOverride_GetProviderCapsThenOverride(t *testing.T) {
ctx := t.Context()
ps, err := inference.NewProviderSetAPI()
if err != nil {
t.Fatal(err)
}
_, err = ps.AddProvider(ctx, "openai-responses", &inference.AddProviderConfig{
SDKType: spec.ProviderSDKTypeOpenAIResponses,
Origin: spec.DefaultOpenAIOrigin,
ChatCompletionPathPrefix: "/v1/responses",
APIKeyHeaderKey: spec.DefaultAuthorizationHeaderKey,
})
if err != nil {
t.Fatal(err)
}
// 1) Get SDK-wide default capabilities programmatically.
baseCaps, err := ps.GetProviderCapability(ctx, "openai-responses")
if err != nil {
t.Fatal(err)
}
// 2) Override per-model capabilities.
//
// This is an example override (not necessarily reflecting OpenAI’s real limits):
// - disallow file input
// - disallow reasoning level xhigh
//
// The point is: *capabilities are the authoritative enforcement mechanism*.
override := baseCaps
override.ModalitiesIn = []spec.Modality{spec.ModalityTextIn, spec.ModalityImageIn} // drop fileIn
if override.ReasoningCapabilities != nil {
override.ReasoningCapabilities.SupportedReasoningLevels = []spec.ReasoningLevel{
spec.ReasoningLevelLow,
spec.ReasoningLevelMedium,
spec.ReasoningLevelHigh,
}
}
resolver := overrideResolver{
byModel: map[spec.ModelName]*spec.ModelCapabilities{
"gpt-5-mini": &override,
},
}
t.Run("modalities: file input rejected when fileIn unsupported", func(t *testing.T) {
req := &spec.FetchCompletionRequest{
ModelParam: spec.ModelParam{Name: "gpt-5-mini"},
Inputs: []spec.InputUnion{{
Kind: spec.InputKindInputMessage,
InputMessage: &spec.InputOutputContent{
Role: spec.RoleUser,
Contents: []spec.InputOutputContentItemUnion{
{Kind: spec.ContentItemKindText, TextItem: &spec.ContentItemText{Text: "hi"}},
{
Kind: spec.ContentItemKindFile,
FileItem: &spec.ContentItemFile{
FileURL: "https://example.com/a.pdf",
FileMIME: "application/pdf",
},
},
},
},
}},
}
_, _, err := sdkutil.NormalizeRequestForSDK(
ctx,
req,
&spec.FetchCompletionOptions{
CompletionKey: "gpt5mini",
CapabilityResolver: resolver,
},
spec.ProviderSDKTypeOpenAIResponses,
baseCaps,
)
if err == nil {
t.Fatalf("expected modality error, got nil")
}
})
t.Run("reasoning: unsupported level dropped with warning", func(t *testing.T) {
req := &spec.FetchCompletionRequest{
ModelParam: spec.ModelParam{
Name: "gpt-5-mini",
Reasoning: &spec.ReasoningParam{
Type: spec.ReasoningTypeSingleWithLevels,
Level: spec.ReasoningLevelXHigh,
},
},
Inputs: []spec.InputUnion{{
Kind: spec.InputKindInputMessage,
InputMessage: &spec.InputOutputContent{
Role: spec.RoleUser,
Contents: []spec.InputOutputContentItemUnion{
{Kind: spec.ContentItemKindText, TextItem: &spec.ContentItemText{Text: "hi"}},
},
},
}},
}
capped, warns, err := sdkutil.NormalizeRequestForSDK(
ctx,
req,
&spec.FetchCompletionOptions{
CompletionKey: "gpt5mini",
CapabilityResolver: resolver,
},
spec.ProviderSDKTypeOpenAIResponses,
baseCaps,
)
if err != nil {
t.Fatal(err)
}
if capped.ModelParam.Reasoning != nil {
t.Fatalf("expected reasoning dropped, got %#v", capped.ModelParam.Reasoning)
}
found := false
for _, w := range warns {
if w.Code == "reasoning_dropped_invalid_level" {
found = true
break
}
}
if !found {
t.Fatalf("expected reasoning_dropped_invalid_level warning; got %#v", warns)
}
})
}