Skip to content

Commit 1ea101d

Browse files
committed
refactor: split disabled models for endpoints mapping into chat completions endpoint-disabled and completions endpoint-enabled mappings
1 parent d68a683 commit 1ea101d

File tree

2 files changed

+62
-67
lines changed

2 files changed

+62
-67
lines changed

client_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,10 @@ func TestClientReturnsRequestBuilderErrors(t *testing.T) {
276276

277277
testCases := []TestCase{
278278
{"CreateCompletion", func() (any, error) {
279-
return client.CreateCompletion(ctx, CompletionRequest{Prompt: "testing"})
279+
return client.CreateCompletion(ctx, CompletionRequest{Model: GPT3Dot5TurboInstruct, Prompt: "testing"})
280280
}},
281281
{"CreateCompletionStream", func() (any, error) {
282-
return client.CreateCompletionStream(ctx, CompletionRequest{Prompt: ""})
282+
return client.CreateCompletionStream(ctx, CompletionRequest{Model: GPT3Dot5TurboInstruct, Prompt: ""})
283283
}},
284284
{"CreateChatCompletion", func() (any, error) {
285285
return client.CreateChatCompletion(ctx, ChatCompletionRequest{Model: GPT3Dot5Turbo})
@@ -475,11 +475,11 @@ func TestClientReturnsRequestBuilderErrorsAddition(t *testing.T) {
475475
client := NewClientWithConfig(config)
476476
client.requestBuilder = &failingRequestBuilder{}
477477
ctx := context.Background()
478-
_, err := client.CreateCompletion(ctx, CompletionRequest{Prompt: 1})
478+
_, err := client.CreateCompletion(ctx, CompletionRequest{Model: GPT3TextDavinci003, Prompt: 1})
479479
if !errors.Is(err, ErrCompletionRequestPromptTypeNotSupported) {
480480
t.Fatalf("Did not return error when request builder failed: %v", err)
481481
}
482-
_, err = client.CreateCompletionStream(ctx, CompletionRequest{Prompt: 1})
482+
_, err = client.CreateCompletionStream(ctx, CompletionRequest{Model: GPT3TextDavinci003, Prompt: 1})
483483
if !errors.Is(err, ErrCompletionRequestPromptTypeNotSupported) {
484484
t.Fatalf("Did not return error when request builder failed: %v", err)
485485
}

completion.go

Lines changed: 58 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import (
55
"net/http"
66
)
77

8+
// Endpoint constants.
9+
const (
10+
completionsEndpoint = "/completions"
11+
)
12+
813
// GPT3 Defines the models provided by OpenAI to use when generating
914
// completions from OpenAI.
1015
// GPT3 Models are designed for text-based tasks. For code-specific
@@ -91,71 +96,61 @@ const (
9196
CodexCodeDavinci001 = "code-davinci-001"
9297
)
9398

94-
var disabledModelsForEndpoints = map[string]map[string]bool{
95-
"/completions": {
96-
O1Mini: true,
97-
O1Mini20240912: true,
98-
O1Preview: true,
99-
O1Preview20240912: true,
100-
O3Mini: true,
101-
O3Mini20250131: true,
102-
GPT3Dot5Turbo: true,
103-
GPT3Dot5Turbo0301: true,
104-
GPT3Dot5Turbo0613: true,
105-
GPT3Dot5Turbo1106: true,
106-
GPT3Dot5Turbo0125: true,
107-
GPT3Dot5Turbo16K: true,
108-
GPT3Dot5Turbo16K0613: true,
109-
GPT4: true,
110-
GPT4Dot5Preview: true,
111-
GPT4Dot5Preview20250227: true,
112-
GPT4o: true,
113-
GPT4o20240513: true,
114-
GPT4o20240806: true,
115-
GPT4o20241120: true,
116-
GPT4oLatest: true,
117-
GPT4oMini: true,
118-
GPT4oMini20240718: true,
119-
GPT4TurboPreview: true,
120-
GPT4VisionPreview: true,
121-
GPT4Turbo1106: true,
122-
GPT4Turbo0125: true,
123-
GPT4Turbo: true,
124-
GPT4Turbo20240409: true,
125-
GPT40314: true,
126-
GPT40613: true,
127-
GPT432K: true,
128-
GPT432K0314: true,
129-
GPT432K0613: true,
130-
O1: true,
131-
GPT4Dot1: true,
132-
GPT4Dot120250414: true,
133-
GPT4Dot1Mini: true,
134-
GPT4Dot1Mini20250414: true,
135-
GPT4Dot1Nano: true,
136-
GPT4Dot1Nano20250414: true,
137-
},
138-
chatCompletionsSuffix: {
139-
CodexCodeDavinci002: true,
140-
CodexCodeCushman001: true,
141-
CodexCodeDavinci001: true,
142-
GPT3TextDavinci003: true,
143-
GPT3TextDavinci002: true,
144-
GPT3TextCurie001: true,
145-
GPT3TextBabbage001: true,
146-
GPT3TextAda001: true,
147-
GPT3TextDavinci001: true,
148-
GPT3DavinciInstructBeta: true,
149-
GPT3Davinci: true,
150-
GPT3CurieInstructBeta: true,
151-
GPT3Curie: true,
152-
GPT3Ada: true,
153-
GPT3Babbage: true,
154-
},
99+
// This map lists models that are *not* supported by the chat completions endpoint.
100+
// We maintain this deny list because most new models *are* supported by chat completions.
101+
// It is simpler to list the exceptions rather than updating an allow list
102+
// each time a new model is released and supported.
103+
var disabledModelsForChatCompletionsEndpoint = map[string]bool{
104+
CodexCodeCushman001: true,
105+
CodexCodeDavinci001: true,
106+
CodexCodeDavinci002: true,
107+
GPT3Ada: true,
108+
GPT3Babbage: true,
109+
GPT3Curie: true,
110+
GPT3CurieInstructBeta: true,
111+
GPT3Davinci: true,
112+
GPT3DavinciInstructBeta: true,
113+
GPT3TextAda001: true,
114+
GPT3TextBabbage001: true,
115+
GPT3TextCurie001: true,
116+
GPT3TextDavinci001: true,
117+
GPT3TextDavinci002: true,
118+
GPT3TextDavinci003: true,
119+
}
120+
121+
// This map lists models that *are* supported by the deprecated completions endpoint.
122+
// We maintain this allow list because the completions endpoint is deprecated and most new models
123+
// are *not* supported by it. It is simpler to list the older models that are supported
124+
// rather than updating a deny list each time a new model is released and *not* supported.
125+
var enabledModelsForDeprecatedCompletionsEndpoint = map[string]bool{
126+
CodexCodeCushman001: true,
127+
CodexCodeDavinci001: true,
128+
CodexCodeDavinci002: true,
129+
GPT3Ada: true,
130+
GPT3Ada002: true,
131+
GPT3Babbage: true,
132+
GPT3Babbage002: true,
133+
GPT3Curie: true,
134+
GPT3Curie002: true,
135+
GPT3CurieInstructBeta: true,
136+
GPT3Davinci: true,
137+
GPT3Davinci002: true,
138+
GPT3DavinciInstructBeta: true,
139+
GPT3Dot5TurboInstruct: true,
140+
GPT3TextAda001: true,
141+
GPT3TextBabbage001: true,
142+
GPT3TextCurie001: true,
143+
GPT3TextDavinci001: true,
144+
GPT3TextDavinci002: true,
145+
GPT3TextDavinci003: true,
155146
}
156147

157148
func checkEndpointSupportsModel(endpoint, model string) bool {
158-
return !disabledModelsForEndpoints[endpoint][model]
149+
if endpoint == completionsEndpoint {
150+
return enabledModelsForDeprecatedCompletionsEndpoint[model]
151+
}
152+
153+
return !disabledModelsForChatCompletionsEndpoint[model]
159154
}
160155

161156
func checkPromptType(prompt any) bool {
@@ -251,7 +246,7 @@ func (c *Client) CreateCompletion(
251246
return
252247
}
253248

254-
urlSuffix := "/completions"
249+
urlSuffix := completionsEndpoint
255250
if !checkEndpointSupportsModel(urlSuffix, request.Model) {
256251
err = ErrCompletionUnsupportedModel
257252
return

0 commit comments

Comments
 (0)